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.
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)
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 >
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` )
4. Add FULLTEXT (full-text index)
mysql>ALTER TABLE `table_name` ADD FULLTEXT ( `column`)
5. Add multi-column index
ALTER TABLE `table_name` ADD INDEX index_name ( `column1`, `column2`, `column3` )
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!