Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How mysql deletes unique key

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces mysql how to delete unique key related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will have something to gain after reading this mysql how to delete unique key article, let's take a look.

In mysql, the ALTER TABLE table name DROP INDEX unique key name statement can be used to delete the unique key;ALTER TABLE statement to add, delete, or modify data, and the DROP INDEX statement is used to indicate the delete constraint operation.

The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.

How does mysql delete unique key?

The MySQL unique constraint (Unique Key) means that the values of fields in all records cannot be repeated.

For example, when you add a uniqueness constraint to the id field, the id value of each record is unique and cannot be duplicated. If one of the records has an id value of '0001', then the other record cannot have an id value of '0001' in the table.

The only constraints that are similar to primary key constraints are that they both ensure the uniqueness of the column. The difference is that there can be multiple unique constraints in a table, and columns that set unique constraints allow null values, but only one null value. The primary key constraint can only have one in a table, and null values are not allowed. For example, in the user information table, to avoid duplicate user names in the table, you can set the user name to a unique constraint.

Delete unique constraint

The syntax format for deleting unique constraints in MySQL is as follows:

ALTER TABLE DROP INDEX

Examples are as follows:

Delete the unique constraint unique_name,SQL statement in the data table tb_dept1 and run the result as shown below.

Mysql > ALTER TABLE tb_dept1-> DROP INDEX unique_name;Query OK, 0 rows affected (0.20 sec) Records: 0 Duplicates: 0 Warnings: 0mysql > DESC tb_dept1 +-+ | Field | Type | Null | Key | Default | Extra | +- +-+ | id | int (11) | NO | PRI | NULL | name | varchar (22) | NO | | NULL | | location | varchar (50) | YES | | NULL | | +- -+-+ 3 rows in set (0.00 sec)

Expand knowledge:

Set unique constraints when creating a table

Unique constraints can be set directly when the table is created, usually on columns other than the primary key.

Use the UNIQUE keyword to specify unique constraints directly after the column is defined. The syntax format is as follows:

UNIQUE

Example 1

Create the data table tb_dept2, and specify that the name of the department is unique. The SQL statement and run result are shown below.

Mysql > CREATE TABLE tb_dept2-> (- > id INT (11) PRIMARY KEY,-> name VARCHAR (22) UNIQUE,-> location VARCHAR (50)->); Query OK, 0 rows affected (0.37 sec) mysql > DESC tb_dept2 +-+ | Field | Type | Null | Key | Default | Extra | +- +-+ | id | int (11) | NO | PRI | NULL | name | varchar (40) | YES | UNI | NULL | | location | varchar (50) | YES | | NULL | | +-+ -+ 3 rows in set (0.08 sec) this is the end of the article on "how mysql deletes unique key" Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to delete unique key from mysql". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report