使用另一个表的主键在一个表中创建外键时出现错误,其中为一个表中的两列创建单个主键 [英] Getting an error when created foreign key in one table using primary key of another table where single primary key created for two columns in one table

查看:96
本文介绍了使用另一个表的主键在一个表中创建外键时出现错误,其中为一个表中的两列创建单个主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了两个表,一个表具有一个主键且具有两列

I have created two tables one table having one primary key with two columns

create table PSM.dbo.conversion(Prospect_ID int,Customer_ID int ,Customer_Name varchar(50),Customer_Products_Services varchar(50),constraint pk1 primary key(Prospect_ID ,Customer_ID  ));



它成功运行


第二张表仅作为一列的外键



it run successfully


2nd table as foreign key of one column only

create table PSM.dbo.Customer_follow_up(Customer_ID int references conversion(Customer_ID),Customer_Name varchar(50),Customer_Products_Services varchar(50),Feedback nvarchar(1000));



在这里,得到一个错误


1776消息,级别16,状态0,第1行
在引用的表"PSM.dbo.conversion"中没有与外键"FK__Customer___Custo__5AEE82B9"中的引用列列表匹配的主键或候选键.
消息1750,级别16,状态0,第1行
无法创建约束.请参阅先前的错误.



here,getting an error


Msg 1776, Level 16, State 0, Line 1
There are no primary or candidate keys in the referenced table ''PSM.dbo.conversion'' that match the referencing column list in the foreign key ''FK__Customer___Custo__5AEE82B9''.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.

推荐答案

在引用的表中添加不存在的主键时,会出现此错误. /> 也许 [
When you add a foreign that does not exist as a primary key in the referenced table you will get this error.
Maybe this[^] can explain this better.


如果Customer_ID将是唯一字段,则您可以尝试...我已将UNIQUE约束与Customer_ID字段一起放置,并且FK将成功创建....

If Customer_ID will be a unique field then you can try this...I have put the UNIQUE constraint with Customer_ID field and FK will be created successfully....

create table dbo.conversion(Prospect_ID int,Customer_ID int UNIQUE ,Customer_Name varchar(50),Customer_Products_Services varchar(50),constraint pk1 primary key(Prospect_ID ,Customer_ID ));

create table dbo.Customer_follow_up(Customer_ID int references conversion(Customer_ID),Customer_Name varchar(50),
Customer_Products_Services varchar(50),Feedback nvarchar(1000));



否则,解决方案1中提到的原因将对其进行详细定义..

谢谢



Otherwise the reason mentioned in the Solution 1 will define it in detail..

Thanks


这篇关于使用另一个表的主键在一个表中创建外键时出现错误,其中为一个表中的两列创建单个主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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