目录

前言

数据表结构

数据库文件(按照顺序导入)

1基本SQL-SELECT

1.1基本SQL-SELECT语句笔记

1.2 基本SQL-SELECT语句练习

2过滤和排序数据

2.1过滤和排序数据笔记

2.2过滤和排序数据练习

3单行函数

3.1单行函数笔记

3.2单行函数练习

4多表查询

4.1多表查询笔记

4.2多表查询练习

5分组函数

5.1分组函数笔记

5.2分组函数练习

6子查询

6.1子查询笔记

6.2子查询练习

7创建和管理表

7.1创建和管理表笔记

7.2创建和管理表练习

8数据处理

8.1数据处理笔记

8.2数据处理练习

9约束

9.1约束笔记

9.2约束练习

10视图

10.1视图笔记

10.2视图练习

11其他数据库对象

11.1其他数据库对象笔记

11.2其他数据库对象练习

结尾


前言

数据表结构

数据库文件(按照顺序导入)

先导入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 emailselect last_name || ' `s email is ' || email EMAIL
from employees6. 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). 使用 ANDselect *from employeeswhere salary >= 5000 and salary <= 100002). 使用 BETWEEN .. AND ..,  注意: 包含边界!!select *from employeeswhere salary between 5000 and 1000011. 查询工资等于 6000, 7000, 8000, 9000, 10000 的员工信息1). 使用 ORselect *from employeeswhere salary = 6000 or salary = 7000 or salary = 8000 or salary = 9000 or salary = 100002). 使用 INselect *from employeeswhere salary in (6000, 7000, 8000, 9000, 10000)12. 查询 LAST_NAME 中有 'o' 字符的所有员工信息.select *from employeeswhere last_name like '%o%'13. 查询 LAST_NAME 中第二个字符是 'o' 的所有员工信息.select *from employeeswhere last_name like '_o%'14. 查询 LAST_NAME 中含有 '_' 字符的所有员工信息1). 准备工作:update employeesset last_name = 'Jones_Tom'where employee_id = 1952). 使用 escape 说明转义字符.select *from employeeswhere last_name like '%\_%' escape '\'15. 查询 COMMISSION_PCT 字段为空的所有员工信息select last_name, commission_pctfrom employeeswhere commission_pct is null16. 查询 COMMISSION_PCT 字段不为空的所有员工信息select last_name, commission_pctfrom employeeswhere commission_pct is not null17. 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.89select to_char(1234567.89, '999,999,999.99')from dual20. 字符串转为数字时1). 若字符串中没有特殊字符, 可以进行隐式转换:select '1234567.89' + 100from dual2). 若字符串中有特殊字符, 例如 '1,234,567.89', 则无法进行隐式转换, 需要使用 to_number() 来完成select to_number('1,234,567.89', '999,999,999.99') + 100from dual21. 对于把日期作为查询条件的查询, 一般都使用 to_date() 把一个字符串转为日期,
这样可以不必关注日期格式select last_name, hire_datefrom employeeswhere 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_datefrom employeeswhere hire_date = last_day(hire_date) - 124. 计算公司员工的年薪--错误写法: 因为空值计算的结果还是空值select last_name, salary * 12 * (1 + commission_pct) year_salfrom employees--正确写法select last_name, salary * 12 * (1 + nvl(commission_pct, 0)) year_salfrom employees25. 查询部门号为 10, 20, 30 的员工信息, 若部门号为 10, 则打印其工资的 1.1 倍,20 号部门, 则打印其工资的 1.2 倍, 30 号部门打印其工资的 1.3 倍数--使用 case-when-then-else-endselect last_name, department_id, salary, case department_id when 10  then salary * 1.1when 20  then salary * 1.2when 30  then salary * 1.3end new_salfrom employeeswhere department_id in (10, 20, 30)--使用 decodeselect last_name, department_id, salary, decode(department_id, 10, salary * 1.1,20, salary * 1.2,30, salary * 1.3) new_salfrom employeeswhere 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, cityselect last_name, department_name, cityfrom departments d, employees e, locations lwhere d.department_id = e.department_id and d.location_id = l.location_id28. 查询出 last_name 为 'Chen' 的 manager 的信息. (员工的 manager_id 是某员工的 employee_id) 0). 例如: 老张的员工号为: "1001", 我的员工号为: "1002", 我的 manager_id 为 "1001" --- 我的 manager 是"老张" 1). 通过两条 sql 查询:select manager_idfrom employeeswhere lower(last_name) = 'chen' --返回的结果为 108select *from employeeswhere employee_id = 1082). 通过一条 sql 查询(自连接):select m.*from employees e, employees mwhere e.manager_id = m.employee_id and e.last_name = 'Chen'     3). 通过一条 sql 查询(子查询):   select *from employeeswhere employee_id = (select manager_id from employeeswhere last_name = 'Chen')    29. 查询每个员工的 last_name 和 GRADE_LEVEL(在 JOB_GRADES 表中). ---- 非等值连接select last_name, salary, grade_level, lowest_sal, highest_salfrom employees e, job_grades jwhere e.salary >= j.lowest_sal and e.salary <= j.highest_sal30. 左外连接和右外连接select last_name, e.department_id, department_namefrom employees e, departments dwhere e.department_id = d.department_id(+)select last_name, d.department_id, department_namefrom employees e, departments dwhere e.department_id(+) = d.department_id理解 "(+)" 的位置: 以左外连接为例, 因为左表需要返回更多的记录,右表就需要 "加上" 更多的记录, 所以在右表的链接条件上加上 "(+)"注意: 1). 两边都加上 "(+)" 符号, 会发生语法错误!2). 这种语法为 Oracle 所独有, 不能在其它数据库中使用.          31. SQL 99 连接 Employees 表和 Departments 表1).select *from employees join departmentsusing(department_id)缺点: 要求两个表中必须有一样的列名.2).select *from employees e join departments don e.department_id = d.department_id3).多表连接select e.last_name, d.department_name, l.cityfrom employees e join departments don e.department_id = d.department_idjoin locations lon d.location_id = l.location_id                32. SQL 99 的左外连接, 右外连接, 满外连接1).select last_name, department_namefrom employees e left outer join departments don e.department_id = d.department_id2).select last_name, department_namefrom employees e right join departments don e.department_id = d.department_id3).select last_name, department_namefrom employees e full join departments don 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 employeesgroup by department_id36. 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   7select 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 salaryFROM employeesWHERE last_name = 'Abel'--返回值为 11000SELECT last_name, salaryFROM employeesWHERE salary > 110002). 使用子查询 -- 一条 SQL 语句SELECT last_name, salaryFROM employeesWHERE salary > (SELECT salaryFROM employeesWHERE last_name = 'Abel')子查询注意: 1). 子查询要包含在括号内2). 将子查询放在比较条件的右侧  41. 查询工资最低的员工信息: last_name, salary  42. 查询平均工资最低的部门信息43*. 查询平均工资最低的部门信息和该部门的平均工资44. 查询平均工资最高的 job 信息45. 查询平均工资高于公司平均工资的部门有哪些?46. 查询出公司中所有 manager 的详细信息.47. 各个部门中 最高工资中最低的那个部门的 最低工资是多少48. 查询平均工资最高的部门的 manager 的详细信息: last_name, department_id, email, salary49. 查询 1999 年来公司的人所有员工的最高工资的那个员工的信息./*************************************************************************************************/41. 查询工资最低的员工信息: last_name, salary SELECT last_name, salaryFROM employeesWHERE salary = (SELECT min(salary)FROM employees)42. 查询平均工资最低的部门信息SELECT *FROM departmentsWHERE department_id = (SELECT department_idFROM employeesGROUP BY department_id HAVING avg(salary) = (SELECT min(avg(salary))FROM employeesGROUP 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_idFROM employeesGROUP BY department_id HAVING avg(salary) = (SELECT min(avg(salary))FROM employeesGROUP BY department_id) )44. 查询平均工资最高的 job 信息1). 按 job_id 分组, 查询最高的平均工资  SELECT max(avg(salary))FROM employeesGROUP BY job_id2). 查询出平均工资等于 1) 的 job_idSELECT job_idFROM employeesGROUP BY job_idHAVING avg(salary) = (SELECT max(avg(salary))FROM employeesGROUP BY job_id)3). 查询出 2) 对应的 job 信息SELECT *FROM jobsWHERE job_id = (SELECT job_idFROM employeesGROUP BY job_idHAVING avg(salary) = (SELECT max(avg(salary))FROM employeesGROUP BY job_id))45. 查询平均工资高于公司平均工资的部门有哪些?1). 查询出公司的平均工资SELECT avg(salary)FROM employees2). 查询平均工资高于 1) 的部门 IDSELECT department_idFROM employeesGROUP BY department_idHAVING avg(salary) > (SELECT avg(salary)FROM employees)46. 查询出公司中所有 manager 的详细信息.1). 查询出所有的 manager_idSELECT distinct manager_idFROM employeess2). 查询出 employee_id 为 1) 查询结果的那些员工的信息SELECT employee_id, last_nameFROM employeesWHERE employee_id in (SELECT distinct manager_idFROM employees)47. 各个部门中 最高工资中最低的那个部门的 最低工资是多少1). 查询出各个部门的最高工资SELECT max(salary)FROM employeesGROUP BY department_id2). 查询出 1) 对应的查询结果的最低值: 各个部门中最低的最高工资(无法查询对应的 department_id)SELECT min(max(salary))FROM employeesGROUP BY department_id3). 查询出 2) 所对应的部门 id 是多少: 各个部门中最高工资等于 2) 的那个部门的 idSELECT department_idFROM employeesGROUP BY department_id HAVING max(salary) = (SELECT min(max(salary))FROM employeesGROUP BY department_id)4). 查询出 3) 所在部门的最低工资SELECT min(salary)FROM employeesWHERE department_id = (SELECT department_idFROM employeesGROUP BY department_id HAVING max(salary) = (SELECT min(max(salary))FROM employeesGROUP BY department_id)   )48. 查询平均工资最高的部门的 manager 的详细信息: last_name, department_id, email, salary1). 各个部门中, 查询平均工资最高的平均工资是多少SELECT max(avg(salary))FROM employeesGROUP BY department_id2). 各个部门中, 平均工资等于 1) 的那个部门的部门号是多少SELECT department_idFROM employeesGROUP BY department_idHAVING avg(salary) = (SELECT max(avg(salary))FROM employeesGROUP BY department_id)3). 查询出 2) 对应的部门的 manager_idSELECT manager_idFROM departmentsWHERE department_id = (SELECT department_idFROM employeesGROUP BY department_idHAVING avg(salary) = (SELECT max(avg(salary))FROM employeesGROUP BY department_id)  )4). 查询出 employee_id 为 3) 查询的 manager_id 的员工的 last_name, department_id, email, salarySELECT last_name, department_id, email, salaryFROM employeesWHERE employee_id = (SELECT manager_idFROM departmentsWHERE department_id = (SELECT department_idFROM employeesGROUP BY department_idHAVING avg(salary) = (SELECT max(avg(salary))FROM employeesGROUP BY department_id) )   )49. 查询 1999 年来公司的人所有员工的最高工资的那个员工的信息.1). 查询出 1999 年来公司的所有的员工的 salarySELECT salaryFROM employeesWHERE to_char(hire_date, 'yyyy') = '1999'2). 查询出 1) 对应的结果的最大值SELECT max(salary)FROM employeesWHERE to_char(hire_date, 'yyyy') = '1999'3). 查询工资等于 2) 对应的结果且 1999 年入职的员工信息       SELECT *FROM employeesWHERE to_char(hire_date, 'yyyy') = '1999' AND salary = (SELECT max(salary)FROM employeesWHERE to_char(hire_date, 'yyyy') = '1999')50. 多行子查询的 any 和 allselect department_idfrom employeesgroup by department_idhaving avg(salary) >= any(--所有部门的平均工资select avg(salary)from employeesgroup 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_idfrom departmentswhere location_id = 1700)
6.查询管理者是King的员工姓名和工资
select last_name,salary
from employees
where manager_id in (select employee_idfrom employeeswhere last_name = 'King')

