如果我在SQL Server 2008中使用以下SQL命令来更新带有外键约束的表:
ALTER TABLE Employees
ADD FOREIGN KEY (UserID)
REFERENCES ActiveDirectories(id)UserID是我在Employees表中的FK列。我正在尝试引用ActiveDirectories表中的UserID。我收到这个错误:
外键“UserID”在引用表“Employees”中引用了无效的列“UserID”。
发布于 2012-05-01 03:37:34
错误指示您的Employees表中没有UserID列。尝试先添加该列,然后重新运行该语句。
ALTER TABLE Employees
ADD CONSTRAINT FK_ActiveDirectories_UserID FOREIGN KEY (UserID)
REFERENCES ActiveDirectories(id);发布于 2012-05-01 03:39:32
也许你把你的专栏倒过来了?
ALTER TABLE Employees
ADD FOREIGN KEY (UserID) <-- this needs to be a column of the Employees table
REFERENCES ActiveDirectories(id) <-- this needs to be a column of the ActiveDirectories table会不会列在Employees表中称为ID,而在ActiveDirectories表中称为UserID?
那么你的命令应该是:
ALTER TABLE Employees
ADD FOREIGN KEY (ID) <-- column in table "Employees"
REFERENCES ActiveDirectories(UserID) <-- column in table "ActiveDirectories" 发布于 2015-06-23 21:11:19
MySQL / SQL Server / Oracle / MS访问:
ALTER TABLE Orders
ADD FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)若要允许命名外键约束条件,并在多个列上定义外键约束条件,请使用以下SQL语法:
MySQL / SQL Server / Oracle / MS访问:
ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)https://stackoverflow.com/questions/10389477
复制相似问题