MySQL数据库5.7数据库介绍表连接、子查询、外键 MySQL数据库使用教程

凤凰 2021-1-27 92 1/27

MySQL数据库5.7数据库介绍表连接、子查询、外键 MySQL数据库使用教程

免费学习推荐:mysql视频教程

文章目录

      • 表连接
        • 内连接
        • 左连接
        • 右连接
        • 子查询
      • 自关联
      • 外键
        • 外键介绍
        • 创建表时设置外键约束

表连接

  • 当查询结果的列来源于多张表时,需要将多张表连接成一个大的数据集,再选择合适的列返回mysql
  • 这时需要表进行连接

内连接

  • 内连接仅选出两张表中互相匹配的记录
select * from 表1 inner join 表2 on 表1.列 = 表2.列-- 显示学生的所有信息,但只显示班级名称select s.*, c.name from students s inner join classes c on s.id=c.id;-- 将班级名称显示在第一列select c.name, s.* from students s inner join classes c on s.id=c.id;-- 查询 有能够对应班级的学生以及班级信息,按照班级进行排序select c.name, s.* from students s inner join classes c on s.cls_id = c.id order by c.name asc;-- 当同一个班级时,按照学生的id进行从小到大排序select c.name, s.* from students s inner join classes c on s.cls_id = c.id order by c.name asc, s.id asc;

MySQL数据库5.7数据库介绍表连接、子查询、外键 MySQL数据库使用教程
MySQL数据库5.7数据库介绍表连接、子查询、外键 MySQL数据库使用教程
在这里插入图片描述

左连接

查询的结果为两个表匹配到的数据,左表持有的数据,对于右表中不存的数据使用null填充

select * from 表1 left join 表2 on 表1.列=表2.列-- students表左连接classes表 并查看班级为null的数据select * from students s left join classes c on s.cls_id=c.id having s.cls_id is null;-- 左连接 并且 查询 s.cls_id=1 并且 s.name="small-j" 的数据select * from students s left join classes c on s.cls_id=c.id having s.cls_id=1 and s.name="small-j";

MySQL数据库5.7数据库介绍表连接、子查询、外键 MySQL数据库使用教程

右连接

查询结果为两个表匹配到的数据,右表持有的数据,对于左表中不存在的数据使用null填充。

select * from 表1 right join 表2 on 表1.列 = 表2.列;

子查询

某些情况下,当进行查询的时候,需要的条件是另外一个select语句的结果,这个时候,就要使用到子查询

select * from 表 where 表(子查询语句)-- 查询出students中身高最高的男生。显示名字和身高select s.name, s.high from students s where high=(select max(high) from students) and gender="男";-- 查询出高于平均身高的学生信息select * from students where high>(select avg(high) from students);-- 查询学生班级号cls_id能够对应的学生信息select * from students where cls_id in (select id from students);-- 查询最大年龄的女生的idselect * from students where id=(select max(id) from students where gender="女") and  gender="女";

在这里插入图片描述

自关联

简单理解为自己与自己进行连接查询

-- 查询广东省下的所有广东市select * from cities c inner join provinces p on c.provinceid=p.provinceid having p.province="广东省";-- 查询广东省下的所有广东市-- 自关联select * from areas a inner join areas b on a.id=b.pid having a.name="广东";

MySQL数据库5.7数据库介绍表连接、子查询、外键 MySQL数据库使用教程
MySQL数据库5.7数据库介绍表连接、子查询、外键 MySQL数据库使用教程

外键

外键介绍

  • MySQL的外键(foreing key)是表的一个特殊字段。对于两个具有关联关系的表而言,相关联字段的主键所在表就是主表(父表),外键所在的表是从表(子表)。
  • 注意: 主键不能包含空值,但允许在外键中出现空值,也就是说,只要外键的每个非空值出现在指定的主键中,这个外键的内容就是正确的。

创建表时设置外键约束

  • 当创建外键的时候,必须先删除从表才能删除主表。
  • 主表需存在时创建从表。
  • 从表的外键关联必须是主表的主键,并且主键与外键的类型必须保持一致。
[constraint 外键名] foreign key (字段名 [,字段名2, ...]) references <主表名> 主键列1 [, 主键列2, ...]
-- 创建班级表create table classes(
    id int(4) not null primary key,
    name varchar(36));-- 创建学生表create table student(
    sid int(4) not null primary key,
    sname varchar(30),
    cid int(4) not null);-- 创建直接含有外键关系的学生表create table student(
  	sid int(4) not null primary key,
  	sname varchar(30),
  	cid int(4) not null,
  	constraint pk_id foreign key (cid) references classes(id));-- 通过alter来添加外键关系alter table student add constraint pk_id foreign key (cid) references classes(id);-- 删除外键约束alter table student drop foreign key pk_id;

MySQL数据库5.7数据库介绍表连接、子查询、外键 MySQL数据库使用教程

MySQL数据库5.7数据库介绍表连接、子查询、外键 MySQL数据库使用教程

相关免费学习推荐:mysql数据库(视频)

以上就是MySQL5.7数据库介绍表连接、子查询、外键的详细内容,更多请关注本站其它相关文章!

- THE END -

凤凰

1月27日09:09

最后修改:2021年1月27日
0

非特殊说明,本博所有文章均为博主原创。