0% found this document useful (0 votes)
16 views16 pages

Db2 Trianing Class 07

The document provides an overview of DB2 constraints used to limit data types in tables, including NOT NULL, UNIQUE, CHECK, DEFAULT, PRIMARY KEY, and FOREIGN KEY constraints. It explains how these constraints can be applied during table creation or alteration and their specific functions, such as ensuring uniqueness or defining relationships between tables. Additionally, it covers referential integrity rules related to foreign keys, including actions on delete operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views16 pages

Db2 Trianing Class 07

The document provides an overview of DB2 constraints used to limit data types in tables, including NOT NULL, UNIQUE, CHECK, DEFAULT, PRIMARY KEY, and FOREIGN KEY constraints. It explains how these constraints can be applied during table creation or alteration and their specific functions, such as ensuring uniqueness or defining relationships between tables. Additionally, it covers referential integrity rules related to foreign keys, including actions on delete operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

DB2 Constraints

DB2 Training class 07


Introduction to Constraints

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 null value is used in databases to represent an unknown


state.
 The NOT NULL constraint is used to ensure that a given
column of a table is never assigned the null value.
 Once a NOT NULL constraint has been defined for a
particular column, any insert or update operation that attempts
to place a null value in that column will fail.

CREATE TABLE EMPLOYEES (. . . EMERGENCY_PHONE


CHAR(14) NOT NULL, . . . );
NOT NULL Constraint
UNIQUE Constraint

 UNIQUE CONSTRAINTS ensure that the values in a set of columns are


unique and not null for all rows in the table. The columns specified in a
unique constraint must be defined as NOT NULL.
 Unique constraints can be defined in the CREATE TABLE or ALTER
TABLE statement using the UNIQUE clause.
 Regardless of when a UNIQUE constraint is defined, when it is created,
the DB2 Database Manager looks to see whether an index for the
columns to which the UNIQUE constraint refers already exists.
 There is a distinction between defining a UNIQUE constraint
and creating a unique index. Even though both enforce
uniqueness, a unique index allows NULL values and generally
cannot be used in a referential constraint. A UNIQUE constraint,
on the other hand, does not allow NULL values, and can be
referenced in a foreign key specification.
DEFAULT CONSTRAINT

 the default constraint can be used to ensure that a particular


column in a base table is assigned a predefined value (unless
that value is overridden) each time a record is added to the
table.
 The predefined value provided could be null (if the NOT
NULL constraint has not been defined for the column)
OR
A user-supplied value compatible with the column's data type,
OR
A value furnished by the DB2 Database Manager
DEFAULT Constraint
CHECK 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

The PRIMARY KEY constraint uniquely identifies each record in a


database table.
Primary keys must contain unique values.
A primary key column cannot contain NULL values.
Each table should have a primary key, and each table can have only ONE
primary key.
CREATE TABLE album(
id CHAR(10) NOT NULL PRIMARY KEY,
title VARCHAR(100),
artist VARCHAR(100)
);
FOREIGN KEY Constraint

 Foreign Keys are keys used in a table as an attribute and


are primary keys of some other table
 These are attributes of one table that have matching values
in a primary key in another table, allowing for relationships
between tables.
CREATE TABLE TRACK(
ALBUM CHAR(10),
DSK INTEGER,
POSN INTEGER,
SONG VARCHAR(255),
FOREIGN KEY (ALBUM) REFERENCES ALBUM(ID));
The Insert Rule for Referential
Constraints
The Insert Rule for Referential
Constraints
REFERENTIAL INTEGRITY

 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

You might also like