Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.1k views
in Technique[技术] by (71.8m points)

mysql - ERROR 1062 (23000): Duplicate entry '2147483647' for key 'PRIMARY'

First off all, i've seen many others post like this, however the answer wasn't working on my particular case. Just in case i am only few months studding SQL, i have no much knowledge on that.

there is a table:

CREATE TABLE passenger_details
   (
    bank_card INT(20) NOT NULL AUTO_INCREMENT,
    email VARCHAR(20), 
    mobile INT(15) NOT NULL,
    p_id INT NOT NULL,    
    PRIMARY KEY (bank_card),
    FOREIGN KEY (p_id) REFERENCES passenger(p_id) 
    );

INSERT INTO passenger_details 
VALUES (0123012301230123,'blah_@hotmail.com',0872863492,1234);

select*from passenger_details;
+------------+--------------------+-----------+------+
| bank_card  | email              | mobile    | p_id |
+------------+--------------------+-----------+------+
| 2147483647 | blah_@hotmail.com  | 872863492 | 1234 |
+------------+--------------------+-----------+------+
1 row in set (0.00 sec)  

As we can see, the previous value, just went wrong into table. Should be 16 numbers and not only 10, actually different numbers.
When i try to insert a new value:

INSERT INTO passenger_details 
VALUES (1234258431681842,'blah@icloud.com',0895764829,123548);
ERROR 1062 (23000): Duplicate entry '2147483647' for key 'PRIMARY'

If bank_card is AUTO_INCREMENT why there is error? Shall i change the PK to another place?

Really appreciate any help

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

INT has a maximum signed value of 2147483647. Any number greater than that will be truncated to that value. What you need to do is change that column to be a varchar(20) which will hold strings of up to 20 characters since bank card numbers are strings and not actually numbers (you don't do math with the). You also should remove AUTO_INCREMENT as that is not a value you will be incrementing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...