Entering content frame

CONSTRAINT definition (constraint_definition) 

A CONSTRAINT definition defines an integrity condition (restrictions for column values, see data integrity) that must be fulfilled by all the rows in one table.

Syntax

<constraint_definition> ::= CHECK <search_condition>
| CONSTRAINT <search_condition>
| CONSTRAINT <constraint_name> CHECK <search_condition>

search condition, constraint name

Simple constraint (for one column), example table customer

title CHAR(7) CONSTRAINT title IN ('Mr','Mrs','Comp')

Complex constraint (for several columns), example table reservation

arrival DATE NOT NULL
departure DATE CONSTRAINT departure > arrival

The system checks whether the arrival is before the departure.

Explanation

A CONSTRAINT definition defines an integrity condition that must be fulfilled by all the column values in the columns defined by the column definition with CONSTRAINT definition.

The CONSTRAINT definition in a column is checked when a row is inserted and a column changed that occurs in the CONSTRAINT definition. If the CONSTRAINT definition is violated, the INSERT or UPDATE statement fails.

When you define a constraint, you specify implicitly that the NULL value is not permitted as an input.

The search condition ( search_condition) of the CONSTRAINT definition must not contain a subquery.

The search condition of the CONSTRAINT definition must only contain column names in the form <column name>.

Constraint name

·        No constraint name:
The database system assigns a constraint name that is unique for the table in question.

·        Constraint name is specified:
The constraint name must be different to all other constraint names for this table.

Number of columns in a search condition

·        Contains only one column name in the table:
When the table is created (CREATE TABLE statement), you can check whether an additional DEFAULT value (default_spec) specified as a column attribute fulfills the search condition. If it is not true, the CREATE TABLE statement fails.

·        Contains more than one column name in the table:
When the table is created (CREATE TABLE statement), it is not possible to decide whether DEFAULT values of the table columns fulfill the search condition. In this case, an attempt to insert DEFAULT values in the table when an INSERT or UPDATE statement is executed may fail.

 

Leaving content frame