Thursday , December 14 2017
Home / Uncategorized / Create Foreign key Constraint in Sql Server

Create Foreign key Constraint in Sql Server

Definition:-

it is a column or combination of columns that is used to establish and enforce a link between the data in two tables.Foreign Key can accept duplicate values and Null values.Foreign Key has to take the values from its corresponding Primary Key.

Foreign Key  Constraint can be added to table in two levels

  1. Column Level
  2. Table Level

 

1.Column level constraints:  Here constraints are going to placed on columns, after the definition of each and every individual column and their corresponding data type.

Syntax: (With out Constraint names)

CREATE TABLE TABLENAME (COLUMN 1    DATA TYPE CONSTRAINT TYPE,COLUMN 2   DATA TYPE CONSTRAINT TYPE, ——————————————————–)

E.g. 1:-
 

E.g. 2:

 

Explanation:

  • In this case the Dept table is called as Parent table and Emp table is called as Child table.
  • Deptno is called as Reference Key column on which either Primary Key Constraint or Unique Constraint has to be imposed.
  •   Emp.Deptno is called as Foreign Key column on which the Foreign Key Constraint has to be imposed, with this only the link gets established between the 2 tables

 

Print Friendly, PDF & Email

Give us your valuable feedback

comments

About Samba

Check Also

How to Create A Table

       Definition of Table:- A Table is an object Used to Store data …

%d bloggers like this: