#1062 - Duplicate entry 0' for key 'PRIMARY'

☆樱花仙子☆ 提交于 2019-12-11 08:01:35

问题


INSERT INTO `crm_customer` (`customerid`, `firstname`, `lastname`, `address`, 
  `telephoneno`, `companyname`, `fax`, `comments`, `countryid`, `statename`, 
  `cityname`, `emailaddress`, `zipcode`, `dateofbirth`, `unsubscribe`) 
VALUES ('0', 'jhghjgfk', 'kghjkj', 'hjkghjgh', '8776785', 'hjghjkgyjk', 
     '457665', 'jghjgfhj', '0', 'ghjgfjgf', 'gjgfhj', 'ghjgfhjgfj', 
     '764574576', '2017-03-13', '')

I'm getting error as `#1062 - Duplicate entry 0' for key 'PRIMARY'



回答1:


If you have an auto increment column you don't need the value for key primary .. assuming you key primary is customerid you should use:

    INSERT INTO `crm_customer` ( `firstname`, `lastname`, `address`, 
  `telephoneno`, `companyname`, `fax`, `comments`, `countryid`, `statename`, 
  `cityname`, `emailaddress`, `zipcode`, `dateofbirth`, `unsubscribe`) 
VALUES ( 'jhghjgfk', 'kghjkj', 'hjkghjgh', '8776785', 'hjghjkgyjk', 
     '457665', 'jghjgfhj', '0', 'ghjgfjgf', 'gjgfhj', 'ghjgfhjgfj', 
     '764574576', '2017-03-13', '')

Simply avoid the column name and value in the corresponding part of the insert.

Or, you can use it in column name list but with null value:

    INSERT INTO `crm_customer` (`customerid`,  `firstname`, `lastname`, `address`, 
  `telephoneno`, `companyname`, `fax`, `comments`, `countryid`, `statename`, 
  `cityname`, `emailaddress`, `zipcode`, `dateofbirth`, `unsubscribe`) 
VALUES ( null, 'jhghjgfk', 'kghjkj', 'hjkghjgh', '8776785', 'hjghjkgyjk', 
     '457665', 'jghjgfhj', '0', 'ghjgfjgf', 'gjgfhj', 'ghjgfhjgfj', 
     '764574576', '2017-03-13', '')

And, if you don't have auto increment add it:

 ALTER TABLE crm_customer MODIFY COLUMN customerid INT auto_increment



回答2:


I assume customerid is your primary key and you are trying to make duplicate entry for the same customerid.

Try this

INSERT INTO `crm_customer` ( `firstname`, `lastname`, `address`, 
  `telephoneno`, `companyname`, `fax`, `comments`, `countryid`, `statename`, 
  `cityname`, `emailaddress`, `zipcode`, `dateofbirth`, `unsubscribe`) 
VALUES ('jhghjgfk', 'kghjkj', 'hjkghjgh', '8776785', 'hjghjkgyjk', 
     '457665', 'jghjgfhj', '0', 'ghjgfjgf', 'gjgfhj', 'ghjgfhjgfj', 
     '764574576', '2017-03-13', '');

And if customerid is not autoincremented set, then set it by,

ALTER TABLE crm_customer AUTO_INCREMENT = 1

This will work if and only if you already have primary key in your table.



来源:https://stackoverflow.com/questions/42833926/1062-duplicate-entry-0-for-key-primary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!