Showing posts with label foreign key constraint in sql server. Show all posts
Showing posts with label foreign key constraint in sql server. Show all posts

Friday, August 5, 2016

SQL Foreign key Constraint



A Foreign key in one table points to a primary key in another table.
Firstly we create two tables:-
employee” table: -

E_Id
Name
Address
1
Sachin
Kanpur
2
Amit
Aligarh
3
Sumt
Etah

Emp” table
Emp_id
dept
E_Id
1
IT
2
2
sales
1

The SQL Foreign key constraint on create table
Create table Emp
(
Emp_id int NOT NULL PRIMARY KEY,
dept varchar (250) NOT NULL,
E_Id int Foreign Key references employee(E_Id)
)

SQL foreign Key constraint on ALTER Table
ALTER Table Emp
Add foreign key (E_Id)
References Employee (E_Id)

Drop a Foreign Key Constraint –
Alter table Emp
Drop contraint E_Id

                                                Author : - Sachin Pathak