Home > Database > Mysql Tutorial > Can mysql set a joint unique index?

Can mysql set a joint unique index?

青灯夜游
Release: 2020-10-02 10:49:34
Original
6049 people have browsed it

mysql can set up a joint unique index. Method: Use the "Alter table table name add UNIQUE index index name (field 1, field 2)" statement to set it up. It will delete duplicate records, keep one, and then create Union unique index.

Can mysql set a joint unique index?

Joint Unique Index

The project needs to add unique indexes to two fields of a table to ensure that the values ​​​​of these two fields cannot Repeat simultaneously.

Alter table 表名 add  UNIQUE index 索引名 (字段1,字段2)
Copy after login

When duplicate data already exists in the table, an error will be reported when adding it. At this time, the data needs to be deduplicated.

1. First find out the duplicate data

SELECT * FROM (SELECT 字段,COUNT(1) AS num FROM 表 GROUP BY 字段) temp WHERE num >
Copy after login

and delete it manually.

2.Alter ignore table table name add UNIQUE index index name (field 1, field 2)

It will delete duplicate records (one will be retained), and then Create a unique index, efficient and user-friendly (not tested).

I also found some related content:

1. Add PRIMARY KEY (primary key index)

ALTER TABLE `table_name` ADD PRIMARY KEY ( `column` )

2. Add UNIQUE (unique index)

ALTER TABLE `table_name` ADD UNIQUE ( `column` )

3. Add INDEX (normal index)

ALTER TABLE `table_name` ADD INDEX index_name ( `column` )
Copy after login

4. Add FULLTEXT (full-text index)

mysql>ALTER TABLE `table_name` ADD FULLTEXT ( `column`)
Copy after login

5. Add multi-column index

ALTER TABLE `table_name` ADD INDEX index_name ( `column1`, `column2`, `column3` )
Copy after login

Recommended tutorial: mysql video tutorial

The above is the detailed content of Can mysql set a joint unique index?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template