Tuesday 8 July 2014

How to create foreign key in SQL


Create Table Emp
(
EId Int primary key,
EName varchar(20)
)
Create Table Leavedetails
(
LId int,
NoOfDays int,
Emp_ID int foreign key references Emp(EId)
)
If you check the data base diagram, you can see the relation with Arrow
This Arrow will be from Transaction table to Master Table.
So in foreign key relation always: Source is  Transaction table and Destination is Master Table.
Group By
Select EName, Sum(NoOfDays) NoOfDays from Emp
Inner Join Leavedetails on Emp.EId=Leavedetails.Emp_ID
Group By EName