2015-09-18 86 views
0

我正在創建一個項目的數據庫,我需要爲另一個外鍵聲明一個外鍵,以便檢查約束。外鍵上的外鍵 - SQL Server

我有一個Person表和Groups表,這兩個表都包含一個DepartmentID。每當我在Task表中插入新任務時,我想檢查Groups.DepartmentID是否與Person.DepartmentID匹配。

這個想法是任務鏈接到一個人,並且有兩種類型,一個組合類型,它定義了它的數據庫工作,財務工作等等,以及定義它的維護,培訓等的任務類型。當一個人試圖添加具有不適合他/她部門的groupType的任務應該失敗。

我試圖將這些屬性添加到Task表作爲一個外鍵,但聲明對非唯一的或者非主鍵的外鍵是不是在Microsoft SQL Server(該DepartmentIDPersonGroup表接受不能是唯一的!)。

任何人都知道如何解決這個問題?

CREATE TABLE Department 
(
    ID int PRIMARY KEY IDENTITY, 
    Name varchar(50), 
    UNIQUE ("Name") 
) 

CREATE TABLE Groups 
(
    ID int IDENTITY, 
    GroupType varchar(50) PRIMARY KEY, 
    Description varchar(255) DEFAULT ('-'), 
    DepartmentID int 
     FOREIGN KEY (DepartmentID) REFERENCES Department(ID), 
) 

CREATE TABLE Person 
(
    ID int PRIMARY KEY IDENTITY, 
    Name varchar(50), 
    DepartmentID int 
     FOREIGN KEY (DepartmentID) REFERENCES Department(ID) 
) 

CREATE TABLE TaskType 
(
    ID int IDENTITY, 
    TaskType varchar(50) PRIMARY KEY, 
    Description varchar(255) DEFAULT ('-'), 
) 

CREATE TABLE Task 
(
    ID int IDENTITY, 
    TimeFrame decimal(4,2), 
    Yearcount int, 
    GroupType varchar(50), 
    TaskType varchar(50), 
    WeekNr int, 
    ExceptionDetail varchar(255) DEFAULT ('-'), 
    PersonID int 
) 

這些是FK在未接受了任務表的屬性:

GDID int FOREIGN KEY REFERENCES Groups(DepartmentID), 
PDID int FOREIGN KEY REFERENCES Person(DepartmentID), 
CHECK (GDID = PDID),  

UNIQUE ("TaskType", "GroupType", "WeekNr", "Yearcount"), 
FOREIGN KEY (TaskType) REFERENCES TaskType(TaskType), 
FOREIGN KEY (PersonID) REFERENCES Person(ID), 
FOREIGN KEY (GroupType) REFERENCES Groups(GroupType) 
+0

先生,您需要在其他表中使用主鍵/唯一鍵。你爲什麼想這樣試試? 'Foreignkey數據應該在主表中唯一可用' –

+0

哪些外鍵被sql拒絕?請張貼失敗的陳述。 – Paolo

+0

這些:GDID INT FOREIGN KEY REFERENCES組(DepartmentID), PDID INT FOREIGN KEY REFERENCES人(DepartmentID), – Alim

回答

1

添加更寬「超級鍵」,以這些表包括主鍵附加列 ,然後使用它們聲明外鍵。無論您還刪除多餘的小外鍵是一個品味的問題:

CREATE TABLE Groups(
ID int IDENTITY, 
GroupType varchar(50) PRIMARY KEY, 
Description varchar(255) DEFAULT ('-'), 
DepartmentID int FOREIGN KEY (DepartmentID) REFERENCES Department(ID), 
constraint Group_Dep_XRef UNIQUE (GroupType,DepartmentID) 
) 

CREATE TABLE Person(
ID int PRIMARY KEY IDENTITY, 
Name varchar(50), 
DepartmentID int FOREIGN KEY (DepartmentID) REFERENCES Department(ID), 
constraint Person_Dept_XRef UNIQUE (ID,DepartmentID) 
) 

CREATE TABLE Task(
ID int IDENTITY, 
TimeFrame decimal(4,2), 
Yearcount int, 
GroupType varchar(50), 
TaskType varchar(50), 
WeekNr int, 
ExceptionDetail varchar(255) DEFAULT ('-'), 
PersonID int, 
DepartmentID int, 
constraint FK_Group_Dept_XRef FOREIGN KEY (GroupType,DepartmentID) 
     references Group (GroupType,DepartmentID), 
constraint FK_Person_Dept_XRef FOREIGN KEY (PersonID,DepartmentID) 
     references Person (ID,DepartmentID), 
UNIQUE ("TaskType", "GroupType", "WeekNr", "Yearcount"), 
FOREIGN KEY (TaskType) REFERENCES TaskType(TaskType), 
FOREIGN KEY (PersonID) REFERENCES Person(ID), --Redundant now 
FOREIGN KEY (GroupType) REFERENCES Groups(GroupType) --Also redundant 
) 

(我也鞏固GDIDPDIDDepartmentID - 如果他們總是意味着要平等,爲什麼商店,兩次,然後必須有另一個約束維護自己的平等?)


如果主鍵(或唯一鍵)足以唯一地識別每個行則任何更寬的鍵,其中包括的鍵列其他列也必須足以唯一標識每行。

+0

謝謝! – Alim