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
SSC Eights!

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

Group: General Forum Members
Last Login: Thursday, July 23, 2015 8:24 AM
Points: 922, Visits: 1,423
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
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 7:55 PM
Points: 1,347, Visits: 7,940
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


SSC Eights!

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

Group: General Forum Members
Last Login: Wednesday, July 22, 2015 10:59 AM
Points: 920, Visits: 460
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