目录
前言
数据表结构
数据库文件(按照顺序导入)
先导入del_data.sql
再导入
hr_cre.sql
最后导入hr_popul.sql
提取码:69z3
1基本SQL-SELECT
1.1基本SQL-SELECT语句笔记
笔记
1. 对于日期型数据, 做 *, / 运算不合法
2. 包含空值的数学表达式的值都为空值
3. 别名使用双引号!
4. oracle 中连接字符串使用 "||", 而不是 java 中的 "+"
5. 日期和字符只能在单引号中出现. 输出 last_name`s email is email
select last_name || ' `s email is ' || email EMAIL
from employees
6. distinct 关键字, 以下语法错误
select last_name, distinct department_id
from employees
1.2 基本SQL-SELECT语句练习
练习
1. SQL*PLUS命令可以控制数据库吗? 否!
2. 下面的语句是否可以执行成功 可以
select last_name , job_id , salary as sal
from employees;
3. 下面的语句是否可以执行成功 可以
select * from employees;
4. 找出下面语句中的错误 标点符号需要是英文格式下的。
select employee_id , last_name,
salary * 12 “ANNUAL SALARY”
from employees;
5. 显示表departments的结构,并查询其中的全部数据
desc departments;
select * from departments;
6. 显示出表employees中的全部job_id(不能重复)
Select distinct job_id from employees;
7. 显示出表employees的全部列,各个列之间用逗号连接,列头显示成OUT_PUT
a) select employee_id ||','|| last_name||','||salary "OUT_PUT"
b) from employees
2过滤和排序数据
2.1过滤和排序数据笔记
笔记
7. WHERE 子句紧随 FROM 子句
8. 查询 last_name 为 'King' 的员工信息
错误1: King 没有加上 单引号
select first_name, last_name
from employees
where last_name = King
错误2: 在单引号中的值区分大小写
select first_name, last_name
from employees
where last_name = 'king'
正确
select first_name, last_name
from employees
where last_name = 'King'
9. 查询 1998-4-24 来公司的员工有哪些?
注意: 日期必须要放在单引号中, 且必须是指定的格式
select last_name, hire_date
from employees
where hire_date = '24-4月-1998'
10. 查询工资在 5000 -- 10000 之间的员工信息.
1). 使用 AND
select *
from employees
where salary >= 5000 and salary <= 10000
2). 使用 BETWEEN .. AND .., 注意: 包含边界!!
select *
from employees
where salary between 5000 and 10000
11. 查询工资等于 6000, 7000, 8000, 9000, 10000 的员工信息
1). 使用 OR
select *
from employees
where salary = 6000 or salary = 7000 or salary = 8000 or salary = 9000 or salary = 10000
2). 使用 IN
select *
from employees
where salary in (6000, 7000, 8000, 9000, 10000)
12. 查询 LAST_NAME 中有 'o' 字符的所有员工信息.
select *
from employees
where last_name like '%o%'
13. 查询 LAST_NAME 中第二个字符是 'o' 的所有员工信息.
select *
from employees
where last_name like '_o%'
14. 查询 LAST_NAME 中含有 '_' 字符的所有员工信息
1). 准备工作:
update employees
set last_name = 'Jones_Tom'
where employee_id = 195
2). 使用 escape 说明转义字符.
select *
from employees
where last_name like '%\_%' escape '\'
15. 查询 COMMISSION_PCT 字段为空的所有员工信息
select last_name, commission_pct
from employees
where commission_pct is null
16. 查询 COMMISSION_PCT 字段不为空的所有员工信息
select last_name, commission_pct
from employees
where commission_pct is not null
17. ORDER BY:
1). 若查询中有表达式运算, 一般使用别名排序
2). 按多个列排序: 先按第一列排序, 若第一列中有相同的, 再按第二列排序.
格式: ORDER BY 一级排序列 ASC/DESC,二级排序列 ASC/DESC;
2.2过滤和排序数据练习
测 试
1. 查询工资大于12000的员工姓名和工资
a) select last_name,salary
b) from employees
c) where salary > 12000
2. 查询员工号为176的员工的姓名和部门号
a) select last_name,department_id
b) from employees
c) where employee_id = 176
3. 选择工资不在5000到12000的员工的姓名和工资
a) select last_name,salary
b) from employees
c) --where salary < 5000 or salary > 12000
d) where salary not between 5000 and 12000
4. 选择雇用时间在1998-02-01到1998-05-01之间的员工姓名,job_id和雇用时间
a) select last_name,job_id,hire_date
b) from employees
c) --where hire_date between '1-2月-1998' and '1-5月-1998'
d) where to_char(hire_date,'yyyy-mm-dd') between '1998-02-01' and '1998-05-01'
5. 选择在20或50号部门工作的员工姓名和部门号
a) select last_name,department_id
b) from employees
c) where department_id = 20 or department_id = 50
d) --where department_id in (20,50)
6. 选择在1994年雇用的员工的姓名和雇用时间
a) select last_name,hire_date
b) from employees
c) --where hire_date like '%94'
d) where to_char(hire_date,'yyyy') = '1994'
7. 选择公司中没有管理者的员工姓名及job_id
a) select last_name,job_id
b) from employees
c) where manager_id is null
8. 选择公司中有奖金的员工姓名,工资和奖金级别
a) select last_name,salary,commission_pct
b) from employees
c) where commission_pct is not null
9. 选择员工姓名的第三个字母是a的员工姓名
a) select last_name
b) from employees
c) where last_name like '__a%'
10. 选择姓名中有字母a和e的员工姓名
a) select last_name
b) from employees
c) where last_name like '%a%e%' or last_name like '%e%a%'
3单行函数
3.1单行函数笔记
18. 打印出 "2009年10月14日 9:25:40" 格式的当前系统的日期和时间.
select to_char(sysdate, 'YYYY"年"MM"月"DD"日" HH:MI:SS')
from dual
注意: 使用双引号向日期中添加字符
19. 格式化数字: 1234567.89 为 1,234,567.89
select to_char(1234567.89, '999,999,999.99')
from dual
20. 字符串转为数字时
1). 若字符串中没有特殊字符, 可以进行隐式转换:
select '1234567.89' + 100
from dual
2). 若字符串中有特殊字符, 例如 '1,234,567.89', 则无法进行隐式转换, 需要使用 to_number() 来完成
select to_number('1,234,567.89', '999,999,999.99') + 100
from dual
21. 对于把日期作为查询条件的查询, 一般都使用 to_date() 把一个字符串转为日期,
这样可以不必关注日期格式
select last_name, hire_date
from employees
where hire_date = to_date('1998-5-23', 'yyyy-mm-dd')
-- where to_char(hire_date,'yyyy-mm-dd') = '1998-5-23'
22. 转换函数: to_char(), to_number(), to_date()
23. 查询每个月倒数第 2 天入职的员工的信息.
select last_name, hire_date
from employees
where hire_date = last_day(hire_date) - 1
24. 计算公司员工的年薪
--错误写法: 因为空值计算的结果还是空值
select last_name, salary * 12 * (1 + commission_pct) year_sal
from employees
--正确写法
select last_name, salary * 12 * (1 + nvl(commission_pct, 0)) year_sal
from employees
25. 查询部门号为 10, 20, 30 的员工信息, 若部门号为 10, 则打印其工资的 1.1 倍,
20 号部门, 则打印其工资的 1.2 倍, 30 号部门打印其工资的 1.3 倍数
--使用 case-when-then-else-end
select last_name, department_id, salary, case department_id when 10 then salary * 1.1
when 20 then salary * 1.2
when 30 then salary * 1.3
end new_sal
from employees
where department_id in (10, 20, 30)
--使用 decode
select last_name, department_id, salary, decode(department_id, 10, salary * 1.1,
20, salary * 1.2,
30, salary * 1.3
) new_sal
from employees
where department_id in (10, 20, 30)
3.2单行函数练习
测 试
1. 显示系统时间(注:日期+时间)
a) select to_char(sysdate,'yyyy-mm-dd hh:mi:ss')
b) from dual
2. 查询员工号,姓名,工资,以及工资提高百分之20%后的结果(new salary)
a) select employee_id,last_name,salary,salary*1.2 "new salary"
b) from employees
3. 将员工的姓名按首字母排序,并写出姓名的长度(length)
a) select last_name,length(last_name)
b) from employees
c) order by last_name asc
4. 查询各员工的姓名,并显示出各员工在公司工作的月份数(worked_month)。
a) select last_name,hire_date,round(months_between(sysdate,hire_date),1) workded_month
b) from employees
5. 查询员工的姓名,以及在公司工作的月份数(worked_month),并按月份数降序排列
a) Select last_name,hire_date,round(months_between(sysdate,hire_date),1) workded_month
b) from employees
c) order by workded_month desc
6. 做一个查询,产生下面的结果
<last_name> earns <salary> monthly but wants <salary*3>
Dream Salary
King earns $24000 monthly but wants $72000
select last_name || ' earns '|| to_char(salary,'$999999')||' monthly,but wants '||to_char(3*salary,'$999999') "Dream Salary"
from employees
7. 使用decode函数,按照下面的条件:
job grade
AD_PRES A
ST_MAN B
IT_PROG C
SA_REP D
ST_CLERK E
产生下面的结果
Last_name Job_id Grade
king AD_PRES A
select last_name "Last_name",job_id "Job_id",
decode(job_id,'AD_PRES','A','ST_MAN','B', 'IT_PROG','C', 'SA_REP','D', 'ST_CLERK','E') "Grade"
from employees
8. 将第7题的查询用case函数再写一遍。
a) select last_name "Last_name",job_id "Job_id",
case job_id when 'AD_PRES'then 'A'
b) when 'ST_MAN' then 'B'
c) when 'IT_PROG' then 'C'
d) when 'SA_REP' then 'D'
e) when 'ST_CLERK' then'E' end "Grade"
f) from employees
4多表查询
4.1多表查询笔记
26. 多表连接查询时, 若两个表有同名的列, 必须使用表的别名对列名进行引用, 否则出错!
27. 查询出公司员工的 last_name, department_name, city
select last_name, department_name, city
from departments d, employees e, locations l
where d.department_id = e.department_id and d.location_id = l.location_id
28. 查询出 last_name 为 'Chen' 的 manager 的信息. (员工的 manager_id 是某员工的 employee_id)
0). 例如: 老张的员工号为: "1001", 我的员工号为: "1002",
我的 manager_id 为 "1001" --- 我的 manager 是"老张"
1). 通过两条 sql 查询:
select manager_id
from employees
where lower(last_name) = 'chen' --返回的结果为 108
select *
from employees
where employee_id = 108
2). 通过一条 sql 查询(自连接):
select m.*
from employees e, employees m
where e.manager_id = m.employee_id and e.last_name = 'Chen'
3). 通过一条 sql 查询(子查询):
select *
from employees
where employee_id = (
select manager_id
from employees
where last_name = 'Chen'
)
29. 查询每个员工的 last_name 和 GRADE_LEVEL(在 JOB_GRADES 表中). ---- 非等值连接
select last_name, salary, grade_level, lowest_sal, highest_sal
from employees e, job_grades j
where e.salary >= j.lowest_sal and e.salary <= j.highest_sal
30. 左外连接和右外连接
select last_name, e.department_id, department_name
from employees e, departments d
where e.department_id = d.department_id(+)
select last_name, d.department_id, department_name
from employees e, departments d
where e.department_id(+) = d.department_id
理解 "(+)" 的位置: 以左外连接为例, 因为左表需要返回更多的记录,
右表就需要 "加上" 更多的记录, 所以在右表的链接条件上加上 "(+)"
注意: 1). 两边都加上 "(+)" 符号, 会发生语法错误!
2). 这种语法为 Oracle 所独有, 不能在其它数据库中使用.
31. SQL 99 连接 Employees 表和 Departments 表
1).
select *
from employees join departments
using(department_id)
缺点: 要求两个表中必须有一样的列名.
2).
select *
from employees e join departments d
on e.department_id = d.department_id
3).多表连接
select e.last_name, d.department_name, l.city
from employees e join departments d
on e.department_id = d.department_id
join locations l
on d.location_id = l.location_id
32. SQL 99 的左外连接, 右外连接, 满外连接
1).
select last_name, department_name
from employees e left outer join departments d
on e.department_id = d.department_id
2).
select last_name, department_name
from employees e right join departments d
on e.department_id = d.department_id
3).
select last_name, department_name
from employees e full join departments d
on e.department_id = d.department_id
4.2多表查询练习
测 试
1. 显示所有员工的姓名,部门号和部门名称。
a) select last_name,e.department_id,department_name
b) from employees e,departments d
c) where e.department_id = d.department_id(+)
方法二:
select last_name,e.department_id,department_name
from employees e left outer join departments d
on e.department_id = d.department_id
2. 查询90号部门员工的job_id和90号部门的location_id
a) select distinct job_id,location_id
b) from employees e left outer join departments d
c) on e.department_id = d.department_id
d) where d.department_id = 90
3. 选择所有有奖金的员工的
last_name , department_name , location_id , city
select last_name,department_name,d.location_id,city
from employees e join departments d
on e.department_id = d.department_id
join locations l
on d.location_id = l.location_id
where e.commission_pct is not null
4. 选择city在Toronto工作的员工的
last_name , job_id , department_id , department_name
select last_name , job_id , e.department_id , department_name
from employees e ,departments d,locations l
where e.department_id = d.department_id and l.city = 'Toronto' and d.location_id = l.location_id
5. 选择指定员工的姓名,员工号,以及他的管理者的姓名和员工号,结果类似于下面的格式
employees Emp# manager Mgr#
kochhar 101 king 100
select e1.last_name "employees",e1.employee_id "Emp#",e2.last_name"Manger",e2.employee_id "Mgr#"
from employees e1,employees e2
where e1.manager_id = e2.employee_id(+)
5分组函数
5.1分组函数笔记
33. 查询 employees 表中有多少个部门
select count(distinct department_id)
from employees
34. 查询全公司奖金基数的平均值(没有奖金的人按 0 计算)
select avg(nvl(commission_pct, 0))
from employees
35. 查询各个部门的平均工资
--错误: avg(salary) 返回公司平均工资, 只有一个值; 而 department_id 有多个值, 无法匹配返回
select department_id, avg(salary)
from employees
**在 SELECT 列表中所有未包含在组函数中的列都应该包含在 GROUP BY 子句中
--正确: 按 department_id 进行分组
select department_id, avg(salary)
from employees
group by department_id
36. Toronto 这个城市的员工的平均工资
SELECT avg(salary)
FROM employees e JOIN departments d
ON e.department_id = d.department_id
JOIN locations l
ON d.location_id = l.location_id
WHERE city = 'Toronto'
37. (有员工的城市)各个城市的平均工资
SELECT city, avg(salary)
FROM employees e JOIN departments d
ON e.department_id = d.department_id
JOIN locations l
ON d.location_id = l.location_id
GROUP BY city
38. 查询平均工资高于 8000 的部门 id 和它的平均工资.
SELECT department_id, avg(salary)
FROM employees e
GROUP BY department_id
HAVING avg(salary) > 8000
39. 查询平均工资高于 6000 的 job_title 有哪些
SELECT job_title, avg(salary)
FROM employees e join jobs j
ON e.job_id = j.job_id
GROUP BY job_title
HAVING avg(salary) > 6000
5.2分组函数练习
测 试
1. 组函数处理多行返回一行吗?
是
2. 组函数不计算空值吗?
是
3. where子句可否使用组函数进行过滤?
不可以,用having替代
4. 查询公司员工工资的最大值,最小值,平均值,总和
a) select max(salary),min(salary),avg(salary),sum(salary)
b) from employees
5. 查询各job_id的员工工资的最大值,最小值,平均值,总和
a) select job_id,max(salary),min(salary),avg(salary),sum(salary)
b) from employees
c) group by job_id
6. 选择具有各个job_id的员工人数
a) select job_id,count(employee_id)
b) from employees
c) group by job_id
7. 查询员工最高工资和最低工资的差距(DIFFERENCE)
a) select max(salary),min(salary),max(salary)-min(salary) "DIFFERENCE"
b) from employees
8. 查询各个管理者手下员工的最低工资,其中最低工资不能低于6000,没有管理者的员工不计算在内
a) select manager_id,min(salary)
b) from employees
c) where manager_id is not null
d) group by manager_id
e) having min(salary) >= 6000
9. 查询所有部门的名字,location_id,员工数量和工资平均值
a) select department_name,location_id,count(employee_id),avg(salary)
b) from employees e right outer join departments d
c) on e.department_id = d.department_id
d) group by department_name,location_id
10. 查询公司在1995-1998年之间,每年雇用的人数,结果类似下面的格式
total 1995 1996 1997 1998
20 3 4 6 7
select count(*) "total",
count(decode(to_char(hire_date,'yyyy'),'1995',1,null)) "1995",
count(decode(to_char(hire_date,'yyyy'),'1996',1,null)) "1996",
count(decode(to_char(hire_date,'yyyy'),'1997',1,null)) "1997",
count(decode(to_char(hire_date,'yyyy'),'1998',1,null)) "1998"
from employees
where to_char(hire_date,'yyyy') in ('1995','1996','1997','1998')
6子查询
6.1子查询笔记
40. 谁的工资比 Abel 高?
1). 写两条 SQL 语句.
SELECT salary
FROM employees
WHERE last_name = 'Abel'
--返回值为 11000
SELECT last_name, salary
FROM employees
WHERE salary > 11000
2). 使用子查询 -- 一条 SQL 语句
SELECT last_name, salary
FROM employees
WHERE salary > (
SELECT salary
FROM employees
WHERE last_name = 'Abel'
)
子查询注意:
1). 子查询要包含在括号内
2). 将子查询放在比较条件的右侧
41. 查询工资最低的员工信息: last_name, salary
42. 查询平均工资最低的部门信息
43*. 查询平均工资最低的部门信息和该部门的平均工资
44. 查询平均工资最高的 job 信息
45. 查询平均工资高于公司平均工资的部门有哪些?
46. 查询出公司中所有 manager 的详细信息.
47. 各个部门中 最高工资中最低的那个部门的 最低工资是多少
48. 查询平均工资最高的部门的 manager 的详细信息: last_name, department_id, email, salary
49. 查询 1999 年来公司的人所有员工的最高工资的那个员工的信息.
/*************************************************************************************************/
41. 查询工资最低的员工信息: last_name, salary
SELECT last_name, salary
FROM employees
WHERE salary = (
SELECT min(salary)
FROM employees
)
42. 查询平均工资最低的部门信息
SELECT *
FROM departments
WHERE department_id = (
SELECT department_id
FROM employees
GROUP BY department_id
HAVING avg(salary) = (
SELECT min(avg(salary))
FROM employees
GROUP BY department_id
)
)
43. 查询平均工资最低的部门信息和该部门的平均工资
select d.*, (select avg(salary) from employees where department_id = d.department_id)
from departments d
where d.department_id = (
SELECT department_id
FROM employees
GROUP BY department_id
HAVING avg(salary) = (
SELECT min(avg(salary))
FROM employees
GROUP BY department_id
)
)
44. 查询平均工资最高的 job 信息
1). 按 job_id 分组, 查询最高的平均工资
SELECT max(avg(salary))
FROM employees
GROUP BY job_id
2). 查询出平均工资等于 1) 的 job_id
SELECT job_id
FROM employees
GROUP BY job_id
HAVING avg(salary) = (
SELECT max(avg(salary))
FROM employees
GROUP BY job_id
)
3). 查询出 2) 对应的 job 信息
SELECT *
FROM jobs
WHERE job_id = (
SELECT job_id
FROM employees
GROUP BY job_id
HAVING avg(salary) = (
SELECT max(avg(salary))
FROM employees
GROUP BY job_id
)
)
45. 查询平均工资高于公司平均工资的部门有哪些?
1). 查询出公司的平均工资
SELECT avg(salary)
FROM employees
2). 查询平均工资高于 1) 的部门 ID
SELECT department_id
FROM employees
GROUP BY department_id
HAVING avg(salary) > (
SELECT avg(salary)
FROM employees
)
46. 查询出公司中所有 manager 的详细信息.
1). 查询出所有的 manager_id
SELECT distinct manager_id
FROM employeess
2). 查询出 employee_id 为 1) 查询结果的那些员工的信息
SELECT employee_id, last_name
FROM employees
WHERE employee_id in (
SELECT distinct manager_id
FROM employees
)
47. 各个部门中 最高工资中最低的那个部门的 最低工资是多少
1). 查询出各个部门的最高工资
SELECT max(salary)
FROM employees
GROUP BY department_id
2). 查询出 1) 对应的查询结果的最低值: 各个部门中最低的最高工资(无法查询对应的 department_id)
SELECT min(max(salary))
FROM employees
GROUP BY department_id
3). 查询出 2) 所对应的部门 id 是多少: 各个部门中最高工资等于 2) 的那个部门的 id
SELECT department_id
FROM employees
GROUP BY department_id
HAVING max(salary) = (
SELECT min(max(salary))
FROM employees
GROUP BY department_id
)
4). 查询出 3) 所在部门的最低工资
SELECT min(salary)
FROM employees
WHERE department_id = (
SELECT department_id
FROM employees
GROUP BY department_id
HAVING max(salary) = (
SELECT min(max(salary))
FROM employees
GROUP BY department_id
)
)
48. 查询平均工资最高的部门的 manager 的详细信息: last_name, department_id, email, salary
1). 各个部门中, 查询平均工资最高的平均工资是多少
SELECT max(avg(salary))
FROM employees
GROUP BY department_id
2). 各个部门中, 平均工资等于 1) 的那个部门的部门号是多少
SELECT department_id
FROM employees
GROUP BY department_id
HAVING avg(salary) = (
SELECT max(avg(salary))
FROM employees
GROUP BY department_id
)
3). 查询出 2) 对应的部门的 manager_id
SELECT manager_id
FROM departments
WHERE department_id = (
SELECT department_id
FROM employees
GROUP BY department_id
HAVING avg(salary) = (
SELECT max(avg(salary))
FROM employees
GROUP BY department_id
)
)
4). 查询出 employee_id 为 3) 查询的 manager_id 的员工的 last_name, department_id, email, salary
SELECT last_name, department_id, email, salary
FROM employees
WHERE employee_id = (
SELECT manager_id
FROM departments
WHERE department_id = (
SELECT department_id
FROM employees
GROUP BY department_id
HAVING avg(salary) = (
SELECT max(avg(salary))
FROM employees
GROUP BY department_id
)
)
)
49. 查询 1999 年来公司的人所有员工的最高工资的那个员工的信息.
1). 查询出 1999 年来公司的所有的员工的 salary
SELECT salary
FROM employees
WHERE to_char(hire_date, 'yyyy') = '1999'
2). 查询出 1) 对应的结果的最大值
SELECT max(salary)
FROM employees
WHERE to_char(hire_date, 'yyyy') = '1999'
3). 查询工资等于 2) 对应的结果且 1999 年入职的员工信息
SELECT *
FROM employees
WHERE to_char(hire_date, 'yyyy') = '1999' AND salary = (
SELECT max(salary)
FROM employees
WHERE to_char(hire_date, 'yyyy') = '1999'
)
50. 多行子查询的 any 和 all
select department_id
from employees
group by department_id
having avg(salary) >= any(
--所有部门的平均工资
select avg(salary)
from employees
group by department_id
)
any 和任意一个值比较, 所以其条件最为宽松, 所以实际上只需和平均工资最低的比较, 返回所有值
而 all 是和全部的值比较, 条件最为苛刻, 所以实际上返回的只需和平均工资最高的比较, 所以返回
平均工资最高的 department_id
6.2子查询练习
测 试
1. 查询和Zlotkey相同部门的员工姓名和雇用日期
a) select last_name,hire_date
b) from employees
c) where department_id = (
d) select department_id
e) from employees
f) where last_name = 'Zlotkey'
g) )
h) and last_name <> 'Zlotkey'
2. 查询工资比公司平均工资高的员工的员工号,姓名和工资。
a) select last_name,employee_id,salary
b) from employees
c) where salary > (select avg(salary)
d) from employees)
3. 查询各部门中工资比本部门平均工资高的员工的员工号, 姓名和工资
a) select employee_id,last_name,salary
b) from employees e1
c) where salary > (
d) select avg(salary)
e) from employees e2
f) where e1.department_id = e2.department_id
g) group by department_id
h) )
4. 查询和姓名中包含字母u的员工在相同部门的员工的员工号和姓名
a) select employee_id,last_name
b) from employees
c) where department_id in (
d) select department_id
e) from employees
f) where last_name like '%u%'
g) )
h) and last_name not like '%u%'
5. 查询在部门的location_id为1700的部门工作的员工的员工号
select employee_id
from employees
where department_id in (
select department_id
from departments
where location_id = 1700
)
6.查询管理者是King的员工姓名和工资
select last_name,salary
from employees
where manager_id in (
select employee_id
from employees
where last_name = 'King'
)
7创建和管理表
7.1创建和管理表笔记
51. 利用子查询创建表 myemp,
该表中包含 employees 表的 employee_id(id), last_name(name), salary(sal), email 字段
1). 创建表的同时复制 employees 对应的记录
create table myemp
as
select employee_id id, last_name name, salary sal, email from employees
2). 创建表的同时不包含 employees 中的记录, 即创建一个空表
create table myemp
as
select employee_id id, last_name name, salary sal, email from employees where 1 = 2
52. 对现有的表进行修改操作
1). 添加一个新列
ALTER TABLE myemp
ADD(age number(3))
2). 修改现有列的类型
ALTER TABLE myemp
MODIFY(name varchar2(30));
3). 修改现有列的名字
ALTER TABLE myemp
RENAME COLUMN sal TO salary;
4). 删除现有的列
ALTER TABLE myemp
DROP COLUMN age;
53. 清空表(截断: truncate), 不能回滚!!
54.
1). 创建一个表, 该表和 employees 有相同的表结构, 但为空表:
create table emp2 as select * from employees where 1 = 2;
2). 把 employees 表中 80 号部门的所有数据复制到 emp2 表中:
insert into emp2 select * from employees where department_id = 80;
7.2创建和管理表练习
测 试
1. 创建表dept1
name Null? type
id Number(7)
name Varchar2(25)
create table dept1(
id number(7),
name varchar2(25)
)
2. 将表departments中的数据插入新表dept2中
a) create table dept2
b) as
c) select * from departments
3. 创建表emp5
name Null? type
id Number(7)
First_name Varchar2(25)
Last_name Varchar2(25)
Dept_id Number(7)
create table emp5(
id number(7),
first_name varchar2(25),
last_name varchar2(25),
dept_id number(7)
)
4. 将列Last_name的长度增加到50
a) alter table emp5
b) modify (last_name varchar2(50))
5. 根据表employees创建employees2
a) create table employees2
b) as
c) select * from employees
6. 删除表emp5
drop table emp5;
7. 将表employees2重命名为emp5
rename employees2 to emp5
8. 在表dept和emp5中添加新列test_column,并检查所作的操作
alter table dept
add(test_column number(10));
desc dept;
9. 在表dept和emp5中将列test_column设置成不可用,之后删除
a) alter table emp5
b) set unused column test_column
alter table emp5
drop unused columns
10. 直接删除表emp5中的列 dept_id
Alter table emp5
drop column dept_id
8数据处理
8.1数据处理笔记
55. 更改 108 员工的信息: 使其工资变为所在部门中的最高工资, job 变为公司中平均工资最低的 job
1). 搭建骨架
update employees
set salary = (
), job_id = (
) where employee_id = 108;
2). 所在部门中的最高工资
select max(salary)
from employees
where department_id = (
select department_id
from employees
where employee_id = 108
)
3). 公司中平均工资最低的 job
select job_id
from employees
group by job_id
having avg(salary) = (
select min(avg(salary))
from employees
group by job_id
)
4). 填充
update employees e set salary = (
select max(salary)
from employees
where department_id = e.department_id
), job_id = (
select job_id
from employees
group by job_id
having avg(salary) = (
select min(avg(salary))
from employees
group by job_id
)
) where employee_id = 108;
56. 删除 108 号员工所在部门中工资最低的那个员工.
1). 查询 108 员工所在的部门 id
select department_id
from employees
where employee_id = 108;
2). 查询 1) 部门中的最低工资:
select min(salary)
from employees
where department_id = (
select department_id
from employees
where employee_id = 108
)
3). 删除 1) 部门中工资为 2) 的员工信息:
delete from employees e
where department_id = (
select department_id
from employees e
where employee_id = 108
) and salary = (
select min(salary)
from employees
where department_id = e.department_id
)
8.2数据处理练习
测 试
1. 运行以下脚本创建表my_employees
Create table my_employee ( id number(3),
first_name varchar2(10),
Last_name varchar2(10),
User_id varchar2(10),
Salary number(5));
2. 显示表my_employees的结构
DESC my_employees;
3. 向表中插入下列数据
ID FIRST_NAME LAST_NAME USERID SALARY
1 patel Ralph Rpatel 895
2 Dancs Betty Bdancs 860
3 Biri Ben Bbiri 1100
4 Newman Chad Cnewman 750
5 Ropeburn Audrey Aropebur 1550
INSERT INTO my_employee
VALUES(1,’patel’,’Palph’,’Rpatel’895);
4. 提交
COMMIT;
5. 将3号员工的last_name修改为“drelxer”
UPDATE my_employees
SET last_name = ‘drelxer’
WHERE id = 3;
6. 将所有工资少于900的员工的工资修改为1000
UPDATE my_employees
SET salary = 1000
WHERE salary< 900
7. 检查所作的修正
SELECT * FROM my_employees
WHERE salary < 900
8. 提交
COMMIT;
9. 删除所有数据
DELETE FROM my_employees;
10. 检查所作的修正
SELECT * FROM my_employees;
11. 回滚
ROLLBACK;
12. 清空表my_employees
TRUNCATE TABLE my_employees
9约束
9.1约束笔记
57. 定义非空约束
1). 非空约束只能定义在列级.
2). 不指定约束名
create table emp2 (
name varchar2(30) not null,
age number(3)
);
3). 指定约束名
create table emp3(
name varchar2(30) constraint name_not_null not null,
age number(3));
58. 唯一约束
1). 列级定义
①. 不指定约束名
create table emp2 (
name varchar2(30) unique,
age number(3)
);
②. 指定约束名
create table emp3 (
name varchar2(30) constraint name_uq unique,
age number(3)
);
2). 表级定义: 必须指定约束名
①. 指定约束名
create table emp3 (
name varchar2(30),
age number(3),
constraint name_uq unique(name)
);
58.1 主键约束:唯一确定一行记录。表明此属性:非空,唯一
59. 外键约束
1). 列级定义
①. 不指定约束名
create table emp2(
emp_id number(6),
name varchar2(25),
dept_id number(4) references dept2(dept_id))
②. 指定约束名
create table emp3(
emp_id number(6),
name varchar2(25),
dept_id number(4) constraint dept_fk3 references dept2(dept_id))
2). 表级定义: 必须指定约束名
①. 指定约束名
create table emp4(
emp_id number(6),
name varchar2(25),
dept_id number(4),
constraint dept_fk2 foreign key(dept_id) references dept2(dept_id))
60. 约束需要注意的地方
1). ** 非空约束(not null)只能定义在列级
2). ** 唯一约束(unique)的列值可以为空
3). ** 外键(foreign key)引用的列起码要有一个唯一约束
61. 建立外键约束时的级联删除问题:
1). 级联删除:
create table emp2(
id number(3) primary key,
name varchar2(25) unique,
dept_id number(3) references dept2(dept_id) on delete cascade)
2). 级联置空
create table emp3(
id number(3) primary key,
name varchar2(25) unique,
dept_id number(3) references dept2(dept_id) on delete set null)
9.2约束练习
测 试
1. 向表emp2的id列中添加PRIMARY KEY约束(my_emp_id_pk)
ALTER table emp2
ADD constraint my_emp_id_pk primary key(id);
2. 向表dept2的id列中添加PRIMARY KEY约束(my_dept_id_pk)
ALTER table dept2
ADD constraint my_dept_id_pk primary key(id)
3. 向表emp2中添加列dept_id,并在其中定义FOREIGN KEY约束,与之相关联的列是dept2表中的id列。
ALTER table emp2
ADD (dept_id number(10) constraint emp2_dept_id_fk references dept2(id));
准备工作:
create table emp2 as select employee_id id, last_name name, salary from employees
create table dept2 as select department_id id, department_name dept_name from departments
10视图
10.1视图笔记
测 试
1. 使用表employees创建视图employee_vu,其中包括姓名(LAST_NAME),员工号(EMPLOYEE_ID),部门号(DEPARTMENT_ID).
a) create or replace view employee_vu
b) as
c) select last_name,employee_id,department_id
d) from employees
2. 显示视图的结构
desc employee_vu;
3. 查询视图中的全部内容
SELECT * FROM employee_vu;
4. 将视图中的数据限定在部门号是80的范围内
a) create or replace view employee_vu
b) as
c) select last_name,employee_id,department_id
d) from employees
e) where department_id = 80
5. 将视图改变成只读视图
create or replace view employee_vu
as
select last_name,employee_id,department_id
from employees
where department_id = 80
with read only
10.2视图练习
测 试
1. 创建序列dept_id_seq,开始值为200,每次增长10,最大值为10000
a) create sequence dept_id_seq
b) start with 200
c) increment by 10
d) maxvalue 10000
2. 使用序列向表dept中插入数据
a) insert into dept01
b) values(dept_id_seq.nextval,'Account')
附:
create table dept as
select department_id id,department_name name
from departments
where 1=2
11其他数据库对象
11.1其他数据库对象笔记
65. 创建序列:
1). create sequence hs
increment by 10
start with 10
2). NEXTVAL 应在 CURRVAL 之前指定 ,二者应同时有效
65. 序列通常用来生成主键:
INSERT INTO emp2 VALUES (emp2_seq.nextval, 'xx', ...)
总结: what -- why -- how
表table
视图view
序列sequence
索引index
同义词synonym
11.2其他数据库对象练习
测 试
1. 查询和Zlotkey相同部门的员工姓名和雇用日期
a) select last_name,hire_date
b) from employees
c) where department_id = (
d) select department_id
e) from employees
f) where last_name = 'Zlotkey'
g) )
h) and last_name <> 'Zlotkey'
2. 查询工资比公司平均工资高的员工的员工号,姓名和工资。
a) select last_name,employee_id,salary
b) from employees
c) where salary > (select avg(salary)
d) from employees)
3. 查询各部门中工资比本部门平均工资高的员工的员工号, 姓名和工资
a) select employee_id,last_name,salary
b) from employees e1
c) where salary > (
d) select avg(salary)
e) from employees e2
f) where e1.department_id = e2.department_id
g) group by department_id
h) )
4. 查询和姓名中包含字母u的员工在相同部门的员工的员工号和姓名
a) select employee_id,last_name
b) from employees
c) where department_id in (
d) select department_id
e) from employees
f) where last_name like '%u%'
g) )
h) and last_name not like '%u%'
5. 查询在部门的location_id为1700的部门工作的员工的员工号
select employee_id
from employees
where department_id in (
select department_id
from departments
where location_id = 1700
)
6.查询管理者是King的员工姓名和工资
select last_name,salary
from employees
where manager_id in (
select employee_id
from employees
where last_name = 'King'
)
结尾
这是歌谣学习oracle的相关数据笔记,没事会拿出来读读。每个技术栈都会有计划有时间的看完,期待你的努力和成长。
我是歌谣,欢迎一起沟通交流,前端学习ing。一个执着于技术的沉迷者。
推荐链接 其他文件目录参照