Java学习路线-42:SQL进阶:约束、关系、连接

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
云数据库 RDS PostgreSQL,高可用系列 2核4GB
简介: Java学习路线-42:SQL进阶:约束、关系、连接

SQL进阶:约束、关系、连接

课时1 1.单表的查询练习

可视化客户端 SQLyog

-- 查询部门编号为30的所有员工
select * from emp where deptno=30;
-- 查询所有销售员的姓名、编号和部门编号
select ename, empno, deptno from emp where job='销售员'
-- 查询奖金高于工资的员工
select * from emp where comm > sal;
-- 查询奖金高于工资60%的员工
select * from emp where comm > sal * 0.6;
-- 查询部门编号为10中所有经理,和部门编号编号为20中所有销售员的详细资料
select * from emp 
where (deptno=10 and job='经理')
or (deptno=20 and job='销售员');
-- 查询部门编号为10中所有经理,和部门编号编号为20中所有销售员,
-- 还有既不是经理又不是销售员但工资大于等于20000的所有员工详细资料
select * from emp 
where (deptno=10 and job='经理')
or (deptno=20 and job='销售员')
or (job not in ('经理', '销售员') and sal >= 2000);
-- 无奖金或奖金低于1000的员工
select * from emp where comm is null or comm < 1000;
-- 查询名字由3个字组成的员工(3个下划线)
select * from emp where ename like '___';
-- 查询2000年入职的员工
select * from emp where hiredate like '2000-%';
-- 查询所有员工,用编号升序排序
select * from emp order by empno asc;
-- 查询所有员工详细信息,用工资降序排序,如果工资相同使用入职日期升序排序
select * from emp order by sal desc, hiredate asc;
-- 查询每个部门的平均工资
select deptno, avg(sal) from emp group by deptno;
-- 查询每个部门雇员数量
select deptno, count(*) from emp group by deptno;
-- 查询每种工作的最高工资,最低工资,人数
select job, max(sal), min(sal), count(*) from emp group by job;

课时2 2.mysql编码问题

-- 查看MySQL的数据库编码
show variables like 'char%';
-- 临时设置变量
set character_set_client=utf8
set character_set_results=utf8

永久设置:

my.ini中配置

课时3 3.mysql备份与恢复数据库1、备份:数据库->SQL语句

$ mysqldump -u用户名 -p密码 数据库名 > 要生成的SQL脚本路径


2、恢复:SQL语句->数据库

$ mysql -u用户名 -p密码 数据库名 < 要生成的SQL脚本路径
# 或者
> source 要生成的SQL脚本路径

课时4 4.约束之主键约束

特点:唯一,非空,被引用

指定id列为主键列

-- 1、创建表时指定主键
create table stu(
    id int primary key,
    name varchar(20)
)
-- 或者
create table stu(
    id int,
    name varchar(20),
    primary key(id)
)
-- 2、已存在表添加主键
alter table stu add primary key(id);
-- 3、删除主键
alter table stu drop primary key;

课时5 5.主键自增长

保证插入数据时主键唯一非空

-- 1、创建表时指定主键自增长
create table stu(
    id int primary key auto_increment,
    name varchar(20)
)
-- 设置字段自增长
alter table stu change id id int auto_increment;
-- 删除自增长
alter table stu change id id int;

uuid作为主键

课时6 6.非空和唯一约束

非空约束:不能为null

not null

唯一约束:不能重复

unique

create table stu(
    id int primary key auto_increment,
    name varchar(20) not null unique 
)

课时7 7.概述模型、对象模型、关系模型

1、对象模型

is a 继承

has a 关联 1对1 1对多 多对多

use a

2、关系模型

数据库中的表

代码实现

// 一对一关系 丈夫-妻子
class  Husband{
    private Wife wife;
}
class Wife{
    private Husband husband;
}
// 一对多关系 部门-员工
class Employee{
    private Department department;
}
class Department{
    private List<Employee> employee;
}
// 多对多关系 老师-学生
class Student{
    private List<Teacher> teachers
}
class Teacher{
    private List<Student> students
}

