Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.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 2014中添加外键_Sql_Foreign Key Relationship_Sql Server 2014 - Fatal编程技术网

在SQL Server 2014中添加外键

在SQL Server 2014中添加外键,sql,foreign-key-relationship,sql-server-2014,Sql,Foreign Key Relationship,Sql Server 2014,我正在使用SQL Server 2014,在向表中添加外键时遇到问题 alter table product add FOREIGN KEY (productid) references orderdetail(productid) 我试图在我的product表中添加一个外键,该外键引用另一列productid,该列也可以在orderdetail表中找到,但当我添加外键时,我得到了错误 味精156,第15级,状态1,第46行 关键字“FOREIGN”附近的语法不正确 我可以重写我的

我正在使用SQL Server 2014,在向表中添加外键时遇到问题

alter table product
   add FOREIGN KEY (productid)
   references orderdetail(productid)
我试图在我的product表中添加一个外键,该外键引用另一列
productid
,该列也可以在
orderdetail
表中找到,但当我添加外键时,我得到了错误

味精156,第15级,状态1,第46行
关键字“FOREIGN”附近的语法不正确


我可以重写我的语句,但如果我使用addconstraint,它会在关键字constraint附近说不正确的语法。非常感谢您的任何意见

您缺少
约束

alter table product
add constraint give_it_a_name_here
FOREIGN KEY (productid)
references orderdetail(productid)