Skip to main content

SQLSERVER_7: Referential Integrity / Foreign Key Constraint

In the real world, we have to use constraints that are not only dependent upon one table rather on two tables. This kind of setup is known as Primary Key - Foreign key constraint or also known as “Referential Integrity”. For e.g. lets see the example below:

a.    Dept Table


b.    Emp Table


In order to determine the relationship, we have taken a column “Deptno” in Emp table which is validated from “Dept” table. It means “Emp” table’s column Deptno is having all the values from the Dept table and it cannot have a value that is not there in the Dept Table.





To define the foreign key constraint, we can use the following syntax:

FOREIGN KEY / REFRENTIAL INTEGRITY
Create Table EmpTemp1
(
deptno numeric(10) constraint fk references dept(deptno)
)


Comments