注意:如果Key是MUL, 那么该列的值可以重复。该列是一个非唯一索引的前导列(第一列)或者是一个唯一性索引的组成部分但是可以含有空值NULL。
1.3 删除索引语法:
alter table 表名 drop key 索引名(或字段名);例子:
MariaDB [book]> alter table demo drop key pwd; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [book]> alter table demo1 drop key pwd; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [book]> alter table demo2 drop key index_pwd; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [book]> alter table books add key (bName,bTypeId); Query OK, 44 rows affected (0.01 sec) Records: 44 Duplicates: 0 Warnings: 0 2. 唯一索引与普通索引基本相同,但有一个区别:
索引列的所有值都只能出现一次,即必须唯一,用来约束内容。 字段值只能出现一次时,应该加唯一索引。 唯一性允许有NULL值<允许为空>。 2.1 创建唯一索引 2.1.1 方法一:创建表时加唯一索引语法:
create table 表名( 列定义: unique key 索引名 (字段); )注意:常用在值不能重复的字段上,比如说用户名,电话号码,身份证号。
例子:
MariaDB [book]> create table demo3(id int(4) auto_increment primary key,uName varchar(20),uPwd varchar(20),unique index(uName)); Query OK, 0 rows affected (0.07 sec) MariaDB [book]> desc demo3; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(4) | NO | PRI | NULL | auto_increment | | uName | varchar(20) | YES | UNI | NULL | | | uPwd | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec) 2.1.2 方法二:修改表时加唯一索引语法:
alter table 表名 add unique 索引名 (字段);例子:
MariaDB [book]> desc books; +------------+------------------------------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+------------------------------------------------+------+-----+---------+----------------+ | bId | int(4) | NO | PRI | NULL | auto_increment | | bName | varchar(255) | YES | | NULL | | | bTypeId | enum('1','2','3','4','5','6','7','8','9','10') | YES | | NULL | | | publishing | varchar(255) | YES | | NULL | | | price | int(4) | YES | | NULL | | | pubDate | date | YES | | NULL | | | author | varchar(30) | YES | | NULL | | | ISBN | varchar(255) | YES | | NULL | | +------------+------------------------------------------------+------+-----+---------+----------------+ 8 rows in set (0.01 sec) MariaDB [book]> alter table books add unique index_bName (bName); Query OK, 43 rows affected (0.01 sec) Records: 43 Duplicates: 0 Warnings: 0 MariaDB [book]> desc books; +------------+------------------------------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+------------------------------------------------+------+-----+---------+----------------+ | bId | int(4) | NO | PRI | NULL | auto_increment | | bName | varchar(255) | YES | UNI | NULL | | | bTypeId | enum('1','2','3','4','5','6','7','8','9','10') | YES | | NULL | | | publishing | varchar(255) | YES | | NULL | | | price | int(4) | YES | | NULL | | | pubDate | date | YES | | NULL | | | author | varchar(30) | YES | | NULL | | | ISBN | varchar(255) | YES | | NULL | | +------------+------------------------------------------------+------+-----+---------+----------------+ 8 rows in set (0.02 sec) 2.2 删除唯一索引 alter table 表名 drop key 索引名(或字段名);