1. 程式人生 > 實用技巧 >mysql8學習筆記11--create index

mysql8學習筆記11--create index

• Create index語句用來在表中建立索引 • Index_col_name可以包含一個欄位,也可以包含多個欄位(逗號隔開),如果包含多個欄位,則表明此索引是複合索引 • Unique index代表索引中的值不能有重複 • Fulltext index只能建立在innodb和myisam儲存引擎的char,varchar和text欄位上 • Index可以建立在包含NULL值的欄位上 • Key_block_size=value是在myisam儲存引擎的表上指定索引鍵的block大小 • create index idx_st_sname on students(sname);##建立普通索引 • create index idx_st_union on students(sname,sex);##建立複合索引 • create unique index idx_st_sid on students(sid);##建立唯一索引 • mysql> insert into students values(1,‘eee’,0);##插入重複資料失敗 • ERROR 1062 (23000): Duplicate entry '1' for key 'idx_st_sid' • Index_type代表建立索引的型別

mysql> select * from orders_temp;
+-----------+---------------------+---------+----+------------+
| order_num | order_date          | cust_id | id | order_num2 |
+-----------+---------------------+---------+----+------------+
|     20007 | 2005-09-30 00:00:00 |   10004 |  1 |       NULL |
|     20008 | 2005-10-03 00:00:00
| 10005 | 2 | NULL | | 20009 | 2005-10-08 00:00:00 | 10001 | 3 | NULL | +-----------+---------------------+---------+----+------------+ 3 rows in set (0.00 sec) mysql> show create table orders_temp; +-------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | orders_temp | CREATE TABLE `orders_temp` ( `order_num`
int(11) NOT NULL DEFAULT '0', `order_date` datetime NOT NULL, `cust_id` int(11) NOT NULL, `id` int(10) NOT NULL AUTO_INCREMENT, `order_num2` int(10) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci | +-------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1
row in set (0.00 sec) mysql> create index order_num_index on orders_temp(order_num); Query OK, 0 rows affected (0.10 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table orders_temp; +-------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | orders_temp | CREATE TABLE `orders_temp` ( `order_num` int(11) NOT NULL DEFAULT '0', `order_date` datetime NOT NULL, `cust_id` int(11) NOT NULL, `id` int(10) NOT NULL AUTO_INCREMENT, `order_num2` int(10) DEFAULT NULL, PRIMARY KEY (`id`), KEY `order_num_index` (`order_num`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci | +-------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> create index order_date_index on orders_temp(order_date); Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table orders_temp; +-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | orders_temp | CREATE TABLE `orders_temp` ( `order_num` int(11) NOT NULL DEFAULT '0', `order_date` datetime NOT NULL, `cust_id` int(11) NOT NULL, `id` int(10) NOT NULL AUTO_INCREMENT, `order_num2` int(10) DEFAULT NULL, PRIMARY KEY (`id`), KEY `order_num_index` (`order_num`), KEY `order_date_index` (`order_date`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci | +-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> create index ordere_index_union on orders_temp(order_num,order_date); Query OK, 0 rows affected (0.09 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table orders_temp; +-------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | orders_temp | CREATE TABLE `orders_temp` ( `order_num` int(11) NOT NULL DEFAULT '0', `order_date` datetime NOT NULL, `cust_id` int(11) NOT NULL, `id` int(10) NOT NULL AUTO_INCREMENT, `order_num2` int(10) DEFAULT NULL, PRIMARY KEY (`id`), KEY `order_num_index` (`order_num`), KEY `order_date_index` (`order_date`), KEY `ordere_index_union` (`order_num`,`order_date`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci | +-------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> create unique index cust_id_index on orders_temp(cust_id); Query OK, 0 rows affected (0.04 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table orders_temp; +-------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | orders_temp | CREATE TABLE `orders_temp` ( `order_num` int(11) NOT NULL DEFAULT '0', `order_date` datetime NOT NULL, `cust_id` int(11) NOT NULL, `id` int(10) NOT NULL AUTO_INCREMENT, `order_num2` int(10) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `cust_id_index` (`cust_id`), KEY `order_num_index` (`order_num`), KEY `order_date_index` (`order_date`), KEY `ordere_index_union` (`order_num`,`order_date`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci | +-------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql>