程序媛计划——mysql索引

MySQL索引详解
本文详细介绍了MySQL数据库中索引的概念及应用,包括索引的创建、查看与删除操作,并解释了唯一索引的作用。
定义:
  索引是一种单独的、物理的对数据库表中一列或多列的值进行排序的一种存储结构
 
#为字段创建索引
#在表中的字段中创建索引
mysql> create index ind_score on score(name); Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 #查看索引 mysql> show index from score; +-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | score | 0 | PRIMARY | 1 | id | A | 4 | NULL | NULL | | BTREE | | | | score | 1 | ind_score | 1 | name | A | 4 | NULL | NULL | | BTREE | | | +-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 2 rows in set (0.00 sec)

#也可以在创建表的时候创建索引

 

#删除索引

mysql> drop index ind_score on score;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

#用alter创建/删除索引

mysql> alter table score add index ind_score(name);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table score drop index ind_score;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

 

#创建索引值唯一的索引(好处是数据很多时能通过唯一索引快速查询到数据)

mysql> create unique index uni_index on score(score);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

 

转载于:https://www.cnblogs.com/IcarusYu/p/7497716.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值