在SQL中添加外键约束 [英] add foreign key constraints in sql

查看:142
本文介绍了在SQL中添加外键约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我有两张桌子.当我尝试使用此查询添加外键时


 更改  tbl_user_login 添加 约束 fkcid 外国 关键 (fk_Consumer_ID)引用 tbl_Consumer_details(Consumer_ID)




它显示一个错误:

在引用的表"tbl_Consumer_details"中没有与外键"fkcid"中的引用列列表匹配的主键或候选键.


帮帮我

在此先感谢

解决方案

请查看以下链接,以获取有关" FOREIGN KEY Constraint "的信息.

http://www.w3schools.com/sql/sql_foreignkey.asp

http://msdn.microsoft.com/en-us/library/ms175464.aspx


,这意味着您的表2没有要由表1引用的主键.
首先将主键分配给要加入的辅助表

问候,
Eduard


在参考表中将Consumer_ID更改为主键


hi i have two tables. when i tried to add a foreign key using this query


alter  table tbl_user_login add constraint fkcid foreign key(fk_Consumer_ID) references tbl_Consumer_details(Consumer_ID)




it shows an error:

There are no primary or candidate keys in the referenced table ''tbl_Consumer_details'' that match the referencing column list in the foreign key ''fkcid''.


help me

thanks in advance

解决方案

Have a look at below links for information on "FOREIGN KEY Constraint".

http://www.w3schools.com/sql/sql_foreignkey.asp

http://msdn.microsoft.com/en-us/library/ms175464.aspx


this means your table 2 doesn''t have a primary key to be referenced by table 1.

Assign a primary key first to the secondary table you are joining

Regards,
Eduard


Change Consumer_ID as Primary Key in the reference table


这篇关于在SQL中添加外键约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