MySQL Insert Ignore

Have you ever heard?
If not, Me neither... hehe...
I just found out when there was user request to import/export data from comma delimited file.
In SQL Server I used to use the IF EXISTS syntax to check availability data before inserting.

This mysql syntax provide this scenario:
Insert data if not exist, otherwise ignore it.

Single Record:
INSERT IGNORE INTO namatable
    (primaryKeyfield, field1, field2)
VALUES
    ('primaryKeyvalue', 'field1value', 'field2value');


Multiple Records:
INSERT IGNORE INTO namatable
    (primaryKeyfield, field1, field2)
VALUES
    ('primaryKeyvalue1', 'field1value1', 'field2value1'),


Click here if you like this article.


Next: INSERT...ON DUPLICATE KEY UPDATE

Post a Comment

0 Comments