1. 程式人生 > >Mysql普通索引建立例項

Mysql普通索引建立例項

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      |         |               |
+-------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+---