简体   繁体   English

SQL Server中的外键错误

[英]Foreign Key Error in SQL Server

I have 2 tables ( Project1 and Accounts ). 我有2个表( Project1Accounts )。

In Projects1 table, I have 8 columns, 5 of which are assigned as the primary key. Projects1表中,我有8列,其中5列被指定为主键。

In Accounts table I have 2 columns one of which is the primary key, and the other should be a foreign key that references to a column in Project1 table. 在“ Accounts表中,我有2列,其中一列是主键,另一列应是引用Project1表中一列的外键。

  • Project1 : Id (Primary Key), Name (Primary Key), AccType(Primary Key), AccNumber, DOC (Primary Key), DOM (Primary Key), Source, Status Project1 :Id(主密钥),名称(主密钥),AccType(主密钥),AccNumber,DOC(主密钥),DOM(主密钥),源,状态

  • Accounts : Id (Primary Key), AccType (Foreign Key) Accounts :ID(主键),AccType(外键)

So here the AccType column in the Accounts table points to the AccType column in the Project1 table. 所以这里的AccTypeAccounts表指向AccTypeProject1表。

AccType column in the Project1 table is the PRIMARY KEY in the Project1 table. AccTypeProject1表中的PRIMARY KEY Project1表。 The AccType column in the Accounts table is a FOREIGN KEY. Accounts表中的AccType列是一个外键。

Query: 查询:

alter table Accounts  
add foreign key (AccType) references Project (AccType);

Here I am getting an error as: 在这里我得到一个错误为:

There are no primary or candidate keys in the referenced table 'Project1' that match the referencing column list in the foreign key 'FK__Accounts__Accoun__2C3393D0'. 在引用表'Project1'中没有与外键'FK__Accounts__Accoun__2C3393D0'中的引用列列表匹配的主键或候选键。

TIA -Mithali TIA-米他利

Dear you Can't Apply Primary key More Than One time It will give You Error Because Foreign Key Will Depend on Another table Primary key 亲爱的您不能多次应用主键,因为外键将依赖于另一个表,这会给您带来错误

Suppose 假设

TABLE 1 (USER) 表1(用户)

UID(Primary Key) | UID(主键)| Name | 姓名| Contact | 联系|

1 Harsh 123456 1严厉123456

2 Guddu 789456 2古杜789456

TABLE 2 (Email) 表2(电子邮件)

Here You Have to assign UID to apply Index key Then in relation View Assign UID Of TABLE 1 (USER) To Connect UID of TABLE 2 (EMAIL) 在这里,您必须分配UID以应用索引键,然后在视图中分配表1的UID(用户)以连接表2的UID(电子邮件)


EID(Primary Key) | EID(主键)| UID(Foreign Key) | UID(外键)| Email | 电邮|


1 | 1 | 1 | 1 | a@a.com a@a.com

2 | 2 | 2 | 2 | b@b.com b@b.com

[Download Image From Below Link] [从下面的链接下载图像]

http://i.stack.imgur.com/Rpn6J.png http://i.stack.imgur.com/Rpn6J.png

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM