30 Chapter 1: Creating
primary key, you must specify the column names from the UNIQUE constraint
in the REFERENCES clause.
The ON UPDATE and ON DELETE clauses work as they do in a foreign
key column constraint: They specify what action, if any, will be taken to silently
repair constraint violations caused by update and delete operations performed
on the corresponding parent row. RESTRICT is the default; it produces an error
message and prevents the operation.
The CHECK ON COMMIT clause defers checking of this constraint until a
COMMIT is executed. This feature bends Rule 12 of relational databases, which
states it must not be possible to bypass the integrity rules or constraints when
manipulating the data. The FOREIGN KEY constraint isnt being bypassed
altogether; its application is simply being postponed.
CHECK ON COMMIT can help when it is inconvenient to make changes
in the correct order, (i.e., insert parents first, delete parents last, and so on).
An application can insert, delete, and update rows in any order it wants as long
as the FOREIGN KEY constraint is not violated when the changes are complete
and a COMMIT is issued.
The index automatically created on the foreign key columns may be
defined as CLUSTERED or NONCLUSTERED, with NONCLUSTERED
being the default. For more information about clustered indexes, see Section
10.7, CREATE INDEX.
1.13.4 UNIQUE Table Constraint
<unique_table_constraint> ::= [ <constraint_prefix> ] UNIQUE
[ <clustering> ]
"(" <column_name_list> ")"
The UNIQUE table constraint is exactly the same as the UNIQUE column con-
straint, except that a list of one or more column names is required. If you
specify two or more column names, it is the combination of column values that
must be unique, not each separate column. Here is an example of three inserts
that work and one that violates a UNIQUE constraint:
CREATE TABLE t (
c1 INTEGER PRIMARY KEY,
c2 INTEGER,
c3 INTEGER,
UNIQUE ( c2, c3 ) );
INSERT t VALUES ( 1, 1, 1 ); -- OK
INSERT t VALUES ( 2, 1, 2 ); -- OK
INSERT t VALUES ( 3, 2, 1 ); -- OK
INSERT t VALUES ( 4, 1, 1 ); -- fails
The unique index automatically created on the UNIQUE constraint columns
may be defined as CLUSTERED or NONCLUSTERED, with
NONCLUSTERED being the default. For more information about clustered
indexes, see Section 10.7, CREATE INDEX.