MySQL普通查询及索引

查询语句结构及执行顺序

3、select …聚合函数 from 表名
1、where
2、group by
4、having
5、order by
6、limit …;

  • 聚合函数
方法 功能
avg(字段名) 该字段的平均值
max(字段名) 该字段的最大值
min(字段名) 该字段的最小值
sum(字段名) 该字段所有记录的和
count(字段名) 统计该字段记录的个数
count(*) 可统计所有数据,包括值为null的数据

例如1 : 找出表中的最大攻击力的值?
select max(attack) from sanguo;

例如2 : 表中共有多少个英雄?
select count(*) as number from sanguo;

例如3 : 蜀国英雄中攻击值大于200的英雄的数量
select count(id) from sanguo where country='蜀国' and attack > 200;

  • group by

给查询的结果进行分组
例如1 : 计算每个国家的平均攻击力
select country , avg(attack) from sanguo group by country;

例如2 : 所有国家的男英雄中 英雄数量最多的前2名的 国家名称及英雄数量

select country, count(id) as number from sanguo
where gender = 'M'
group by country
order by number DESC
limit 2

group by后字段名必须要为select后的字段
查询字段和group by后字段不一致,则必须对该字段进行聚合处理(聚合函数)

  • having语句

对分组聚合后的结果进行进一步筛选

例如 : 找出平均攻击力大于105的国家的前2名,显示国家名称和平均攻击力
select country, avg(attack) from sanguo
group by country
having avg(attack) > 105
order by avg(attack) DESC
limit 2;

注意:
having语句通常与group by联合使用,having语句存在弥补了where关键字不能与聚合函数联合使用的不足where只能操作表中实际存在的字段,having操作的是聚合函数生成的显示列

  • distinct语句

不显示字段重复值

例如1: 表中都有哪些国家
select distinct country from sanguo;
例如2 : 计算一共有多少个国家
select count(distinct country) from sanguo;

注意
distinct和from之间所有字段都相同才会去重
distinct不能对任何字段做聚合处理

  • 查询表记录时做数学运算

运算符 : + - * / % **

例如1: 查询时显示攻击力翻倍
select attack*2 from sanguo
例如2: 更新蜀国所有英雄攻击力 * 2
update sanguo set attack = attack*2 where country='蜀国'

索引

  • 定义
    对数据库表的一列或多列的值进行排序的一种结构(Btree方式)

  • 优点
    加快数据检索速度

  • 缺点

占用物理存储空间(/var/lib/mysql)
当对表中数据更新时,索引需要动态维护,降低数据维护速度

  • 索引示例

1、开启运行时间检测
mysql>show variables like '%pro%';
mysql>set profiling=1;
2、执行查询语句(无索引)
select name from students where name='Tom99999';
3、查看执行时间
show profiles;
4、在name字段创建索引
create index name on students(name);
5、再执行查询语句
select name from students where name='Tom88888';
6、查看执行时间
show profiles;

索引分类

普通(MUL) and 唯一(UNI)

  • 使用规则

1、可设置多个字段
2、普通索引 :字段值无约束,KEY标志为 MUL
3、唯一索引(unique) :字段值不允许重复,但可为 NULL,KEY标志为 UNI
4、哪些字段创建索引:经常用来查询的字段、where条件判断字段、order by排序字段

  • 创建普通索引and唯一索引
    创建表时

create table 表名(
字段名 数据类型,
字段名 数据类型,
index(字段名),
index(字段名),
unique(字段名)
);

已有表中创建

create [unique] index 索引名 on 表名(字段名);

  • 查看索引

1、desc 表名; KEY标志为:MULUNI
2、show index from 表名\G;

  • 删除索引

drop index 索引名 on 表名;

联合索引

  • 概念
    联合索引又叫复合索引,即一个覆盖表中两列或者以上的索引。
    例如:index_name(column a,column b)
  • 创建方式

方式一:alter table table_name add index index_name(column_list)
方式二:create index index_name on table_name(column_list)
index_name是创建的联合索引的名字,可以没有,没有的话系统会根据该索引包含的第一列来赋值名称;
table_name是要创建该索引的表名;
column_list是该索引所包含的表的字段名。

例如:

create table stu
(
id int,
name varchar(10),
age int,
primary key(id)
);
ALTER TABLE stu ADD INDEX LianHeIndex (name,age);
或者
create index LianHeIndex on stu(name,age);

注意:
一、联合索引遵守一个最左原则
index LianHeIndex (name,age),该索引支持name或者name age组合查询,而不支持age单独查询
二、最佳左前缀特性
如果我们是在nameage上分别创建单个索引的话,由于mysql查询每次只能使用一个索引,所以虽然这样已经相对不做索引时全表扫描提高了很多效率,但是如果在name、age两列上创建复合索引的话将带来更高的效率。如果我们创建了(name, age)的复合索引,那么其实相当于创建了(name)、(name,age)两个索引,因此我们在创建复合索引时应该将最常用作限制条件的列放在最左边,依次递减。
三、只要列中包含有NULL值,该列对于联合索引时无效的

主键(PRI)and自增长(auto_increment)

  • 使用规则

1、只能有一个主键字段
2、所带约束 :不允许重复,且不能为NULL
3、KEY标志(primary) :PRI
4、通常设置记录编号字段id,能唯一锁定一条记录

  • 创建

创建表添加主键

create table student(
id int auto_increment,
name varchar(20),
primary key(id)
)charset=utf8,auto_increment=10000;##设置自增长起始值

已有表添加主键

alter table 表名 add primary key(id);

已有表操作自增长属性

1、已有表添加自增长属性
alter table 表名 modify id int auto_increment;
2、已有表重新指定起始值:
alter table 表名 auto_increment=20000;

  • 删除

1、删除自增长属性(modify)
alter table 表名 modify id int;
2、删除主键索引
alter table 表名 drop primary key;

发布了40 篇原创文章 · 获赞 12 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/chiaotien/article/details/104698244