0% found this document useful (0 votes)
19 views

Cascading Foreign Key SQL

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Cascading Foreign Key SQL

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Cascading Referential

Integrity Constraints /
Cascading Foreign Key In
SQL Server
PRESENTER: MOHAMMAD ADIL
Customer Table
Primary Key
CUSTOMER ID CUSTOMER NAME ADDRESS CITY
1 Ali Latifabad Unit: 12 Hyderabad
2 Amir Latifabad Unit: 11 Hyderabad
3 Anas Gulshan e Iqbal Karachi
4 Usman Latifabad Unit: 8 Hyderabad
5 Afnan Latifabad Unit: 6 Hyderabad
Order Table
Primary Key Foreign Key
ORDER ID ORDER ITEM ORDER PRICE OF 1 CUSTOMER ID
QUANTITY
100 Hardisk 2 500 4
101 Ram 1 350 2
102 Printer 2 4000 3
103 Monitor 3 1500 4
104 Mouse 3 300 5
What are Cascading Referential
Integrity Constraints in SQL Server?
The Cascading Referential Integrity Constraints in SQL Server are the
foreign key constraints that tell SQL Server to perform certain actions
whenever a user attempts to delete or update a primary key to
which an existing foreign keys point.
What are the Actions Performed
By SQL Server?
In order to tell the SQL Server what actions to perform whenever a
user trying to delete or update a primary key value to which existing
foreign key points, we are provided with the following options while
working with Cascading Referential Integrity Constraints.
1. NO ACTION (By Default)
When we add a foreign key to a
2. CASCADE
column then we have to add one
3. SET DEFAULT of these action with foreign key.
4. SET NULL
NO ACTION
This is the default action that SQL Server performs.
This specifies that if an update or deletes statement affects rows in
foreign key tables, then the action will be denied and rolled back. An
error message will be raised.
Customer Table
Primary Key
CUSTOMER ID CUSTOMER NAME ADDRESS CITY
1 Ali Latifabad Unit: 12 Hyderabad
2 Amir Latifabad Unit: 11 Hyderabad
3 Anas Gulshan e Iqbal Karachi
4 Usman Latifabad Unit: 8 Hyderabad
5 Afnan Latifabad Unit: 6 Hyderabad
Order Table
Primary Key No Action (Default) Foreign Key
ORDER ID ORDER ITEM ORDER PRICE OF 1 CUSTOMER ID
QUANTITY
100 Hardisk 2 500 4
101 Ram 1 350 2
102 Printer 2 4000 3
103 Monitor 3 1500 4
104 Mouse 3 300 5
When “No Action” Specified
CASCADE
If a user tries to delete the statement(s) which will affect the rows in
the foreign key table, then those rows will be deleted when the
primary key record is deleted.
Similarly, if an update statement affects rows in the foreign key table,
then those rows will be updated with the value from the primary key
record after it has been updated.
Customer Table
Primary Key
CUSTOMER ID CUSTOMER NAME ADDRESS CITY
1 Ali Latifabad Unit: 12 Hyderabad
8 2 Amir Latifabad Unit: 11 Hyderabad
3 Anas Gulshan e Iqbal Karachi
4 Usman Latifabad Unit: 8 Hyderabad
5 Afnan Latifabad Unit: 6 Hyderabad
Order Table
Primary Key Cascade Foreign Key
ORDER ID ORDER ITEM ORDER PRICE OF 1 CUSTOMER ID
QUANTITY
100 Hardisk 2 500 4
101 Ram 1 350 8 2
102 Printer 2 4000 3
103 Monitor 3 1500 4
104 Mouse 3 300 5
SET NULL
If a user tries to delete or update statement(s) that will affect rows in
the foreign key table, then those values will be set to NULL when the
primary key record is deleted or updated in the Primary key table.
The important thing that we need to keep in mind that the foreign
key columns affected must allow NULL values.
Customer Table
Primary Key
CUSTOMER ID CUSTOMER NAME ADDRESS CITY
1 Ali Latifabad Unit: 12 Hyderabad
2 Amir Latifabad Unit: 11 Hyderabad
3 Anas Gulshan e Iqbal Karachi
4 Usman Latifabad Unit: 8 Hyderabad
5 Afnan Latifabad Unit: 6 Hyderabad
Order Table
Primary Key SET NULL Foreign Key
ORDER ID ORDER ITEM ORDER PRICE OF 1 CUSTOMER ID
QUANTITY
100 Hardisk 2 500 NULL
101 Ram 1 350 2
102 Printer 2 4000 3
103 Monitor 3 1500 NULL
104 Mouse 3 300 5
SET DEFAULT
If a delete or update statement affects rows in a foreign key table,
then all rows containing those foreign keys are set to the default
value.
All foreign key columns in the related table must have default
constraints defined on them.
Customer Table
Primary Key
CUSTOMER ID CUSTOMER NAME ADDRESS CITY
1 Ali Latifabad Unit: 12 Hyderabad
2 Amir Latifabad Unit: 11 Hyderabad
3 Anas Gulshan e Iqbal Karachi
4 Usman Latifabad Unit: 8 Hyderabad
5 Afnan Latifabad Unit: 6 Hyderabad
Order Table
Primary Key SET DEFAULT Foreign Key
ORDER ID ORDER ITEM ORDER PRICE OF 1 CUSTOMER ID
QUANTITY
100 Hardisk 2 500 0
101 Ram 1 350 2
102 Printer 2 4000 3
103 Monitor 3 1500 0
104 Mouse 3 300 5
Important Note
You can not add ON DELETE CASCADE to an already existing
constraint.
You will have to drop and re-create the constraint.
The documentation shows that the MODIFY CONSTRAINT clause can
only modify the state of a constraint (i-e: ENABLED/DISABLED...).
Alter Foreign Key Constraint
ALTER TABLE TABLEName
drop CONSTRAINT FK_CONSTRAINTNAME;

ALTER TABLE TABLENAME


ADD CONSTRAINT FK_CONSTRAINTNAME
FOREIGN KEY (FId)
REFERENCES OTHERTABLE
(Id)
ON DELETE CASCADE ON UPDATE NO ACTION;

You might also like