1
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'


Schematics of table

Diwakar yadav
  • 39
  • 2
  • 8

3 Answers3

2

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
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

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.

Rahul
  • 18,271
  • 7
  • 41
  • 60
0

you have to auto increment primary key then your error will be solve. suppose we have a person table

CREATE TABLE Persons (
    Personid int NOT NULL AUTO_INCREMENT,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int,
    PRIMARY KEY (Personid)
);*

emphasized text

    *
Jaydeep
  • 1,686
  • 1
  • 16
  • 29