超详细的mysql多表操作教程

目录

外键约束

概念

特点

操作

多表联合查询

概念

操作

多表操作总结


 

外键约束

概念

85079d4dd28c48f88b5dfdb769b7c9a6.png

特点

定义一个外键时,需要遵守下列规则:

主表必须已经存在于数据库中,或者是当前正在创建的表。

必须为主表定义主键。

主键不能包含空值,但允许在外键中出现空值。也就是说,只要外键的每个非空值出现在指定的主键中,这 个外键的内容就是正确的。

在主表的表名后面指定列名或列名的组合。这个列或列的组合必须是主表的主键或候选键。

外键中列的数目必须和主表的主键中列的数目相同。

外键中列的数据类型必须和主表主键中对应列的数据类型相同。

bb056964cd5646e2a91cfecfe81f4939.png

操作

  • 建立外键约束
create database  mydb3;
use mydb3;
create table if not exists dep
(
pid int primary key,
name varchar(20)
);
create table if not exists per
(
id int primary key,
name varchar(20),
age int,
depid int,
constraint fok foreign key(depid) references dep(pid)
);



create table if not exists dep3
(
pid int primary key,
name varchar(20)
);
create table if not exists per3
(
id int primary key,
name varchar(20),
age int,
depid int
);
alter table per3 add constraint fok3 foreign key(depid) references dep3(pid);

bf485d6f6a1044c1957509a410814ef1.png

  •  数据插入

必须先给主表添加数据,且从表外键列的值必须依赖于主表的主键列

insert into dep3 values('1001','研发部');
insert into dep3 values('1002','销售部');
insert into dep3 values('1003','财务部');
insert into dep3 values('1004','人事部');

-- 给per3表添加数据
insert into per3 values('1','乔峰',20, '1001');
insert into per3 values('2','段誉',21, '1001');
insert into per3 values('3','虚竹',23, '1001');
insert into per3 values('4','阿紫',18, '1001');
insert into per3 values('5','扫地僧',85, '1002');
insert into per3 values('6','李秋水',33, '1002');
insert into per3 values('7','鸠摩智',50, '1002'); 
insert into per3 values('8','天山童姥',60, '1003');
insert into per3 values('9','慕容博',58, '1003');
  • 数据删除

主表数据被从表依赖时不能删除,否则可以删除;从表的数据可以随便删除。

如下,第一句和第二句执行成功,第三句执行失败

delete from per3 where depid=1003;
delete from dep3 where pid=1004;
delete from dep3 where pid=1002;
  • 删除外键约束

语法:alter table 从表 drop foreign key 关键词名;

alter table per3 drop foreign key fok3;

 

多表联合查询

概念

7d02676d0e15413fb0b3a0508c927876.png

操作

  • 交叉连接查询

d09ce2e1da814b1eb0645d9b1f81f5b9.png

select * from dept,emp;
  • 内连接查询

93595677f88d4b579b1dc8efc00f92de.png

注释;上面是隐式内连接,下面是显式内连接 

select * from dept,emp where dept.deptno=emp.dept_id;
select * from dept join emp on dept.deptno=emp.dept_id;

select * from dept join emp on dept.deptno=emp.dept_id and name='研发部';
select * from dept join emp on dept.deptno=emp.dept_id and name='研发部';

select * from dept join emp on dept.deptno=emp.dept_id and (name='研发部' or name='销售部');
select * from dept join emp on dept.deptno=emp.dept_id and (name='研发部' or name ='销售部');
select * from dept join emp on dept.deptno=emp.dept_id and name in ('研发部','销售部');

select a.name,a.deptno,count(*) from dept a join emp on a.deptno=emp.dept_id group by dept_id;
select a.name,a.deptno,count(*) total from dept a join emp on a.deptno=emp.dept_id group by dept_id having total >=3 order by total desc;
  • 外连接查询

若是对应的外表没有数据就补NULL

fdf46996310d45b3b1466ad8f0ea92bc.png

