How to fix the Error Code 1062 :Duplicate entry in MySQL?

Viewed 1434

I'm creating table 'book' to insert book details : book_id(ISBN),title, price,publisher with book_id is set to 'primary key'. When I run the insert commmand I recept the "Duplicate entry" Error like this

Error Code: 1062. Duplicate entry '2147483647' for key 'PRIMARY' 0.016 sec

Here is my code:

create table book (
book_id integer primary key,
title varchar(40) not null,
price decimal check (price>0),
publisher varchar(30) not null
                  );
insert into book
values (9784295002680,'Basic Informatic Exam',1280,'Impress'),
(9784781912400,'Numeric calculation',1850,'Science'),
(9784800314673,'prepare for SPI',1400,'Yoizumi'),
(9784797371260,'Java programming lesson',2400,'SB Creative'),
(9784797377026,'Understand C',2300,'SB Creative');
1 Answers

The book_ids you are using are larger than what int column can accept.

Surprisingly, they are apparently stored as the max int value which is 2147483647. Thus your 4 inserts are actually having the same key, which explains the error.

Related