1024programmer Mysql Can mysql set a joint unique index?

Can mysql set a joint unique index?

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!

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/can-mysql-set-a-joint-unique-index/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索