重新学习数据库(2)
数据库学习
单元概述
通过本章的学习能够了解MySQL数据库中多表查询的含义,掌握多表查询的基本写法,掌握特殊的多表查询的写法,掌握内连接和外连接的区别
测试数据脚本
drop TABLE emp;
drop TABLE dept;
drop TABLE salgrade;
create table dept
(
deptno integer(4) not null,
dname varchar(14),
loc varchar(13)
);
alter table dept add constraint pk_dept primary key (deptno);
create table emp
(
empno integer(4) not null,
ename varchar(10),
job varchar(9),
mgr integer(4),
hiredate date,
sal decimal(7,2),
comm decimal(7,2),
deptno integer(2)
);
alter table emp add constraint pk_emp primary key (empno);
alter table emp add constraint fk_deptno foreign key (deptno) references dept (deptno);
create table salgrade
(
grade integer(1),
losal decimal(7,2),
hisal decimal(7,2)
);
insert into DEPT (DEPTNO, DNAME, LOC) values (10, 'ACCOUNTING', 'NEW YORK');
insert into DEPT (DEPTNO, DNAME, LOC) values (20, 'RESEARCH', 'DALLAS');
insert into DEPT (DEPTNO, DNAME, LOC) values (30, 'SALES', 'CHICAGO');
insert into DEPT (DEPTNO, DNAME, LOC) values (40, 'OPERATIONS', 'BOSTON');
insert into SALGRADE (GRADE, LOSAL, HISAL) values (1, 700, 1200);
insert into SALGRADE (GRADE, LOSAL, HISAL) values (2, 1201, 1400);
insert into SALGRADE (GRADE, LOSAL, HISAL) values (3, 1401, 2000);
insert into SALGRADE (GRADE, LOSAL, HISAL) values (4, 2001, 3000);
insert into SALGRADE (GRADE, LOSAL, HISAL) values (5, 3001, 9999);
-- 下面要特别注意,%d-%m-%Y 里的 d 和 m 一定要小写
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7369, 'SMITH', 'CLERK', 7902, STR_TO_DATE('17-12-1980', '%d-%m-%Y'), 800.00, null, 20);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7499, 'ALLEN', 'SALESMAN', 7698, STR_TO_DATE('20-02-1981', '%d-%m-%Y'), 1600.00, 300.00, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7521, 'WARD', 'SALESMAN', 7698, STR_TO_DATE('22-02-1981', '%d-%m-%Y'), 1250.00, 500.00, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7566, 'JONES', 'MANAGER', 7839, STR_TO_DATE('02-04-1981', '%d-%m-%Y'), 2975.00, null, 20);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7654, 'MARTIN', 'SALESMAN', 7698, STR_TO_DATE('28-09-1981', '%d-%m-%Y'), 1250.00, 1400.00, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7698, 'BLAKE', 'MANAGER', 7839, STR_TO_DATE('01-05-1981', '%d-%m-%Y'), 2850.00, null, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7782, 'CLARK', 'MANAGER', 7839, STR_TO_DATE('09-06-1981', '%d-%m-%Y'), 2450.00, null, 10);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7788, 'SCOTT', 'ANALYST', 7566, STR_TO_DATE('19-04-1987', '%d-%m-%Y'), 3000.00, null, 20);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7839, 'KING', 'PRESIDENT', null, STR_TO_DATE('17-11-1981', '%d-%m-%Y'), 5000.00, null, 10);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7844, 'TURNER', 'SALESMAN', 7698, STR_TO_DATE('08-09-1981', '%d-%m-%Y'), 1500.00, 0.00, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7876, 'ADAMS', 'CLERK', 7788, STR_TO_DATE('23-05-1987', '%d-%m-%Y'), 1100.00, null, 20);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7900, 'JAMES', 'CLERK', 7698, STR_TO_DATE('03-12-1981', '%d-%m-%Y'), 950.00, null, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7902, 'FORD', 'ANALYST', 7566, STR_TO_DATE('03-12-1981', '%d-%m-%Y'), 3000.00, null, 20);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7934, 'MILLER', 'CLERK', 7782, STR_TO_DATE('23-01-1982', '%d-%m-%Y'), 1300.00, null, 10);
单元练习
1. 写一个查询,显示所有员工姓名,部门编号,部门名称。
SELECT e.ename,e.deptno,d.dname
FROM emp e, dept d
WHERE e.deptno = d.deptno;
2. 写一个查询,显示所有工作在CHICAGO并且奖金不为空的员工姓名,工作地点,奖金
SELECT e.ename,d.loc,e.comm
FROM emp e,dept d
WHERE e.deptno = d.deptno
AND e.comm is not NULL
AND d.loc = 'CHICAGO';
3. 写一个查询,显示所有姓名中含有A字符的员工姓名,工作地点。
SELECT e.ename,d.loc
FROM emp e, dept d
WHERE e.deptno =d.deptno
AND ename LIKE '%A%';
1.查询每个员工的编号,姓名,工资,工资等级,所在工作城市,按照工资等级进行升序排序
SELECT e.empno,e.ename,e.sal,s.grade,d.loc
FROM emp e,dept d,salgrade s
WHERE e.deptno = d.deptno
AND (e.sal BETWEEN s.losal AND s.hisal)
ORDER BY grade ASC;
1.查询所有工作在NEW YORK和CHICAGO的员工姓名,员工编号,以及他们的经理姓名,经理编号
SELECT worker.ename 'ENAME',worker.empno 'ENO',manager.ename 'MNAME',manager.empno 'MNO'
FROM emp worker,emp manager,dept d
WHERE worker.mgr = manager.empno
AND worker.deptno = d.deptno
AND d.loc IN ('NEW YORK','CHICAGO');
1. 创建一个员工表和部门表的交叉连接。
select * from emp cross join dept;
2. 使用自然连接,显示入职日期在80年5月1日之后的员工姓名,部门名称,入职日期
SELECT e.ename,e.deptno,e.hiredate
FROM emp e NATURAL JOIN dept d
WHERE hiredate >'1980-05-01';
3. 使用左连接,查询每个员工的姓名,经理姓名,没有经理的King也要显示出来。
SELECT worker.ename 'WNAME',manager.ename 'MNAME'
FROM emp worker
LEFT OUTER JOIN emp manager
ON (worker.mgr = manager.empno);
4. 使用右连接,查询每个员工的姓名,经理姓名,没有经理的King也要显示出来
SELECT worker.ename 'WNAME',manager.ename 'MNAME'
FROM emp manager
RIGHT OUTER JOIN emp worker
ON (worker.mgr = manager.empno);
课后练习
1. 显示员工SMITH的姓名,部门名称,直接上级名称
SELECT worker.ename 'WNAME',d.dname 'DNAME',manager.ename 'MNAME'
FROM emp worker,dept d,emp manager
WHERE worker.mgr = manager.empno
AND worker.deptno = d.deptno
AND worker.ename = 'SMITH';
2. 显示员工姓名,部门名称,工资,工资级别,要求工资级别大于4级。
SELECT e.ename,d.dname,e.sal,s.grade
FROM emp e, dept d,salgrade s
WHERE e.deptno = d.deptno
AND(e.sal BETWEEN s.losal AND s.hisal)
AND s.grade > 4;
3. 显示员工KING和FORD管理的员工姓名及其经理姓名。
SELECT e.ename "员工姓名",m.ename "经理姓名"
FROM emp e,emp m
WHERE e.mgr=m.empno
AND m.ename ='KING' or e.mgr=m.empno and m.ename ='FORD';
4. 显示员工姓名,参加工作时间,经理名,参加工作时间,要求参加时间比经理早
SELECT e.ename "员工姓名", e.hiredate "参加工作时间" ,m.ename "经理姓名", m.hiredate "经理参加工作时间"
FROM emp e,emp m
WHERE e.mgr=m.empno
总结
对等值连接、不等值连接、自身连接、外连接有了更深刻的理解,查询语句写得更加熟练了