SQL高级查询与索引

  1. SQL高级查询
  2. SQL查询
  3. 嵌套查询(子查询)
  4. 多表查询
  5. 连接查询
    • 内连接
    • 外连接
      • 左连接
      • 右连接
  6. 索引主键外键

1. SQL高级查询


  1. 总结 执行步骤
    3\select ...聚合函数 from 表名
    1\where ...
    2\group by ...
    4\having ...
    5\order by ...
    6\limit ...;
  2. order by:给查询结果排序
    1. order by 字段名 ASC(默认)/DESC
  3. limit (永远放在sql命令的最后)
    1. 显示查询记录的条数
      limit n; 显示n条记录
      limit m,n; 从第m+1条记录开始,显示n条
      limit 2,3; 显示第3, 4,5三条记录
    2. 分页
      每页显示n条记录,显示第x页的内容
      第一页: limit 0 * ,n
      ...
      第x页: limit (x-1) * n,n
  4. 聚合函数
    avg(字段名):求该字段的平均值
    sum(字段名):求和
    max(字段名):最大值
    min(字段名):最小值
    count(字段名):统计该字段记录的个数
    示例: select max(gongji) as 最大 from sanguo;
  5. group by:给查询的结果进行分组
    1. 查询表中都有哪些国家
      select country from sanguo group by country;
    2. 计算每个国家的平均攻击力
      select country,avg(gongji) from sanguo group by country;
    3. 注意:select 之后的字段名如果没有在group by之后出现, 则必须要对该字段进行聚合处理(聚合函数)
  6. having语句
    1. 作用:对查询结果进行进一步的筛选
      练习:找出平均攻击力大于105的国家的前2名, 显示国家名的平均攻击力
    select country,avg(gongji) from sanguo 
    group by country 
    having avg(gongji)>105 
    order by avg(gongji) desc 
    limit 0,2;
    1. 注意
      1. having语句通常和group by 语句联合使用, 过滤由group by语句返回的记录集
      2. where只能操作表中实际存在的字段,having语句可操作聚合函数生成的显示列

2. SQL查询


  1. distinct : 不显示字段的重复值
    select distinct 字段1, 字段2 from 表名;
  2. 示例:
    1. 表中有哪些国家
      select distinct country from sanguo;
    2. 表中一共有几个国家
      select count(distinct country) from sanguo;
    3. distinct 和from 之间的所有的值都相同才会去重
  3. 查询表记录时可以做数学运算
    1. 运算符: + - * / %
    2. 示例
      1. 查询时显示所有英雄攻击国翻倍
        select id,name,gongji* 2 from sanguo;

3. 嵌套查询(子查询)


  1. 定义 :把内层的查询结果作为外层的查询条件
  2. 语法
    select ... from 表名 where 字段名 运算符 (select ... from 表名 where 条件);
  3. 练习
    1. 把攻击值小于平均攻击值的名字和攻击值显示出来
      1. 先求平均值
        select avg(gongji) from sanguo;
      2. 找结果
        select name,gongji from sanguo where gongji<值;
        3.合成一句
      select name,gongji from sanguo where 
      gongji<(select avg(gongji) from sanguo);
  4. 找出每个国家攻击力最高的英雄的名字和攻击值
select name,gongji from sanguo
where
(country,gongji) in(select country,max(gongji) from sanguo group by country);

4. 多表查询


两种方式

  1. 笛卡尔积 :不加where条件
    select ... from 表1,表2;
  2. 加where条件
    select ... from 表1,表2 where 条件;
select sheng.s_name,city.c_name from sheng,city
where 
sheng.s_id=city.cfather_id;
select sheng.s_name,city.c_name,xian.x_name from sheng,city,xian
where
sheng.s_id=city.cfather_id and city.c_id=xian.xfather_id;

5. 连接查询


内连接(inner join)

内连接使用比较运算符根据每个表共有的列的值匹配两个表中的行

  1. 语法格式
    select 字段名列表 from 表1
    inner join 表2 on 条件
    inner join 表3 on 条件;
  2. 练习
  3. 显示省、市详细信息(只显示匹配到的)
    select sheng.s_name,city.c_name from sheng
    inner join city
    on sheng.s_id=city.cfather_id;
  4. 显示省市县详细信息
select sheng.s_name,city.c_name,xian.x_name from sheng
inner join city on sheng.s_id=city.cfather_id
inner join xian on city.c_id=xian.x_father_id;

外连接

  1. 左连接(left join)
    以左表为主 显示查询结果
    左向外连接的结果集包括LEFT OUTER子句中指定的左表的所有行,而不仅仅是连接列所匹配的行。如果左表的某行在右表中没有匹配行,则在相关联的结果集行中右表的所有选择列表列均为空值。
    select 字段名列表 from 表1 left join 表2 on 条件 left join 表3 on 条件;

练习:显示省市县详细信息,要求省全部显示

select sheng.s_name,city.c_name,xian.x_name from sheng
left join city on sheng.s_id=city.cfather_id
left join xian on city.c_id=xian.xfather_id;
  1. 右连接(right join)
    将返回右表的所有行。如果右表的某行在左表中没有匹配行,则将为左表返回空值。
    以右表为主显示查询结果,用法同左连接

索引主键外键

索引:

对数据库中表的一列或多列的值进行排序的一种结构(BTree)
优点: 加快数据的检索速度
缺点:

  • 当对表中数据更新时, 索引需要动态维护, 降低了数据的维护速度
  • 索引需要占用物理存储空间

SQL命令运行时间检测
1、开启 :mysql> set profiling=1;
2、查看 :mysql> show profiles;
3、关闭 :mysql> set profiling=0;

1. 普通索引(index)


  1. 可设置多个字段, 字段值无约束
  2. 把经常用来查询的字段设置为索引字段
  3. KEY标志: MUL

创建索引

  1. 创建表时
create table t1(
...,
...,
index(name),
index(id));
  1. 已有表中
    create index 索引名 on 表名(字段名);

查看索引

  1. desc 表名; KEY标志为 MUL
  2. show index from 表名;

删除索引

drop index 索引名 on 表名;

2. 唯一索引(unique)


  1. 可设置多个字段
  2. 约束: 字段的值不允许重复, 但可以为NULL, 多个NULL不为重复
  3. KEY标志: UNI

创建

  1. 创建表时
    unique(phnumber),
    unique(carnumber)
  2. 已有表
    create unique index 索引名 on 表名();

查看,删除同普通索引

3. 主键索引(primary key)&&自增长(auto_increment)


  1. 只能有1个字段为主键字段
  2. 约束: 字段值不允许重复, 也不能为NULL
  3. KEY标志: PRI
  4. 通常设置记录编号字段 id,能够唯一锁定一条记录

创建

  1. 创建表时
    id int primary key auto_increment,
(id int auto_increment,
name varchar(20),
primary key(id,name))auto_increment=10000; # 复合主键
  1. 已有表
    alter table 表名 add primary key(id)

删除主键

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

##外键(foreign key)

  1. 定义 :让当前表的字段值在另一张表的范围内去选择

1. 创建

  1. 建表时
foreign key(参考字段名)
references 主表(被参考字段名)
on delete 级联动作
on update 级联动作;
  1. 已有表添加外键
alter table 表名 add 
foreign key(stu_id) references jftab(id)
on delete 级联动作
on update 级联动作;

2. 删除外键

alter table 表名 drop foreign key 外键名;

3.外键名查看

show create table 表名;

  1. 使用规则
    1. 主表、从表字段数据类型要一致
    2. 主表 :被参考字段是主键
  2. 级联动作
    1. cascade
      数据级联删除,级联更新(参考字段)
    2. restrict(默认)
      从表中有相关联记录,不允许主表操作
    3. set null
      主表删除、更新,从表相关联记录字段值为NULL
  3. 示例
    表1:缴费信息表(财务) :jftab
id 姓名 班级 缴费金额
1 唐伯虎 AID07 300
2 点秋香 AID07 300

表2:学生信息表(班主任) :bjtab

stu_id 姓名 缴费金额

表1 :jftab

create table jftab(
id int primary key,
name varchar(20) not null,
class char(5),
money smallint
)character set utf8;
insert into jftab values
(1,"唐伯虎","AID07",300),
(2,"点秋香","AID07",300),
(3,"文征明","AID07",300);

表2 :bjtab

create table bjtab(
stu_id int,
name varchar(20),
money smallint,
foreign key(stu_id) references jftab(id)
on delete cascade
on update cascade
)character set utf8;

猜你喜欢

转载自www.cnblogs.com/ravener/p/9657353.html
今日推荐