- 创建szu_test数据库
在服务器后台命令行中使用命令登录进数据库
mysql -h localhost -u szu_test -p
查看系统中有哪些数据库,并使用szu_test数据库
一开始数据库是空的
- 建立DEPT表格
导入数据dept
Sql语句
- INSERT INTO DEPT (DEPTNO,DNAME,LOC)
- VALUES
- (10 , 'ACCOUNTING' ,'LONDON'),
- (20 , 'RESEARCH' ,'PRESTON'),
- (30 , 'SALES' ,'LIVERPOOL'),
- (40 , 'OPERATIONS' ,'STAFFORD'),
- (50 , 'MARKETING' ,'LUTON');
查看导入的情况
- 建立EMP表格
导入数据emp
(由于文字太多,这里采用图片显示)
Sql语句
查看导入的情况
2、完成EXERCISES 1 对表格的操作,1-21题
题目:
List all information about the employees.
解析:
这个题目要求我们列出所有的员工信息
结果:
- SELECT * FROM EMP;
题目:
List all information about the departments
解析:
列出所有部门的信息
结果:
- SELECT * FROM DEPT;
题目:
List only the following information from the EMP table ( Employee name, employee number, salary, department number)
解析:
选择出特定的几个字段
结果:
- SELECT
- EMPNO, ENAME,SAL, DEPTNO
- FROM EMP;
题目:
List details of employees in departments 10 and 30.
解析:
条件查询,用or来表明逻辑关系
结果:
- SELECT *
- FROM EMP
- WHERE DEPTNO=10 or DEPTNO=30;
5.
题目:
List all the jobs in the EMP table eliminating duplicates.
解析:
使用distinct特殊字来对查找出的内容进行去重
结果:
- SELECT
- distinct JOB
- FROM EMP;
题目:
What are the names of the employees who earn less than £20,000?
解析:
(条件小于)查询
结果:
- SELECT ENAME
- FROM EMP
- WHERE SAL<20000;
7.
题目:
What is the name, job title and employee number of the person in department 20 who earns more than £25000?
解析:
And条件查询
结果:
- SELECT EMPNO, ENAME, JOB
- FROM EMP
- WHERE DEPTNO=20
- and SAL> 25000;
题目:
Find all employees whose job is either Clerk or Salesman.
解析:
or条件查询,判断字符串相等
结果:
- SELECT *
- FROM EMP
- WHERE JOB='Clerk'
- or JOB='Salesman';
题目:
Find any Clerk who is not in department 10.
解析:
and 条件查询
结果:
- SELECT *
- FROM EMP
- WHERE DEPTNO!=10
- and JOB='Clerk';
题目:
Find everyone whose job is Salesman and all the Analysts in department 20.
解析:
使用括号的多层逻辑查询
结果:
- SELECT *
- FROM EMP
- WHERE (JOB='Salesman')
- or (JOB='Analyst'and DEPTNO=20);
题目:
Find all the employees who earn between £15,000 and £20,000.
Show the employee name, department and salary.
解析:
使用 BETWEEN AND关键字进行查询,找出SAL字段在某个区间的数据
结果:
- SELECT ENAME, SAL, DEPTNO
- FROM EMP
- WHERE
- SAL BETWEEN 15000 AND 20000;
题目:
Find the name of the President.
解析:
条件查询
结果:
- SELECT ENAME
- FROM EMP
- WHERE JOB='President';
题目:
Find all the employees whose last names end with S
解析:
使用like关键字来进行特定字符结尾的查询
结果:
- SELECT *
- FROM EMP
- WHERE ENAME like '%S';
题目:
List the employees whose names have TH or LL in them
解析:
使用like关键字来进行特定字符结尾的or逻辑查询
结果:
- SELECT *
- FROM EMP
- WHERE ENAME like '%th%'
- or ENAME like '%ll%';
题目:
List only those employees who receive commission.
解析:
条件查询,判断COMM字段不为空
结果:
- SELECT *
- FROM EMP
- WHERE COMM is not null;
题目:
Find the name, job, salary, hiredate, and department number of all employees by alphabetical order of name.
解析:
将查询结果用order by关键字排序
结果:
- SELECT ENAME, JOB, HIREDATE, SAL, DEPTNO
- FROM EMP
- order by ENAME;
题目:
Find the name, job, salary, hiredate and department number of all employees in ascending order by their salaries.
解析:
将查询结果用order by关键字排序
结果:
- SELECT ENAME, JOB, HIREDATE, SAL, DEPTNO
- FROM EMP
- order by SAL asc;
题目:
List all salesmen in descending order by commission divided by their salary.
解析:
将查询结果用order by和 desc关键字降序排序
结果:
- SELECT *
- FROM EMP
- WHERE JOB='salesman'
- order by COMM/SAL
- desc;
题目:
Order employees in department 30 who receive commision, in ascending order by commission
解析:
and逻辑查询,并且将结果按comm进行升序排序
结果:
- SELECT * FROM EMP
- WHERE DEPTNO=30 and COMM is not null
- order by COMM
- asc;
题目:
Find the names, jobs, salaries and commissions of all employees who do not have managers.
解析:
判断某个字段非null,进行查询
结果:
- SELECT ENAME, JOB, SAL, COMM
- FROM EMP
- WHERE MGR is null;
题目:
Find all the salesmen in department 30 who have a salary greater than or equal to £18000.
解析:
多条件and逻辑查询,并将结果按升序排序
结果:
- SELECT *
- FROM EMP
- WHERE JOB='salesman'
- and DEPTNO=30
- and SAL>=18000
- ORDER BY COMM asc;
3、完成EXERCISES 2对表格的操作,1-6题
题目:
Find the name and salary of employees in Luton.
解析:
多表连接查询,首先利用逻辑EMP.deptno = DEPT.deptno,将两张表对应位置合并,再根据loc字段来进行查询,最终找到所有loc=Luton的雇员
结果:
- SELECT ename, sal
- FROM EMP, DEPT
- WHERE loc = 'LUTON'
- AND EMP.deptno = DEPT.deptno;
题目:
Join the DEPT table to the EMP table and show in department number order.
解析:
多表连接查询,首先利用逻辑EMP.deptno = DEPT.deptno,将两张表对应位置合并,再根据deptno字段进行排序
结果:
- SELECT empno, ename, job, mgr, hiredate, sal, comm, DEPT.deptno, DEPT.dname, DEPT.loc
- FROM EMP, DEPT
- WHERE EMP.deptno = DEPT.deptno
- ORDER BY deptno;
题目:
List the names of all salesmen who work in SALES
解析:
连接查询,查找出所有在sales工作的saleman
结果:
- SELECT ename
- FROM EMP , DEPT
- WHERE job = 'SALESMAN'
- AND dname = 'SALES';
题目:
List all departments that do not have any employees.
解析:
多级查询,首先利用DEPT.deptno = EMP.deptno语句将emp和dept两表连接,查询出deptno字段作为集合,在集合中利用deptno字段查找出没有任何雇员的部门,
结果:
- SELECT DEPT.deptno
- FROM DEPT
- WHERE deptno NOT IN (
- SELECT DEPT.deptno
- FROM DEPT, EMP
- WHERE DEPT.deptno = EMP.deptno
- );
题目:
For each employee whose salary exceeds his manager's salary, list the employee's name and salary and the manager's name and salary.
解析:
利用worker.mgr=manager.empno语句将两表连接,再根据条件进行查询worker.sal > manager.SAL
结果:
- SELECT worker.ename,worker.sal,manager.ename,manager.sal
- FROM EMP worker,EMP manager
- WHERE worker.mgr=manager.empno
- AND worker.sal > manager.SAL;
题目:
List the employees who have BLAKE as their manager.
解析:
利用worker.mgr=manager.empno语句将两表连接,再根据条件进行查询manager.ename = 'BLAKE'
结果:
- SELECT worker.*
- FROM EMP worker, EMP manager
- WHERE worker.mgr = manager.empno
- AND manager.ename = 'BLAKE';
实验结论或体会:
本次实验由于给的时间比较长,所以我一共使用两种方式完成了实验
在wampserver上完成实验
使用自己的阿里云弹性云服务器完成实验
由于在服务器上完成实验会更贴近开发中实际使用数据库的环境,于是在实验报告中我呈现的内容为在服务器上完成的
本次实验收获还是很大的,但是也遇到了一些困难
一开始的服务器配置,mysql的安装废了我不少功夫
在导入数据上也下了一番功夫,最后是采用的sql语句中的insert方式插入的
在一开始的1-21题中倒是没有遇到非常难的问题
在之后的1-6题中,个人感觉某些连接查询的部分还是会费点功夫的
例如在第二部分的第4题中查询的逻辑就比较复杂
用到了多级查询,首先利用DEPT.deptno = EMP.deptno语句将emp和dept两表连接,查询出deptno字段作为集合,在集合中利用deptno字段查找出没有任何雇员的部门,
- 创建szu_test数据库
在服务器后台命令行中使用命令登录进数据库
mysql -h localhost -u szu_test -p
查看系统中有哪些数据库,并使用szu_test数据库
一开始数据库是空的
- 建立DEPT表格
导入数据dept
Sql语句
- INSERT INTO DEPT (DEPTNO,DNAME,LOC)
- VALUES
- (10 , 'ACCOUNTING' ,'LONDON'),
- (20 , 'RESEARCH' ,'PRESTON'),
- (30 , 'SALES' ,'LIVERPOOL'),
- (40 , 'OPERATIONS' ,'STAFFORD'),
- (50 , 'MARKETING' ,'LUTON');
查看导入的情况
- 建立EMP表格
导入数据emp
(由于文字太多,这里采用图片显示)
Sql语句
查看导入的情况
2、完成EXERCISES 1 对表格的操作,1-21题
题目:
List all information about the employees.
解析:
这个题目要求我们列出所有的员工信息
结果:
- SELECT * FROM EMP;
题目:
List all information about the departments
解析:
列出所有部门的信息
结果:
- SELECT * FROM DEPT;
题目:
List only the following information from the EMP table ( Employee name, employee number, salary, department number)
解析:
选择出特定的几个字段
结果:
- SELECT
- EMPNO, ENAME,SAL, DEPTNO
- FROM EMP;
题目:
List details of employees in departments 10 and 30.
解析:
条件查询,用or来表明逻辑关系
结果:
- SELECT *
- FROM EMP
- WHERE DEPTNO=10 or DEPTNO=30;
5.
题目:
List all the jobs in the EMP table eliminating duplicates.
解析:
使用distinct特殊字来对查找出的内容进行去重
结果:
- SELECT
- distinct JOB
- FROM EMP;
题目:
What are the names of the employees who earn less than £20,000?
解析:
(条件小于)查询
结果:
- SELECT ENAME
- FROM EMP
- WHERE SAL<20000;
7.
题目:
What is the name, job title and employee number of the person in department 20 who earns more than £25000?
解析:
And条件查询
结果:
- SELECT EMPNO, ENAME, JOB
- FROM EMP
- WHERE DEPTNO=20
- and SAL> 25000;
题目:
Find all employees whose job is either Clerk or Salesman.
解析:
or条件查询,判断字符串相等
结果:
- SELECT *
- FROM EMP
- WHERE JOB='Clerk'
- or JOB='Salesman';
题目:
Find any Clerk who is not in department 10.
解析:
and 条件查询
结果:
- SELECT *
- FROM EMP
- WHERE DEPTNO!=10
- and JOB='Clerk';
题目:
Find everyone whose job is Salesman and all the Analysts in department 20.
解析:
使用括号的多层逻辑查询
结果:
- SELECT *
- FROM EMP
- WHERE (JOB='Salesman')
- or (JOB='Analyst'and DEPTNO=20);
题目:
Find all the employees who earn between £15,000 and £20,000.
Show the employee name, department and salary.
解析:
使用 BETWEEN AND关键字进行查询,找出SAL字段在某个区间的数据
结果:
- SELECT ENAME, SAL, DEPTNO
- FROM EMP
- WHERE
- SAL BETWEEN 15000 AND 20000;
题目:
Find the name of the President.
解析:
条件查询
结果:
- SELECT ENAME
- FROM EMP
- WHERE JOB='President';
题目:
Find all the employees whose last names end with S
解析:
使用like关键字来进行特定字符结尾的查询
结果:
- SELECT *
- FROM EMP
- WHERE ENAME like '%S';
题目:
List the employees whose names have TH or LL in them
解析:
使用like关键字来进行特定字符结尾的or逻辑查询
结果:
- SELECT *
- FROM EMP
- WHERE ENAME like '%th%'
- or ENAME like '%ll%';
题目:
List only those employees who receive commission.
解析:
条件查询,判断COMM字段不为空
结果:
- SELECT *
- FROM EMP
- WHERE COMM is not null;
题目:
Find the name, job, salary, hiredate, and department number of all employees by alphabetical order of name.
解析:
将查询结果用order by关键字排序
结果:
- SELECT ENAME, JOB, HIREDATE, SAL, DEPTNO
- FROM EMP
- order by ENAME;
题目:
Find the name, job, salary, hiredate and department number of all employees in ascending order by their salaries.
解析:
将查询结果用order by关键字排序
结果:
- SELECT ENAME, JOB, HIREDATE, SAL, DEPTNO
- FROM EMP
- order by SAL asc;
题目:
List all salesmen in descending order by commission divided by their salary.
解析:
将查询结果用order by和 desc关键字降序排序
结果:
- SELECT *
- FROM EMP
- WHERE JOB='salesman'
- order by COMM/SAL
- desc;
题目:
Order employees in department 30 who receive commision, in ascending order by commission
解析:
and逻辑查询,并且将结果按comm进行升序排序
结果:
- SELECT * FROM EMP
- WHERE DEPTNO=30 and COMM is not null
- order by COMM
- asc;
题目:
Find the names, jobs, salaries and commissions of all employees who do not have managers.
解析:
判断某个字段非null,进行查询
结果:
- SELECT ENAME, JOB, SAL, COMM
- FROM EMP
- WHERE MGR is null;
题目:
Find all the salesmen in department 30 who have a salary greater than or equal to £18000.
解析:
多条件and逻辑查询,并将结果按升序排序
结果:
- SELECT *
- FROM EMP
- WHERE JOB='salesman'
- and DEPTNO=30
- and SAL>=18000
- ORDER BY COMM asc;
3、完成EXERCISES 2对表格的操作,1-6题
题目:
Find the name and salary of employees in Luton.
解析:
多表连接查询,首先利用逻辑EMP.deptno = DEPT.deptno,将两张表对应位置合并,再根据loc字段来进行查询,最终找到所有loc=Luton的雇员
结果:
- SELECT ename, sal
- FROM EMP, DEPT
- WHERE loc = 'LUTON'
- AND EMP.deptno = DEPT.deptno;
题目:
Join the DEPT table to the EMP table and show in department number order.
解析:
多表连接查询,首先利用逻辑EMP.deptno = DEPT.deptno,将两张表对应位置合并,再根据deptno字段进行排序
结果:
- SELECT empno, ename, job, mgr, hiredate, sal, comm, DEPT.deptno, DEPT.dname, DEPT.loc
- FROM EMP, DEPT
- WHERE EMP.deptno = DEPT.deptno
- ORDER BY deptno;
题目:
List the names of all salesmen who work in SALES
解析:
连接查询,查找出所有在sales工作的saleman
结果:
- SELECT ename
- FROM EMP , DEPT
- WHERE job = 'SALESMAN'
- AND dname = 'SALES';
题目:
List all departments that do not have any employees.
解析:
多级查询,首先利用DEPT.deptno = EMP.deptno语句将emp和dept两表连接,查询出deptno字段作为集合,在集合中利用deptno字段查找出没有任何雇员的部门,
结果:
- SELECT DEPT.deptno
- FROM DEPT
- WHERE deptno NOT IN (
- SELECT DEPT.deptno
- FROM DEPT, EMP
- WHERE DEPT.deptno = EMP.deptno
- );
题目:
For each employee whose salary exceeds his manager's salary, list the employee's name and salary and the manager's name and salary.
解析:
利用worker.mgr=manager.empno语句将两表连接,再根据条件进行查询worker.sal > manager.SAL
结果:
- SELECT worker.ename,worker.sal,manager.ename,manager.sal
- FROM EMP worker,EMP manager
- WHERE worker.mgr=manager.empno
- AND worker.sal > manager.SAL;
题目:
List the employees who have BLAKE as their manager.
解析:
利用worker.mgr=manager.empno语句将两表连接,再根据条件进行查询manager.ename = 'BLAKE'
结果:
- SELECT worker.*
- FROM EMP worker, EMP manager
- WHERE worker.mgr = manager.empno
- AND manager.ename = 'BLAKE';
实验结论或体会:
本次实验由于给的时间比较长,所以我一共使用两种方式完成了实验
在wampserver上完成实验
使用自己的阿里云弹性云服务器完成实验
由于在服务器上完成实验会更贴近开发中实际使用数据库的环境,于是在实验报告中我呈现的内容为在服务器上完成的
本次实验收获还是很大的,但是也遇到了一些困难
一开始的服务器配置,mysql的安装废了我不少功夫
在导入数据上也下了一番功夫,最后是采用的sql语句中的insert方式插入的
在一开始的1-21题中倒是没有遇到非常难的问题
在之后的1-6题中,个人感觉某些连接查询的部分还是会费点功夫的
例如在第二部分的第4题中查询的逻辑就比较复杂
用到了多级查询,首先利用DEPT.deptno = EMP.deptno语句将emp和dept两表连接,查询出deptno字段作为集合,在集合中利用deptno字段查找出没有任何雇员的部门,