日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

Oracle数据库知识小结

發布時間:2025/3/17 数据库 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Oracle数据库知识小结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

1. 對于日期型數據, 做 *, / 運算不合法


2. 包含空值的數學表達式的值都為空值


3. oracle 中連接字符串使用 "||", 而不是 java 中的 "+"


4. 日期和字符只能在單引號中出現.?


5. WHERE 子句緊隨 FROM 子句


6.?日期必須要放在單引號中, 且必須是指定的格式


7. 查詢 LAST_NAME 中有 'o' 字符的所有員工信息.


select *from employees?where last_name like '%o%'


8. 查詢 LAST_NAME 中第二個字符是 'o' 的所有員工信息.

select *from employees?where last_name like '_o%'


9. 查詢 LAST_NAME 中含有 '_' 字符的所有員工信息

?使用 escape 說明轉義字符.

select *from employees?where last_name like '%\_%' ?escape '\'


10. ORDER BY:desc(降序),asc(升序,默認的)

1). 若查詢中有表達式運算, 一般使用別名排序

2). 按多個列排序: 先按第一列排序, 若第一列中有相同的, 再按第二列排序.?


11. 打印出 "2009年10月14日 9:25:40" 格式的日期和時間.

select to_char(sysdate, 'YYYY"年"MM"月"DD"日" HH:MI:SS')

from dual

注意: 使用雙引號向日期中添加字符

12. 格式化數字: 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')


22. 轉換函數: to_char()把日期轉為字符串

select ?to_char(sysdate, 'yyyy-mm-dd')

from dual


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)


26. 多表連接查詢時, 若兩個表有同名的列, 必須使用表的別名對列名進行引用, 否則出錯!


27. 查詢出 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'

? ? ? ? ? ? ? ? ? ?)

28. 左外連接和右外連接


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 所獨有, 不能在其它數據庫中使用.

? ? ?

29. 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 ? ??

30. SQL 99 的左外連接, 右外連接, 滿外連接

1).

select last_name, department_name

from employees e left 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

31. 子查詢注意:?

1). 子查詢要包含在括號內

2). 將子查詢放在比較條件的右側

? ? ? ? ? ? ? ?3). 在 SELECT 列表中所有未包含在組函數中的列都應該包含在 GROUP BY 子句中

例:?按 department_id 進行分組

select department_id, avg(salary)

from employees

group by department_id

32. 利用子查詢創建表 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 2 = 3

33. 對現有的表進行修改操作


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;

? ? ? ?5). 添加約束(主外鍵約束)

? ? ? ? ??ALTER TABLE EMPLOYEES ADD?

? ? ? ? ? ?CONSTRAINT DEPARTMENT_ID_FK FOREIGN KEY(DEPARTMENT_ID) REFERENCES ? ? ? ? ? ? ? ? ?DEPARTMENTS(DEPARTMENT_ID)?


34. 清空表(截斷: truncate), 不能回滾!!

35.?

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;


36. 定義非空約束

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));

37. 唯一約束

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));

38. 外鍵約束

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))

39 約束需要注意的地方

1). ** 非空約束只能定義在列級

2). ** 唯一約束的列值可以為空

3). ** 外鍵引用的列起碼要有一個唯一約束

40. 建立外鍵約束時的級聯刪除問題:

1). 級聯刪除:

create table emp2(

? ? ? ?id number(3) primary key,?

? ? ? ?name varchar2(25) unique,?

? ? ? ?dept_id references dept2(dept_id) on delete cascade)

2). 級聯置空

create table emp3(

? ? ? ?id number(3) primary key,?

? ? ? ?name varchar2(25) unique,?

? ? ? ?dept_id references dept2(dept_id) on delete set null)

? ? ? ?

41. 查詢員工表中 salary 前 10 的員工信息.


select last_name, salary

from (select last_name, salary from employees order by salary desc)

where rownum <= 10

說明: rownum "偽列" ---- 數據表本身并沒有這樣的列, 是 oracle 數據庫為每個數據表 "加上的" ?列. 可以標識行號.

? ? ?默認情況下 rownum 按主索引來排序. 若沒有主索引則自然排序.

?

注意: **對 ROWNUM 只能使用 < 或 <=, 而是用 =, >, >= 都將不能返回任何數據.(用別名) ??


42. 查詢員工表中 salary 10 - 20 的員工信息. ? ?


select *

from(

? select rownum rn, temp.*

? from (

? ? select last_name, salary

? ? from employees e

? ? order by salary desc

? ) temp

)

where rn > 10 and rn < 21


43. 對 oralce 數據庫中記錄進行分頁: 每頁顯示 10 條記錄, 查詢第 5 頁的數據?


select employee_id, last_name, salary

from (

? ? ? ? select rownum rn, employee_id, last_name, salary

? ? ? ? from employees

? ? ?) e

where e.rn <= 50 and e.rn > 40 ??


注意: **對 oracle 分頁必須使用 rownum "偽列"!


select employee_id, last_name, salary

from (

? ? ? ? select rownum rn, employee_id, last_name, salary

? ? ? ? from employees

? ? ?) e

where e.rn <= pageNo * pageSize and e.rn > (pageNo - 1) * pageSize


? ?對MySql的分頁: select * from employees limit (pageNo - 1) * pageSize,pageSize

44. 序列通常用來生成主鍵:

INSERT INTO emp2 VALUES (emp2_seq.nextval, 'xx', ...)?

45.

序列的創建

create sequence seq_newsId
increment by 1
start with 1
maxvalue 999999999;

得到序列的SQL語句

select seq_newsid.nextval from sys.dual;

刪除序列的SQL

?? DROP SEQUENCE seq_newsId;




轉載于:https://my.oschina.net/u/1453975/blog/204963

總結

以上是生活随笔為你收集整理的Oracle数据库知识小结的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。