Mysql小练习

\1. 以自己的姓名创建一个数据库。

create database if not exists pyh;

\2. 在此数据库下创建如下3表,数据类型,宽度,是否为空等,根据实际情况自己定义。

A. 雇员表(employee):

雇员编号(empid),

姓名(name),

性别(gender),

职称(title),

出生日期(birthday),

所在部门编号(depid);

其中雇员编号为主键;

B. 部门表(department):

部门编号(depid),

部门名称(depname);

其中部门编号为主键。

C. 工资表(salary):

雇员编号(empid),

基本工资(base_salary),

职务工资(title_salary),

扣除(deduction)。

其中雇员编号为主键。

create table employee
(empid int not null primary key,
name char(11),
gender enum('男', '女'),
title char(11),
birthday date,
depid int
)default charset utf8mb4;
​
create table department
(depid int,
depname char(11))default charset utf8mb4;
​
create table salary
(empid int not null primary key,
base_salary decimal(6, 2),
title_salary decimal(6, 2),
deduction decimal(6, 2))default charset utf8mb4;

\3. 修改表结构,在部门表中添加一个”部门简介”字段。

alter table department add column introduction varchar(256);

\4. 在上面的3个表中各输入若干条记录,内容如下。

雇员表:

雇员编号姓名性别职称出生日期所在部门编号
1001张三高级程师1975-1-1111
1002李四助工1985-1-1111
1003王五工程师1978-11-11222
1004赵六工程师1979-1-1222

部门表:

部门编号部门名称部门简介
111生产部Null
222销售部Null
333人事部Null

工资表:

雇员编号基本工资职务工资扣除
100122001100200
10021200200100
10031900700200
10041950700150
insert into employee
values
(1001, '张三', 1, '高级工程师', '1975-1-1', 111),
(1002, '李四', 2, '助工', '1985-1-1', 111),
(1003, '王五', 1, '工程师', '1978-11-11', 222),
(1004, '赵六', 1, '工程师', '1979-1-1', 222);
​
insert into department
values
(111, '生产部', null),
(222, '销售部', null),
(333, '人事部', null);
​
insert into salary
values
(1001, 2200, 1100, 200),
(1002, 1200, 200, 100),
(1003, 1900, 700, 200),
(1004, 1950, 700, 150);

\5. 将李四的职称改为“工程师”,并将她的基本工资改为5700元,职务工资为600。

update employee set title = '工程师' where name = '李四';
update salary set base_salary = 5700, title_salary = 600 where empid = (select empid from employee where name = '李四');

\6. 查询出每个雇员的雇员编号,姓名,职称,所在部门,实发工资和应发工资。

select e.empid as 雇员编号, e.name as 雇员姓名, e.title as 职称, d.depname as 所在部门, (s.base_salary + s.title_salary - s.deduction) as 实发工资, (s.base_salary + s.title_salary) as 应发工资 from employee e inner join department d on d.depid = e.depid inner join salary s on s.empid = e.empid where e.depid = d.depid;

\7. 查询姓“张”且年龄小于40岁的员工的记录。

# +0将日期值转换为数值型
select * from employee where (year(curdate())-year(birthday)) < 40 and name like '张%';

\8. 查询销售部所有雇员的雇员编号,姓名,职称,部门名称,实发工资。

select e.empid as 雇员编号, e.name as 姓名, e.title as 职称, d.depname as 部门名称, (s.base_salary + s.title_salary - s.deduction) as 实发工资 from employee e inner join department d on d.depid = e.depid inner join salary s on s.empid = e.empid where e.depid = d.depid and e.depid = 222;

\9. 统计各类职称的人数。

select distinct title as 职称, count(title) as 人数 from employee group by title;

\10. 统计各部门的部门名称,实发工资总和,平均工资。

select d.depname as 部门名称, sum(s.base_salary + s.title_salary - s.deduction) as 实发工资总和, avg(s.base_salary + s.title_salary - s.deduction) as 平均工资 from employee e inner join department d on d.depid = e.depid inner join salary s on s.empid = e.empid group by d.depname;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值