学习时间:
2024.3.26
学习目标:
- A 掌握基本SQL建表,查询,修改,随机插入10000条数据等;
学习内容A:
- 创建一个员工表(employee),包含以下字段:
员工编号(employee_id)| 整数类型,自增,主键
员工姓名(eployee_name)| 字符类型,必输
年龄(age)|数字类型,需要接受小数;
入职日期(entry_date)|日期类型
入职部门(department)|字符类型
员工状态(status)|状态;在职、已离职、停薪留职、退休;
员工编号(employee_id)|整数类型,自增,主键
员工姓名(eployee_name)|字符类型,必输
年龄(age)|数字类型,需要接受小数;
入职日期(entry_date)|日期类型
入职部门(department)|字符类型
员工状态(status)|状态;在职、已离职、停薪留职、退休; - 在员工表中插入10条以上的数据,包含不同员工状态、不同部门的员工数据;
- 编写sql语句,把年龄(age)大于等于60的员工状态(status)变更为“退休”;
- 编写查询语句,分别查询出在职、已离职、停薪留职、退休人员数量;
- 用分组查询语句实现第4题;
- 编写查询语句,查询入职日期早于某一日期的员工姓名;
- 查询出重名的员工有哪些,给出重名员工编号;
- 用你所知的任何方法,对员工表填充1万条数据;
学习产出A:
- 8段SQL语句
- 自查bug补充SQL语句一条
1.创建一个员工表(employee):
use study;
create table employeeZJ
(
employee_id int AUTO_INCREMENT primary key not null comment '员工编号',
employee_name varchar(20) not null comment '员工姓名',
age float comment '年龄',
entry_date date comment '入职日期',
department varchar(20) comment '入职部门',
status varchar(20) comment '员工状态:在职、已离职、停薪留职、退休'
);
- 在员工表中插入10条以上的数据,包含不同员工状态、不同部门的员工数据:
INSERT INTO employeeZJ (employee_name, age, entry_date, department, status)
VALUES ('香馨', 56.1, '2022-01-01', 'Dept_A', '在职');
INSERT INTO employeeZJ (employee_name, age, entry_date, department, status)
VALUES ('向卉', 72.7, '2021-07-15', 'Dept_B', '已离职');
INSERT INTO employeeZJ (employee_name, age, entry_date, department, status)
VALUES ('向彤', 24.5, '2020-12-25', 'Dept_C', '已离职');
INSERT INTO employeeZJ (employee_name, age, entry_date, department, status)
VALUES ('向雪', 35.6, '2020-12-25', 'Dept_C', '停薪留职');
INSERT INTO employeeZJ (employee_name, age, entry_date, department, status)
VALUES ('晓燕', 39.5, '2020-12-25', 'Dept_C', '在职');
INSERT INTO employeeZJ (employee_name, age, entry_date, department, status)
VALUES ('晓莉', 75.4, '2020-12-25', 'Dept_C', '已离职');
INSERT INTO employeeZJ (employee_name, age, entry_date, department, status)
VALUES ('晓凡', 48.2, '2020-12-25', 'Dept_C', '在职');
INSERT INTO employeeZJ (employee_name, age, entry_date, department, status)
- 编写sql语句,把年龄(age)大于等于60的员工状态(status)变更为“退休”;
update employeeZJ
set status ="退休"
where age >= 60;
- 编写查询语句,分别查询出在职、已离职、停薪留职、退休人员数量:
select count(*) as "在职人数"
from employeeZJ
where status = "在职";
select count(*) as "已离职人数"
from employeeZJ
where status = "已离职";
select count(*) as "停薪留职人数"
from employeeZJ
where status = "停薪留职";
select count(*) as "退休人数"
from employeeZJ
where status = "退休";
- 用分组查询语句实现第4题:
select status, COUNT(*) as cnt
from employeeZJ
Group by status;
- 编写查询语句,查询入职日期早于某一日期的员工姓名:
select employee_name
from employeezJ
where entry_date > "2020-12-25";
- 查询出重名的员工有哪些,给出重名员工编号:
select employee_id, employee_name from employeeZJ
where employee_name in
(select employee_name from employeeZJ
group by employee_name having count(employee_name) > 1);
补充:最终检查发现,status应该限制只有以上几种状态,需要用枚举类型限制。
更新如下:
alter table employeeZJ
modify column status enum('在职','已离职','停薪留职','退休')
null comment '员工状态:在职、已离职、停薪留职、退休;';
- 用你所知的任何方法,对员工表填充1万条数据;
-- 清空员工表
truncate table employeezj;
-- 如果已存在,删除populateEmployees存储过程
DROP PROCEDURE IF EXISTS populateEmployees;
-- 定义存储过程,用于批量插入员工数据
DELIMITER $$
CREATE PROCEDURE populateEmployees(IN total INT)
BEGIN
-- 定义循环变量
DECLARE i INT DEFAULT 1;
-- 使用循环批量插入数据
WHILE i <= total DO
INSERT INTO employeeZJ (
employee_id, employee_name, age,entry_date, Department,status)
-- 生成随机员工信息
VALUES (i+60,
concat(substring('赵钱孙李周吴郑王冯陈诸卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳酆鲍史唐费廉岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅皮齐康伍余元卜顾孟平黄和穆萧尹姚邵堪汪祁毛禹狄米贝明臧计伏成戴谈宋茅庞熊纪舒屈项祝董粱杜阮蓝闵席季麻强贾路娄危江童颜郭梅盛林刁钟徐邱骆高夏蔡田樊胡凌霍虞万支柯咎管卢莫经房裘干解应宗丁宣贲邓郁单杭洪包诸左石崔吉钮龚'
,floor(1+190*rand()),1), --随机获取一个姓氏
substring('明国华建文平志伟东海强晓生光林小民永杰 军金健一忠洪江福祥中正振勇耀春大宁亮宇兴宝少剑云学仁涛瑞飞鹏安亚泽世汉达卫利胜敏群波成荣新峰刚家龙德庆斌辉良玉俊立浩天宏子松克清长嘉红山贤阳乐锋智青跃元武广思雄锦威启昌铭维义宗英凯鸿森超坚旭政传康继翔栋仲权奇礼楠炜友年震鑫雷兵万星骏伦绍麟雨行才希彦兆贵源有景升惠臣慧开章润高佳虎根远力进泉茂毅富博霖顺信凡豪树和恩向道川彬柏磊敬书鸣芳培全炳基冠晖京欣廷哲保秋君劲轩帆若连勋祖锡吉崇钧田石奕发洲彪钢运伯满庭申湘皓承梓雪孟其潮冰怀鲁裕翰征谦航士尧标洁城寿枫革纯风化逸腾岳银鹤琳显焕来心凤睿勤延凌昊西羽百捷定琦圣佩麒虹如靖日咏会久昕黎桂玮燕可越彤雁孝宪萌颖艺夏桐月瑜沛诚夫声冬奎扬双坤镇楚水铁喜之迪泰方同滨邦先聪朝善非恒晋汝丹为晨乃秀岩辰洋然厚灿卓杨钰兰怡灵淇美琪亦晶舒菁真涵爽雅爱依静棋宜男蔚芝菲露娜珊雯淑曼萍珠诗璇琴素梅玲蕾艳紫珍丽仪梦倩伊茜妍碧芬儿岚婷菊妮媛莲娟一'
,floor(1+400*rand()),floor(rand()*2)), --随机获取0个或者1个字
substring('明国华建文平志伟东海强晓生光林小民永杰军金健一忠洪江福祥中正振勇耀春大宁亮宇兴宝少剑云学仁涛瑞飞鹏安亚泽世汉达卫利胜敏群波成荣新峰刚家龙德庆斌辉良玉俊立浩天宏子松克清长嘉红山贤阳乐锋智青跃元武广思雄锦威启昌铭维义宗英凯鸿森超坚旭政传康继翔栋仲权奇礼楠炜友年震鑫雷兵万星骏伦绍麟雨行才希彦兆贵源有景升惠臣慧开章润高佳虎根远力进泉茂毅富博霖顺信凡豪树和恩向道川彬柏磊敬书鸣芳培全炳基冠晖京欣廷哲保秋君劲轩帆若连勋祖锡吉崇钧田石奕发洲彪钢运伯满庭申湘皓承梓雪孟其潮冰怀鲁裕翰征谦航士尧标洁城寿枫革纯风化逸腾岳银鹤琳显焕来心凤睿勤延凌昊西羽百捷定琦圣佩麒虹如靖日咏会久昕黎桂玮燕可越彤雁孝宪萌颖艺夏桐月瑜沛诚夫声冬奎扬双坤镇楚水铁喜之迪泰方同滨邦先聪朝善非恒晋汝丹为晨乃秀岩辰洋然厚灿卓杨钰兰怡灵淇美琪亦晶舒菁真涵爽雅爱依静棋宜男蔚芝菲露娜珊雯淑曼萍珠诗璇琴素梅玲蕾艳紫珍丽仪梦倩伊茜妍碧芬儿岚婷菊妮媛莲娟一'
,floor(1+400*rand()),1) --随机获取一个字
),
FLOOR(RAND()*(85-20+1))+20, -- 随机年龄
DATE_SUB(NOW(), INTERVAL FLOOR(RAND() * 365) DAY), -- 随机入职日期
CASE FLOOR(RAND() * 5) + 1
WHEN 1 THEN '人事'
WHEN 2 THEN '开发'
WHEN 3 THEN '测试'
WHEN 4 THEN '总裁'
WHEN 5 THEN '后勤'
END, -- 随机部门
CASE FLOOR(RAND() * 4) + 1
WHEN 1 THEN '在职'
WHEN 2 THEN '已离职'
WHEN 3 THEN '退休'
WHEN 4 THEN '停薪留职'
END); -- 随机状态
SET i = i + 1;
END WHILE;
END$$
DELIMITER ;
-- 调用存储过程,传入10000作为参数,插入10000条员工数据
CALL populateEmployees(10000);
学习笔记
A SQL生成随机手机号,邮箱,姓名等:
- 随机座机号码
select concat(rpad(CEIL(RAND()*1000), 3, '0'), '-',rpad(CEIL(RAND()*1000), 3, '0'), '-',rpad(CEIL(RAND()*10000), 3, '0'))
- 随机手机号码
select UNIX_TIMESTAMP()*10+floor(rand()*4000000000)
- 随机email邮箱地址
select concat(char(if(floor(rand()*2)=0,65+floor(rand()*26),48+floor(rand()*9)),if(floor(rand()*2)=0,65+floor(rand()*26),48+floor(rand()*9)),if(floor(rand()*2)=0,65+floor(rand()*26),48+floor(rand()*9)),if(floor(rand()*2)=0,65+floor(rand()*26),48+floor(rand()*9)),if(floor(rand()*2)=0,65+floor(rand()*26),48+floor(rand()*9)),if(floor(rand()*2)=0,65+floor(rand()*26),48+floor(rand()*9))),internal_id, '@', char(if(floor(rand()*2)=0,65+floor(rand()*26),48+floor(rand()*9)),if(floor(rand()*2)=0,65+floor(rand()*26),48+floor(rand()*9)),if(floor(rand()*2)=0,65+floor(rand()*26),48+floor(rand()*9)),if(floor(rand()*2)=0,65+floor(rand()*26),48+floor(rand()*9)),if(floor(rand()*2)=0,65+floor(rand()*26),48+floor(rand()*9)),if(floor(rand()*2)=0,65+floor(rand()*26),48+floor(rand()*9))), internal_id, '.com');
- 随机中文名
select concat(substring('赵钱孙李周吴郑王冯陈诸卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳酆鲍史唐费廉岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅皮齐康伍余元卜顾孟平黄和穆萧尹姚邵堪汪祁毛禹狄米贝明臧计伏成戴谈宋茅庞熊纪舒屈项祝董粱杜阮蓝闵席季麻强贾路娄危江童颜郭梅盛林刁钟徐邱骆高夏蔡田樊胡凌霍虞万支柯咎管卢莫经房裘干解应宗丁宣贲邓郁单杭洪包诸左石崔吉钮龚',floor(1+190*rand()),1),substring('明国华建文平志伟东海强晓生光林小民永杰军金健一忠洪江福祥中正振勇耀春大宁亮宇兴宝少剑云学仁涛瑞飞鹏安亚泽世汉达卫利胜敏群波成荣新峰刚家龙德庆斌辉良玉俊立浩天宏子松克清长嘉红山贤阳乐锋智青跃元武广思雄锦威启昌铭维义宗英凯鸿森超坚旭政传康继翔栋仲权奇礼楠炜友年震鑫雷兵万星骏伦绍麟雨行才希彦兆贵源有景升惠臣慧开章润高佳虎根远力进泉茂毅富博霖顺信凡豪树和恩向道川彬柏磊敬书鸣芳培全炳基冠晖京欣廷哲保秋君劲轩帆若连勋祖锡吉崇钧田石奕发洲彪钢运伯满庭申湘皓承梓雪孟其潮冰怀鲁裕翰征谦航士尧标洁城寿枫革纯风化逸腾岳银鹤琳显焕来心凤睿勤延凌昊西羽百捷定琦圣佩麒虹如靖日咏会久昕黎桂玮燕可越彤雁孝宪萌颖艺夏桐月瑜沛诚夫声冬奎扬双坤镇楚水铁喜之迪泰方同滨邦先聪朝善非恒晋汝丹为晨乃秀岩辰洋然厚灿卓杨钰兰怡灵淇美琪亦晶舒菁真涵爽雅爱依静棋宜男蔚芝菲露娜珊雯淑曼萍珠诗璇琴素梅玲蕾艳紫珍丽仪梦倩伊茜妍碧芬儿岚婷菊妮媛莲娟一',floor(1+400*rand()),1),substring('明国华建文平志伟东海强晓生光林小民永杰军金健一忠洪江福祥中正振勇耀春大宁亮宇兴宝少剑云学仁涛瑞飞鹏安亚泽世汉达卫利胜敏群波成荣新峰刚家龙德庆斌辉良玉俊立浩天宏子松克清长嘉红山贤阳乐锋智青跃元武广思雄锦威启昌铭维义宗英凯鸿森超坚旭政传康继翔栋仲权奇礼楠炜友年震鑫雷兵万星骏伦绍麟雨行才希彦兆贵源有景升惠臣慧开章润高佳虎根远力进泉茂毅富博霖顺信凡豪树和恩向道川彬柏磊敬书鸣芳培全炳基冠晖京欣廷哲保秋君劲轩帆若连勋祖锡吉崇钧田石奕发洲彪钢运伯满庭申湘皓承梓雪孟其潮冰怀鲁裕翰征谦航士尧标洁城寿枫革纯风化逸腾岳银鹤琳显焕来心凤睿勤延凌昊西羽百捷定琦圣佩麒虹如靖日咏会久昕黎桂玮燕可越彤雁孝宪萌颖艺夏桐月瑜沛诚夫声冬奎扬双坤镇楚水铁喜之迪泰方同滨邦先聪朝善非恒晋汝丹为晨乃秀岩辰洋然厚灿卓杨钰兰怡灵淇美琪亦晶舒菁真涵爽雅爱依静棋宜男蔚芝菲露娜珊雯淑曼萍珠诗璇琴素梅玲蕾艳紫珍丽仪梦倩伊茜妍碧芬儿岚婷菊妮媛莲娟一',floor(1+400*rand()),1));