7创建和管理表

7.1创建和管理表笔记

51. 利用子查询创建表 myemp,
该表中包含 employees 表的 employee_id(id), last_name(name), salary(sal), email 字段1). 创建表的同时复制 employees 对应的记录create table myemp asselect employee_id id, last_name name, salary sal, email from employees  2). 创建表的同时不包含 employees 中的记录, 即创建一个空表create table myemp asselect employee_id id, last_name name, salary sal, email from employees where 1 = 252. 对现有的表进行修改操作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_columnalter table emp5
drop unused columns
10. 直接删除表emp5中的列 dept_id
Alter table emp5
drop column dept_id

8数据处理

8.1数据处理笔记

55. 更改 108 员工的信息: 使其工资变为所在部门中的最高工资, job 变为公司中平均工资最低的 job1). 搭建骨架update employees set salary = (), job_id = () where employee_id = 108;2). 所在部门中的最高工资  select max(salary)from employeeswhere department_id = (select department_idfrom employeeswhere employee_id = 108)3). 公司中平均工资最低的 jobselect job_idfrom employeesgroup by job_idhaving avg(salary) =  (select min(avg(salary))from employeesgroup by job_id)4). 填充update employees e set salary = (select max(salary)from employeeswhere department_id = e.department_id), job_id = (select job_idfrom employeesgroup by job_idhaving avg(salary) =  (select min(avg(salary))from employeesgroup by job_id)) where employee_id = 108;56. 删除 108 号员工所在部门中工资最低的那个员工.1). 查询 108 员工所在的部门 idselect department_idfrom employees where employee_id = 108;2). 查询 1) 部门中的最低工资:select min(salary)from employeeswhere department_id = (select department_idfrom employees where employee_id = 108)3). 删除 1) 部门中工资为 2) 的员工信息:delete from employees ewhere department_id = (select department_idfrom employees ewhere employee_id = 108) and salary = (select min(salary)from employeeswhere 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    1550INSERT INTO my_employeeVALUES(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 employeescreate 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 employees2.    显示视图的结构
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 = 805. 将视图改变成只读视图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 102). 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_idfrom departmentswhere location_id = 1700)
6.查询管理者是King的员工姓名和工资
select last_name,salary
from employees
where manager_id in (select employee_idfrom employeeswhere last_name = 'King')

