can we have a foreign key which is not a primary key in any other table?

前端 未结 4 677
春和景丽
春和景丽 2020-12-02 20:29

it is written in every book that foreign keys are actually primary key in some other table but can we have a foreign key which is not primary key in any other table

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 20:56

    Yes - you can have a foreign key that references a unique index in another table.

    CREATE UNIQUE INDEX UX01_YourTable ON dbo.YourTable(SomeUniqueColumn)
    
    ALTER TABLE dbo.YourChildTable
       ADD CONSTRAINT FK_ChildTable_Table
       FOREIGN KEY(YourFKColumn) REFERENCES dbo.YourTable(SomeUniqueColumn)
    

提交回复
热议问题