Note/大学笔记/数据库导论/试题/综合案例3——数据表查询操作.md

291 lines
8.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 综合案例3——数据表查询操作
SQL语句可以分为两部分一部分用来创建数据库对象另一部分用来操作这些对象本部分内容详细介绍了操作数据库对象的数据表查询语句。通过部分内容的介绍同学们可以了解到SQL中的查询语言功能的强大用户可以根据需要灵活使用。此综合案例将回顾这些查询语句。
## 1、案例目的
根据不同条件对表进行查询操作掌握数据表的查询语句。employee、dept表结构以及表中的记录如表1~表4所示。
表1 employee表结构
| 字段名 | 数据类型 | 主键 | 外键 | 非空 | 唯一 | 自增 | 字段说明 |
| -------- | ------------- | ---- | ---- | ---- | ---- | ---- | -------- |
| e_no | int11 | 是 | 否 | 是 | 是 | 否 | 员工编号 |
| e_name | varchar50 | 否 | 否 | 是 | 否 | 否 | 员工姓名 |
| e_gender | char2 | 否 | 否 | 否 | 否 | 否 | 员工性别 |
| dept_no | int11 | 否 | 是 | 是 | 否 | 否 | 部门编号 |
| e_job | varchar50 | 否 | 否 | 是 | 否 | 否 | 职位 |
| e_salary | int11 | 否 | 否 | 是 | 否 | 否 | 薪水 |
| hiredate | date | 否 | 否 | 是 | 否 | 否 | 入职日期 |
表2 dept表结构
| 字段名 | 数据类型 | 主键 | 外键 | 非空 | 唯一 | 自增 | 字段说明 |
| ---------- | -------------- | ---- | ---- | ---- | ---- | ---- | -------- |
| d_no | int11 | 是 | 否 | 是 | 是 | 是 | 部门编号 |
| d_name | varchar50 | 否 | 否 | 是 | 否 | 否 | 部门姓名 |
| d_location | varchar100 | 否 | 否 | 否 | 否 | 否 | 部门地址 |
表3 employee表中的记录
| e_no | e_name | e_gender | dept_no | e_job | e_salary | hiredate |
| ---- | ------ | -------- | ------- | --------- | -------- | ---------- |
| 1001 | Smith | m | 20 | clerk | 800 | 2005-11-12 |
| 1002 | Allen | f | 30 | salesman | 1600 | 2003-05-12 |
| 1003 | Ward | f | 30 | salesman | 1250 | 2003-05-12 |
| 1004 | Jones | m | 20 | manager | 2975 | 1998-05-18 |
| 1005 | Martin | m | 30 | salesman | 1250 | 2001-06-12 |
| 1006 | Blake | f | 30 | manager | 2850 | 1997-02-15 |
| 1007 | Clark | m | 10 | manager | 2450 | 2002-09-12 |
| 1008 | Scott | m | 20 | analyst | 3000 | 2003-05-12 |
| 1009 | King | f | 10 | president | 5000 | 1995-01-01 |
| 1010 | Turner | f | 30 | salesman | 1500 | 1997-10-12 |
| 1011 | Adams | m | 20 | clerk | 1100 | 1999-10-05 |
| 1012 | James | f | 30 | clerk | 950 | 2008-06-15 |
表4 dept表中的记录
| d_no | d_name | d_location |
| ---- | ---------- | ---------- |
| 10 | accounting | ShangHai |
| 20 | research | BeiJing |
| 30 | sales | ShenZhen |
| 40 | operations | FuJian |
## 2、案例操作过程
1登录MySQL数据库。
```mysql
mysql -h localhost -u root -p
```
截图:
![image-20220329093954268](https://lsky.hhdxw.top/imghub/img/image-20220329093954268.png)
2创建数据库MANGER并选择使用此数据库。
```mysql
create database MANGER;
use MANGER;
```
截图:
![image-20220329094314369](https://lsky.hhdxw.top/imghub/img/image-20220329094314369.png)
3按照表1、表2创建表employee和dept创建成功后用desc查看表结构。
```mysql
create table dept(
d_no int(11) not null unique auto_increment,
d_name varchar(50) not null,
d_location varchar(100) null,
primary key(d_no)
);
desc dept;
```
```mysql
create table employee(
e_no int(11) not null unique,
e_name varchar(50) not null,
e_gender char(2) null,
dept_no int(11) not null,
e_job varchar(50) not null,
e_salary int(11) not null,
hiredate date not null,
primary key(e_no),
foreign key (dept_no)references dept(d_no)
);
desc employee;
```
截图:
![image-20220329094933122](https://lsky.hhdxw.top/imghub/img/image-20220329094933122.png)
![image-20220329101229507](https://lsky.hhdxw.top/imghub/img/image-20220329101229507.png)
4将表3、表4的记录分别插入两个表中。
```mysql
insert into dept (d_no,d_name,d_location)
values(10,'accounting','ShangHai'),
(20,'research','BeiJing'),
(30,'sales','ShenZhen'),
(40,'operations','FuJian');
```
```mysql
insert into employee (e_no,e_name,e_gender,dept_no,e_job,e_salary,hiredate)
values(1001,'Smith','m',20,'clerk',800,'2005-11-12'),
(1002,'Allen','f',30,'salesman',1600,'2003-05-12'),
(1003,'Ward','f',30,'salesman',1250,'2003-05-12'),
(1004,'Jones','m',20,'manager',2975,'1998-05-18'),
(1005,'Martin','m',30,'salesman',1250,'2001-06-12'),
(1006,'Blake','f',30,'manager',2850,'1997-02-15'),
(1007,'Clark','m',10,'manager',2450,'2002-09-12'),
(1008,'Scott','m',20,'analyst',3000,'2003-05-12'),
(1009,'King','f',10,'president',5000,'1995-01-01'),
(1010,'Turner','f',30,'salesman',1500,'1997-10-12'),
(1011,'Adams','m',20,'clerk',1100,'1999-10-05'),
(1012,'James','f',30,'clerk',950,'2008-06-15');
```
截图:
![image-20220329155245302](https://lsky.hhdxw.top/imghub/img/image-20220329155245302.png)
![image-20220329155332888](https://lsky.hhdxw.top/imghub/img/image-20220329155332888.png)
5在employee表中查询所有记录的e_no、e_name和e_salary字段值。
```mysql
select e_no,e_name,e_salary from employee;
```
截图:
![image-20220329155628020](https://lsky.hhdxw.top/imghub/img/image-20220329155628020.png)
6在employee表中查询dept_no等于10和20的所有记录。
```mysql
select * from employee where dept_no=10 or dept_no=20;
```
截图:
![image-20220329155759471](https://lsky.hhdxw.top/imghub/img/image-20220329155759471.png)
7在employee表中查询工资范围在800~2500之间的员工信息。
```mysql
select * from employee where e_salary>=800 and e_salary<=2500;
```
截图:
![image-20220329155930614](https://lsky.hhdxw.top/imghub/img/image-20220329155930614.png)
8在employee表中查询部门编号为20的部门中的员工所有信息。
```mysql
select * from employee where dept_no=20;
```
截图:
![image-20220329160154098](https://lsky.hhdxw.top/imghub/img/image-20220329160154098.png)
9在employee表中查询每个部门最高工资的员工信息。
```mysql
select *,max(e_salary) from employee group by dept_no;
```
截图:
![image-20220329160825143](https://lsky.hhdxw.top/imghub/img/image-20220329160825143.png)
10查询员工Blake所在部门编号和部门所在地。
```mysql
select d_name,d_location from dept as de where de.d_no=(select dept_no from employee where e_name='Blake');
```
截图:
![image-20220329164419168](https://lsky.hhdxw.top/imghub/img/image-20220329164419168.png)
11使用连接查询查询所有员工的部门和和部门信息。
```mysql
select * from employee as em,dept as de where de.d_no=em.dept_no;
```
截图:
![image-20220329163914411](https://lsky.hhdxw.top/imghub/img/image-20220329163914411.png)
12在employee表中计算每个部门各有多少名员工。
```mysql
select count(e_name),dept_no from employee group by dept_no;
```
截图:
![image-20220329164610824](https://lsky.hhdxw.top/imghub/img/image-20220329164610824.png)
13在employee表中计算不同类型职工的总工资数。
```mysql
select e_job,sum(e_salary) from employee group by e_job;
```
截图:
![image-20220329161757013](https://lsky.hhdxw.top/imghub/img/image-20220329161757013.png)
14在employee表中计算不同部门的平均工资。
```mysql
select dept_no,avg(e_salary) from employee group by dept_no;
```
截图:
![image-20220329162920630](https://lsky.hhdxw.top/imghub/img/image-20220329162920630.png)
15在employee表中查询工资低于1500的员工信息。
```mysql
select * from employee where e_salary<1500;
```
截图:
![image-20220329163014313](https://lsky.hhdxw.top/imghub/img/image-20220329163014313.png)
16在employee表中将查询记录先按部门编号由高到低排列若部门相同再按员工工资由高到低排列。
```mysql
select * from employee order by dept_no desc,e_salary desc;
```
截图:
![image-20220329165111978](https://lsky.hhdxw.top/imghub/img/image-20220329165111978.png)
17在employee表中查询员工姓名以字母A或S开头的员工信息。
```mysql
select * from employee where e_name like 'A%' or e_name like 'S%';
```
截图:
![image-20220329163253424](https://lsky.hhdxw.top/imghub/img/image-20220329163253424.png)
18在employee表中查询到目前为止工龄大于等于20年的员工信息。
```mysql
select * from employee as em where (select year(now())-(select year(em.hiredate)))>20;
```
截图:
![image-20220329165903364](https://lsky.hhdxw.top/imghub/img/image-20220329165903364.png)