Home >Database >Mysql Tutorial >MySQL field processing methods

MySQL field processing methods

小云云
小云云Original
2018-02-23 10:21:011313browse

How to deal with the uniqueness of multiple fields?

  • 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('你是好','我是谁')))

How to handle it if the data exists, it is updated, if it does not exist, it is inserted?

  • 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn