mysql can set the joint unique index, method: use the “Alter table table name add UNIQUE index index name (field 1, field 2)” statement to set, it will delete duplicate records, keep one, and then create Joint unique index.
joint unique index
The project needs to add a unique index to a certain two fields of a table , to ensure that the values of these two fields cannot be duplicated at the same time.
Alter table table name add UNIQUE index index name (field 1, field 2)
When duplicate data already exists in the table , an error will be reported when adding, and the data needs to be deduplicated.
1. Check out the duplicate data first
SELECT * FROM (SELECT field, COUNT(1 ) AS num FROM table GROUP BY field) temp WHERE num >
Delete manually.
2.Alter ignore table table name add UNIQUE index index name (field 1, field 2)
It will delete duplicate records (one will be kept ), and then create a unique index, which is 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 (ordinary 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 can mysql set a joint unique index? For more details, please pay attention to other related articles on 1024programmer.com!