删除唯一索引命令格式:
alter table 表格名称 drop index 字段名;
添加唯一索引命令格式:
alter table 表格名称 add unique(字段名称,…);
---提前创建好的表格,现在在name字段上面做测试,预先已经添加了unique,
----先删除unique
mysql> desc test_unique;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(30) | NO | UNI | NULL | |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
---删除字段name的唯一索引
mysql> alter table test_unique drop index name;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc test_unique;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(30) | NO | | NULL | |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
---显示表的信息之后,可以看到name 的unique属性没有了
---在测试一下添加唯一索引命令
mysql> alter table test_unique add unique(name);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
--查看表的详情,可以看到name字段的唯一索引添加成功
mysql> desc test_unique;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(30) | NO | UNI | NULL | |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
---现在测试一下多字段的情况,使用预先建立好的表格,在name和email字段上面
---添加唯一索引,然后删除唯一索引
mysql> desc test_unique;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(30) | NO | | NULL | |
| email | varchar(50) | NO | | NULL | |
+-------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
----注意添加两个字段唯一索引的命令格式
mysql> alter table test_unique add unique(name, email);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
---显示表格信息
mysql> desc test_unique;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(30) | NO | MUL | NULL | |
| email | varchar(50) | NO | | NULL | |
+-------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
----现在删除唯一索引,
mysql> alter table test_unique drop index name;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
---显示表格信息,可以看到多字段的唯一索引没有了,大家测试的时候,可以采用
---show create table 表格名称;
----使用上面命令也可以查看表格的详细信息。
mysql> desc test_unique;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(30) | NO | | NULL | |
| email | varchar(50) | NO | | NULL | |
+-------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)