Primary and ForeignKey relations

  • How can you create foreign key relation to the same table which contains primary key.

  • 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.

  • 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.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply