Oracle从小白到大牛的刷题之路(建议收藏学习)
生活随笔
收集整理的這篇文章主要介紹了
Oracle从小白到大牛的刷题之路(建议收藏学习)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
前言
數據表結構
數據庫文件(按照順序導入)
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 employees1.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 employees2過濾和排序數據
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 employees4多表查詢
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_id4.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) > 60005.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_id6.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_id8數據處理
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_employees9約束
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 departments10視圖
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 only10.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=211其他數據庫對象
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 同義詞synonym11.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从小白到大牛的刷题之路(建议收藏学习)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 前端学习(1909)vue之电商管理系统
- 下一篇: 泛微OA漏洞(综合)