Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

[ MySQL INSERT IGNORE ] Insert the 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.

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




This post first appeared on Devdummy, please read the originial post: here

Share the post

[ MySQL INSERT IGNORE ] Insert the record only If it does not Exist

×

Subscribe to Devdummy

Get updates delivered right to your inbox!

Thank you for your subscription

×