HRS_create_and_init.sql 2.5 KB
Newer Older
1 2 3 4 5 6 7 8 9
drop database if exists hrs;
create database hrs default charset utf8;

use hrs;

drop table if exists tb_emp;
drop table if exists tb_dept;

create table tb_dept
骆昊的技术专栏's avatar
骆昊的技术专栏 已提交
10
(
11 12 13 14
dno int not null comment '编号',
dname varchar(10) not null comment '名称',
dloc varchar(20) not null comment '所在地',
primary key (dno)
骆昊的技术专栏's avatar
骆昊的技术专栏 已提交
15
);
16 17 18 19 20 21 22 23

insert into tb_dept values 
	(10, '会计部', '北京'),
	(20, '研发部', '成都'),
	(30, '销售部', '重庆'),
	(40, '运维部', '深圳');

create table tb_emp
骆昊的技术专栏's avatar
骆昊的技术专栏 已提交
24
(
25 26 27 28 29 30 31 32
eno int not null comment '员工编号',
ename varchar(20) not null comment '员工姓名',
job varchar(20) not null comment '员工职位',
mgr int comment '主管编号',
sal int not null comment '员工月薪',
comm int comment '每月补贴',
dno int comment '所在部门编号',
primary key (eno)
骆昊的技术专栏's avatar
骆昊的技术专栏 已提交
33
);
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

alter table tb_emp add constraint fk_emp_dno foreign key (dno) references tb_dept (dno);

insert into tb_emp values 
	(7800, '张三丰', '总裁', null, 9000, 1200, 20),
	(2056, '乔峰', '分析师', 7800, 5000, 1500, 20),
	(3088, '李莫愁', '设计师', 2056, 3500, 800, 20),
	(3211, '张无忌', '程序员', 2056, 3200, null, 20),
	(3233, '丘处机', '程序员', 2056, 3400, null, 20),
	(3251, '张翠山', '程序员', 2056, 4000, null, 20),
	(5566, '宋远桥', '会计师', 7800, 4000, 1000, 10),
	(5234, '郭靖', '出纳', 5566, 2000, null, 10),
	(3344, '黄蓉', '销售主管', 7800, 3000, 800, 30),
	(1359, '胡一刀', '销售员', 3344, 1800, 200, 30),
	(4466, '苗人凤', '销售员', 3344, 2500, null, 30),
	(3244, '欧阳锋', '程序员', 3088, 3200, null, 20),
	(3577, '杨过', '会计', 5566, 2200, null, 10),
	(3588, '朱九真', '会计', 5566, 2500, null, 10);

骆昊的技术专栏's avatar
骆昊的技术专栏 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

-- 查询薪资最高的员工姓名和工资

-- 查询员工的姓名和年薪((月薪+补贴)*12)

-- 查询有员工的部门的编号和人数

-- 查询所有部门的名称和人数

-- 查询薪资最高的员工(Boss除外)的姓名和工资

-- 查询薪水超过平均薪水的员工的姓名和工资

-- 查询薪水超过其所在部门平均薪水的员工的姓名、部门编号和工资

-- 查询部门中薪水最高的人姓名、工资和所在部门名称

-- 查询主管的姓名和职位

-- 查询薪资排名4~6名的员工姓名和工资

74
-- use hrs;
骆昊的技术专栏's avatar
骆昊的技术专栏 已提交
75

76
-- drop procedure if exists sp_avg_sal_by_dept;
骆昊的技术专栏's avatar
骆昊的技术专栏 已提交
77 78


79 80 81 82
-- create procedure sp_avg_sal_by_dept(dno integer, out avg_sal float)
-- begin 
--     select avg(sal) into avg_sal from tb_emp where dno=dno;
-- end;
骆昊的技术专栏's avatar
骆昊的技术专栏 已提交
83 84 85 86




87
-- call sp_avg_sal_by_dept(10, @avgSal);
骆昊的技术专栏's avatar
骆昊的技术专栏 已提交
88

89
-- select @avgSal;
骆昊的技术专栏's avatar
骆昊的技术专栏 已提交
90 91 92