结尾

这是歌谣学习oracle的相关数据笔记,没事会拿出来读读。每个技术栈都会有计划有时间的看完,期待你的努力和成长。

我是歌谣,欢迎一起沟通交流,前端学习ing。一个执着于技术的沉迷者。

推荐链接 其他文件目录参照

“睡服“面试官系列之各系列目录汇总(建议学习收藏)

Oracle从小白到大牛的刷题之路(建议收藏学习)相关推荐

  1. 判断输入的字符串是否为回文_刷题之路(九)--判断数字是否回文

    Palindrome Number 问题简介:判断输入数字是否是回文,不是返回0,负数返回0 举例: 1: 输入: 121 输出: true 2: 输入: -121 输出: false 解释: 回文为 ...

  2. 【Leetcode】刷题之路2(python)

    哈希映射类题目(简单题小试牛刀啦bhn) 242.有效的字母异位词 349.两个数组的交集 1002.查找常用字符 202.快乐数 383.赎金信 242. 有效的字母异位词 用python的Coun ...

  3. 【Leetcode】 刷题之路1(python)

    leetcode 刷题之路1(python) 看到有大佬总结了一些相关题目,想着先刷一类. 1.两数之和 15.三数之和 16.最接近的三数之和 11.盛最多的水 18.四数之和 454.四数相加II ...

  4. 刷题之路:DP思想(动态规划)

    dp一般用于解决决策问题,比如说你的每一步都有好几种处理方式,怎么选择使得最后的结果满足或者接近于你的预期是需要考虑的问题. 所以dp问题实际上也就是最优解的问题 一般采用的方式就是将问题拆分成若干个 ...

  5. LeetCode 刷题之路(python版)

    摘自:https://blog.csdn.net/qq_32384313/article/details/90745354 LeetCode 刷题之路(python版) 小坏wz 2019-06-02 ...

  6. 2021.5.21开始的兔系刷题之路 根据LeetCode分类进行逐个击破 培养出自己的套路~

    十二月了 再更一波 最近的题解都写在这个仓库中,另外仓库中也记录了自己学习前端过程中的收获~ 近期刷题情况-- 2021-11突然好多人看这篇XD 来更一波,依旧在保持刷题啦~ 目前是跟着一本前端算法 ...

  7. 蓝桥杯备考-刷题之路-动态规划算法(DP算法)Part1

    之前在刷力扣的时候就是浑浑噩噩的,照着评论区的答案写了一遍就万事大吉了,没有深度思考过.这次备考蓝桥杯看到DP算法的第一道题就不会,更难受的是看答案了依然完全不理解,所以决心把DP算法一次弄懂. 开始 ...

  8. 漫画:小白为了面试如何刷题?(呕心沥血算法指导篇)

    来自:小浩算法 三年高考,五年刷题.leetcode不算从其他各处收录的题目,单就自己的题库,总共有1600+,如果按照每天刷一道的话,总共需要5年.那我们真的需要把这些题目全部刷完吗?如果不是,刷多 ...

  9. Python刷题之路,怎样做才能让技术突飞猛进

    比你优秀的人比你还努力 这个世界最可悲的就是 , 比你优秀的人比你还努力 偶然的机会,通过Python认识了一位华为的文职工作人员.起初只是问我,Python初学者看什么书能快速入门.而两个月过后,她 ...

