less than 1 minute read

If you see Duplicate entry of Primary Index (Primary Key) Error Then most of case might be an user clicked request multiple times or query executed multiple times.

If you can solve it from frondend, the event should be unbinded until get the response. If you want to backend solved, try below samples depends on your business logic.

INSERT IGNORE

Ignore when the key is duplicated

INSERT IGNORE 
INTO `user`(`email`, `id`) 
VALUES('sample@gmail.com', 'testuser');

REPLACE INTO

DELETE EXISTING ROW and INSERT NEW ROW

REPLACE INTO `user`(`email`, `id`) 
VALUES('sample@gmail.com', 'testuser');

ON DUPLICATE KEY UPDATE

UPDATE ROW when the key is duplicated

INSERT INTO `user`(`email`, `id`) 
VALUES('sample@gmail.com', 'testuser')
    ON DUPLICATE KEY UPDATE `id`=VALUES(`id`);

Categories: ,

Updated:

Comments