Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of setting row value uniquely by mysql

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you the only way to set the row value of mysql, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Mysql can set row values to be unique by adding uniqueness constraints to rows (fields), with syntax formats "CREATE TABLE table name (field name data type UNIQUE);" and "ALTER TABLE table name ADD CONSTRAINT unique constraint name UNIQUE (column name);".

MySQL database uniqueness setting unique index

The fields that set the primary key in the database design are not duplicated and unique. What if there are other fields that need to be unique? For example, a table that stores user information should make sure that each record has a different mobile phone number. At this point, you need to set the uniqueness of the field.

After setting the uniqueness of the field, it is guaranteed that the same value will not appear in the field at the database level. Here's how to set uniqueness:

Mysql command line operation index

1. Add when building the meter

CREATE TABLE `member` (`id` int (11) NOT NULL AUTO_INCREMENT, `name` varchar (18) NOT NULL, `phone` varchar (18) NOT NULL unique,PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8

2. Change the table after building it

Alter table member add unique (phone); use the Navicat tool to add indexes

It is more convenient to use navicat to operate mysql database. It is recommended that you use this method more often.

When designing a table: select the index as shown in the figure, the first 'name', and just choose a meaningful name for yourself. The second item is that you want to set the unique field, and then select unique as the index type.

Select the name of the table you want to modify, and right-click to select the design table

Problems often encountered

If the uniqueness is added later (after the table is designed, and there is already some data in the table)

There may be some data in the table that is not unique. For example, if you want to set the uniqueness of the field phone, but there are two data phone in the table with the same mobile phone number. If you set the uniqueness, you will make an error.

As shown in the figure above (this can also happen under command line operations, but in the form of text)

At this point, you should delete the duplicate data and then set the uniqueness.

The combination of two or more fields into a unique index

Example: a user with a name and mobile phone number

Only one name for each mobile number is allowed, that is, the combination of name and phone cannot be duplicated, that is, there cannot be two records with the same name and phone.

How to set it in mysql, here is only the method in navicat: as shown in the figure, you only need to select two fields!

These are all the contents of the only way for mysql to set row values. Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report