CONSTRAINT clauses


Use Constraint clauses to define indexes, primary and foreign keys, and attribute constraints. Their syntax appears below:

Attribute (or field) constraints.

Insert these into the parent statement (immediately following the field definition), e.g. CREATE TABLE Contact (ID char(8) CONSTRAINT PrimaryKey PRIMARY KEY, Name char(32))
Constraint type Syntax
Primary key CONSTRAINT name PRIMARY KEY
Unique index CONSTRAINT name UNIQUE
Foreign key CONSTRAINT name REFERENCES foreigntable [(foreignfield1[, foreignfield2, [...]]]


Table (or compound) constraints.

These constraints specify one or more fields. Insert them at the end of the parent statement, after all field definitions, e.g. CREATE TABLE Contact (ID char(8), Name char(32) CONSTRAINT PrimaryKey PRIMARY KEY (ID))
Constraint type Syntax
Primary key PRIMARY KEY (field1[, field2[, ...]])
Unique index UNIQUE (field1[, field2[, ...]])
Foreign key FOREIGN KEY (field1[, field2[, ...]]) REFERENCES foreigntable [(foreignfield1[, foreignfield2[, ...]])]


The parts of the CONSTRAINT clause have the following meanings:
Part Meaning
name The name of the constraint to be created
fieldn The name of the field constrained by the constraint
foreigntable The name of the table containing the referenced fields
foreignfieldn The referenced fields in the referenced table.

Note: You must list the referenced fields in the same order as the corresponding referencing fields.



[Return to SQL Syntax]