Integrity Constraint 
and 
Referential Integrity 
Made By: 
Suman Kumar 
0802cs133D11 Guided By: 
Mr Dinesh Chandra Jain
SQL Constraint Meaning 
Syntax Of Create Constraint 
Constraint Type
Constraints are used to prohibit 
illegal work in to a database 
 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)
You can define constraints in two ways: 
1. As part of the definition of an individual 
column or attribute. This is called inline 
specification Or called column level 
definition 
2. As part of the table definition. 
This is called out-of-line specification 
Or called table level definition
Syntax For Create Constraint 
Sql> CREATE TABLE <table name>( 
<attribute name> DATA TYPE (<size>) [constraint], 
<attribute name> DATA TYPE (<size>) [constraint], 
<attribute name> DATA TYPE (<size>) [constraint], 
……………………. 
…………………… 
); 
NOTE: [ ]->Optional,( )->Required, < >->Required & depend 
on programmer;
1. Not Null 
2. Unique Key 
3. Primary Key 
4. Foreign Key 
5. Check Key 
6. Default Key
Not Null 
 constraint enforces a column to NOT accept 
NULL values. 
 This means that you cannot insert a 
new record, or update a record without 
adding a value to this field. 
 Expression of Not Null we use short cut NN
1. Not Null Example :- 
Sql>CREATE TABLE customer 
( 
c_code CHAR (3) PRIMERY KEY, 
c_name CHAR (20) NOT NULL, 
c_city CHAR (10) DEFAULT “patna”, 
gender CHAR (6) CHECK (gender=“Male” 
OR gender=“Female”), 
mob_no CHAR (14) UNIQUE, 
);
All other constraints 
can be declared either 
inline or out of line.
2. Unique Key 
This constraint ensures that a column or 
a group of columns in each row have a 
distinct value. A column(s) can have a 
null value but the values cannot be 
duplicated.
2. Unique Key Example 
Sql>CREATE TABLE customer 
( 
c_code CHAR (3) PRIMERY KEY, 
c_name CHAR (20) NOT NULL, 
c_city CHAR (10) DEFAULT “patna”, 
gender CHAR (6) CHECK 
(gender=“Male” OR 
gender=“Female”) 
mob_no CHAR (14) UNIQUE, 
);
3. Primary Key 
This constraint defines a column or combination of 
columns which uniquely identifies each row in the 
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
3. Primary Key Example 
Sql>CREATE TABLE product 
( 
p_code CHAR (3) PRIMERY KEY, 
p_name CHAR (20) NOT NULL, 
p_rate NUMBER (5,2) CHECK 
(c_rate>0), 
qoh NUMBER(2), 
);
Unique Key Vs Primary 
Unique 
Key 
Unique Key 
Key 
1.Unique key use 
many times in a table 
2Unique key accept 
only one null value 
Primary 
Key 
1.Primary key use only 
one times in a table 
2 Primary key does not 
accept null value
4. Foreign Key or Referential 
Integrity 
Unique Key 
This constraint identifies any column referencing the 
PRIMARY KEY in another table. It establishes a relationship 
between two columns in the same table or between different 
tables. For a column to be defined as a Foreign Key, it 
should be a defined as a Primary Key in the table which it is 
referring. One or more columns can be defined as Foreign 
key. 
 A FOREIGN KEY in one table points to a 
PRIMARY KEY in another table 
 The FOREIGN KEY constraint is used 
