2016-08-23 77 views
1

這是表中的一個我想引用DesignationId到其他表,但它不工作非主鍵列可以從外鍵引用嗎?

create table Employees 
(
    EmployeeID int identity(1,1) primary key, 
    EmployeeNumber int not null, 
    LocationID int not null, 
    EmployeeName varchar(20) not null, 
    DesignationID int not null, 
    CategoryID int not null, 
) 

二表是..上第三排它顯示錯誤

create table Designation 
(
    DesignationID int primary key , 
    JobTitle varchar(20) not null, 

    CONSTRAINT fk_Designation_Employees 
     FOREIGN KEY (DesignationID) 
     REFERENCES Employees (DesignationID), 
) 
+3

表#2 **中的外鍵必須**引用表#1中的主鍵**(並且除了* complete *主鍵之外的任何東西),或者引用表中的任何NOT NULL列獨特的索引。因此,如果您有一列(或一組列)是唯一的,那麼您可以在該(那些)列上放置一個唯一索引並引用該列。 –

回答

4

你創建這個不正確。請嘗試以下方式代替:

create table Designation 
(
    DesignationID int primary key , 
    JobTitle varchar(20) not null, 

) 

create table Employees 
(
    EmployeeID int identity(1,1) primary key, 
    EmployeeNumber int not null, 
    LocationID int not null, 
    EmployeeName varchar(20) not null, 
    DesignationID int not null, 
    CategoryID int not null, 


    CONSTRAINT fk_Employees_Designation 
     FOREIGN KEY (DesignationID) 
     REFERENCES Designation (DesignationID) 
) 

許多員工與指定相關聯。一對多的關係。