前言
目录
目录
一、多表关系
1.概述
1.1 一对一
1.1.1 创建用户基本信息
create table tb_user
(
id int auto_increment comment '主键id'
primary key,
name varchar(10) null comment '姓名',
age int null comment '年龄',
gender char null comment '年龄 1男 2女',
phone char(11) null comment '手机号'
)
comment '用户基本信息表';
1.1.2 创建用户教育信息表
create table tb_user_edu
(
id int auto_increment comment '主键id'
primary key,
degree varchar(20) null comment '学历',
major varchar(50) null comment '专业',
primaryschool varchar(50) null comment '小学',
middleschool varchar(50) null comment '中学',
university varchar(50) null comment '大学',
userid int null comment '用户id',
constraint userid
unique (userid),
constraint fk_userid
foreign key (userid) references tb_user (id)
)
comment '用户教育信息表';
1.2 一对多(多对一)
1.3 多对多
建立三张表的代码如下
1.3.1 创建学生表且插入数据
create table student(id int auto_increment primary key comment'主键id',
name varchar(10) comment'姓名',
novar char(10) comment'学号') comment'学生表';
insert into student values(null,'宋江','001'),(null,'鲁智深','002'),(null,'李逵','001');
1.3.2 创建课程表且插入数据
create table course(
id int auto_increment primary key comment '主键id',
name varchar(10) comment'课程名称'
) comment'课程表';
insert into course values(null,'java'),(null,'js'),(null,'MySQL');
1.3.3 创建课程中间表且插入数据
create table student_course
(
id int auto_increment comment '主键'
primary key,
studentid int not null comment '学生id',
courseid int not null comment '课程id',
constraint f_kcourseid
foreign key (courseid) references course (id),
constraint f_kstudentid
foreign key (studentid) references student (id)
)
comment '学生课程中间表';
二、多表查询概述
创建两张表:部门表、员工表
create table dept
(
id int auto_increment comment 'id'
primary key,
name varchar(50) not null comment '部门名称'
)
comment '部门表';
create table emp
(
id int auto_increment comment 'id'
primary key,
name varchar(50) not null comment '姓名',
age int null comment '年龄',
job varchar(20) null comment '职位',
salary int null comment '薪水',
entrydate date null comment '入职时间',
mangagerid int null comment '直属的领导id',
dept_id int null comment '部门id',
constraint dept_id
foreign key (dept_id) references dept (id)
)
comment '员工表';
三、多表查询分类
3.1 连接查询
3.2 内连接语法
3.2.1隐式内连接
select 字段名 from 表1,表2 where 条件....;
3.2.2 显式内连接
select 字段名 from 表1 [inner] join 表2 no 连接条件;
内连接是两张表交集的部分
3.3 内连接练习
3.3.1 查询每一个员工的姓名,及关联的部门的名称(隐式内连接实现)
select emp.name,dept.name from emp,dept where emp.id=dept.id;
3.3.2 查询每一个员工的姓名,及关联的部门的名称(显示内连接实现)
select emp.name,dept.name from emp join dept on emp.id=dept.id;
3.4 外连接语法
左外连接
select 字段列表 from 表1 left [outer] join 表2 on 条件...;
相当于查询表1(左表)的所有数据 包含 表1和表2交集部分的数据
右外连接
select 字段列表 from 表1 right [outer] join 表2 on 条件...;
相当于查询表2(右表)的所有数据 包含 表1和表2交集部分的数据
3.5 外连接练习
连接条件:emp.id=dept.id
3.5.1 查询emp表的所有数据,和对应部门信息(左外连接)
select e.*,d.name from emp e left outer join dept d on d.id=e.id;
3.5.2 查询dept表的所有数据,和对应的员工信息(右外连接)
select d.*,e.* from emp e right outer join dept d on d.id=e.id;
右连接可以改为左连接
select d.*,e.* from dept d left outer join emp e on d.id=e.id;
3.6 自连接语法
select 字段列表 from 表A 别名A join 表B 别名B on 条件;
子连接查询,可以是内连接查询,也可以是外连接查询。
3.7 联合查询 union,union all
union 语法
SELECT 字段列表 FROM 表A ....
UNION [ ALL]
SELECT 字段列表 FROM 表B ....;