select * from dept a left join emp b on a.deptno=b.dept_id;
select * from dept a right join emp b on a.deptno=b.dept_id;
-- select * from dept a full join emp b on a.deptno=b.dept_id; --不能执行
-- 用下面的方法代替上面的full join 
select * from dept a left join emp b on a.deptno=b.dept_id union select * from dept a right join emp b on a.deptno=b.dept_id;
-- 对比union all,发现union all没有去重过滤
select * from dept a left join emp b on a.deptno=b.dept_id union all select * from dept a right join emp b on a.deptno=b.dept_id;
  • 子查询

a4156bb4c22b48b99a7513e779dad9b8.png

select * from emp where age<(select avg(age) from emp);
select * from emp a where a.dept_id in (select deptno from dept where name in ('研发部','销售部'));
-- 对比关联查询和子查询如下
select * from emp a join dept b on a.dept_id=b.deptno and (b.name='研发部' and age<30);
select * from (select * from dept where name='研发部') a join (select * from emp where age<30) b on b.dept_id=a.deptno;
  • 子查询关键字

all关键字的用法

24c41c2d2a3c465c956fd651c1986bd5.png

select * from emp where age>all(select age from emp where dept_id='1003');
select * from emp a where a.dept_id!=all(select deptno from dept);

any(some)关键字的用法

21c06c46520c4643a624d11dfbe9ea73.png

select * from emp where age>any(select age from emp where dept_id='1003') and dept_id!='1003';

in关键字的用法

e2e3e708e3eb44e6bdca797fef3d45a1.png

select ename,eid from emp where dept_id in (select deptno from dept where name in ('研发部','销售部'));

 exists关键字的用法

d5225183b42c4e1e8498074ddb9cf4a0.png

select * from emp a where a.age<30;
select * from emp a where exists(select * from emp where a.age<30);

select * from emp a where a.dept_id in (select deptno from dept b);
select * from emp a where exists (select * from dept b where a.dept_id = b.deptno);
  • 自关联查询

7fd1414872354020910972ad0487f149.png

e5d73682643b493399dbdb2382e09e8c.png

 

多表操作总结

9969e95b29fc4640a10f16c34c1914ff.png

 

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
1 前言 1 2 连接 MYSQL 1 2.1 命令行 1 2.2 Navicate 2 2.3 例 1:连接到本机上的 MYSQL 2 2.4 例 2:连接到远程主机上的 MYSQL 3 2.5 退出 MYSQL 命令: exit (回车) 4 3 修改密码 4 3.1 例 1:给 root 加个密码 ab12 。 4 3.2 例 2:再将 root 的密码改为 djg345 。 4 4 增加新用户 4 4.1 例 1、增加一个用户 test1 密码为 abc 4 4.2 例 2、增加一个用户 test2 密码为 abc 4 5 MySQL常用命令 5 5.1 启动 MySQL 服务器 5 5.2 进入 mysql 交互操作界面 5 5.3 退出 MySQL 操作界面 5 5.4 第一条命令 6 5.5 多行语句 6 5.6 一行多命令 7 5.7 显示当前存在的数据库 7 5.8 选择数据库并显示当前选择的数据库 8 5.9 显示当前数据库中存在的表 8 5.10 显示表 (db) 的内容 8 5.11 命令的取消 8 6 创建数据库和数据表 9 6.1 使用 SHOW 语句找出在服务器上当前存在什么数据库 9 6.2 创建一个数据库 abccs 9 6.3 选择你所创建的数据库 9 6.4 创建一个数据库表 9 6.5 显示表的结构 10 6.6 往表中加入记录 11 6.7 用文本方式将数据装入一个数据库表 11 7 检索数据 12 7.1 从数据库表中检索信息 12 7.2 查询所有数据 12 7.3 修正错误记录 12 7.4 选择特定行 13 7.5 选择特定列 13 7.6 对行进行排序 14 7.7 行计数 15 8 多表操作 15 8.1 查看第一个表 mytable 的内容 16 8.2 创建第二个表 title (包括作者、文章标题、发表日期) 16 8.3 多表查询 17 9 数据库表和数据库的修改和删除 18 9.1 增加一列 18 9.2 修改记录 18 9.3 增加记录 18 9.4 删除记录 19 9.5 删除表 19 9.6 数据库的删除 19 9.7 数据库的备份 20 9.8 用批处理方式使用 MySQL: 20

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

coleak

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值