SlideShare a Scribd company logo
Introduction
Referential
Integrity
referential integrity
► A referential integrity constraint is also known as foreign key
constraint. A foreign key is a key whose values are derived from the
Primary key of another table.
► The table from which the values are derived is known as Master or
Referenced Table and the Table in which values are inserted
accordingly is known as Child or Referencing Table.
► In other words, we can say that the table containing the foreign
key is called the child table, and the table containing the Primary
key/candidate key is called the referenced or parent table.
► The syntax of the Master Table or Referenced table is:
► CREATE TABLE Student (Roll int PRIMARY KEY,
Name varchar(25) , Course varchar(10) )
SQL Constraint
CREATE TABLE Subject (Roll int ,
SubCode int, SubName varchar(10)
FOREIGN KEY (Roll) REFERENCES Studnet(Roll)
);
In the above table, column Roll is acting as Foreign Key, whose
values are derived using the Roll value of Primary key from Master
table.
Referential Integrity constraint
►Foreign Key Constraint OR Referential Integrity constraint.
►There are two referential integrity constraint:
►Insert Constraint: Value cannot be inserted in CHILD Table if the value is not
lying in MASTER Table
►Delete Constraint: Value cannot be deleted from MASTER Table if the value is
lying in CHILD Table
►Suppose you wanted to insert Roll = 05 with other values of columns in SUBJECT
Table, then you will immediately see an error "Foreign key Constraint
Violated" i.e. on running an insertion command as:
►Insert into SUBJECT values(5, 786, OS)
will not be entertained by SQL due to Insertion Constraint
( As you cannot insert value in a child table if the value is not lying in the
master table, since Roll = 5 is not present in the master table, hence it will not
be allowed to enter Roll = 5 in child table )
►Similarly, if you want to delete Roll = 4 from STUDENT Table, then you will
immediately see an error "Foreign key Constraint Violated" i.e. on running a
Referential Integrity constraint
Delete from STUDENT where Roll = 4; will not be entertained by SQL due to
Deletion Constraint.
( As you cannot delete the value from the master table if the value is lying in
the child table, since Roll = 5 is present in the child table, hence it will not be
allowed to delete Roll = 5 from the master table, lets if somehow we managed
to delete Roll = 5, then Roll = 5 will be available in child table which will
ultimately violate insertion constraint. )
►ON DELETE CASCADE.
►As per deletion constraint: Value cannot be deleted from the MASTER Table if
the value is lying in CHILD Table.
►The next question comes can we delete the value from the master table if the
value is lying in the child table without violating the deletion constraint? i.e.
►The moment we delete the value from the master table the value
corresponding to it should also get deleted from the child table.
►The answer to the above question is YES, we can delete the value from the
master table if the value is lying in the child table without violating the
deletion constraint, we have to do slight modification while creating the child
table, i.e. by adding on delete cascade.
Referential Integrity constraint
TABLE SYNTAX
CREATE TABLE Subject (Roll int ,
SubCode int, SubName varchar(10)
FOREIGN KEY (Roll) REFERENCES Studnet(Roll)
On delete cascade);
In the above syntax, just after references keyword( used for creating foreign
key), we have added on delete cascade, by adding such now, we can delete the
value from the master table if the value is lying in the child table without
violating deletion constraint. Now if you wanted to delete Roll = 5 from the
master table even though Roll = 5 is lying in the child table, it is possible
because the moment you give the command to delete Roll = 5 from the master
table, the row having Roll = 5 from child table will also get deleted.
Refrential Integrity
The above two tables STUDENT
and SUBJECT having four values
each are shown, now suppose you
are looking to delete Roll = 4 from
STUDENT( Master ) Table by
writing a SQL command:
delete from STUDENT where
Roll = 4;
The moment SQL execute the
above command the row
having Roll = 4 from SUBJECT(
Child ) Table will also get
deleted, The
resultant STUDENT and
SUBJECT table will look like:
Refrential Integrity
From the above two tables STUDENT and
SUBJECT, you can see that in both the
table Roll = 4 gets deleted at one go
without violating deletion constraint.
Sometimes a very important question is
asked in interviews that: Can Foreign
Key have NULL values?
The answer to the above question is
YES, it may have NULL values, whereas
the Primary key cannot be NULL at any
cost. To understand the above question
practically let's understand below the
concept of delete null.
Refrential Integrity
ON DELETE NULL.
As per deletion constraint: Value cannot be deleted from the MASTER Table if
the value is lying in CHILD Table.
The next question comes can we delete the value from the master table if the
value is lying in the child table without violating the deletion constraint? i.e.
The moment we delete the value from the master table the value
corresponding to it should also get deleted from the child table or can be
replaced with the NULL value.
The answer to the above question is YES, we can delete the value from the
master table if the value is lying in child table without violating deletion
constraint by inserting NULL in the foreign key, we have to do slight
modification while creating child table, i.e. by adding on delete null.
Refrential Integrity
TABLE SYNTAX:
TABLE SYNTAX
CREATE TABLE Subject (Roll int ,
SubCode int, SubName varchar(10)
FOREIGN KEY (Roll) REFERENCES Studnet(Roll)
On delete null);
In the above syntax, just after references keyword( used for creating foreign
key), we have added on delete null, by adding such now, we can delete the
value from the master table if the value is lying in the child table without
violating deletion constraint.
Now if you wanted to delete Roll = 4 from the master table even though Roll
=4 is lying in the child table, it is possible because the moment you give the
command to delete Roll = 4 from the master table, the row having Roll = 4
from child table will get replaced by a NULL value.
Refrential Integrity
The above two tables STUDENT and
SUBJECT having four values each are shown,
now suppose you are looking to delete Roll = 4
from STUDENT( Master ) Table by writing a
SQL command:
delete from STUDENT where Roll = 4;
The moment SQL execute the above
command the row having Roll = 4 from
SUBJECT( Child ) Table will get replaced
by a NULL value, The resultant STUDENT
and SUBJECT table will look like:
Refrential Integrity
From the above two tables STUDENT and
SUBJECT, you can see that in table
STUDENT Roll = 4 get deleted while the
value of Roll = 4 in the SUBJECT table is
replaced by NULL. This proves that the
Foreign key can have null values. If in the
case in SUBJECT Table, column Roll is
Primary Key along with Foreign Key then
in that case we could not make a foreign
key to have NULL values.

