0% found this document useful (0 votes)
8 views38 pages

Integrity Constraints

The document provides an overview of integrity constraints in database management systems, detailing various types such as NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, and CHECK constraints. It includes SQL syntax for creating, modifying, and dropping constraints, along with examples demonstrating their application and violation. Additionally, it discusses the implications of constraints on data integrity and referential integrity within relational databases.

Uploaded by

justice.chitra.v
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views38 pages

Integrity Constraints

The document provides an overview of integrity constraints in database management systems, detailing various types such as NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, and CHECK constraints. It includes SQL syntax for creating, modifying, and dropping constraints, along with examples demonstrating their application and violation. Additionally, it discusses the implications of constraints on data integrity and referential integrity within relational databases.

Uploaded by

justice.chitra.v
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

CSE2004: Database Management System

Integrity Constraints
Topics to be Covered
 Set Constraints – Create Table
 Add Constraints – Alter Table
 Modify Constraints – Alter Table
 Drop Constraints – Alter Table
Types of SQL Constraints
 NOT NULL Constraint: Ensures that a column cannot have NULL
value.
 DEFAULT Constraint: Provides a default value for a column when
none is specified.
 UNIQUE Constraint: Ensures that all values in a column are different.
 PRIMARY Key: Uniquely identified each rows/records in a database
table.
 FOREIGN Key: Uniquely identified a rows/records in any another
database table.
 CHECK Constraint: The CHECK constraint ensures that all values in
a column satisfy certain conditions.
Syntax
create table table_name(attribute_name1 datatype(size),
attribute_name2 datatype(size),
constraint constraint_name
constraint_type(attribute_name1));
Schema
supplier(supplier_no, company)
part(part_no, part_name)
shipment(supplier_no, part_no, quantity)
Constraints
1. Primary Key Constraints
Create table with primary key

Supplier Table:
SQL> create table supplier(supplier_no number(3),company char(8),constraint
pk_sno primary key(supplier_no));

Table created.

SQL> desc supplier


Name Null? Type
----------------------------------------- -------- ----------------
SUPPLIER_NO NOT NULL NUMBER(3)
COMPANY CHAR(8)
Constraints
Create table without Primary Key and with unique constraint.

Part Table:
SQL> create table part(part_no number(4),part_name char(10), constraint
pk_pname unique(part_name));

Table created.

SQL> desc part


Name Null? Type
----------------------------------------- -------- -----------------
PART_NO NUMBER(4)
PART_NAME CHAR(10)
Constraints
Add primary key to part table

SQL> alter table part add constraint pk_pno primary key(part_no);

Table altered.

SQL> desc part


Name Null? Type
----------------------------------------- -------- --------------
PART_NO NOT NULL NUMBER(4)
PART_NAME CHAR(10)
Constraints
2. Foreign Key Constraints
Create foreign key constraint

Shipment Table
SQL> create table shipment(supplier_no number(3), part_no number(4),
constraint fk_sno foreign key(supplier_no) references supplier, constraint
fk_pno foreign key(part_no) references part);

Table created.

SQL> desc shipment;


Name Null? Type
----------------------------------------- -------- -----------------
SUPPLIER_NO NUMBER(3)
PART_NO NUMBER(4)
Constraints
Add an attribute quantity to shipment table

SQL> alter table shipment add quantity number(5);

Table altered.

SQL> desc shipment


Name Null? Type
----------------------------------------- -------- --------------
SUPPLIER_NO NUMBER(3)
PART_NO NUMBER(4)
QUANTITY NUMBER(5)
Constraints
Add a check constraint to part table

SQL> alter table shipment add constraint ck_qty check(quantity>0);

Table altered.

SQL> desc shipment


Name Null? Type
----------------------------------------- -------- ------------------
SUPPLIER_NO NUMBER(3)
PART_NO NUMBER(4)
QUANTITY NUMBER(5)
Constraints
Constraints Set:

Constraint
Attribute Referencing
Table Name Name – User Constraint Type
Name Table
defined
supplier_no supplier pk_sno primary key -
part_name part pk_pname unique -
part_no part pk_pno primary key -
supplier_no shipment fk_sno foreign key supplier
part_no shipment fk_pno part part
quantity shipment ck_qty check -
Constraints
Violation of Constraints:

Primary Key – supplier_no – supplier


