Belajar Tidak Identik dengan Buku

MySQL – INSERT IGNORE INTO

When you try to insert a new record in MySQL and the primary key specified in the insert query already exists, then an error will occur and the query will be aborted. MySQL provides several alternatives to the standard INSERT statement and each handles duplicate rows slightly differently: “INSERT IGNORE,” “INSERT … ON DUPLICATE KEY UPDATE,” and “REPLACE.”

Using the “IGNORE” keyword prevents errors from occurring. The records containing duplicate primary keys are ignored and the rest of the records are added. In the former case, no record will be inserted if such an error occurs.

The syntax is simple – just add “IGNORE” after “INSERT”. Lets see an example:

INSERT IGNORE INTO tablename (id, field1, field2) VALUES (1, ‘value1′, ‘value2′);

In the above query, the id is assumed to be the primary key. If a record in the table ‘tablename’ has already a record with id = 1 then the above query will just be ignored instead of producing a warning. Note: Use the IGNORE keyword only during the development phase. It could cause unexpected results as you cannot identify if the record has been inserted or not.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Happy Paperless located at Banjarnegara , Jawa Tengah, Indonesia . Reviewed by 28 customers rated: 1 / 5



Rumus-Excel.Com
Kunjungi rumus-excel.com untuk belajar excel. Anda juga dapat menyumbangkan artikelnya ke kami untuk kita publish