Integrity Constraints
Integrity Constraints
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.
Part Table:
SQL> create table part(part_no number(4),part_name char(10), constraint
pk_pname unique(part_name));
Table created.
Table altered.
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.
Table altered.
Table altered.
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:
1 row created.
SUPPLIER_NO COMPANY
----------- --------
10 xyz inc
Constraints
SQL> insert into supplier values(1,'abc inc');
1 row created.
SUPPLIER_NO COMPANY
----------- --------
10 xyz inc
1 abc inc
Constraints
Primary Key - Unique constraint – supplier_no - supplier
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
1 row created.
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
Table altered.
PART_NO PART_NAME
---------- ----------
100 bolt
200 nut
300 screw
400 screw
Constraints
SQL> insert into shipment values(20, 500, 2300);
1 row created.
Table altered.
PART_NO PART_NAME
---------- ----------
100 bolt
200 nut
300 screw
400 screw
Constraints
SQL> insert into part values(400,'screw');
1 row created.
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
Table altered.
Table altered.
1 row created.
Constraints
7. Default Constraints
1 row created.