外键约束

外键引用主键,必须引用另一张表主键

外键可以重复

外键可以为空

一张表中可以有多个外键

课时8 8.外键约束

create table dept(
    deptno int primary key auto_increment,
    dname varchar(50)
)
insert into dept values(10, '人力部');
insert into dept values(20, '研发部');
insert into dept values(30, '财务部');
-- 创建表时添加外键约束
create table emp(
    empno int primary key auto_increment,
    ename varchar(50),
    dno int,
    constraint fk_emp_dept foreign key(dno) references dept(deptno)
)
-- 添加外键约束
alter table emp add constraint fk_emp_dept foreign key(dno) references dept(deptno)
insert into emp(ename) values('张三');
insert into emp(ename, dno) values('李四', 10);
insert into emp(ename, dno) values('王五', 20);

课时9 9.一对一关系

从表的主键就是外键

create table husband(
    hid int primary key auto_increment,
    hname varchar(50)
)
insert into husband(hname) values ('刘备'), ('关羽'), ('张飞')
create table wife(
    wid int primary key auto_increment,
    wname varchar(50),
    constraint fk_wife_husband foreign key(wid) references husband(hid)
)
-- wid 非空,唯一,引用hid
insert into wife(wid, wname) values(1, '杨贵妃');
insert into wife(wid, wname) values(2, '西施');

课时10 10.多对多关系

create table student(
    sid int primary key auto_increment,
    sname varchar(50)
)
create table teacher(
    tid int primary key auto_increment,
    tname varchar(50)
)
create table stu_tea(
    sid int,
    tid int,
    constraint fk_student foreign key(sid) references student(sid),
    constraint fk_teacher foreign key(tid) references teacher(tid)
)
insert into student(sname) values('段誉');
insert into student(sname) values('乔峰');
insert into student(sname) values('虚竹');
insert into teacher(tname) values('黄老师');
insert into teacher(tname) values('刘老师');
insert into teacher(tname) values('李老师');
insert into stu_tea(sid, tid) values(1, 1);
insert into stu_tea(sid, tid) values(2, 1);
insert into stu_tea(sid, tid) values(3, 1);
insert into stu_tea(sid, tid) values(1, 2);
insert into stu_tea(sid, tid) values(3, 2);
insert into stu_tea(sid, tid) values(1, 3);
insert into stu_tea(sid, tid) values(2, 3);

课时11 11.合并结果集

要合并的结果集表结构一样(列数, 列类型)

create table tb_a(id int, a_name varchar(50));
insert into tb_a(id, a_name) values(1, '1');
insert into tb_a(id, a_name) values(2, '2');
insert into tb_a(id, a_name) values(3, '3');
create table tb_b(id int, b_name varchar(50));
insert into tb_b(id, b_name) values(3, '3');
insert into tb_b(id, b_name) values(4, '4');
insert into tb_b(id, b_name) values(5, '5');
-- 不合并重复行
select * from tb_a union all select * from tb_b;
-- 合并重复行
select * from tb_a union select * from tb_b;

课时12 12.连接查询之内连接(方言)

-- 方言
select * from 表1 别名1, 表2 别名2 where 别名1.xx = 别名2.xx;
-- 标准 (推荐)
select * from 表1 别名1 inner join 表2 别名2 on 别名1.xx = 别名2.xx;
-- 自然
select * from 表1 别名1 natural join 表2 别名2

笛卡尔积:

(a, b, c) X (1, 2)
-> 
a1, a2, b1, b2, c1, c2
-- 笛卡尔积
select * from emp, dept
-- 员工对应部门信息
select * from emp, dept where emp.dno=dept.deptno;
-- 打印所有员工的姓名,部门名称, 取别名
select e.ename, d.dname
from emp e, dept d
where e.dno=d.deptno;

课时13 13.连接查询之内连接(标签和自然)

-- 标准推荐
select * from emp inner join dept on emp.dno=dept.deptno;
-- 自动加where条件
select * from emp natural join dept

