Foreign Key

FOREIGN KEY (Referential Integrity Constraint)
            To establish a parent-child or a master-detail relationship between two tables having a common column. We make use of referential integrity constraints. To implement this we should define the column in the parent table as a primary key and the same column in the child tables as a foreign key referring to the corresponding parent entry.
Concepts:-
Foreign Key:- A column or combination of columns included in the definition of referential integrity which would refer to a referenced key.
Referenced Key:- It is unique or a primary key which is defined on a column belonging to the parent table.
Chile Table:-  This table depends upon the values present in the referenced key of the parent table, which is referred by a foreign key.
Parent Table:- This table determines whether insertion or updation of data can be done in child table. This table would be referred by child ‘s table foreign key.
The table in which the foreign key is defined is called a foreign table or Details table . the table that defines the primary key and is referenced by the foreign key is called the Primary table or Master Table.
            The master table can be referenced in the foreign key definition by using the references adverb. If the name of the column is not specified by default Oracle references the primary key in the master table.
Insert Or Update operation in the foreign table:- the existence of a foreign key implies that the table with the foreign key is derived. A foreign key must have a corresponding primary key or unique key value in the master table.
Delete operation on the primary table:- Oracle Displays an error message if the user tries to delete a record in the master table when corresponding records exist in the detail table.
Principles of Foreign key:-

Syntax:- columnname datatype(size) REFERENCES tablename [(col-name)]
[ON DELETE CASCADE]   à Column level
FOREIGN KEY (col-name) REFERENCES tablename à Table level.

Ex:-     1) Create table master(eno number(5) primary key,na varchar2(20),
Sal number(5));
Insert into master values(&eno,’&na’,&sal);

2) Create table trans(eno number(5) references master,na varchar2(20),
                                    Sal number(5));
             Insert into trans values(100,’prasad’,3200);
             Insert into trans values(200,’prasad’,2100);

For
More Explanation
&
Online Classes

Contact Us:
+919885348743