mysql索引

来源:互联网 发布:淘宝橱窗推荐怎么设置 编辑:程序博客网 时间:2024/06/01 07:57

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;

0 0
原创粉丝点击