Get the App
SLTechnology News&Howtos  ›  Database  › 

How to deal with MySQL field

Shulou Source: shulou.com Published: 2022-05-31 23:50:04 10月03日 Update

This article will explain in detail how to deal with the MySQL field. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

How to deal with the uniqueness of multiple fields?

Scenario

There is a table in which there are multiple fields that need to be unique and cannot be repeated, otherwise the data will be repeated and cannot be inserted.

Traditional practice

Directly add a unique index to multiple fields, simple and rough

The present practice

Add a new field with a unique index, so that you don't have to add too many unique indexes insert into news (new_title, new_abstr, new_code) values ('you are good', 'who am I', MD5 ('you are good', 'who am I')) data exists, update, do not exist, insert how to deal with it?

Scenario

There is a table in which there can be no duplicate records, update the records if they exist, and insert them if they do not exist.

Traditional practice

Query select first, update if it exists, and update if it doesn't exist

The present practice

Mysql has a special way to write, on DUPLICATE key Updateinsert into news (new_title, new_abstr, new_code, update_time, create_time) values ('you are good', 'who am I', MD5 (CONCAT ('you are good', 'who am I')) on DUPLICATE key Updateupdate_time=now (), create_time=now () on "how to deal with MySQL fields" this article is shared here, I hope the above content can be helpful to you. So that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Tags: Fields practices multiple articles indexing processing traditions scenarios data more updated good practical bold rude no content writing uniqueness that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Linux Microsoft Huawei macOS