开发者

Specify a Composite Foreign Key

开发者 https://www.devze.com 2023-03-17 05:13 出处:网络
I\'m trying to create a Foreign Key out of two values in two different tables, some pointers would be appreciated!

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

精彩评论

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