黑马2024AI+JavaWeb开发入门Day05-数据库DDL、DML、DQL飞书作业

视频地址:哔哩哔哩

讲义作业飞书地址:day05作业(MySQL)

Day05的SQL挺简单的,固定格式多敲敲就好。

1、作业1

第一个作业是上课老师讲过的代码:

sql 复制代码
--  =================== DQL: 基本查询 ======================
-- 1. 查询指定字段 name,entry_date 并返回
select name, entry_date from emp;

-- 2. 查询返回所有字段
# 快捷方式,项目开发中不推荐
select * from emp;

-- 3. 查询所有员工的 name,entry_date, 并起别名(姓名、入职日期)
-- 别名中间有空格时需要加引号。
select name as 姓名, entry_date as ee from emp;

-- 4. 查询已有的员工关联了哪几种职位(不要重复)
select distinct job from emp;

--  =================== DQL: 条件查询 ======================
-- 1. 查询 姓名 为 柴进 的员工
select * from emp where name='柴进';

-- 2. 查询 薪资小于等于5000 的员工信息
select * from emp where salary <= 5000;

-- 3. 查询 没有分配职位 的员工信息
select * from emp where job is null;

-- 4. 查询 有职位 的员工信息
select * from emp where job is not null;

-- 5. 查询 密码不等于 '123456' 的员工信息
select * from emp where password !='123456';
select * from emp where password <> '123456';

-- 6. 查询 入职日期 在 '2000-01-01' (包含) 到 '2010-01-01'(包含) 之间的员工信息
select * from emp where entry_date between '2000-01-01' and '2010-01-01';

-- 7. 查询 入职时间 在 '2000-01-01' (包含) 到 '2010-01-01'(包含) 之间 且 性别为女 的员工信息
select * from emp where entry_date between '2000-01-01' and '2010-01-01' and gender = 2;
select * from emp where (entry_date between '2000-01-01' and '2010-01-01') and gender = 2;

-- 8. 查询 职位是 2 (讲师), 3 (学工主管), 4 (教研主管) 的员工信息
select * from emp where job = 2 or job = 3 or job = 4;
select * from emp where job in (2,3,4);

-- 9. 查询 姓名 为两个字的员工信息
select * from emp where name like '__';

-- 10. 查询 姓 '李' 的员工信息
select * from emp where name like '阮%';

-- 11. 查询 姓名中包含 '二' 的员工信息
select * from emp where name like '%二%';

--  =================== DQL: 分组查询 ======================
-- 聚合函数

-- 1. 统计该企业员工数量
-- 所有的聚合函数不参与null值的统计
-- 最好是count主键,或者是*,或者是count常量也可以
select count(id) from emp;
select count(username) from emp;
select count(job) from emp;
select count(*) from emp;
select count(1) from emp;

-- 2. 统计该企业员工的平均薪资
select avg(emp.salary) from emp;

-- 3. 统计该企业员工的最低薪资
select min(emp.salary) from emp;

-- 4. 统计该企业员工的最高薪资
select max(emp.salary) from emp;

-- 5. 统计该企业每月要给员工发放的薪资总额(薪资之和)
select sum(emp.salary) from emp;


-- 分组
-- 注意:分组之后,select后的字段列表不能随意书写,能写的一般是分组字段 + 聚合函数;
    -- where 后面不能写聚合函数,只能写在having里
-- 1. 根据性别分组 , 统计男性和女性员工的数量
select gender, count(*) from emp group by gender;

-- 2. 先查询入职时间在 '2015-01-01' (包含) 以前的员工 , 并对结果根据职位分组 , 获取员工数量大于等于2的职位
select emp.job from emp where entry_date <= '2015-01-01' group by job having count(job) >= 2;

--  =================== 排序查询 ======================
-- 1. 根据入职时间, 对员工进行升序排序
select * from emp order by entry_date asc;

-- 2. 根据入职时间, 对员工进行降序排序
select * from emp order by entry_date desc;

-- 3. 根据 入职时间 对公司的员工进行 升序排序 , 入职时间相同 , 再按照 更新时间 进行降序排序
select * from emp order by entry_date,update_time desc ;

select emp.job,count(job) from emp where entry_date <= '2015-01-01' group by job having count(job) >= 2 order by count(job) desc;


--  =================== 分页查询 ======================
-- 1. 从起始索引0开始查询员工数据, 每页展示5条记录
select * from emp limit 5;

-- 2. 查询 第1页 员工数据, 每页展示5条记录
select * from emp limit 5;

-- 3. 查询 第2页 员工数据, 每页展示5条记录
select * from emp limit 5,5;


-- 4. 查询 第3页 员工数据, 每页展示5条记录
select * from emp limit 10,5;

-- 起始索引 = (页码 - 1) * 每页展示记录数

2、作业2

sql 复制代码
create table clazz(
    id int unsigned primary key auto_increment comment 'ID,主键',
    class_name varchar(30) not null unique comment '班级名称',
    classroom varchar(20) comment '班级教室',
    class_start date not null comment '开课时间',
    class_end date not null comment '结课时间',
    class_status int not null comment '开课状态',
    subject int not null comment '学科',
    create_time datetime comment '创建时间',
    update_time datetime comment '修改时间'
) comment '班级表';


create table dept(
    id int unsigned primary key auto_increment comment 'ID,主键',
    dept_name varchar(10) not null unique comment '部门名称',
    create_time datetime comment '创建时间',
    update_time datetime comment '修改时间'
) comment '部门表';
相关推荐
程序猿阿伟32 分钟前
《深度剖析SQL之WHERE子句:数据过滤的艺术》
数据库·sql·oracle
zl0_00_044 分钟前
sql注入语句学习
数据库·sql·学习
猫咪-95271 小时前
Mysql索引
mysql
Arbori_262151 小时前
mysql 索引
数据库·mysql
小李小李快乐不已2 小时前
3.1.3 MYSQL连接池
数据库·mysql·adb
明月看潮生2 小时前
青少年编程与数学 02-011 MySQL数据库应用 18课题、性能监控
数据库·mysql·青少年编程·编程与数学
ssxueyi3 小时前
MySQL 语句解析json字符串
数据库·mysql·json
simpleGq5 小时前
MySQL笔记
笔记·mysql
冲浪中台5 小时前
MySQL 的 JSON 路径格式
mysql·adb·json
王嘉俊9255 小时前
MySQL事务详解
数据库·mysql