课时14 14.连接查询之外连接

主表中所有记录都会打印, 副表没有null补位

-- 左外连接, 左表为主
select e.ename, ifnull(d.dname, '无部门') as dname
from emp e left outer join dept d
on e.dno=d.deptno;
-- 右外连接, 右表为主
select e.ename, d.dname
from emp e right outer join dept d
on e.dno=d.deptno;
-- 全外连接
select e.ename, d.dname
from emp e left outer join dept d
on e.dno=d.deptno;
union
select e.ename, d.dname
from emp e right outer join dept d
on e.dno=d.deptno;

课时15 15.子查询

查询中有查询

-- 查询本公司工资最高的员工详细信息
select * from emp where sal=(select max(sal) from emp);

子查询出现的位置

from 后作为表存在(多行多列)
where 后作为条件存在


条件

-- 单行单列 
select * from 表1 别名1 where 列1[=, >, <, >=, <=, !=]
(select 列 from 表2 别名2 where 条件)
-- 多行单列
select * from 表1 别名1 where 列1[in, all, any]
(select 列 from 表2 别名2 where 条件)
-- 单行多列(一个对象)
select * from 表1 别名1 where (列1, 列2) in
(select 列1, 列2 from 表2 别名2 where 条件)
-- 多行多列
select * from 表1 别名1, 
(select ...) 别名2 where 条件

eg:

-- 工资高于平均工资的员工
select * from emp where sal > (select avg(sal) from emp);
-- 大于30部门所有人的工资的员工
select * from emp where sal > all(select sal from emp where deptno=30);
-- 和李白岗位部门都相同的员工
select * from emp where (job, deptno) in (select jobm, deptno from emp where ename ='李白');

课时16 16.多表查询练习第1题

查询至少有一个员工的部门,显示部门编号,部门名称,部门位置,部门人数

-- 部门编号,部门名称,部门位置
select * from dept;
-- 部门人数
select deptno, count(*) from emp group by deptno
-- 整合
select d.* e1.cnt from dept d inner join 
(select deptno, count(*) cnt from emp group by deptno) as e1
on d.deptno=e1.deptno

课时17 17.多表查询练习第2题

列出所有员工的姓名及其直接上级的姓名

select e.ename, m.ename
from emp e left outer join emp m
on e.mgr=m.empno;

课时18 18.多表查询练习第4题

列出受雇日期早于直接上级的所有员工编号,姓名,部门名称

-- 1、先查询员工
select e.empno, e.ename, e.deptno
from emp e, emp m
where e.mgr=m.empno and e.hiredate<m.hiredate
-- 2、查询部门名称
select e.empno, e.ename, d.dname
from emp e, emp m, dept d
where e.mgr=m.empno 
and e.hiredate<m.hiredate 
and e.deptno=d.deptno

课时19 19.多表查询练习第5题

列出部门名称和这些部门的员工信息,同时列出哪些没有员工的部门

select *
from emp e right outer join dept d
on e.deptno=d.deptno;

课时20 20.多表查询练习第7题

列出最低薪金大于15000的各种工作及从事此工作的员工人数

select job, count(*)
from emp e
group by job
having min(sal) > 15000

课时21 21.多表查询练习第8题

列出在销售部工作的员工姓名,假定不知道销售部的部门编号

select ename
from emp e
where e.deptno = (select deptno from dept where dname='销售部')

课时22 22.多表查询练习第9题

列出薪金高于公司平均薪金的所有员工信息,所在部门名称,上级领导,工资等级

-- 薪金高于公司平均薪金的所有员工信息
select * from emp where e.sal>(select avg(sal) from emp)
select e.*, d.dname, m.ename, s.grade 
from 
    emp e left outer join dept d on e.deptno=d.deptno
          left outer join emp m on e.mgr=m.empno
          left outer join salgrade s on e.sal between s.losal and s.hisal
where e.sal>(select avg(sal) from emp)

课时23 23.多表查询练习第10题

列出与庞统从事相同工作的所有员工及部门名称

