You are on page 1of 9

Constraints in DBMS

Prepared by Harish Patnaik

School of Computer Engineering, KIIT Deemed to be University


Content

1. Intro to constraint
2. Domain integrity constraint
1. Entity integrity constraint
2. Referential integrity constraint
Introduction
• Success of a database system depends on
security, integrity and friendliness of the
system.
• Constraints are guidelines or limitations
imposed on database tables to maintain the
integrity, correctness, and consistency of data.
• Constraints can be used to
• verify uniqueness of the data
• prevent insertion of erroneous data
• enforce data linkages across tables
• Types of integrity constraint
• Domain integrity constraint
• Entity integrity constraint
• Referential integrity constraint
Domain integrity constraint
• These constraints set a range
• Types of domain integrity constraint
• Not null constraint - forces a value
• Can be enforced during creation of table
• Table can be altered even after creation
create table tname ( field1 type, field2 type
constraint cname not null, field3 type);
alter table tname modify field1 not null;
Domain integrity constraint

• Check constraint - range of values


• Can be enforced during creation of table
• Table can be altered even after creation
create table tname (field1 type, field2 type
constraint cname check(field2>18), field3 type);
alter table tname add constraint cname
check(field2>18);
Entity integrity constraint
• These constraints set a range
• Types of entity integrity constraint
i. unique constraint - prevents duplicates but
allows null value
• Can be enforced during creation of table
• Table can be altered even after creation
create table tname ( field1 type, field2 type,
field3 type constraint cname unique(field2));
alter table tname add constraint cname
unique( field2);
Entity integrity constraint

ii. Primary key constraint - prevents


duplicates, doesn’t allow null values
• Can be enforced during creation of table
create table tname ( field1 type constraint
cname pimary key, field2 type, field3 type);
Referential integrity constraint
• Establishes parent-child relationship between
two tables having a common field
• Field in the parent table should be a primary
key
• Can be enforced during creation of table
• Table can be altered even after creation
create table tname ( field1 type constraint cname
references table1( field2),field2 type, field3 type);
alter table tname add constraint cname foreign key
(field1) references table1(field2);

You might also like