Editorial / Best Answer
Answered by:
MANAS KUMAR ROUT ( USA ) (TCS) (Eli LILLY ) - PFIZ
Hey Ramesh,
I understand your interviewer's enthusiasm to know about the Foreign Key. See foreign key definitions says its states the relationship of one attribute(Column) of one table to the corresponding column on the other table.
So one os a parent and other is child. PARENT is that table which holds the primary key and CHILD is table which holds the referential key. Hence every entry in the PRIMARY table is unique where as same entry might be repeating in the child table. BUT that same has to be present in the PARENT table (Primary KEY).
Now Going by definition Foreign key can be of two types :
========================================
1. ON DELETE SET NULL
2. ON DELETE SET CASCADE
DETAILS:
=======
On delete set null :
=======
When a foreign key is created by on delete set null definition then when you delete one row from the primary column (of parent table ) , then the corresponding entry in the foreign key table (Child table ) have the value "NULL" for that particular column.
ON Delete Set Cascade:
================
When the foreign key is created by this definition then when you delete the primary column (any one row -unique data) , then the Child table forcefully deletes all the rows in the child table having same value for that particular column.
I hope u got the crux of the Foreign key
Thanks
Manas Kumar Rout
SAS Consultant
Oracle Certified Associate
Unix Consultant
What is the main use of FOREIGN KEY?
Profile Answers by ramesh_etta Questions by ramesh_etta
Questions by ramesh_etta
Editorial / Best Answer
Answered by: MANAS KUMAR ROUT ( USA ) (TCS) (Eli LILLY ) - PFIZ
Hey Ramesh,
I understand your interviewer's enthusiasm to know about the Foreign Key. See foreign key definitions says its states the relationship of one attribute(Column) of one table to the corresponding column on the other table.
So one os a parent and other is child. PARENT is that table which holds the primary key and CHILD is table which holds the referential key. Hence every entry in the PRIMARY table is unique where as same entry might be repeating in the child table. BUT that same has to be present in the PARENT table (Primary KEY).
Now Going by definition Foreign key can be of two types :
========================================
1. ON DELETE SET NULL
2. ON DELETE SET CASCADE
DETAILS:
=======
On delete set null :
=======
When a foreign key is created by on delete set null definition then when you delete one row from the primary column (of parent table ) , then the corresponding entry in the foreign key table (Child table ) have the value "NULL" for that particular column.
ON Delete Set Cascade:
================
When the foreign key is created by this definition then when you delete the primary column (any one row -unique data) , then the Child table forcefully deletes all the rows in the child table having same value for that particular column.
I hope u got the crux of the Foreign key
Thanks
Manas Kumar Rout
SAS Consultant
Oracle Certified Associate
Unix Consultant
Related Answered Questions
Related Open Questions