SlideShare a Scribd company logo
1 of 26
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

More Related Content

What's hot

Sql basics
Sql basicsSql basics
Sql basicsKumar
 
SQL Quick Reference Card
SQL Quick Reference CardSQL Quick Reference Card
SQL Quick Reference CardTechcanvass
 
Constraints In Sql
Constraints In SqlConstraints In Sql
Constraints In SqlAnurag
 
06.01 sql select distinct
06.01 sql select distinct06.01 sql select distinct
06.01 sql select distinctBishal Ghimire
 
Sql clauses by Manan Pasricha
Sql clauses by Manan PasrichaSql clauses by Manan Pasricha
Sql clauses by Manan PasrichaMananPasricha
 
Database Management - Lecture 3 - SQL Aggregate Functions, Join
Database Management - Lecture 3 - SQL Aggregate Functions, JoinDatabase Management - Lecture 3 - SQL Aggregate Functions, Join
Database Management - Lecture 3 - SQL Aggregate Functions, JoinAl-Mamun Sarkar
 
Intro to tsql unit 3
Intro to tsql   unit 3Intro to tsql   unit 3
Intro to tsql unit 3Syed Asrarali
 
Database Systems - SQL - DDL Statements (Chapter 3/2)
Database Systems - SQL - DDL Statements (Chapter 3/2)Database Systems - SQL - DDL Statements (Chapter 3/2)
Database Systems - SQL - DDL Statements (Chapter 3/2)Vidyasagar Mundroy
 
Null values, insert, delete and update in database
Null values, insert, delete and update in databaseNull values, insert, delete and update in database
Null values, insert, delete and update in databaseHemant Suthar
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for BeginnersAbdelhay Shafi
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteAl-Mamun Sarkar
 

What's hot (18)

Les02
Les02Les02
Les02
 
Sql commands
Sql commandsSql commands
Sql commands
 
Sql basics
Sql basicsSql basics
Sql basics
 
SQL Quick Reference Card
SQL Quick Reference CardSQL Quick Reference Card
SQL Quick Reference Card
 
Constraints In Sql
Constraints In SqlConstraints In Sql
Constraints In Sql
 
06.01 sql select distinct
06.01 sql select distinct06.01 sql select distinct
06.01 sql select distinct
 
Sql reference from w3 schools
Sql reference from w3 schools Sql reference from w3 schools
Sql reference from w3 schools
 
Sql clauses by Manan Pasricha
Sql clauses by Manan PasrichaSql clauses by Manan Pasricha
Sql clauses by Manan Pasricha
 
Database Management - Lecture 3 - SQL Aggregate Functions, Join
Database Management - Lecture 3 - SQL Aggregate Functions, JoinDatabase Management - Lecture 3 - SQL Aggregate Functions, Join
Database Management - Lecture 3 - SQL Aggregate Functions, Join
 
MY SQL
MY SQLMY SQL
MY SQL
 
Intro to tsql unit 3
Intro to tsql   unit 3Intro to tsql   unit 3
Intro to tsql unit 3
 
Database Systems - SQL - DDL Statements (Chapter 3/2)
Database Systems - SQL - DDL Statements (Chapter 3/2)Database Systems - SQL - DDL Statements (Chapter 3/2)
Database Systems - SQL - DDL Statements (Chapter 3/2)
 
Five Common SQL Errors
Five Common SQL ErrorsFive Common SQL Errors
Five Common SQL Errors
 
MULTIPLE TABLES
MULTIPLE TABLES MULTIPLE TABLES
MULTIPLE TABLES
 
SQL Introduction to displaying data from multiple tables
SQL Introduction to displaying data from multiple tables  SQL Introduction to displaying data from multiple tables
SQL Introduction to displaying data from multiple tables
 
Null values, insert, delete and update in database
Null values, insert, delete and update in databaseNull values, insert, delete and update in database
Null values, insert, delete and update in database
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for Beginners
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and delete
 

Similar to Entigrity constraint

Sql integrity constraints
Sql integrity constraintsSql integrity constraints
Sql integrity constraintsVivek Singh
 
Sql server ___________session_15(data integrity)
Sql server  ___________session_15(data integrity)Sql server  ___________session_15(data integrity)
Sql server ___________session_15(data integrity)Ehtisham Ali
 
e computer notes - Including constraints
e computer notes - Including constraintse computer notes - Including constraints
e computer notes - Including constraintsecomputernotes
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commandsBelle Wx
 
Sql ch 12 - creating database
Sql ch 12 - creating databaseSql ch 12 - creating database
Sql ch 12 - creating databaseMukesh Tekwani
 
03Constraints - last.pdf
03Constraints - last.pdf03Constraints - last.pdf
03Constraints - last.pdfssuserfd620b
 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...SakkaravarthiS1
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server iiIblesoft
 
2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL UsedTheVerse1
 
Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)Achmad Solichin
 

Similar to Entigrity constraint (20)

Sql integrity constraints
Sql integrity constraintsSql integrity constraints
Sql integrity constraints
 
Sql server ___________session_15(data integrity)
Sql server  ___________session_15(data integrity)Sql server  ___________session_15(data integrity)
Sql server ___________session_15(data integrity)
 
Sql
SqlSql
Sql
 
Oraclesql
OraclesqlOraclesql
Oraclesql
 
Integrity and security
Integrity and securityIntegrity and security
Integrity and security
 
e computer notes - Including constraints
e computer notes - Including constraintse computer notes - Including constraints
e computer notes - Including constraints
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
 
Sql ch 12 - creating database
Sql ch 12 - creating databaseSql ch 12 - creating database
Sql ch 12 - creating database
 
Sql commands
Sql commandsSql commands
Sql commands
 
Database testing
Database testingDatabase testing
Database testing
 
DBMS.pptx
DBMS.pptxDBMS.pptx
DBMS.pptx
 
Les11
Les11Les11
Les11
 
03Constraints - last.pdf
03Constraints - last.pdf03Constraints - last.pdf
03Constraints - last.pdf
 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
 
chapter 8 SQL.ppt
chapter 8 SQL.pptchapter 8 SQL.ppt
chapter 8 SQL.ppt
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
 
3. DDL.pdf
3. DDL.pdf3. DDL.pdf
3. DDL.pdf
 
2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used
 
Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)
 
Les09
Les09Les09
Les09
 

Recently uploaded

Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01KreezheaRecto
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfRagavanV2
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdfSuman Jyoti
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfrs7054576148
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 

Recently uploaded (20)

Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 

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 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)
  • 4. 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
  • 5. 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;
  • 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 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, );
  • 9.
  • 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 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, );
  • 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 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), );
  • 15. 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
  • 16. 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
  • 17. 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), );
  • 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 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
  • 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 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), );
  • 23. 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.
  • 24. 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, );
  • 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. Thank you for listen