Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2008 SQL将外键添加到现有列_Sql Server 2008_Foreign Key Relationship - Fatal编程技术网

Sql server 2008 SQL将外键添加到现有列

Sql server 2008 SQL将外键添加到现有列,sql-server-2008,foreign-key-relationship,Sql Server 2008,Foreign Key Relationship,如果在SQL Server 2008中使用以下SQL命令来更新具有外键约束的表: ALTER TABLE Employees ADD FOREIGN KEY (UserID) REFERENCES ActiveDirectories(id) UserID是Employees表中的“我的FK”列。我正在尝试引用我的ActiveDirectories表中的用户ID。我收到这个错误: 外键“UserID”在引用中引用了无效列“UserID” 表“雇员” 错误指示Employees表中没有UserID

如果在SQL Server 2008中使用以下SQL命令来更新具有外键约束的表:

ALTER TABLE Employees
ADD FOREIGN KEY (UserID)
REFERENCES ActiveDirectories(id)
UserID
Employees
表中的“我的FK”列。我正在尝试引用我的
ActiveDirectories
表中的
用户ID
。我收到这个错误:

外键“UserID”在引用中引用了无效列“UserID” 表“雇员”


错误指示Employees表中没有UserID列。尝试先添加列,然后重新运行该语句

ALTER TABLE Employees
ADD CONSTRAINT FK_ActiveDirectories_UserID FOREIGN KEY (UserID)
    REFERENCES ActiveDirectories(id);

也许你的专栏倒了

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

MySQL/SQL Server/Oracle/MS访问:

ALTER TABLE Orders
ADD FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
要允许命名外键约束,并在多列上定义外键约束,请使用以下SQL语法:


MySQL/SQL Server/Oracle/MS访问:

ALTER TABLE Orders
ADD FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)

为ActiveDirectory(id)创建外键的方法是正确的,我认为主要错误是您没有在ActiveDirectory表中提到id的主键

ALTER TABLE Faculty 
WITH CHECK ADD  CONSTRAINT FKFacultyBook
FOREIGN KEY FacId
REFERENCES Book Book_Id

ALTER TABLE Faculty 
WITH CHECK ADD  CONSTRAINT FKFacultyStudent 
FOREIGN KEY FacId
REFERENCES Student StuId
ALTER TABLE Employees
ADD UserID int;

ALTER TABLE Employees
ADD CONSTRAINT FK_ActiveDirectories_UserID FOREIGN KEY (UserID)
    REFERENCES ActiveDirectories(id);

如果已创建表,请执行以下操作:

首先要做:

ALTER TABLE`table1\u name`添加唯一(`column\u name`);
然后:

ALTER TABLE`table1\u name`添加外键(`column\u name`)引用`table2\u name`(`column\u name`);

能否提供两个表的模式?请参考此链接,这是正确的。我们的数据库没有更新add列。这已经解决了,但我们的专栏还没有建立,我仍然不能添加约束<代码>引用的表“ActiveDirectory”中没有与外键“FK\UU Employees\UUU UserI\UU 04E4BC85”中的引用列列表匹配的主键或候选键。看起来,FK\UU Employees\UUU UserI\UU 04E4BC85引用的任何列都没有在ActiveDirectory表中定义为主键或候选键。是的,但它是当然,我们在ActiveDirectory表中的PK已经解决:在构建之前创建ERD并建立关系是有原因的。一个表中的记录太多,这导致尝试创建与另一个表的关系时出错。谢谢大家。参考这个链接我知道它很奇怪,但不幸的是,ActiveDirectory表中的名称是你应该提供一些解释和你的答案