Oracle數據庫知識小結

1. 對於日期型數據, 作 *, / 運算不合法java


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


3. oracle 中鏈接字符串使用 "||", 而不是 java 中的 "+"數據庫


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


5. WHERE 子句緊隨 FROM 子句函數


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


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


select *from employees where last_name like '%o%'排序


8. 查詢 LAST_NAME 中第二個字符是 'o' 的全部員工信息.索引

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


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;

相關文章
相關標籤/搜索