sandeep
sandeep

Reputation: 11

When inserting data, Insert statement conflicted with the Foreign Key constraint

I have a table called Book_details, which has a primary key as Bid and now this BID is also a foreign key for other two table in the same database (I.e slend_details, Book_inventory)

When I insert data into inventory_details table, I'm getting the following error:

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.

This is the insert statement I used:

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

This is the create statement I used for my book_inventory table:

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

I have even checked the values i.e the data I am trying to enter in book_inventory table is present in the Book_details table. Still I get the error. Can someone help me out?

Upvotes: 1

Views: 88

Answers (2)

Satish Sharma
Satish Sharma

Reputation: 9635

column Bid is varchar and you are using it as integer. try this query

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

Upvotes: 0

Joachim Isaksson
Joachim Isaksson

Reputation: 180917

Your insert;

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

inserts Bid as an integer, which most likely means that it simplifies the value to 1 which does not exist as a Bid.

Quoting the value and inserting it as an actual string should work;

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

Upvotes: 3

Related Questions