1.1

Table Of Contents
Table Constraint
This constraint allows you to specify table-level constraints, including the primary key, a unique key, or a foreign
key. Alternatively, a check constraint can be specied that is tested for each INSERT, UPDATE, and DELETE
operation.
Syntax
[ CONSTRAINT constraint-name ]
{
CHECK (
CHECK Constraint Search Condition
) |
{
PRIMARY KEY ( column-name [ , column-name ]* ) |
UNIQUE ( column-name [ , column-name ]* ) |
FOREIGN KEY ( column-name [ , column-name ]* )
REFERENCES table-name [ ( column-name [ , column-name ]* ) ]
[ ON DELETE RESTRICT ]
}
}
CHECK Constraint Search Condition
A search condition is a boolean expression that is specied in a CHECK constraint. The expression cannot be
dynamic and so cannot contain dynamic parameters, date/time functions, user functions, or sub-queries.
If a check constraint is included as part of a column-denition, a column reference can only be made to the same
column.
If a check constraint is specied as part of a table denition, column references can specify any columns that
were previously dened in the CREATE TABLE statement. When specied in a table constraint, the search
condition must be satised for all rows in the table. The search condition is applied to each row that is modied
on an INSERT or UPDATE at the time of the row modication. The entire statement is aborted if any check
constraint is violated.
Examples of Constraints
-- column-level primary key constraint named OUT_TRAY_PK:
CREATE TABLE SAMP.OUT_TRAY
(
SENT TIMESTAMP,
DESTINATION CHAR(8),
SUBJECT CHAR(64) NOT NULL CONSTRAINT OUT_TRAY_PK PRIMARY KEY,
NOTE_TEXT VARCHAR(3000)
);
-- the table-level primary key definition allows you to
-- include more than one columns in the primary key definition:
CREATE TABLE SAMP.SCHED
(
CLASS_CODE CHAR(7) NOT NULL,
DAY SMALLINT NOT NULL,
STARTING TIME,
ENDING TIME,
PRIMARY KEY (CLASS_CODE, DAY)
);
481
SQL Language Reference