MySql 数据库题目
数据表
1. 创建oa数据库
2. 创建dept及emp数据表
字段名 | 说明 | 类型 | 长度 | 约束 |
---|---|---|---|---|
id | 编号 | int | 主键,不为空,自增长 | |
name | 名称 | varchar | 20 | 唯一 |
字段名 | 说明 | 类型 | 长度 | 约束 |
---|---|---|---|---|
id | 编号 | int | 主键,不为空,自增长 | |
name | 姓名 | varchar | 20 | 非空 |
age | 年龄 | int | ||
job | 职位 | varchar | 20 | 非空 |
salary | 薪水 | int | ||
entrydate | 入职日期 | date | ||
managerid | 直属领导ID | int | ||
dept_id | 部门ID | int | 非空,外键(引用dept表主键) |
数据
创建数据库
# 创建数据库
create database db_oa;
# 使用数据库
use oa;
创建表,数据导入
# 如果表存在直接删除
drop table if exists tb_dept;
drop table if exists tb_emp;
#创建新的表
create table tb_dept
(
id int primary key auto_increment not null comment '编号',
name varchar(20) unique comment '名称'
)comment '部门表';
#删除表
drop table tb_emp
create table tb_emp
(
id int primary key auto_increment not null comment '编号',
name varchar(20) not null comment '名称',
age int comment '年龄',
job varchar(20) not null comment '职位',
salary int comment '薪水',
entrydate date comment '入职时间',
managerid int comment '直属领导id',
dept_id int not null comment '部门id',
foreign key (dept_id) references tb_dept(id)
)comment '员工表';
# 查询表
select * from tb_dept
select * from tb_emp
# 数据新增
insert into tb_dept values
(1,'研发部'),
(2,'市场部'),
(3,'财务部'),
(4,'销售部'),
(5,'总经办')
insert into tb_emp values
(1,'金庸',66,'总裁',20000,'2000-01-01',null,5),
(2,'张无忌',20,'项目经理',12500,'2005-12-05',1,1),
(3,'杨肖',33,'开发',8400,'2000-11-03',2,1),
(4,'韦一笑',48,'开发',11000,'2002-02-05',2,1),
(5,'常遇春',43,'开发',10500,'2004-09-07',3,1),
(6,'小昭',19,'程序员鼓励师',6600,'2004-10-12',2,1)
要求
1. 查询dept表中所有列及所有行
select * from dept;
2. 查询emp表中id,name,age三列所有记录
select id,name,age from emp;
3. 查询emp表中年龄在50岁以上的所有员工
select * from emp where age >= 50;
4. 查询emp表中薪水在8000至12000之间的所有员工信息
select * from emp where salary between 8000 and 12000;
5. 查询emp表中所有职务为开发的员工信息
select * from emp where job ='开发';
7. 显示emp表中入职时间最长的前3位员工信息
select * from emp order by entrydate asc limit 3;
8. 查询emp表中所有职务的平均薪水,并按均薪水降序显示
select job, avg(salary) as avg from emp group by job order by avg desc;
9. 查询emp表中各职务的人数,并按人数降序显示
select job, count(*) as num from emp group by job order by num desc;
10. 将emp表中所有员工工资降10%
update emp set salary = salary * 0.9;
11. 将emp表中所有年龄在60岁以上的员工删除
select * from emp where age >= 60
delete from emp where age >= 60