Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#1062 - Duplicate entry for key 'PRIMARY'

So my MySQL database is behaving a little bit wierd. This is my table:

Name shares id  price   indvprc
cat   2     4   81      0
goog  4     4   20      20
fb    4     9   20      20

I'm getting this #1062 error when I try to insert into the table. So I looked into it further and realized that when I try to insert values into the table, in which the name and shares values are the same, it will return the #1062 error. For example, If i inserted:

fb    4      6     20   20 

It would return an error. But if i changed the shares number to 6, it would run fine. Is it because of one of my columns that could be unique, or is it just something with mysql?

like image 696
irosenb Avatar asked Jul 24 '12 20:07

irosenb


4 Answers

You need to remove shares as your PRIMARY KEY OR UNIQUE_KEY

like image 156
mlishn Avatar answered Nov 15 '22 11:11

mlishn


Use SHOW CREATE TABLE your-table-name to see what column is your primary key.

like image 41
Majid Fouladpour Avatar answered Nov 15 '22 11:11

Majid Fouladpour


  1. Make sure PRIMARY KEY was selected AUTO_INCREMENT.
  2. Just enable Auto increment by :
    ALTER TABLE [table name] AUTO_INCREMENT = 1
  3. When you execute the insert command you have to skip this key.
like image 20
Đoàn Nhật Duẩn Avatar answered Nov 15 '22 11:11

Đoàn Nhật Duẩn


I solved it by changing the "lock" property from "shared" to "exclusive":

ALTER TABLE `table` 
CHANGE COLUMN `ID` `ID` INT(11) NOT NULL AUTO_INCREMENT COMMENT '' , LOCK = EXCLUSIVE;
like image 36
Hegle Leonardo Melgar Avatar answered Nov 15 '22 12:11

Hegle Leonardo Melgar