Db2 Trianing Class 07
Db2 Trianing Class 07
Constraints are used to limit the type of data that can go into a
table. Constraints can be specified when a table is created (with
the CREATE TABLE statement) or after the table is created
(with the ALTER TABLE statement).
We will focus on the following constraints:
NOT NULL
UNIQUE
CHECK
DEFAULT
PRIMARY KEY
FOREIGN KEY
NOT NULL Constraint
The CHECK constraint is used to limit the value range that can be
placed in a column.
If you define a CHECK constraint on a single column it allows only
certain values for this column.
If you define a CHECK constraint on a table it can limit the values in
certain columns based on values in other columns in the row.
CREATE TABLE PERSONS
(
E_ID INT NOT NULL CHECK (E_ID>0),
LASTNAME VARCHAR(255) NOT NULL,
FIRSTNAME VARCHAR(255),
ADDRESS VARCHAR(255),
CITY VARCHAR(255)
)
CHECK CONSTRAINT
PRIMARY KEY Constraint
ON DELETE CASCADE:
If we delete the rows in a parent table then the related rows of
dependent table will be deleted automatically.
ON DELETE SET NULL:
If we delete the rows of parent table then the foreign key values of the
related rows in dependent table will set to nulls.
ON DELETE RESTRICT:
If we delete rows of parent table which are having matching foreign key
values in dependent table then we will get SQL error -532.
In order to overcome this error we can delete the rows from dependent
table first and then delete the rows from parent table.
Practical’s
Thank You