Wednesday, January 18, 2012

INSERT IGNORE


An error will occur when entering a new record in MySQL, if the primary key specified in the insert query already exists. Using the keyword "IGNORE" to prevent errors occur because there is a duplication of records and other queries can still be executed.

Why ??

You should not try to insert a record without first checking if the primary key you want to use already exists, there may be time for this query is required, such as when some developers need to update their own data backups of the database, and a particular record may already exist in other database tables.


Inserting a single record
The Query is very simple, just add "IGNORE" after "INSERT" as:
INSERT IGNORE INTO pegawai
    (primaryKey, NIK, no_meja)
VALUES
    ('abc', 1, 2);

Inserting multiple records
When inserting multiple records at once, record the exact same as the above data will be ignored and that no records will be in the right insert.
INSERT IGNORE INTO pegawai
    (primaryKey, NIK, no_meja)
VALUES
    ('abc', 1, 2),
    ('def', 3, 4),
    ('ghi', 5, 6);


Well from the above data, record 'abc', 1, 2 will not be in the insert because the data already exists.

Conclusion : Insert ignore used when we want to add records are records that may already exist in the table in the insert so that no data redundancy (duplicate).


Don't miss it
vote web ini yaa klik disini!!

No comments:

Post a Comment