to prevent actions that would destroy
4. Foreign Key Example 
Sql>CREATE TABLE sales 
( 
c_code CHAR(3) REFERENCES 
Unique customer Key 
(c_code), 
p_code CHAR(3) REFERENCES 
product (p_code), 
qty NUMBER (4) CHECK (qty>=0), 
);
Structure of Customer 
table 
Unique Key 
The “Costomer" table: 
Column name Data type Size Constraint 
C_code Char/text 3 PRIMARY KEY 
C_name Char/text 20 NOT NULL 
C_city Char/text 20 DEFAULT(Patna) 
gender Char/text 6 CHECK(male,female) 
Mob_no Char/text 14 UNIQUE,NOT NULL
Structure of Product 
table 
Unique Key 
The “Product" table: 
Column name Data type Size Constraint 
p_code Char/text 3 PRIMARY KEY 
p_name Char/text 20 NOT NULL 
P_rate Number 5,2 CHECK(>0),NOT NULL 
Qoh Number 5 CHECK(>=0),NOT NULL
Structure of Sales table 
Unique Key 
The “Sales" table: 
Column 
name 
Data type Size Constraint 
c_code Char/text 3 FOREIGN KEY,(RFFERENCES 
CUSTOMER (c_code)) 
p_code Char/text 3 FOREIGN KEY,(RFFERENCES 
PRODUCT (p_code)) 
qty Number 5 CHECK(>0),NOT NULL
5. Check Key 
This constraint defines a business rule on a 
column. All the rows must satisfy this rule. The 
constraint can be applied for a single column or a 
group of columns.
5. Check Key Example 
Sql>CREATE TABLE product 
( 
p_code CHAR(3) PRIMERY KEY, 
p_name CHAR(20) NOT NULL, 
p_rate NUMBER(5,2) CHECK (c_rate>0), 
qoh NUMBER(2), 
);
65.. DCehefacukl tK Keeyy 
 The DEFAULT constraint is used to 
insert a default value into a column 
 The default value will be added to 
all new records 
if no other value is specified.
65.. DCehefacukl tK Keeyy Example 
Sql>CREATE TABLE customer 
( 
c_code CHAR (3) PRIMERY KEY, 
c_name CHAR (20) NOT NULL, 
c_city CHAR (10) DEFAULT “patna”, 
gender CHAR (6) CHECK 
(gender=“Male” OR gender=“Female”) 
mob_no CHAR (14) UNIQUE, 
);
Some Other Keys 
Composite Key : 
A composite key is a combination of more 
than one column to identify a unique row in a 
table. 
Candidate Key: 
All keys in a table that become unique called 
as candidate key. Ex-email id,mob no 
Alternate Key: 
Among of candidate keys if any single key or 
combination of keys made as primary key 
then rest candidate key called as alternate 
key.
Thank you for listen

