MySQL丨基本语法练习1

#以自己名字创建一个数据库
create database dxw;
use dxw;
#在数据库中创建以下三个表,雇员表,部门表,工资表,数据类型,宽度根据实际需要进行设置
#雇员表 employee 雇员编号 empid 姓名 name 性别 sex 职称title 出生日期birthday 所在部门编号depid 雇员编号为主键,部门编号为外键
#部门表 department 部门编号 depid 部门名称depname 部门编号为主键
#工资表 salary 雇员编号empid 基本工资basesalary 职务工资titlesalary 扣除deduction 雇员编号为外键
create table department(
    depid int primary key auto_increment,
    depname varchar(35)
)
create table employee(
    empid int primary key,
    name varchar(35),
    sex char(5),
    title varchar(35),
    birthday date,
    depid int, 
    foreign key(depid) references department(depid)
)
create table salary(
    empid int,
    basesalary int,
    titlesalary int,
    deduction int,
    foreign key(empid) references employee(empid)
)
#修改表结构,在部门表中添加部门简介字段
alter table department add depinfo varchar(35);
desc department;
#为三个表插入数据
insert into department(depid,depname) values(222,'销售部'),(111,'生产部'),(333,'人事部');department

insert into employee values(1001,'张三','男','高级工程师','1975-1-1',111),
(1002,'李四','女','助理','1985-1-1',111),
(1003,'王五','男','工程师','1978-11-11',222),(1004,'赵六','男','工程师','1979-1-1',222);

insert into salary values(1001,2200,1100,200),(1002,1200,200,100),(1003,1900,700,200),(1004,1950,700,150);
#将李四的职称改成 工程师,并将他的基本工资改为1700,职务工资600
update employee,salary set employee.title='工程师',salary.basesalary=1700,salary.titlesalary=600 
where employee.name='李四' and employee.empid = salary.empid;
select * from employee;
select * from salary;
#删除人事部门的记录
delete from department where department.depname = '人事部';
select * from department;
#查询出部门的雇员编号,实发工资,应发工资
select empid,basesalary+titlesalary as 应发工资,basesalary+titlesalary-deduction as
实发工资 from salary;
#查询姓张的且年龄小于40
select * from employee where name like '张%' and 2018-birthday < 40;
#查询雇员的编号,姓名,职称,部门名称,实发工资
select employee.empid,employee.name,department.depname,basesalary+titlesalary-deduction
as '实发工资' from employee,department,salary
where employee.empid = salary.empid and employee.depid = department.depid;
#查询销售部门的雇员姓名,基本工资
select employee.name,salary.basesalary from employee,salary
where employee.empid = salary.empid and employee.depid = (
select department.depid from department
where department.depname = '销售部')
#统计各职称的人数
select title,count(title) from employee group by title;
#统计各部门的部门名称,实发工资和平均工资
select department.depname as '部门名称',sum(basesalary)+sum(titlesalary)-sum(deduction) as '实发工资',
avg(basesalary)+avg(titlesalary)-avg(deduction) as '平均工资'
from department,salary,employee
where department.depid = employee.depid 
and employee.empid = salary.empid
group by '部门名称';

select department.depname as '部门',employee.name as '员工名',salary.basesalary as '基本工资',salary.titlesalary '职位工资'
,salary.deduction as '扣除'
-- , sum(basesalary+titlesalary-deduction),avg(basesalary+titlesalary-deduction)
from department,employee,salary
where department.depid = employee.depid and employee.empid = salary.empid
-- group by depname;
#查询比销售部门所有员工基本工资都高的雇员信息
select name,sex,title,birthday from employee,salary
where employee.empid = salary.empid and salary.basesalary > (
    select max(salary.basesalary) from salary,department,employee 
    where department.depname = '销售部' 
    and department.depid = employee.depid
    and employee.empid = salary.empid
)

猜你喜欢

转载自blog.csdn.net/qq_36134369/article/details/83022069