1. 创建表时创建唯一索引
create table table_name(
      属性名 数据类型,
      属性名 数据类型,
      ...
      属性名 数据类型,
      unique index|key 索引名(属性名1)【(长度)】【ASC|DESC】)
);
注意:在上述语句中,比创建普通索引多了一个unique关键字。其中unique index或unique key 表时创建唯一索引。

for example: create table t_dept(
             deptno int unique,
             dname varchar(30),
             loc varchar(30),
             unique index index_deptno(deptno)
             );
show create table t_dept \G;
explain select * from t_dept where depnto=10 \G;
-------------------------------------------------
2.在已经存在的表上,创建唯一索引
create unique index 索引名 on 表名 (属性名 【(长度)】 【ASC|DESC】)
for example: create unique index index_deptno on t_dept(deptno);
mysql> create unique index uni_ind_name on student(name);
Query OK, 0 rows affected (0.36 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc student;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | char(20)    | NO   | UNI | NULL    |                |
| age   | tinyint(2)  | NO   |     | 0       |                |
| dept  | varchar(16) | YES  | MUL | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
--------------------------------------------------------------------
3.alter table table_name add unique index|key 索引名(属性名 【(长度)】 【ASC|DESC】)
for example: alter table t_dept add unique index index_deptno(deptno);
--------------------------------------------------------------------------------------