最新文章

  1. 直接下载mongodb版本
  2. Linux安全管理:一,sshd配置
  3. EXCEL中SUMIF函数介绍
  4. 有多少用户痛点,你是听回来的,而不是经过深思过后找出来的
  5. Java 语法糖详解
  6. WPF 放大镜(Magnifying Glass)
  7. SAP CRM service contract和individual object
  8. WINCE下I/O操作基础
  9. kotlin泛型类、泛型接口
  10. C++11多线程---future和promise
  11. win11系统正式版介绍
  12. python怎么读取excel-python读写excel文件
  13. 【图像隐写】基于matlab DWT数字水印嵌入+提取+攻击【含Matlab源码 622期】
  14. Elastic ik插件配置热更新功能
  15. Xmind8 绿色版安装教程
  16. win7不用破解工具,最简单的去黑屏办法
  17. Delaunay三角网构建,并进行可视化
  18. 让这世界再多一份GNU m4 教程 (全文整理)
  19. Android中实现一键分享功能
  20. Nginx 之四: Nginx服务器的rewrite、全局变量、重定向和防盗链相关功能

热门文章

  1. 二分图----最大匹配,最小点覆盖,最大点独立集
  2. 2015已经过去三分之一了
  3. Yii2.0 技巧总结
  4. asp.net如何给每张图片动态添加水印方法(二)
  5. Overlay Surfaces (覆盖表面)
  6. 十年只为一个摧残的梦(转载)
  7. php修改mysql数据库中的表格,如何修改mysql数据库表?
  8. 获得picker选项的当前年月值_如果你用OPPO手机!千万记得开启开发者选项,手机性能大幅度提升...
  9. 前端if else_应该记录的一些项目代码(前端)
  10. java customerservlet_顾客管理系统java+servlet