Entigrity constraint

  • 1.
    Integrity Constraint and Referential Integrity Made By: Suman Kumar 0802cs133D11 Guided By: Mr Dinesh Chandra Jain
  • 2.
    SQL Constraint Meaning Syntax Of Create Constraint Constraint Type
  • 3.
    Constraints are usedto prohibit illegal work in to a database  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)
  • 4.
    You can defineconstraints in two ways: 1. As part of the definition of an individual column or attribute. This is called inline specification Or called column level definition 2. As part of the table definition. This is called out-of-line specification Or called table level definition
  • 5.
    Syntax For CreateConstraint Sql> CREATE TABLE <table name>( <attribute name> DATA TYPE (<size>) [constraint], <attribute name> DATA TYPE (<size>) [constraint], <attribute name> DATA TYPE (<size>) [constraint], ……………………. …………………… ); NOTE: [ ]->Optional,( )->Required, < >->Required & depend on programmer;
  • 6.
    1. Not Null 2. Unique Key 3. Primary Key 4. Foreign Key 5. Check Key 6. Default Key
  • 7.
    Not Null constraint enforces a column to NOT accept NULL values.  This means that you cannot insert a new record, or update a record without adding a value to this field.  Expression of Not Null we use short cut NN
  • 8.
    1. Not NullExample :- Sql>CREATE TABLE customer ( c_code CHAR (3) PRIMERY KEY, c_name CHAR (20) NOT NULL, c_city CHAR (10) DEFAULT “patna”, gender CHAR (6) CHECK (gender=“Male” OR gender=“Female”), mob_no CHAR (14) UNIQUE, );
  • 10.
    All other constraints can be declared either inline or out of line.
  • 11.
    2. Unique Key This constraint ensures that a column or a group of columns in each row have a distinct value. A column(s) can have a null value but the values cannot be duplicated.
  • 12.
    2. Unique KeyExample Sql>CREATE TABLE customer ( c_code CHAR (3) PRIMERY KEY, c_name CHAR (20) NOT NULL, c_city CHAR (10) DEFAULT “patna”, gender CHAR (6) CHECK (gender=“Male” OR gender=“Female”) mob_no CHAR (14) UNIQUE, );
  • 13.
    3. Primary Key This constraint defines a column or combination of columns which uniquely identifies each row in the 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
  • 14.
    3. Primary KeyExample Sql>CREATE TABLE product ( p_code CHAR (3) PRIMERY KEY, p_name CHAR (20) NOT NULL, p_rate NUMBER (5,2) CHECK (c_rate>0), qoh NUMBER(2), );
  • 15.
    Unique Key VsPrimary Unique Key Unique Key Key 1.Unique key use many times in a table 2Unique key accept only one null value Primary Key 1.Primary key use only one times in a table 2 Primary key does not accept null value
  • 16.
    4. Foreign Keyor Referential Integrity Unique Key This constraint identifies any column referencing the PRIMARY KEY in another table. It establishes a relationship between two columns in the same table or between different tables. For a column to be defined as a Foreign Key, it should be a defined as a Primary Key in the table which it is referring. One or more columns can be defined as Foreign key.  A FOREIGN KEY in one table points to a PRIMARY KEY in another table  The FOREIGN KEY constraint is used to prevent actions that would destroy
  • 17.
    4. Foreign KeyExample Sql>CREATE TABLE sales ( c_code CHAR(3) REFERENCES Unique customer Key (c_code), p_code CHAR(3) REFERENCES product (p_code), qty NUMBER (4) CHECK (qty>=0), );
  • 18.
    Structure of Customer table Unique Key The “Costomer" table: Column name Data type Size Constraint C_code Char/text 3 PRIMARY KEY C_name Char/text 20 NOT NULL C_city Char/text 20 DEFAULT(Patna) gender Char/text 6 CHECK(male,female) Mob_no Char/text 14 UNIQUE,NOT NULL
  • 19.
    Structure of Product table Unique Key The “Product" table: Column name Data type Size Constraint p_code Char/text 3 PRIMARY KEY p_name Char/text 20 NOT NULL P_rate Number 5,2 CHECK(>0),NOT NULL Qoh Number 5 CHECK(>=0),NOT NULL
  • 20.
    Structure of Salestable Unique Key The “Sales" table: Column name Data type Size Constraint c_code Char/text 3 FOREIGN KEY,(RFFERENCES CUSTOMER (c_code)) p_code Char/text 3 FOREIGN KEY,(RFFERENCES PRODUCT (p_code)) qty Number 5 CHECK(>0),NOT NULL
  • 21.
    5. Check Key This constraint defines a business rule on a column. All the rows must satisfy this rule. The constraint can be applied for a single column or a group of columns.
  • 22.
    5. Check KeyExample Sql>CREATE TABLE product ( p_code CHAR(3) PRIMERY KEY, p_name CHAR(20) NOT NULL, p_rate NUMBER(5,2) CHECK (c_rate>0), qoh NUMBER(2), );
  • 23.
    65.. DCehefacukl tKKeeyy  The DEFAULT constraint is used to insert a default value into a column  The default value will be added to all new records if no other value is specified.
  • 24.
    65.. DCehefacukl tKKeeyy Example Sql>CREATE TABLE customer ( c_code CHAR (3) PRIMERY KEY, c_name CHAR (20) NOT NULL, c_city CHAR (10) DEFAULT “patna”, gender CHAR (6) CHECK (gender=“Male” OR gender=“Female”) mob_no CHAR (14) UNIQUE, );
  • 25.
    Some Other Keys Composite Key : A composite key is a combination of more than one column to identify a unique row in a table. Candidate Key: All keys in a table that become unique called as candidate key. Ex-email id,mob no Alternate Key: Among of candidate keys if any single key or combination of keys made as primary key then rest candidate key called as alternate key.
  • 26.