Two foreign keys instead of primary

后端 未结 8 1608
陌清茗
陌清茗 2020-12-17 21:28

I was wondering, is there any possibility to create a table without a primary key, but with two foreign keys, where the foreign keys pairs are always different? For example,

8条回答
  •  暖寄归人
    2020-12-17 22:12

    You can create a primary key on two columns: click on both columns in designer view > click on pk

    Or, you could add a unique constraint on 2 columns:

    ALTER TABLE [dbo].[RepresentativeData] 
    add CONSTRAINT [UK_Representative_repRecID_AppID] unique (repRecID,AppId)
    go
    

    I prefer the compound primary key, because it enforces that the value does exist in the other tables.

提交回复
热议问题