要从MySQL删除唯一键,请使用DROP命令。语法如下-
ALTER TABLE yourTableName DROP INDEX yourKeyName;
为了理解上述语法,让我们用唯一键创建一个表。创建表的查询如下-
mysql> create table DropIndexDemo −> ( −> BookId int unique key, −> BookName varchar(200) −> );
现在,您可以借助show命令检查键名称。此唯一键将被删除。查询如下-
mysql> show index from DropIndexDemo;
以下是输出-
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+ | dropindex | 0 | BookId | 1 | BookId | A | 0 | NULL | NULL | YES | BTREE | | | YES | +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+ 1 row in set (0.17 sec)
查看上面的示例输出,您的键名是'BookId'。现在这是删除唯一键的查询-
mysql> alter table DropIndex drop index BookId ; Records: 0 Duplicates: 0 Warnings: 0
我们已经从MySQL表DropIndex中删除了唯一键。BookId列之前具有唯一键。
现在借助desc命令显示表结构。由于我们已将其删除,因此不会显示唯一键-
mysql> desc DropIndex;
以下是输出-
+----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+--------------+------+-----+---------+-------+ | BookId | int(11) | YES | | NULL | | | BookName | varchar(200) | YES | | NULL | | +----------+--------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
查看上面的示例输出,在BookId列中没有唯一键。