MySQL INSERT IGNORE : Insert a Record Only If it does not Exist - DevDummy

Latest

Development Guide for Dummies, Simple Tech Guides, Shared ...

Tuesday, July 31, 2018

MySQL INSERT IGNORE : Insert a Record Only If it does not Exist



INSERT IGNORE option by MySQL is a very convenient keyword to gracefully handle an INSERT of a record with an existing Key.

The regular INSERT always lead to a Primary Key violation error on above condition. But with IGNORE option, the query silently ignore the insert request on existing Key insertion.

Note : isbn is the Unique Key Column in this example

INSERT IGNORE INTO 
book (isbn, author, name, category, year, publisher) 
VALUES 
(9780596009205, 'Kathy Sierra, Bert Bates', 'Head First Java', 'Technology', 2013, ' O'Reilly Media, Inc.');


1 row(s) affected

INSERT IGNORE INTO 
book (isbn, author, name, category, year, publisher)
VALUES 
(9780596009205, 'Kathy Sierra, Bert Bates', 'Head First Java', 'Technology', 2013, ' O'Reilly Media, Inc.');

0 row(s) affected, 1 warning(s): 1062 Duplicate entry '9780596009205' for key 'PRIMARY'


https://dev.mysql.com/doc/refman/8.0/en/insert.html


No comments:

Post a Comment