You can use “constraints” to restrict the domain of the data type in a column.
With just a few exceptions, you can formulate in a CONSTRAINT definition everything that also counts as a search condition. You can use the AND, OR, and NOT operators to link together several conditions. You can address as many columns as required. Note, however, that constraints reduce the speed of the database system when changes are made to entries in the table.
See also:
CONSTRAINT Definition (constraint_definition)
You can specify a constraint when you define the table or add it to an existing table at a later stage.
You can formulate constraints that refer to one table column only.
You can use the CREATE TABLE statement to define constraints when you create a table.
...
1.
If
necessary, drop the city
table.
DROP TABLE
city
2.
Create
the city table as follows:
CREATE TABLE city
(zip CHAR(5) PRIMARY KEY
CONSTRAINT zip_cons CHECK
SUBSTR(zip,1,1)
BETWEEN '0' AND '9' AND
SUBSTR(zip,2,1)
BETWEEN '0' AND '9' AND
SUBSTR(zip,3,1)
BETWEEN '0' AND '9' AND
SUBSTR(zip,4,1)
BETWEEN '0' AND '9' AND
SUBSTR(zip,5,1)
BETWEEN '0' AND '9',
name CHAR(20) NOT NULL,
state CHAR(2) NOT
NULL)
The city table is created with a simple constraint in the zip code column. This constraint ensures that only digits from 0 to 9 can be entered for the zip code; all other characters will be rejected.
Specifying NOT NULL has the effect that a value has to be assigned to a column. This column is then designated as mandatory.
When you define a constraint, you specify implicitly that the NULL value is not permitted as an input.
See also:
CREATE TABLE Statement (create_table_statement)
You can use the ALTER TABLE statement to add constraints to existing tables.
ALTER TABLE
customer ADD CONSTRAINT zip_cons CHECK
SUBSTR(zip,1,1)
BETWEEN '0' AND '9' AND
SUBSTR(zip,2,1)
BETWEEN '0' AND '9' AND
SUBSTR(zip,3,1)
BETWEEN '0' AND '9' AND
SUBSTR(zip,4,1)
BETWEEN '0' AND '9' AND
SUBSTR(zip,5,1)
BETWEEN '0' AND '9'
A simple constraint is added to the zip code column in the customer table. This constraint ensures that only digits from 0 to 9 can be entered for the zip code; all other characters will be rejected.
ALTER TABLE customer ADD CONSTRAINT cno_cons CHECK cno > 10
ALTER TABLE customer ADD CONSTRAINT title_cons CHECK title IN ('Mr', 'Mrs', 'Company')
Additional simple constraints are defined for the customer table. The customer number must be greater than 10; you are permitted to enter only one of the character sets Mr, Mrs, or Company in the title column.
In a complex constraint, you formulate conditions that refer to several columns in the table.
ALTER TABLE
reservation ADD CONSTRAINT staying CHECK
departure
> arrival
In the reservation table, check that the arrival date is before the departure date.
See also:
ALTER TABLE Statement (alter_table_statement)
ADD Definition (add_definition)
You can use the ALTER TABLE statement to change existing constraints.
ALTER TABLE customer ALTER CONSTRAINT cno_cons CHECK cno > 0
For the customer table, the cno_cons constraint has changed. The customer number must be greater than 0.
See also:
ALTER Definition (alter_definition)
You can use the ALTER TABLE statement to drop a constraint.
ALTER TABLE reservation DROP CONSTRAINT staying
See also:
DROP Definition (drop_definition)