您的位置:首页 > 数据库 > MySQL

mysql索引

2016-10-31 09:22 176 查看
1 索引的创建

1.1  普通索引





1.2  .唯一索引

mysql> create UNIQUE index index_unique on user1(username);

mysql> show create table user1\G;

       Table: user1

Create Table: CREATE TABLE `user1` (

  `id` int(11) DEFAULT NULL,

  `username` char(10) DEFAULT NULL,

  `password` varchar(20) DEFAULTNULL,

  `address` varchar(20) DEFAULTNULL,

  UNIQUE KEY `index_unique`(`username`)

) ENGINE=InnoDB DEFAULT CHARSET=gbk

1 row in set (0.00 sec)

mysql>

1. 3 .主键索引

mysql>  alter table user1 addprimary key(id);

Query OK, 2 rows affected (0.16 sec)

Records: 2  Duplicates: 0  Warnings: 0

 

mysql> show create table user1\G;

      Table: user1

Create Table: CREATE TABLE `user1` (

  `id` int(11) NOT NULL DEFAULT '0',

  `username` char(10) DEFAULT NULL,

  `password` varchar(20) DEFAULTNULL,

  `address` varchar(20) DEFAULTNULL,

  PRIMARY KEY (`id`),

  UNIQUE KEY `index_unique`(`username`)

) ENGINE=InnoDB DEFAULT CHARSET=gbk

1 row in set (0.00 sec)

2 索引删除

删除唯一索引

mysql>alter table user1 drop index index_unique;

删除主键索引

第一种方法:

mysql> alter table user1 drop index primary key;

第二种方法:

mysql> drop index index_name on users;

3  复合索引

mysql> alter table user1 add index index_address_age(address,age);

Query OK, 0 rows affected (0.21 sec)

mysql> show create table user1\G;

Table: user1

Create Table: CREATE TABLE `user1` (

  `id` int(11) NOT NULL DEFAULT '0',

  `username` char(10) DEFAULT NULL,

  `password` varchar(20) DEFAULTNULL,

  `address` varchar(20) DEFAULTNULL,

  `age` int(11) DEFAULT NULL,

  KEY `index_address_age`(`address`,`age`)

) ENGINE=InnoDB DEFAULT CHARSET=gbk

1 row in set (0.00 sec)

mysql>

4 索引查看

第一种方法:

mysql> show create table  users\G;

第二种方法:

mysql> show index from users \G;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息