select e.*, d.dname
from emp e left outer join dept d
on e.deptno=d.deptno
where e.job=(select job from emp where ename='庞统')

课时24 24.多表查询练习第11题

列出薪金高于部门30工作的所有员工的薪金的员工姓名和薪金,部门名称

select e.ename, e.sal, d.dname
from ename e left outer join deptno d
on e.deptno=d.deptno
where e.sal>(select max(sal) from emp where deptno=30)

课时25 24.多表查询练习第13题

查出年份,利润,年度增长比

select * from tb_year
year  zz
2000  100
2001  150
2002  250
2003  300
select y1.* ifnull(concat((y1.zz-y2.zz)/y2.zz * 100, '%'), '0%') 增长比
from tb_year y1 left outer join tb_year y2
on y1.year=y2.year+1


相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
7月前
|
SQL Java 数据库连接
Java中实现SQL分页的方法
无论何种情况,选择适合自己的,理解了背后的工作原理,并能根据实际需求灵活变通的方式才是最重要的。
194 9
|
11月前
|
SQL NoSQL Java
Java使用sql查询mongodb
通过MongoDB Atlas Data Lake或Apache Drill,可以在Java中使用SQL语法查询MongoDB数据。这两种方法都需要适当的配置和依赖库的支持。希望本文提供的示例和说明能够帮助开发者实现这一目标。
429 17
|
11月前
|
SQL Java 数据库连接
如何在 Java 代码中使用 JSqlParser 解析复杂的 SQL 语句?
大家好,我是 V 哥。JSqlParser 是一个用于解析 SQL 语句的 Java 库,可将 SQL 解析为 Java 对象树,支持多种 SQL 类型(如 `SELECT`、`INSERT` 等)。它适用于 SQL 分析、修改、生成和验证等场景。通过 Maven 或 Gradle 安装后,可以方便地在 Java 代码中使用。
3377 11
|
11月前
|
SQL Java 数据库连接
【潜意识Java】MyBatis中的动态SQL灵活、高效的数据库查询以及深度总结
本文详细介绍了MyBatis中的动态SQL功能,涵盖其背景、应用场景及实现方式。
1150 6
|
11月前
|
SQL Java 数据库连接
如何用 Java 校验 SQL 语句的合法性?
本文介绍了五种校验 SQL 语句合法性的方案:1) 使用 JDBC API 的 `execute()` 方法,通过捕获异常判断合法性;2) 使用 JSqlParser 库解析 SQL 语句为 Java 对象;3) 使用正则表达式检查 SQL 语句格式;4) 使用 ANTLR 生成 SQL 解析器;5) 使用 Apache Calcite 解析 SQL。每种方法各有优劣,具体选择取决于需求和个人偏好。需要注意的是,这些方法仅能校验语法合法性,无法保证语义正确性,仍需防范 SQL 注入攻击。
449 6
|
12月前
|
SQL NoSQL Java
Java使用sql查询mongodb
通过使用 MongoDB Connector for BI 和 JDBC,开发者可以在 Java 中使用 SQL 语法查询 MongoDB 数据库。这种方法对于熟悉 SQL 的团队非常有帮助,能够快速实现对 MongoDB 数据的操作。同时,也需要注意到这种方法的性能和功能限制,根据具体应用场景进行选择和优化。
444 9
|
SQL Java
使用java在未知表字段情况下通过sql查询信息
使用java在未知表字段情况下通过sql查询信息
156 8
|
SQL 数据库
如何应用SQL约束条件?
【10月更文挑战第28天】如何应用SQL约束条件?
349 11
|
SQL 存储 Oracle
sql约束条件
【10月更文挑战第28天】sql约束条件
227 8
|
SQL 分布式计算 Java
Hadoop-11-MapReduce JOIN 操作的Java实现 Driver Mapper Reducer具体实现逻辑 模拟SQL进行联表操作
Hadoop-11-MapReduce JOIN 操作的Java实现 Driver Mapper Reducer具体实现逻辑 模拟SQL进行联表操作
220 3