简体   繁体   中英

The column in table 'Users' do not match an existing primary key or unique constraint

I have two tables, 'Users' and 'IssueBooks'. In both the tables, I have ADMIN_ID,USN and E_ID as columns.

When I try to connect Users Table to IssueBooks Table making those columns foreign key, this error pops up:-

The column in table 'Users' do not match an existing primary key or unique constraint

All three columns are primary key and I want them to be foreign key in IssueBooks table.

All these columns are spelled same in both the tables.

It means that Users is having a combination not present in the parent table IssueBooks . Figure out the value and update it accordingly.

  1. Figure out the combination, which is missing in parent
SELECT ADMIN_ID, USN, E_ID
FROM Users AS uo
WHERE NOT EXISTS (SELECT 1 FROM IssueBooks 
                  WHERE ADMIN_ID = uo.ADMIN_ID 
                    AND USN = uo.USN 
                    AND E_ID = uo.E_ID)
  1. Now, you need to either update or delete the combination in Users :
UPDATE Users
SET ADMIN_ID = <newvalue>,
    USN = <newvalue>,
    E_ID = <newvalue>
WHERE ADMIN_ID = <oldvalue> 
  AND USN = <oldvalue> 
  AND E_ID = <oldvalue>

or

DELETE Users
WHERE ADMIN_ID = <oldvalue> AND USN = <oldvalue> AND E_ID = <oldvalue>
  1. Now create the foreign key accordingly in Users table:
ALTER TABLE Users 
    ADD CONSTRAINT FK_Users_issueBooks 
        FOREIGN KEY (ADMIN_ID, USN, E_ID) 
        REFERENCES IssueBooks(ADMIN_ID, USN, E_ID)

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