SQL INSERT 语句与 FOREIGN KEY 约束冲突。数据库发生冲突

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29235773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:27:36  来源:igfitidea点击:

The INSERT statement conflicted with the FOREIGN KEY constraint. The conflict occurred in database

sqlsql-serverforeign-keys

提问by problems

I have been having this problem for a couple of hours now. In SQL Server, I ran this query :

我已经遇到这个问题几个小时了。在SQL Server,我运行了这个查询:

INSERT INTO USERS_AVATAR(userId, avatId) VALUES ('1', '213');
INSERT INTO USERS_AVATAR(userId, avatId) VALUES ('2', '312');

but the message shows up saying :

但消息显示:

Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "FKUSERS_AVAT731248". The conflict occurred in database "gk314", table "gk314.USERS", column 'userId'.

Msg 547, Level 16, State 0, Line 2
The INSERT statement conflicted with the FOREIGN KEY constraint "FKUSERS_AVAT731248". The conflict occurred in database "gk314", table "gk314.USERS", column 'userId'.

消息 547,级别 16,状态 0,第 1 行
INSERT 语句与 FOREIGN KEY 约束“FKUSERS_AVAT731248”冲突。冲突发生在数据库“gk314”、表“gk314.USERS”、“userId”列中。

消息 547,级别 16,状态 0,第 2 行
INSERT 语句与 FOREIGN KEY 约束“FKUSERS_AVAT731248”冲突。冲突发生在数据库“gk314”、表“gk314.USERS”、“userId”列中。

Help please!

请帮忙!

回答by Russ

Before inserting userIds 1and 2into USERS_AVATAR, you must first insert them into table USERS. That is what the FOREIGN KEY constraint is requiring.

在插入 userIds12into之前USERS_AVATAR,您必须先将它们插入 table USERS。这就是 FOREIGN KEY 约束所要求的。

回答by Adam V

Foreign key constraints are SQL's way of saying "this table expects data to exist in other tables". It allows you to reference other tables without the data having to exist twice or to be kept in sync.

外键约束是 SQL 的说法“此表期望数据存在于其他表中”。它允许您引用其他表,而无需数据存在两次或保持同步。

In this case, there's a table for user data (USERS) and a table for avatar data (AVATARS), and the USERS_AVATARtable links the two together. You will need to add a user to the users table, then an avatar to the avatar table, then you can link the two together. It will look something like this:

在这种情况下,有一个用户数据表 ( USERS) 和一个头像数据表 ( AVATARS),并且该USERS_AVATAR表将两者链接在一起。您需要将用户添加到用户表中,然后将头像添加到头像表中,然后您可以将两者链接在一起。它看起来像这样:

INSERT INTO USERS (userId, email, password, status) VALUES (1, '[email protected]',' gk314', 'strong')
INSERT INTO AVATARS (avatId, name, ...) VALUES (1, 'Avatar1', ...)
INSERT INTO USERS_AVATAR (userId, avatId) VALUES (1, 1)


Original Answer:

原答案:

This means that there's a table, gk314.USERS, that doesn't have a userIdthat matches the userIdyou're attempting to add to USERS_AVATAR. Check the USERStable and add users with userId1 and 2, and then you should be able to add to the USERS_AVATARtable.

这意味着有一个表,gk314.USERS, 没有userIduserId您尝试添加到的相匹配的USERS_AVATAR。检查USERS表并添加用户userId1和2,然后您应该可以添加到USERS_AVATAR表中。