Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Primary and ForeignKey relations Expand / Collapse
Author
Message
Posted Saturday, December 14, 2013 12:29 PM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Yesterday @ 4:18 PM
Points: 619, Visits: 806
How can you create foreign key relation to the same table which contains primary key.
Post #1522965
Posted Saturday, December 14, 2013 4:44 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 9:43 PM
Points: 849, Visits: 5,476
ramana3327 (12/14/2013)
How can you create foreign key relation to the same table which contains primary key.


So you're doing a self-join? Like in an Employee/Manager scenario?

See this article: http://msdn.microsoft.com/en-us/library/ms190273.aspx

Section E.

there are examples of creating primary and foreign key constraints.
Post #1522986
Posted Monday, December 16, 2013 10:06 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Thursday, April 3, 2014 1:32 PM
Points: 689, Visits: 331
How?

Yep, I can create like below...

CREATE TABLE Emp(
EmpID INT PRIMARY KEY,
Emp_Name VARCHAR(100),
Emp_JoinDate DATE,
Emp_Salary Money,
Emp_Designation Varchar(50),
Emp_ManagerID INT FOREIGN KEY REFERENCES Employee(EmpID)
)

You may get issues while inserting into Emp_ManagerID , you may use null value for that.
Post #1523332
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse