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

5、Mysql普通索引创建实例

2016-04-04 11:24 645 查看
1、前面讲到了Mysql主键索引的创建,下面将介绍Mysql普通索引创建的实例

2、Mysql普通索引创建的实例

2.1 在创建表的时候,指定普通索引

create table table_index
(   id int primary key auto_increment  ,
name varchar(20) ,
index index_name (name)
);
利用 下面的sql语句,来查看表的创建语句
show  create  table   table_index;
+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| Table | Create Table |

+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| table_index | CREATE TABLE `table_index` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`name` varchar(20) DEFAULT NULL,

PRIMARY KEY (`id`),

KEY `index_name` (`name`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 |

+-------------+--------------------------------------------

利用下面的sql语句,来查看表的索引信息

show  index from table_index;


+-------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |

+-------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

| table_index | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |

| table_index | 1 | index_name | 1 | name | A | 0 | NULL | NULL | YES | BTREE | | |

+-------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

2.2 先创建表的时候没有普通索引,然后利用alter table 或者 create index 添加索引

create table table_index
(   id int primary key auto_increment  ,
name varchar(20)
);

alter table table_index
add index index_name (name);
或者

create table table_index
(   id int primary key auto_increment  ,
name varchar(20)
);
create index index_name On  table_index( name );

利用sql语句,来查看表的索引信息

show  index from table_index;


+-------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |

+-------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

| table_index | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |

| table_index | 1 | index_name | 1 | name | A | 0 | NULL | NULL | YES | BTREE | | |

+-------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+---
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: