The CONSTRAINT clause is used to maintain data integrity by providing limits on the values that can be inserted into a column or table.
While a CONSTRAINT clause is somewhat similar to an INDEX, a CONSTRAINT can establish a relationship with another table. To place a constraint on a single field in a CREATE TABLE or ALTER TABLE statement, follow the definition of that field with a CONSTRAINT clause. This consists of a name for the constraint and one of the following reserved words: PRIMARY KEY, UNIQUE, NOT NULL or REFERENCES.
CREATE TABLE Names (NameID INTEGER CONSTRAINT NameIDKey PRIMARY KEY,
FirstName VARCHAR (20), LastName VARCHAR (20), DateOfBirth DATETIME);
The command(s) completed successfully.
The PRIMARY KEY reserved word designates a field (or set of fields) as a primary key. It is mandatory that all values in the primary key must be unique and not NULL. An error will occur if you try to use a PRIMARY KEY constraint on a table that already has a primary key. This example sets the NameID field to be the primary key of the Names table.
CREATE TABLE Names (NameID INTEGER, FirstName VARCHAR (20) CONSTRAINT
UniqueName UNIQUE, LastName VARCHAR (20), DateOfBirth DATETIME);
The command(s) completed successfully.
The reserved word UNIQUE requires that the value entered into the specified field (or combination of fields) be unique, as in this example which only allows unique first names.
CREATE TABLE Names (NameID INTEGER, FirstName VARCHAR (20) NOT NULL,
LastName VARCHAR (20) NOT NULL, DateOfBirth DATETIME NOT NULL);
The command(s) completed successfully.
This statement shows how the reserved word NOT NULL can be used to specify that a field in a table must always contain valid data (and cannot contain NULL).
CREATE TABLE Sales (SalesID INTEGER, ProductID INTEGER, Item TEXT CONSTRAINT
ForeignRefs REFERENCES Products (Item));
The command(s) completed successfully.
This example demonstrates how relationships can be established with a field in a foreign table (as long as it only contains unique values) by using the reserved word REFERENCES and naming the foreign table and field.
CREATE TABLE Sales (SalesID INTEGER, ProductID INTEGER CONSTRAINT
ForeignKeyRef REFERENCES Products, Item TEXT);
The command(s) completed successfully.
If the field in the foreign table is the primary key, you only need name the table and the database engine references it by default.
CREATE TABLE Names (NameID INTEGER, FirstName VARCHAR (20),
LastName VARCHAR (20), DateOfBirth DATETIME,
CONSTRAINT NameKey PRIMARY KEY(FirstName, LastName));
The command(s) completed successfully.
When you want to apply a constraint to more than one field, a multiple-field constraint, you can do so by adding the CONSTRAINT clause after all the field definitions. The preceding example makes the two fields FirstName and LastName a joint primary key.
CREATE TABLE Names (NameID INTEGER, FirstName VARCHAR (20),
LastName VARCHAR (20), DateOfBirth DATETIME,
CONSTRAINT UniqueFields UNIQUE(FirstName, LastName, DateOfBirth));
The command(s) completed successfully.
This example requires the combination of FirstName, LastName and DateOfBirth to be unique.
Note: It is acceptable for one or more of the fields in a multiple-field constraint to contain values that are the same, as long as the combination of values in all the constrained fields is unique.
CREATE TABLE Cartoons (CartoonID INTEGER, ShowName TEXT,
ToonName VARCHAR (15), ToonType VARCHAR (15),
CONSTRAINT ReferForeignField FOREIGN KEY(ToonName, ToonType)
REFERENCES Toons (CharacterName, ToonType));
The command(s) completed successfully.
If you want to include a FOREIGN KEY that consists of more than one field, you must use a multiple-field constraint definition. The constraint definition must list the names of the referencing fields, the foreign table, and the referenced fields in the foreign table. The order of the referenced fields must correspond to the order of the referring fields.