Note: Primary key together satisfies unique and not null constraints
SQL> insert into supplier values(10,'xyz inc');

1 row created.

SQL> select * from supplier;

SUPPLIER_NO COMPANY
----------- --------
10 xyz inc
Constraints
SQL> insert into supplier values(1,'abc inc');

1 row created.

SQL> select * from supplier;

SUPPLIER_NO COMPANY
----------- --------
10 xyz inc
1 abc inc
Constraints
Primary Key - Unique constraint – supplier_no - supplier

SQL> insert into supplier values(10,'avg org');


insert into supplier values(10,'avg org')
*
ERROR at line 1:
ORA-00001: unique constraint (SYSTEM.PK_SNO) violated
Constraints
SQL> insert into supplier values(20,'xyz inc');
1 row created.

SQL> insert into supplier values(40,'speedy');


1 row created.

SQL> select * from supplier;


SUPPLIER_NO COMPANY
----------- --------
Note: company is same for
supplier_no 10 and 20 since
10 xyz inc
there exists no unique
1 abc inc
constraint for company.
20 xyz inc
40 speedy
Constraints
Primary Key - Not Null constraint - supplier_no - supplier

SQL> insert into supplier values(null,'speedy');


insert into supplier values(null,'speedy')
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SYSTEM"."SUPPLIER"."SUPPLIER_NO")

Note: Doesn’t allow null values for primary key


Constraints
2. Unique Constraint – part_name, primary key – part_no - part
SQL> insert into part values(100,'bolt');
1 row created.
SQL> insert into part values(200,'nut');
1 row created.
SQL> insert into part values(300,'screw');
1 row created.
SQL> select * from part;
PART_NO PART_NAME
---------- ----------
100 bolt
200 nut
300 screw
Constraints
Unique constraint violation for part_name - part
SQL> insert into part values(300,'screw');
insert into part values(300,'screw')
*
ERROR at line 1:
ORA-00001: unique constraint (SYSTEM.PK_PNAME) violated

Unique constraint violation for part_name - part


SQL> insert into part values(400,'screw');
insert into part values(400,'screw')
*
ERROR at line 1:
ORA-00001: unique constraint (SYSTEM.PK_PNAME) violated
Constraints
Unique constraint violation for part_no - part

SQL> insert into part values(300,'spanner');


insert into part values(300,'spanner')
*
ERROR at line 1:
ORA-00001: unique constraint (SYSTEM.PK_PNO) violated
Constraints
3. Check constraint – quantity - shipment
SQL> insert into shipment values(10, 200, 5000);
1 row created.
SQL> insert into shipment values(10, 300, 4000);
1 row created.
SQL> insert into shipment values(40, 100, 1000);
1 row created.
SQL> select * from shipment;
SUPPLIER_NO PART_NO QUANTITY
----------- ---------- ----------
10 200 5000
10 300 4000
40 100 1000
Constraints
SQL> insert into shipment values(40, 200, -1);
insert into shipment values(40, 200, -1)
*
ERROR at line 1:
ORA-02290: check constraint (SYSTEM.CK_QTY) violated

SQL> insert into shipment values(40, 200, 0);


insert into shipment values(40, 200, 0)
*
ERROR at line 1:
ORA-02290: check constraint (SYSTEM.CK_QTY) violated

Note: The value of quantity attribute should be always greater than 0.


Constraints
4. Referential Integrity Constraint – Foreign key – shipment
SQL> select * from part;

PART_NO PART_NAME
SQL> select * from supplier; ---------- ----------
100 bolt
SUPPLIER_NO COMPANY 200 nut
----------- -------- 300 screw
10 xyz inc
1 abc inc SQL> select * from shipment;
20 xyz inc
40 speedy SUPPLIER_NO PART_NO QUANTITY
----------- ---------- ----------
10 200 5000
10 300 4000
40 100 1000
Constraints
Note:
1. Permissible values for supplier_no in shipment are 10, 1, 20 and 40
as referred in supplier table
2. Permissible values for part_no in shipment are 100, 200 and 300 as
referred In supplier table
SQL> insert into shipment values(30, 200, 2000);
insert into shipment values(30, 200, 2000)
*
ERROR at line 1:
ORA-02291: integrity constraint (SYSTEM.FK_SNO) violated - parent key
not found

Note: 30 cannot be inserted to supplier_no in shipment table.


