0
Duplicate entry 100 for key employees.primary for sql..... How do I solve this problem
4 Answers
0
Almost definitely, your primary key is getting incorrectly initialized to 100 despite 100 already being used.
The following should help you gather more detailed information.
I would do the following to confirm a few things:
1. Connect with your SQL server and verify that one of your records already has a primary key value of 100.
A query like this would do the trick:
select * from employees where id=100;
I'm almost 100% sure it exists but maybe you're connecting to the wrong database or something unknown is happening.
2. Check how your new employee id is getting calculated. Is there an auto_increment on the primary key column?
If this is MySQL and it is auto incremented,
desc employees;
mysql> desc employees;
+----------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | UNI | NULL | |
+----------------------+------------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
If it isn't auto incremented, find out where that primary key is initialized from. Maybe you have code that looks for max(id) when it should add 1 to that.
Something like this could help if your primary key's auto incremented value is weirdly generating 100 despite it already existing:
ALTER TABLE employees AUTO_INCREMENT = 101;
0
wow....thanks for the response sir!!!!
0
Please share what you learn, Jules. I wonder what you find from those checks and how you fix it.
0
I had actually made a silly mistake.... I had already inserted the input before.. hence the duplicate entry error.