Tuesday, May 8, 2012

MySql Inserting Distinct Values in Table

Way 1 -  using REPLACE

REPLACE INTO `candidates`
    SET `candidate_id` = '000000146',
    `candidate_name` = 12345,
    `candidate_rollnumber` = 12678;
If the record exists, it will be overwritten; if it does not yet exist, it will be created.

Way 2 -  using INSERT IGNORE

   
INSERT IGNORE INTO `candidates`
   SET `candidate_id` = '000000146',
    `candidate_name` = 12345,
    `candidate_rollnumber` = 12678;


Here, if the 'candidate_id' is already present in the database, it will be silently skipped (ignored).

- Varun Rathore

About Me