CONSTRAINT Clause

A constraint is similar to an index, although it can also be used to establish a relationship with another table.

You use the CONSTRAINT clause in ALTER TABLE and CREATE TABLE statements to create or delete constraints. There are two types of CONSTRAINT clauses: one for creating a constraint on a single field and one for creating a constraint on more than one field.


Note The Microsoft Jet database engine does not support the use of CONSTRAINT, or any of the data definition language (DDL) statements, with non-Microsoft Jet databases. Use the DAO Create methods instead.


Syntax

Single-field constraint:

CONSTRAINT name {PRIMARY KEY | UNIQUE | NOT NULL |
    REFERENCES foreigntable [(foreignfield1, foreignfield2)]
    [ON UPDATE CASCADE | SET NULL]
    [ON DELETE CASCADE | SET NULL]}

Multiple-field constraint:

CONSTRAINT name
    {PRIMARY KEY (primary1[, primary2 [, ...]]) |
    UNIQUE (unique1[, unique2 [, ...]]) |
    NOT NULL (notnull1[, notnull2 [, ...]]) |
    FOREIGN KEY [NO INDEX] (ref1[, ref2 [, ...]]) REFERENCES foreigntable [(foreignfield1 [, foreignfield2 [, ...]])]
    [ON UPDATE CASCADE | SET NULL]
    [ON DELETE CASCADE | SET NULL]}

The CONSTRAINT clause has these parts:

Part Description
name The name of the constraint to be created.
primary1, primary2 The name of the field or fields to be designated the primary key.
unique1, unique2 The name of the field or fields to be designated as a unique key.
notnull1, notnull2 The name of the field or fields that are restricted to non-Null values.
ref1, ref2 The name of a foreign key field or fields that refer to fields in another table.
foreigntable The name of the foreign table containing the field or fields specified by foreignfield.
foreignfield1, foreignfield2 The name of the field or fields in foreigntable specified by ref1, ref2. You can omit this clause if the referenced field is the primary key of foreigntable.

Remarks

You use the syntax for a single-field constraint in the field-definition clause of an ALTER TABLE or CREATE TABLE statement immediately following the specification of the field's data type.

You use the syntax for a multiple-field constraint whenever you use the reserved word CONSTRAINT outside a field-definition clause in an ALTER TABLE or CREATE TABLE statement.

Using CONSTRAINT you can designate a field as one of the following types of constraints:

To prevent the automatic creation of indexes for foreign keys, the modifier NO INDEX can be used. This form of foreign key definition should be used only in cases where the resulting index values would be frequently duplicated. Where the values in a foreign key index are frequently duplicated, using an index can be less efficient than simply performing a table scan. Maintaining this type of index, with rows inserted and deleted from the table, degrades performance and does not provide any benefit.

See Also
ADD USER Statement CREATE USER or GROUP Statement
ALTER USER or DATABASE Statement CREATE VIEW Statement
ALTER TABLE Statement DROP Statement
CREATE INDEX Statement DROP USER or GROUP Statement
CREATE PROCEDURE Statement GRANT Statement
CREATE TABLE Statement REVOKE Statement

Example

CREATE TABLE Statement, CONSRAINT Clause Example Example