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: Today @ 11:22 AM
Points: 641, Visits: 869
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: Today @ 1:08 PM
Points: 889, Visits: 5,701
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


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Yesterday @ 7:56 AM
Points: 744, Visits: 352
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