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
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: Today @ 7:24 AM
Points: 732, Visits: 1,019
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: Today @ 10:01 PM
Points: 1,071, Visits: 6,514
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: Tuesday, February 17, 2015 1:46 PM
Points: 920, Visits: 449
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