MySQL database uniqueness setting (unique index)

We know that the field set as the primary key in the database design will not be repeated and unique, but if there are other fields that also need to be unique, how should it be set? For example, in a table that stores student information, it is necessary to ensure that the student number of each record is different. At this time, you need to set the uniqueness of this field. 
The field after uniqueness is set ensures that the field will not have the same value at the database level. Here's how to set the uniqueness:

1. Command line operation

There are two types. One is to think about adding uniqueness when building the table, and the other is to find out that uniqueness needs to be set later. 
When creating a table:

CREATE TABLE `t_user` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(18) NOT NULL unique,
`password` varchar(18) NOT NULL,
PRIMARY KEY (`Id`)  www.2cto.com
) ENGINE=InnoDB AUTO_INCREMENT=1018 DEFAULT CHARSET=gbk;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Later add:

 ALTER TABLE ·t_user· ADD unique(`username`)
  • 1

Note: The symbols at both ends of t_user and username are not quotation marks, but the symbols entered by the key next to the number key 1 above the tab on the keyboard (different keyboards may be slightly different, generally in the upper left corner)

2. Navicat method

It is more convenient to use navicat to operate the mysql database. It is recommended that you use this method to 
design tables: select the index as shown in the figure, the first item is 'name', and you can choose a meaningful name by yourself. The second item is the field you want to set unique, and then select unique for the index type. 
    
image.png

The table has been built: select the table, right-click - design table - do the same as above.

3. Possible errors

If the uniqueness is added later (after the table is designed, and there is already some data  in the
table), there may be some cases where the data in the table does not meet the uniqueness, for example: I want to set uniqueness for the field sid, but there are Both data sids are 472, which is an error when uniqueness is set. 
As shown in the figure below (this situation also occurs under the command line operation, but in the form of text)

image.png

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

4. Advanced operation - uniqueness of combined index

I only talked about the method of setting uniqueness for one field. What if there is a combination of two or more fields that needs to be unique? 
Example: There is a table that holds final reports uploaded each week by seniors. It has fields such as student id: sid, week number: week, report content: content, etc. 
Here, each student is only allowed to upload one weekly report per week, that is, the combination of sid and week cannot be duplicated, that is, there cannot be two records whose sid and week are the same. 
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!

image.png

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324773409&siteId=291194637