非主键列可以从外键引用吗?

问题描述:

这是表中的一个我想引用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列独特的索引。因此,如果您有一列(或一组列)是唯一的,那么您可以在该(那些)列上放置一个唯一索引并引用该列。 –

你创建这个不正确。请尝试以下方式代替:

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) 
) 

许多员工与指定相关联。一对多的关系。