开发者

Specify a Composite Foreign Key

I'm trying to create a Foreign Key out of two values in two different tables, some pointers would be appreciated!

Below is the code I am using for this :

CREATE TABLE [dbo].[EmployeeUnsetZone](
    [EmployeeID] [char](2) NULL,
    [ZoneOfficeID] [int] NOT NULL,
    [ZoneID] [char](4) NULL
    CONSTRAINT EmployeeUnsetZone_EmployeeFK FOREIGN KEY([EmployeeID],[ZoneID])
    REFERENCES [Employee]开发者_JAVA百科([ID]), [ZoneByOffice]([ID])
)


I'd use:

CREATE TABLE [dbo].[EmployeeUnsetZone](
  [EmployeeID] [char](2) NULL,
  [ZoneOfficeID] [int] NOT NULL,
  [ZoneID] [char](4) NULL,
  PRIMARY KEY ([EmployeeID], [ZoneID]),
  CONSTRAINT fk_employeeid FOREIGN KEY([EmployeeID]) REFERENCES [Employee]([ID]),
  CONSTRAINT fk_zoneid FOREIGN KEY([ZoneID]) REFERENCES [ZoneByOffice]([ID])
)

The primary key will stop duplicates, and also setup the clustered key for the two columns to make searching against better.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