码迷,mamicode.com
首页 > 数据库 > 详细

mysql 索引

时间:2020-04-30 15:19:55      阅读:60      评论:0      收藏:0      [点我收藏+]

标签:first   weight   letter   res   rop   ima   cte   unique   ica   

(1)直接创建索引

CREATE INDEX index_name ON table(column(length))

CREATE UNIQUE INDEX zipindex on ec_address(FIRST_LETTER(3))

mysql> create index index_name on t1(id);
Query OK, 0 rows affected (2.80 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table t1;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t1    | CREATE TABLE `t1` (
  `id` int DEFAULT NULL,
  `name` varchar(50) DEFAULT NULL,
  KEY `index_name` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

 

(2)修改表结构的方式添加索引

普通索引

ALTER TABLE table_name ADD INDEX index_name (column(length))

唯一索引

ALTER TABLE ec_address add UNIQUE INDEX suoyin  (FIRST_LETTER(3))

主键

alter table ec_address add primary key(ID)

(3)删除索引

DROP INDEX index_name ON table

mysql> drop index index_name on t1;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table t1;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                      |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------+
| t1    | CREATE TABLE `t1` (
  `id` int DEFAULT NULL,
  `name` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------+

 

mysql 索引

标签:first   weight   letter   res   rop   ima   cte   unique   ica   

原文地址:https://www.cnblogs.com/hb15988111121/p/12808743.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!