插入数据时,Insert语句与Foreign Key约束冲突

时间:2013-12-26 07:01:28

标签: mysql sql database

我有一个名为 Book_details 的表,其主键为出价,现在此BID也是同一数据库中其他两个表的外键(即slend_details,Book_inventory)

当我将数据插入inventory_details表时,我收到以下错误:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK__book_invent__Bid__1367E606". The conflict occurred in database "library_management", table "dbo.Book_details", column 'Bid'.
The statement has been terminated.

这是我使用的插入语句:

insert into Book_inventory (Bid, present) values (001,1)

这是我用于 book_inventory 表的创建声明:

create table book_inventory (
    Bid varchar(50) Not null Foreign Key references Book_details(Bid),
    present bit Not null*
)

我甚至检查过这些值,即我试图在book_inventory表中输入的数据出现在Book_details表中。我仍然得到错误。有人可以帮助我吗?

2 个答案:

答案 0 :(得分:3)

您的插入内容;

insert into Book_inventory (Bid,present) values (001,1)

Bid作为整数插入,这很可能意味着它将1的值简化为Bid不存在。

引用该值并将其作为实际字符串插入应该有效;

insert into Book_inventory (Bid,present) values ('001',1)

答案 1 :(得分:0)

列Bid是varchar,您将其用作整数。试试这个查询

insert into Book_inventory (Bid, present) values ('001',1)