简体   繁体   中英

Foreign Key Error in SQL Server

I have 2 tables ( Project1 and Accounts ).

In Projects1 table, I have 8 columns, 5 of which are assigned as the primary key.

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.

  • Project1 : Id (Primary Key), Name (Primary Key), AccType(Primary Key), AccNumber, DOC (Primary Key), DOM (Primary Key), Source, Status

  • Accounts : Id (Primary Key), AccType (Foreign Key)

So here the AccType column in the Accounts table points to the AccType column in the Project1 table.

AccType column in the Project1 table is the PRIMARY KEY in the Project1 table. The AccType column in the Accounts table is a FOREIGN KEY.

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'.

TIA -Mithali

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)

UID(Primary Key) | Name | Contact |

1 Harsh 123456

2 Guddu 789456

TABLE 2 (Email)

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)


EID(Primary Key) | UID(Foreign Key) | Email |


1 | 1 | a@a.com

2 | 2 | b@b.com

[Download Image From Below Link]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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