INTEGRITY CONSTRAINTS

- An IC describes conditions that must be satisfied by every legal instance of a (set of) relation.

- Inserts/deletes/updates that violate IC's are disallowed.

- Help to express application semantics (eg ssn is a primary key)

- Used to prevent data inconsistencies (eg sname has to be string, age should be less than 40)

 

Different types of constraints

- Key constraints (primary key or candidate key)

- Referential integrity constraints (foreign key constraints)

- Domain constraints (or value constraints)

Other value constraints: NOT NULL, UNIQUE, CHECK (condition), DEFAULT

NOT NULL
Forces the user to enter a value into a column.
Use when the column cannot be left blank.

UNIQUE
Use when no duplicate values are allowed in the column.

Remark:
NULL values, however, may appear in several rows if NOT NULL is not used in conjunction with UNIQUE
UNIQUE and PRIMARY KEY are not used together

Example - create table with column constraints :

CREATE TABLE Salesperson
(empid NUMBER(5) CONSTRAINT Salesperson_empid_pk PRIMARY KEY,
ename CHAR(15) CONSTRAINT
Salesperson_ename_nn NOT NULL,
rank NUMBER(1) DEFAULT 1 CONSTRAINT
Salesperson_rank_cc CHECK (rank IN (1,2,3)),
salary NUMBER(7,2) CONSTRAINT
Salesperson_salary_nn NOT NULL
CONSTRAINT Salesperson_salary_cc
CHECK (salary >= 1000.00));

eg: without constraint names:

CREATE TABLE Customer
(custid NUMBER(5) PRIMARY KEY,
cname CHAR(15) NOT NULL,
caddress VARCHAR2(25) NOT NULL,
credit CHAR(1) NOT NULL
CHECK (credit IN (‘A’, ‘B’, ‘C’)));