Professional Documents
Culture Documents
INF 312
DATABASE DESIGN AND
DEVELOPMENT
Semester 1, 2023
Week 4 – Lecture 4
Constraints
26 February 2023 2
Lecture 4 Objectives
After completing this chapter, you should be able to do the
following:
• Explain the purpose of constraints in a table
• Distinguish among PRIMARY KEY, FOREIGN KEY,
UNIQUE, CHECK, and NOT NULL constraints and
understand the correct use of each constraint
• Understand how to create constraints when creating a
table or modifying an existing table
• Distinguish between creating constraints at the column
level and the table level
• Create PRIMARY KEY constraints for a single column
and a composite primary key
26 February 2023 3
Lecture 4 Objectives
• Create a FOREIGN KEY constraint
• Create a UNIQUE constraint
• Create a CHECK constraint
• Create a NOT NULL constraint with the ALTER TABLE ...
MODIFY command
• Include constraints during table creation
• Add multiple constraints on a single column
• View constraint information
• Use the DISABLE and ENABLE commands with
constraints
• Use the DROP command with constraints
26 February 2023 4
READINGS
• Casteel, Chapter 4
pp. 99 - 135
• This chapter is important
for SQL skill development.
5
Constraints
• Rules used to enforce business rules, practices, and
policies
• Rules used to ensure accuracy and integrity of data
Constraint Types
Creating Constraints
• Use the optional CONSTRAINT keyword during creation
to assign a name to the constraint
• Informative constraint names can assist in debugging
• Alternatively, let the server define the name for the
constraint using the default format SYS_Cn
Enforcement of Constraints
• All constraints are enforced at the table level, regardless
of how you create them
• If a data value violates a constraint, the entire row is
rejected
Viewing Constraints –
USER_CONSTRAINTS
• Display constraint listing for a specific table
Dropping Constraints
• Constraints cannot be modified; they must be dropped
and recreated if needed.
• Actual syntax depends on type of constraint
• PRIMARY KEY – just list the type of constraint, i.e. PRIMARY KEY
• UNIQUE – include the column name
• All others – include the name of the constraint
INSERT Command
• Used to add a row to an existing table
• Can only add one row at a time to a table
• Identify the table in the INSERT INTO clause
• Specify data in the VALUES clause
Column List
Subquery
UPDATE Command
• UPDATE clause identifies table
• SET clause identifies column(s) being changed and new
value(s)
• Optional WHERE clause specifies a condition for row(s)
to be changed – if omitted, all rows will be updated!
Substitution Variables
• Prompts user for value to make dynamic updates from
user input
• Identified by ampersand (&) preceding variable name
• Can be used to create interactive scripts
Deleting Rows
• DELETE command removes row(s) from a table
WHERE clause
determines which
row(s) are removed
COMMIT Command
• Explicit COMMIT occurs by executing COMMIT;
• Implicit COMMIT occurs when a DDL command is
executed or user properly exits system
• Permanently updates table(s) and allows other users to
view changes
ROLLBACK Command
• Used to “undo” changes that have not been
committed
• Occurs when:
• ROLLBACK; is executed
• System restarts after a crash
Table Locks
• Prevent simultaneous users from changing same data or
objects
• Two types of table locks:
• Shared – prevents DML operations on a portion of table
• Exclusive – locks the entire table preventing other exclusive or
shared locks
SUMMARY
• A constraint is a rule applied to data being added to a
table. It represents business rules, policies, or
procedures. Data violating the constraint isn’t added to
the table.
• A constraint can be included during table creation as
part of the CREATE TABLE command or added to an
existing table with the ALTER TABLE command.
• A constraint based on composite columns (more than
one column) must be created by using the table-level
approach.
• A NOT NULL constraint can be created only with the
column-level approach.
57
SUMMARY
• A PRIMARY KEY constraint doesn’t allow duplicate or
NULL values in the designated column.
• Only one PRIMARY KEY constraint is allowed in a
table.
• A FOREIGN KEY constraint requires that the column
entry match a referenced column entry in the table or be
NULL.
• A UNIQUE constraint is similar to a PRIMARY KEY
constraint, except it allows storing NULL values in the
specified column.
• A CHECK constraint ensures that data meets a given
condition before it’s added to the table. The condition
can’t reference the SYSDATE function or values stored
58
SUMMARY
• A NOT NULL constraint is a special type of CHECK
constraint. If you’re adding to an existing column, the
ALTER TABLE ... MODIFY command must be used.
• A column can be assigned multiple constraints.
• The data dictionary views USER_CONSTRAINTS and
USER_CONS_COLUMNS enable you to verify existing
constraints.
• A constraint can be disabled or enabled with the ALTER
TABLE command and the DISABLE and ENABLE
keywords.
• A constraint can’t be modified. To change a constraint,
you must first drop it with the DROP command and then
re-create it.
26 February 2023 59