Friday 15 January 2010

mysql - Check if a record with the same unique ID exists in SQL table before insert -


How can I check that the table contains only unique identification card (column name id ) , And if no one does anything else, else enter the record?

Should I index the id column and set it as a unique / primary key?

Use an option INSERT IGNORE . As the name suggests, the rows in it must be included, and it will do nothing with two rows.

  make table table 1 (`id` is not an integer; primary elementary key;` fu 'varchar (20)); (1, 'A'), (2, 'A'), (3, 'A'), (4, 'A'), (1, '1' one) (1, 'A');   

Only the first 4 rows above will be inserted, the last 3 rows are not inserted because they have duplicate keys.

INSERT IGNORE requires that you have a primary key or unique index. More info INSERT IGNORE (as well as other ways to handle duplicates):

No comments:

Post a Comment