简体   繁体   English

表“用户”中的列与现有主键或唯一约束不匹配

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

I have two tables, 'Users' and 'IssueBooks'.我有两个表,'Users' 和'IssueBooks'。 In both the tables, I have ADMIN_ID,USN and E_ID as columns.在这两个表中,我都将 ADMIN_ID、USN 和 E_ID 作为列。

When I try to connect Users Table to IssueBooks Table making those columns foreign key, this error pops up:-当我尝试将用户表连接到使这些列成为外键的 IssueBooks 表时,会弹出此错误:-

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.所有三列都是主键,我希望它们是 IssueBooks 表中的外键。

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 .这意味着 Users 具有父表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 :现在,您需要更新或删除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:现在在Users表中相应地创建外键:
ALTER TABLE Users 
    ADD CONSTRAINT FK_Users_issueBooks 
        FOREIGN KEY (ADMIN_ID, USN, E_ID) 
        REFERENCES IssueBooks(ADMIN_ID, USN, E_ID)

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

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