5、Mysql普通索引创建实例
2016-04-04 11:24
645 查看
1、前面讲到了Mysql主键索引的创建,下面将介绍Mysql普通索引创建的实例
2、Mysql普通索引创建的实例
2.1 在创建表的时候,指定普通索引
| 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语句,来查看表的索引信息
+-------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| 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 添加索引
利用sql语句,来查看表的索引信息
+-------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| 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、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 | | |
+-------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+---
相关文章推荐
- MySQL 查询最大最小值优化
- 《MySQL必知必会》读书笔记
- mysql优化(总结)
- 设置mysql可以外网链接
- MySQL备份与恢复实战案例及生产方案
- navicat for mysql mac彻底卸载
- MySQL基本概念
- MySQL常用命令
- MySql key and index
- MySQL 学习笔记(2)2016-04-04
- MySQL 学习笔记(1)2016-04-02
- MySQL 数据库学习笔记 2
- mySQL数据库乱码解决办法
- 管理MySQL的命令及基本函数
- MySQL 数据类型
- mysql关键字
- MySQL存储引擎总结
- MySQL的语句执行顺序
- MySql数据库的基本概念和安装
- Mysql 5.7.11 windows下安装zip版