More Related Content

Similar to sql.pdf

Interview Questions.pdf
Interview Questions.pdfInterview Questions.pdf
Interview Questions.pdf
TarunKumar893717
 
Sql commands
Sql commandsSql commands
Sql commands
Pooja Dixit
 
Data Definition Language (DDL)
Data Definition Language (DDL) Data Definition Language (DDL)
Data Definition Language (DDL)
Mohd Tousif
 
Oracle SQL Part 2
Oracle SQL Part 2Oracle SQL Part 2
Oracle SQL Part 2
Gurpreet singh
 
DBMS.pptx
DBMS.pptxDBMS.pptx
DBMS.pptx
Geetha Kannan
 
Integrity and security
Integrity and securityIntegrity and security
Integrity and security
Surendra Karki Chettri
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
Vidyasagar Mundroy
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for Beginners
Abdelhay Shafi
 
Constraints constraints of oracle data base management systems
Constraints  constraints of oracle data base management systemsConstraints  constraints of oracle data base management systems
Constraints constraints of oracle data base management systems
SHAKIR325211
 
DBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML CommandsDBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML Commands
Mohammad Imam Hossain
 
Its about a sql topic for basic structured query language
Its about a sql topic for basic structured query languageIts about a sql topic for basic structured query language
Its about a sql topic for basic structured query language
IMsKanchanaI
 
SQL PPT.pptx
SQL PPT.pptxSQL PPT.pptx
SQL PPT.pptx
PriyaPandey767008
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
SherinRappai
 
MySQL Essential Training
MySQL Essential TrainingMySQL Essential Training
MySQL Essential Training
HudaRaghibKadhim
 
e computer notes - Including constraints
e computer notes - Including constraintse computer notes - Including constraints
e computer notes - Including constraints
ecomputernotes
 
Introduction to Oracle Database.pptx
Introduction to Oracle Database.pptxIntroduction to Oracle Database.pptx
Introduction to Oracle Database.pptx
SiddhantBhardwaj26
 
constraints2100000000000000000000000000000
constraints2100000000000000000000000000000constraints2100000000000000000000000000000
constraints2100000000000000000000000000000
227567
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
SherinRappai1
 
DBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptxDBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptx
jainendraKUMAR55
 
Steps towards of sql server developer
Steps towards of sql server developerSteps towards of sql server developer
Steps towards of sql server developer
Ahsan Kabir
 

Similar to sql.pdf (20)

Interview Questions.pdf
Interview Questions.pdfInterview Questions.pdf
Interview Questions.pdf
 
Sql commands
Sql commandsSql commands
Sql commands
 
Data Definition Language (DDL)
Data Definition Language (DDL) Data Definition Language (DDL)
Data Definition Language (DDL)
 
Oracle SQL Part 2
Oracle SQL Part 2Oracle SQL Part 2
Oracle SQL Part 2
 
DBMS.pptx
DBMS.pptxDBMS.pptx
DBMS.pptx
 
Integrity and security
Integrity and securityIntegrity and security
Integrity and security
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for Beginners
 
Constraints constraints of oracle data base management systems
Constraints  constraints of oracle data base management systemsConstraints  constraints of oracle data base management systems
Constraints constraints of oracle data base management systems
 
DBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML CommandsDBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML Commands
 
Its about a sql topic for basic structured query language
Its about a sql topic for basic structured query languageIts about a sql topic for basic structured query language
Its about a sql topic for basic structured query language
 
SQL PPT.pptx
SQL PPT.pptxSQL PPT.pptx
SQL PPT.pptx
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
 
MySQL Essential Training
MySQL Essential TrainingMySQL Essential Training
MySQL Essential Training
 
e computer notes - Including constraints
e computer notes - Including constraintse computer notes - Including constraints
e computer notes - Including constraints
 
Introduction to Oracle Database.pptx
Introduction to Oracle Database.pptxIntroduction to Oracle Database.pptx
Introduction to Oracle Database.pptx
 
constraints2100000000000000000000000000000
constraints2100000000000000000000000000000constraints2100000000000000000000000000000
constraints2100000000000000000000000000000
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
 
DBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptxDBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptx
 
Steps towards of sql server developer
Steps towards of sql server developerSteps towards of sql server developer
Steps towards of sql server developer
 

Recently uploaded

Discover the benefits of outsourcing SEO to India
Discover the benefits of outsourcing SEO to IndiaDiscover the benefits of outsourcing SEO to India
Discover the benefits of outsourcing SEO to India
davidjhones387
 
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
uehowe
 
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaalmanuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
wolfsoftcompanyco
 
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
xjq03c34
 
一比一原版(USYD毕业证)悉尼大学毕业证如何办理
一比一原版(USYD毕业证)悉尼大学毕业证如何办理一比一原版(USYD毕业证)悉尼大学毕业证如何办理
一比一原版(USYD毕业证)悉尼大学毕业证如何办理
k4ncd0z
 
办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理
办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理
办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理
uehowe
 
Bengaluru Dreamin' 24 - Personal Branding
Bengaluru Dreamin' 24 - Personal BrandingBengaluru Dreamin' 24 - Personal Branding
Bengaluru Dreamin' 24 - Personal Branding
Tarandeep Singh
 
Should Repositories Participate in the Fediverse?
Should Repositories Participate in the Fediverse?Should Repositories Participate in the Fediverse?
Should Repositories Participate in the Fediverse?
Paul Walk
 
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
3a0sd7z3
 
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
rtunex8r
 
成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理
成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理
成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理
ysasp1
 
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
fovkoyb
 
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
3a0sd7z3
 