Constraints
SQL> insert into shipment values(20, 400, 2000);
insert into shipment values(20, 400, 2000)
*
ERROR at line 1:
ORA-02291: integrity constraint (SYSTEM.FK_PNO) violated - parent key
not found

Note: 4000 cannot be inserted to part_no in shipment table.


Constraints
5. Drop Constraints:
Drop a check constraint from quantity in shipment table
SQL> alter table shipment drop constraint ck_qty;
Table altered.
SQL> insert into shipment values(20,200,0);
1 row created.
SQL> select * from shipment;
SUPPLIER_NO PART_NO QUANTITY
----------- ---------- ----------
10 200 5000
10 300 4000
40 100 1000
20 200 0
Note: Since check constraint is dropped, it allows the value 0
Constraints
Drop a unique constraint from part_name in part table
SQL> alter table part drop constraint pk_pname;
Table altered.

SQL> select * from part;


PART_NO PART_NAME
---------- ----------
100 bolt
200 nut
300 screw
Constraints
SQL> insert into part values(400,'screw');

1 row created.

SQL> select * from part;

PART_NO PART_NAME
---------- ----------
100 bolt
200 nut
300 screw
400 screw
Note: Since unique check constraint is dropped, it allows the value
duplications for part_name
Constraints
Drop a foreign key constraint from part_no in shipment table

SQL> alter table shipment drop constraint fk_pno;

Table altered.

SQL> select * from part;

PART_NO PART_NAME
---------- ----------
100 bolt
200 nut
300 screw
400 screw
Constraints
SQL> insert into shipment values(20, 500, 2300);
1 row created.

SQL> select * from shipment;


SUPPLIER_NO PART_NO QUANTITY
----------- ---------- ----------
10 200 5000
10 300 4000
40 100 1000
20 200 0
20 500 2300
Note: Since foreign key constraint is dropped from part_no, it allows
the value 500 which actually is not referred in part_no of part table
Constraints
Drop a primary key constraint from part_no in part table

SQL> alter table part drop constraint pk_pno;

Table altered.

SQL> select * from part;

PART_NO PART_NAME
---------- ----------
100 bolt
200 nut
300 screw
400 screw
Constraints
SQL> insert into part values(400,'screw');

1 row created.

SQL> select * from part;


PART_NO PART_NAME
---------- ----------
100 bolt
200 nut
300 screw
400 screw
400 screw
Note: Since primary key is dropped, it allows duplications even in
row level and column level.
Constraints
Drop a primary key constraint from supplier_no in supplier table

SQL> alter table supplier drop constraint pk_sno;


alter table supplier drop constraint pk_sno
*
ERROR at line 1:
ORA-02273: this unique/primary key is referenced by some foreign keys

Note:
Since supplier_no is still the referred in shipment table, supplier
table’s primary key cannot be dropped.
A primary key can be dropped only if the entire foreign keys it
referred were dropped.
Constraints
6.Not Null Constraints

Create Not Null Constraint

SQL> create table person(pid number(4) not null, pname char(7));


Table created.
SQL> insert into person values(1,'sai'); SQL> select * from
1 row created. person;
SQL> insert into person values(null,'raj');
insert into person values(null,'raj')
PID PNAME
---------- -------
*
1 sai
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SYSTEM"."PERSON"."PID")
Constraints
Add Not Null Constraint to pname in person table

SQL> alter table person modify pname char(7) not null;

Table altered.

SQL> insert into person values(2,null);


insert into person values(2,null)
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SYSTEM"."PERSON"."PNAME")
Constraints
Drop Not Null Constraint to pname in person table

SQL> alter table person modify pname char(7) null;

Table altered.

SQL> insert into person values(2,null);

1 row created.
Constraints
7. Default Constraints

Create default constraint


SQL> create table player(pno number(2), pname char(7), country
char(10) default 'india');
Table created.
SQL> insert into player(pname) values('sachin');
1 row created.
SQL> select * from player;

PNO PNAME COUNTRY


---------- ------- ----------
sachin india
Constraints
Note: The value for pno and country attribute is not inserted. When
the value is not inserted for country attribute, it considers the
default value ‘india’. Else it considers the inserted value.

SQL> insert into player values(1,'watson','australia');

1 row created.

SQL> select * from player;

PNO PNAME COUNTRY


---------- ------- ----------
sachin india
1 watson australia

You might also like