Home >Database >Mysql Tutorial >MySQL field processing methods
Scenario
There is a table with multiple fields that need to be unique and cannot be repeated, otherwise they will be duplicates Data cannot be inserted
Traditional approach
Add unique indexes directly to multiple fields, simple and crude
Now how to do it
Add a new field and add a unique index to this field, so you don’t need to add too many unique indexes
insert into news(new_title, new_abstr, new_code) values('你是好','我是谁',MD5(CONCAT('你是好','我是谁')))
Scenario
There is a table. The records in it cannot have duplicate records. If the record exists, it will be updated. If not If it exists, insert it
Traditional approach
Query the select first, if it exists, update it, if it does not exist, update it
The current practice
mysql has a special writing method, on DUPLICATE key Update
insert into news(new_title, new_abstr, new_code, update_time, create_time) values('你是好','我是谁',MD5(CONCAT('你是好','我是谁'))) on DUPLICATE key Update update_time=now(), create_time=now()
The above is the detailed content of MySQL field processing methods. For more information, please follow other related articles on the PHP Chinese website!