Tuesday, 20 April 2010

How to Preventing Duplicates from Occurring in a Table:

You can use INSERT IGNORE or REPLACE keyword to prevent duplicates  from occurring in a table.

Use INSERT IGNORE rather than INSERT. If a record doesn't duplicate an existing record, MySQL inserts it as usual. If the record is a duplicate, the IGNORE keyword tells MySQL to discard it silently without generating an error.

for example:

mysql> INSERT IGNORE INTO employee_tbl (lastname, firstname)
    -> VALUES( 'Kumar', 'Ashwani');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT IGNORE INTO employee_tbl (lastname, firstname)
    -> VALUES( 'Kumar', 'Ashwani');
Query OK, 0 rows affected (0.00 sec)
INSERT IGNORE and REPLACE should be chosen according to the
duplicate-handling behavior you want to effect. INSERT IGNORE keeps the
first of a set of duplicated records and discards the rest. REPLACE
keeps the last of a set of duplicates and erase out any earlier ones.
 
 
Cheers!

No comments:

Post a Comment