HijackLoader Evolution: Interactive Process Hollowing
HijackLoader Evolution: Interactive Process HollowingHijackLoader Evolution: Interactive Process Hollowing
HijackLoader Evolution: Interactive Process Hollowing
Donato Onofri
 
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
uehowe
 
Ready to Unlock the Power of Blockchain!
Ready to Unlock the Power of Blockchain!Ready to Unlock the Power of Blockchain!
Ready to Unlock the Power of Blockchain!
Toptal Tech
 

Recently uploaded (16)

Discover the benefits of outsourcing SEO to India
Discover the benefits of outsourcing SEO to IndiaDiscover the benefits of outsourcing SEO to India
Discover the benefits of outsourcing SEO to India
 
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
 
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaalmanuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
 
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
 
一比一原版(USYD毕业证)悉尼大学毕业证如何办理
一比一原版(USYD毕业证)悉尼大学毕业证如何办理一比一原版(USYD毕业证)悉尼大学毕业证如何办理
一比一原版(USYD毕业证)悉尼大学毕业证如何办理
 
办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理
办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理
办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理
 
Bengaluru Dreamin' 24 - Personal Branding
Bengaluru Dreamin' 24 - Personal BrandingBengaluru Dreamin' 24 - Personal Branding
Bengaluru Dreamin' 24 - Personal Branding
 
Should Repositories Participate in the Fediverse?
Should Repositories Participate in the Fediverse?Should Repositories Participate in the Fediverse?
Should Repositories Participate in the Fediverse?
 
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
 
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
 
成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理
成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理
成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理
 
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
 
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
 
HijackLoader Evolution: Interactive Process Hollowing
HijackLoader Evolution: Interactive Process HollowingHijackLoader Evolution: Interactive Process Hollowing
HijackLoader Evolution: Interactive Process Hollowing
 
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
 
Ready to Unlock the Power of Blockchain!
Ready to Unlock the Power of Blockchain!Ready to Unlock the Power of Blockchain!
Ready to Unlock the Power of Blockchain!
 

