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

前端 未结 2 361
旧时难觅i
旧时难觅i 2021-01-13 06:33

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

INSERT INTO USERS_AVATAR(userId, avatId) VALUES (\'         


        
2条回答
  •  醉梦人生
    2021-01-13 07:22

    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.

    In this case, there's a table for user data (USERS) and a table for avatar data (AVATARS), and the USERS_AVATAR table 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:

    INSERT INTO USERS (userId, email, password, status) VALUES (1, 'gk314@hotmail.com',' 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 userId that matches the userId you're attempting to add to USERS_AVATAR. Check the USERS table and add users with userId 1 and 2, and then you should be able to add to the USERS_AVATAR table.

提交回复
热议问题