sql.pdf

  • 2. referential integrity ► A referential integrity constraint is also known as foreign key constraint. A foreign key is a key whose values are derived from the Primary key of another table. ► The table from which the values are derived is known as Master or Referenced Table and the Table in which values are inserted accordingly is known as Child or Referencing Table. ► In other words, we can say that the table containing the foreign key is called the child table, and the table containing the Primary key/candidate key is called the referenced or parent table. ► The syntax of the Master Table or Referenced table is: ► CREATE TABLE Student (Roll int PRIMARY KEY, Name varchar(25) , Course varchar(10) )
  • 3. SQL Constraint CREATE TABLE Subject (Roll int , SubCode int, SubName varchar(10) FOREIGN KEY (Roll) REFERENCES Studnet(Roll) ); In the above table, column Roll is acting as Foreign Key, whose values are derived using the Roll value of Primary key from Master table.
  • 4. Referential Integrity constraint ►Foreign Key Constraint OR Referential Integrity constraint. ►There are two referential integrity constraint: ►Insert Constraint: Value cannot be inserted in CHILD Table if the value is not lying in MASTER Table ►Delete Constraint: Value cannot be deleted from MASTER Table if the value is lying in CHILD Table ►Suppose you wanted to insert Roll = 05 with other values of columns in SUBJECT Table, then you will immediately see an error "Foreign key Constraint Violated" i.e. on running an insertion command as: ►Insert into SUBJECT values(5, 786, OS) will not be entertained by SQL due to Insertion Constraint ( As you cannot insert value in a child table if the value is not lying in the master table, since Roll = 5 is not present in the master table, hence it will not be allowed to enter Roll = 5 in child table ) ►Similarly, if you want to delete Roll = 4 from STUDENT Table, then you will immediately see an error "Foreign key Constraint Violated" i.e. on running a
  • 5. Referential Integrity constraint Delete from STUDENT where Roll = 4; will not be entertained by SQL due to Deletion Constraint. ( As you cannot delete the value from the master table if the value is lying in the child table, since Roll = 5 is present in the child table, hence it will not be allowed to delete Roll = 5 from the master table, lets if somehow we managed to delete Roll = 5, then Roll = 5 will be available in child table which will ultimately violate insertion constraint. ) ►ON DELETE CASCADE. ►As per deletion constraint: Value cannot be deleted from the MASTER Table if the value is lying in CHILD Table. ►The next question comes can we delete the value from the master table if the value is lying in the child table without violating the deletion constraint? i.e. ►The moment we delete the value from the master table the value corresponding to it should also get deleted from the child table. ►The answer to the above question is YES, we can delete the value from the master table if the value is lying in the child table without violating the deletion constraint, we have to do slight modification while creating the child table, i.e. by adding on delete cascade.
  • 6. Referential Integrity constraint TABLE SYNTAX CREATE TABLE Subject (Roll int , SubCode int, SubName varchar(10) FOREIGN KEY (Roll) REFERENCES Studnet(Roll) On delete cascade); In the above syntax, just after references keyword( used for creating foreign key), we have added on delete cascade, by adding such now, we can delete the value from the master table if the value is lying in the child table without violating deletion constraint. Now if you wanted to delete Roll = 5 from the master table even though Roll = 5 is lying in the child table, it is possible because the moment you give the command to delete Roll = 5 from the master table, the row having Roll = 5 from child table will also get deleted.
  • 7. Refrential Integrity The above two tables STUDENT and SUBJECT having four values each are shown, now suppose you are looking to delete Roll = 4 from STUDENT( Master ) Table by writing a SQL command: delete from STUDENT where Roll = 4; The moment SQL execute the above command the row having Roll = 4 from SUBJECT( Child ) Table will also get deleted, The resultant STUDENT and SUBJECT table will look like:
  • 8. Refrential Integrity From the above two tables STUDENT and SUBJECT, you can see that in both the table Roll = 4 gets deleted at one go without violating deletion constraint. Sometimes a very important question is asked in interviews that: Can Foreign Key have NULL values? The answer to the above question is YES, it may have NULL values, whereas the Primary key cannot be NULL at any cost. To understand the above question practically let's understand below the concept of delete null.
  • 9. Refrential Integrity ON DELETE NULL. As per deletion constraint: Value cannot be deleted from the MASTER Table if the value is lying in CHILD Table. The next question comes can we delete the value from the master table if the value is lying in the child table without violating the deletion constraint? i.e. The moment we delete the value from the master table the value corresponding to it should also get deleted from the child table or can be replaced with the NULL value. The answer to the above question is YES, we can delete the value from the master table if the value is lying in child table without violating deletion constraint by inserting NULL in the foreign key, we have to do slight modification while creating child table, i.e. by adding on delete null.
  • 10. Refrential Integrity TABLE SYNTAX: TABLE SYNTAX CREATE TABLE Subject (Roll int , SubCode int, SubName varchar(10) FOREIGN KEY (Roll) REFERENCES Studnet(Roll) On delete null); In the above syntax, just after references keyword( used for creating foreign key), we have added on delete null, by adding such now, we can delete the value from the master table if the value is lying in the child table without violating deletion constraint. Now if you wanted to delete Roll = 4 from the master table even though Roll =4 is lying in the child table, it is possible because the moment you give the command to delete Roll = 4 from the master table, the row having Roll = 4 from child table will get replaced by a NULL value.
  • 11. Refrential Integrity The above two tables STUDENT and SUBJECT having four values each are shown, now suppose you are looking to delete Roll = 4 from STUDENT( Master ) Table by writing a SQL command: delete from STUDENT where Roll = 4; The moment SQL execute the above command the row having Roll = 4 from SUBJECT( Child ) Table will get replaced by a NULL value, The resultant STUDENT and SUBJECT table will look like:
  • 12. Refrential Integrity From the above two tables STUDENT and SUBJECT, you can see that in table STUDENT Roll = 4 get deleted while the value of Roll = 4 in the SUBJECT table is replaced by NULL. This proves that the Foreign key can have null values. If in the case in SUBJECT Table, column Roll is Primary Key along with Foreign Key then in that case we could not make a foreign key to have NULL values.