48
ORACLE SQL Fundamental II 4-5 章章章 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :章 subquery 章章 table , WITH CHECK OPTION) 2. 章 INSERT 章 UPDATE 章章章章章章 default 3. multitable INSERT 1. Unconditional INSERT 2. Conditional INSERT ALL 3. Conditional INSERT FIRST 4. Pivoting INSERT 4.Merge rows in a table 5. Tracking Changes in Data :VERSIONS BETWEEN SCN MINVALUE AND MAXVALUE

ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

Embed Size (px)

Citation preview

Page 1: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

ORACLE SQL Fundamental II

4-5 章重點Chap 4 Managing Objects with Data Dictionary Views

Chap 5 Manipulating Large Data Sets

1. subqueries ( inline view : 以 subquery 取代 table , WITH CHECK OPTION)

2. 在 INSERT 和 UPDATE 敘述明確指定 default 值      3. multitable INSERT

1. Unconditional INSERT

2. Conditional INSERT ALL

3. Conditional INSERT FIRST

4. Pivoting INSERT

4.Merge rows in a table

5. Tracking Changes in Data :VERSIONS BETWEEN SCN MINVALUE AND MAXVALUE

Page 2: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 4-2 Objectives(目標 ) dictionary views :

可存取 metadata 及產生關於你的 schema objects 之報表

2

II 4-4 Data Dictionary Data dictionary:

1. 由 Oracle Server 產生及維護2. read-only , 所以只能做 SQL 查詢

Page 3: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 4-4 Data dictionary structure

Data dictionary structure 包含1. base tables :

只有 Oracle Server 可寫或讀這些 tables, 你很少直接存取2. user-accessible views :

大部分使用者可以存取 Data dictionary 可查到下列資訊 :

n Definitions of all schema objects in the database (tables, views, indexes, synonyms,sequences, procedures, functions, packages, triggers, and so on)

n Default values for columnsn Integrity constraint informationn Names of Oracle usersn Privileges and roles that each user has been grantedn Other general database information

3

Page 4: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 4-6 Data Dictionary Structure View naming convention:

data dictionary 一組包含三個 view, 如 :

n USER_OBJECT: 包含你擁有 (own) 或你 create 的 objects 資訊n ALL_OBJECT: 包含你可以存取 (access) 的 objects 資訊n DBA_OBJECT: 包含被所有使用者 (all users) 擁有 (own) 的 objects 資

訊 以 v$ 開頭的 View :

1.是動態的 (dynamic in nature) 且儲存關於 performance 的資料2. Dynamic performance tables 不是真實 tables

3.不可以被大部使用者 (most users) 存取 . 只有 DBA 可在這些資料表做查詢、 create views 和授權存取這些 view 給其他使用者

1. 以 ALL 或 DBA 為前置詞的 views : 通常有一個額外的欄位 owner , 用於指明誰擁有這個 object

4

Page 5: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 4-7 How to Use the Dictionary Views

DESCRIBE DICTIONARY

SELECT *

FROM dictionary

WHERE table_name = 'USER_OBJECTS';

5

Page 6: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 4-9 USER_OBJECTS View

SELECT object_name, object_type, created, status

FROM user_objects

ORDER BY object_type ;

CAT view : 只有兩個欄位 : TABLE_NAME 及 TABLE_TYPE CAT 是 USER_CATALOG 的 synonym ( 同義字 ) CAT 是一個 view 列示 user 所擁有的 tables 、 views 、 synonyms 和 sequences

6

Page 7: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 4-11 Table Information

USER_TABLES: USER_TABLES view 可獲得全部你的 tables 的 names

DESCRIBE user_tables ;

SELECT table_name FROM user_tables;

7

Page 8: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 4-12 Column Information

USER_TAB_COLUMNS: USER_TAB_COLUMNS view 可得到 columns 更詳細的資料DESCRIBE user_tab_columns ;

8

Page 9: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 4-13 Column Information

SELECT column_name, data_type, data_length,

data_precision, data_scale, nullable

FROM user_tab_columns

WHERE table_name = 'EMPLOYEES';

9

Page 10: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 4-14 Constraint Information

USER_CONSTRAINTS : 描述你的 table 的 constraint 定義

USER_CONS_COLUMNS : 描述你擁有和被用在 constraint 的 columns

DESCRIBE user_constraints ;

10

題庫 105

Page 11: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 4-15 USER_CONSTRAINTS: Example

SELECT constraint_name, constraint_type,

search_condition, r_constraint_name,

delete_rule, status

FROM user_constraints

WHERE table_name = 'EMPLOYEES';

11

STATUS:指 constraint 是否啟用中 (ENABLE)

Page 12: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 4-15 USER_CONSTRAINTS

USER_CONSTRAINTS 的 CONSTRAINT_TYPE:1. C: check 或 NOT NULL

2. P: primary key

3. U:unique key

4. R:referential integrity

5. V: with check option, on a view

6. O:with read-only, on a view

12

題庫 28

Page 13: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 4-16 Querying USER_CONS_COLUMNS

USER_CONS_COLUMNS : 被用在 constraint 的 column 名稱

DESCRIBE user_cons_columns

SELECT constraint_name, column_name

FROM user_cons_columns

WHERE table_name = 'EMPLOYEES';

13

Page 14: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 4-18 View Information USER_VIEWS:

1. TEXT: 以 LONG 資料型態存放 SELECT 敘述 . 預設只顯示 80 個字

元 , SET LONG 1000 , 可看到多於 80 個字元2. TEXT_LENGTH: 為 SELECT 敘述的字元數DESCRIBE user_views;

SELECT view_name FROM user_views;

SELECT text FROM user_views

WHERE view_name = 'EMP_DETAILS_VIEW';

14

Page 15: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 4-19 Sequence Information

DESCRIBE user_sequences;

15

Page 16: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 4-20 Confirming Sequences

SELECT sequence_name, min_value, max_value,

Increment_by, last_number

FROM user_sequences;

LAST_NUMBER 欄位 : 若指定 NOCACHE, 則顯示下一個 sequence number

16

Page 17: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 4-21 Index Information

USER_INDEXES : 提供關於你自己的 indexes 的資訊

USER_IND_COLUMNS : 描述在你資料表上的索引 (indexes) 欄位

DESCRIBE user_indexes;

17

Page 18: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 4-22 USER_INDEXES: Examples

SELECT index_name, table_name, uniqueness

FROM user_indexes

WHERE table_name = ‘EMPLOYEES’;

SELECT index_name, table_name

FROM user_indexes

WHERE table_name = ‘EMP_LIB’;

18

Page 19: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 4-23 Querying USER_IND_COLUMNS

DESCRIBE user_ind_columns;

SELECT index_name, column_name,table_name

FROM user_ind_columns

WHERE index_name = ‘LNAME_IDX’;

19

Page 20: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 4-24 Synonym Information

DESCRIBE user_synonyms;

SELECT * FROM user_synonyms;

private synonyms: 指你擁有 (own) 的 synonyms

20

題庫 26

Page 21: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 4-26 Adding Comments to a Table

comment ( 註解 ) : 存在於 data dictionary

COMMENT ON TABLE employees

IS 'Employee Information';

COMMENT ON COLUMN employees.first_name

IS 'First name of the employee';

Comments 可從 data dictionary 查詢 :n ALL_COL_COMMENTSn USER_COL_COMMENTSn ALL_TAB_COMMENTSn USER_TAB_COMMENTS

設定空字串 ('') 來 drop a commentCOMMENT ON TABLE employees IS '‘ ;

21

Page 22: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 4-27 Quiz

The dictionary views that are based on the dictionary tables contain information such as:a. Definitions of all the schema objects in the database

b. Default values for the columns

c. Integrity constraint information

d. Privileges and roles that each user has been granted

e. All of the above

Answer: e

22

Page 23: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 5-4 Using Subqueries to Manipulate Data inline view :

在 SELECT 敘述的 from 子句用 subquery 取代 table

23

II 5-5 Retrieving Data by Using a Subquery as Source loc 先 create :

先 CREATE TABLE loc AS SELECT * FROM locations ;

SELECT department_name, city

FROM departments

NATURAL JOIN (SELECT l.location_id, l.city, l.country_id

FROM loc l

JOIN countries c

ON(l.country_id = c.country_id)

JOIN regions USING(region_id)

WHERE region_name = 'Europe') ; 多個 JOIN 時 :

JOIN 加 ON 子句可和 JOIN 加 USING 子句使用 inner query 的輸出 , 對 outer query 而言 , 被視為一個 table . inner query 類似於 database view , 但沒有實體名稱 (physical name)

題庫 68

Page 24: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 5-7& 5-8 Inserting by Using a Subquery as a Target

使用 subquery 取代 loc, INSERT INTO 一筆新的 European city 記錄 , 新增的資料會存到 loc

INSERT INTO (SELECT l.location_id, l.city, l.country_id

FROM loc l

JOIN countries c

ON(l.country_id = c.country_id)

JOIN regions USING(region_id)

WHERE region_name = 'Europe')

VALUES (3300, 'Cardiff', 'UK') ;

INSERT INTO :可使用 subquery 取代 table 但 , SELECT list 欄位的個數要和 VALUES 子句的 column

list 一樣SELECT location_id, city, country_id

FROM loc

24

Page 25: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 5-9 Using the WITH CHECK OPTION Keyword on DML Statements

WITH CHECK OPTION : 禁止去改變那些不在 subquery 的 rows

下列程式錯誤 : query 限制 region_name 需為 'Europe', 但 VALUES 子句 , 卻

為 'US'

INSERT INTO ( SELECT location_id, city, country_id

FROM loc

WHERE country_id IN

(SELECT country_id

FROM countries

NATURAL JOIN regions

WHERE region_name = 'Europe')

WITH CHECK OPTION )

VALUES (3600, 'Washington', 'US');

25

Page 26: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 5-9 Using the WITH CHECK OPTION Keyword on DML Statements

WITH CHECK OPTION: 當 subquery 被用於下列敘述 , 沒有包含在 subquery 的 rows, 不

允許被改變1. INSERT

2. UPDATE

3. DELETE

4. CREATE OR REPLACE VIEW

26

Page 27: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 5-12 Explicit Default Feature: Overview

明確的預設值 (explicit defaults) 可用在 :1. INSERT

2. UPDATE

27

II 5-13 Using Explicit Default Values DEFAULT with INSERT:

INSERT INTO deptm3

(department_id, department_name, manager_id)

VALUES ( 300, 'Engineering', DEFAULT); DEFAULT with UPDATE:

UPDATE deptm3

SET manager_id = DEFAULT

WHERE department_id = 10;若 manager_id 沒有定義預設值 , 則會以 null 值取代

Page 28: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 5-14 Copying Rows from Another Table

利用 INSERT INTO 加 subquery : 可從別的 table 複製 row 不可再加 VALUES 子句 INSERT 子句的欄位個數及 data type 要和 subquery 配合

INSERT INTO sales_reps(id, name, salary, commission_pct)

SELECT employee_id, last_name, salary, commission_pct

FROM employees

WHERE job_id LIKE '%REP%';

DML 敘述加 LOG ERRORS 子句 : 可使 DML 不管錯誤 , 去完成操作 Oracle 將錯誤訊息寫到你自己 creat 的 error-logging table

28

Page 29: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 5-16 Multitable INSERT Statements: Overview

multitable insert : 從 subquery 得到的 rows , insert into 到一或多個 table 對 data warehouse 很有用

ETL(extraction, transformation,loading) : 從 source system, 萃取資料並引入 data warehouse 的過程 multitable insert: 就是建置 SQL data transformations 的其中技

術INSERT ALL

INTO target_a VALUES(…,…,…)

INTO target_b VALUES(…,…,…)

INTO target_c VALUES(…,…,…)

SELECT …

FROM sourcetab

WHERE … ;

29

subquery

Page 30: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 5-17 Multitable INSERT Statements: Overview

multitable insert : 使用 INSERT…SELECT 敘述新增 (insert) 資料 (rows) 到多個資

料表 (multiple tables ), 當作單一 DML 敘述的部分 若不使用 multitable insert, 則處理相同 source data n 次 , 就會增

加轉換工作量 n 倍

30

Page 31: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 5-18 Types of Multitable INSERT Statements

multitable insert 有 4 種 type:

1. Uncondictional INSERT: subquery 傳回的每個 row 都要 insert into 每個 target( 目標 )

tables , 只是欄位可能不同2. Condictional INSERT ALL:

subquery 傳回的每個 row 要 insert into 到符合條件的個別each target table

3. Pivoting INSERT: Uncondictional INSERT 的特例 , 所有 insert to 的 table 都一

樣 4. Condictional INSERT FIRST:

subquery 傳回的每個 row 先 insert into 到符合第一個條件的第一個 target table 其餘 rows 再去比較第二個條件 , 若符合第二個條件 , 則

insert into 到第二個 target table, 以此類推

31

Page 32: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 5-19 Multitable INSERT Statements

multitable INSERT 語法 :

INSERT [conditional_insert_clause]

[insert_into_clause values_clause] (subquery)

conditional_insert_clause:

[ALL|FIRST]

[WHEN condition THEN] [insert_into_clause values_clause]

[ELSE] [insert_into_clause values_clause]

32

II 5-20 Multitable INSERT Statements multitable insert:

1. 只能用在 table, 不可用在 views 或 materialized views

2. 不可操作在 remote table

3. 不可指定 table collection expression

4. insert into 子句不能多於 999 個 target columns

Page 33: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 5-21&5-22 Unconditional INSERT ALL Uncondictional ( 無條件 )INSERT ALL:

全部的 rows 都被 insert into 到兩個 table: sal_history 及 mgr_history VALUES 指明從 SELECT 敘述的 column , 要被 insert 到 table 的欄

INSERT ALL

INTO sal_history VALUES(EMPID,HIREDATE,SAL)

INTO mgr_history VALUES(EMPID,MGR,SAL)

SELECT employee_id EMPID, hire_date HIREDATE,

salary SAL, manager_id MGR

FROM employees

WHERE employee_id > 200;

SELECT COUNT(*) total_in_sal FROM sal_history;

SELECT COUNT(*) total_in_mgr FROM mgr_history;

33

總共新增 12 筆資料

每個 row 都要insert into 每個target( 目標 ) tables , 只是欄位可能不同

Page 34: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 5-23 Conditional INSERT ALL: Example

Conditional INSERT ALL :

34

Page 35: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 5-24 Conditional INSERT ALL condictional INSERT ALL: 有條件新增資料

INSERT ALL WHEN 用的欄位名稱 , 同 SELECT list 的別名INSERT ALL

WHEN HIREDATE < '01-JAN-95' THEN

INTO emp_history VALUES(EMPID,HIREDATE,SAL)

WHEN COMM IS NOT NULL THEN

INTO emp_sales VALUES(EMPID,COMM,SAL)

SELECT employee_id EMPID, hire_date HIREDATE,

salary SAL, commission_pct COMM

FROM employees ;

SELECT count(*) FROM emp_history;

SELECT count(*) FROM emp_sales;

35

總共新增 48 筆資料

每個 row 要insert into 到符合條件的個別 each target table

題庫 35

Page 36: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 5-25 Conditional INSERT ALL

INSERT ALL

WHEN job_id IN

(select job_id FROM jobs WHERE job_title LIKE '%Manager%') THEN

INTO managers2(last_name,job_id,SALARY)

VALUES (last_name,job_id,SALARY)

WHEN SALARY>10000 THEN

INTO richpeople(last_name,job_id,SALARY)

VALUES (last_name,job_id,SALARY)

ELSE

INTO poorpeople VALUES (last_name,job_id,SALARY)

SELECT * FROM employees ;

36

題庫 8

題庫 32

Page 37: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 5-26 Conditional INSERT FIRST: Example

若員工的薪資為 2000, 則只有 insert into 到 SAL_LOW 的 table

37

Page 38: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 5-27 Conditional INSERT FIRST

INSERT FIRST: 若第一個 when 符合 , 則這筆 row insert into 到第一個

table(sal_low), 且跳過接續的 when 子句 若第一個 when 不符合 , 但符合第二個 when, 則這筆 row insert into

到第二個 table(sal_mid), 且跳過接續的 when 子句 以此類推 , 若全部 when 條件都不符合 , 則執行 ELSE 子句INSERT FIRST

WHEN salary < 5000 THEN

INTO sal_low VALUES (employee_id, last_name, salary)

WHEN salary between 5000 and 10000 THEN

INTO sal_mid VALUES (employee_id, last_name, salary)

ELSE

INTO sal_high VALUES (employee_id, last_name, salary)

SELECT employee_id, last_name, salary

FROM employees ;

38

題庫 67

Page 39: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 5-29 Pivoting INSERT

Pivoting INSERT : 轉換非關聯式 (nonrelational ) 資料庫到 relational ( 關聯式格式 )

format

39

Page 40: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 5-30 Pivoting INSERT

Pivoting INSERT : INSET ALL INTO 到同一個 table, 只是 column 有點不同 此例 : 一筆 row 轉換成 5 筆資料INSERT ALL

INTO sales_info VALUES (employee_id, week_id, sales_MON)

INTO sales_info VALUES (employee_id, week_id, sales_TUE)

INTO sales_info VALUES (employee_id, week_id, sales_WED)

INTO sales_info VALUES (employee_id, week_id, sales_THUR)

INTO sales_info VALUES (employee_id, week_id, sales_FRI)

SELECT EMPLOYEE_ID, week_id, sales_MON, sales_TUE,

sales_WED, sales_THUR,sales_FRI

FROM sales_source_data ;

40

INSERT INTO 到同一個資料表(salse_info)

題庫 5

Page 41: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 5-33 MERGE Statement

MERGE: 提供條件式 (condictionally)

1. update: 當 row 已存在時2. insert: 新增一個 new row

3. delete 可避免分開的 update 不可在 target table 的相同 row, update 很多次 增加 performance 及使用簡單 對 data warehousing application 有用

41

Page 42: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 5-34 MERGE Statement Syntax MERGE INTO :

USING (table 或 view 或 subquery) INSERT 指令 :

1.可以不用 INTO

2.可用 WHERE 子句MERGE INTO table_name table_alias

USING (table|view|sub_query) alias

ON (join condition)

WHEN MATCHED THEN

UPDATE SET

col1 = col1_val,

col2 = col2_val

WHEN NOT MATCHED THEN

INSERT (column_list)

VALUES (column_values) ;

42

Page 43: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 5-35 Merging Rows: Example 在 COPY_EMP3 資料表新增或修改資料列 MERGE INTO copy_emp3 c

USING (SELECT * FROM EMPLOYEES ) e

ON (c.employee_id = e.employee_id)

WHEN MATCHED THEN

UPDATE SET

c.first_name = e.first_name,

c.last_name = e.last_name,

c.email = e.email,

c.phone_number = e.phone_number,

c.hire_date = e.hire_date,

c.job_id = e.job_id,

c.salary = e.salary*2,

c.commission_pct = e.commission_pct,

c.manager_id = e.manager_id,

c.department_id = e.department_id

DELETE WHERE (E.COMMISSION_PCT IS NOT NULL)

WHEN NOT MATCHED THEN

INSERT VALUES (e.employee_id, e.first_name, e.last_name, e.email,

e.phone_number, e.hire_date, e.job_id,e.salary, e.commission_pct,

e.manager_id, e.department_id) ;43

注意 : Copy_emp3c 資料表要先新增

CREATE TABLE COPY_EMP3 AS SELECT * FROM EMPLOYEESWHERE SALARY<10000 ;

Copy_emp3c 經過 MERGE INTO 後:1.Employees 資料表的SALARY<10000 且 commission_pct 不為 NULL 者 , 會從 Copy_emp3c  被刪除2.Employees 資料表的SALARY<10000 且 commission_pct 為 NULL 者 , 會被更新到 Copy_emp3c 薪水加倍3.Employees 資料表的SALARY>10000 會被新增到Copy_emp3c

題庫 73

題庫 51

注意 :employee_id 欄位不可被 UPDATE

Page 44: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 5-39 Tracking Changes in Data 追蹤資料的改變 :

在查詢資料表時 , 使用 VERSIONS clause 產生所有版本 (versions), 抓取版本的時間為 : 下查詢指令和現在時間 (current time) 之前的undo_retention seconds 之間 undo_retention is an initialization parameter, which is an

autotuned parameter ( 自動調整的參數 )

44

System change number (SCN): The Oracle server assigns an SCN to identify the redo records for each committed transaction

Page 45: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

SELECT salary FROM employees3

WHERE employee_id = 107;

UPDATE employees3 SET salary = salary * 1.30

WHERE employee_id = 107;

COMMIT;

SELECT salary FROM employees3

VERSIONS BETWEEN SCN MINVALUE AND MAXVALUE

WHERE employee_id = 107;

II 5-40 Flashback Version Query: Example

45

1

2

3

看到先後 2 次SELECT 查詢的資料

題庫 138

Page 46: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 5-42 VERSIONS BETWEEN Clause

SELECT versions_starttime "START_DATE",

versions_endtime "END_DATE", salary

FROM employees

VERSIONS BETWEEN SCN MINVALUE AND MAXVALUE

WHERE last_name = 'Lorentz‘ ;

46

Page 47: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

補充 :CREATE TABLE digits

(id NUMBER(2), description VARCHAR2(15));

 

INSERT INTO digits VALUES (1,'ONE');

 

UPDATE digits

SET description ='TWO'

WHERE id=1;

 

INSERT INTO digits VALUES (2,'TWO');

 

COMMIT;

 

DELETE FROM digits;

 

SELECT description

FROM digits

VERSIONS BETWEEN TIMESTAMP MINVALUE AND MAXVALUE;

47

題庫 31

What would be the outcome of the above query? A. It would not display any values. B. It would display the value TWO once. C. It would display the value TWO twice. D. It would display the values ONE, TWO, and TWO.

Answer: C

Page 48: ORACLE SQL Fundamental II 4-5 章重點 Chap 4 Managing Objects with Data Dictionary Views Chap 5 Manipulating Large Data Sets 1. subqueries ( inline view :

II 5-43 Quiz

When you use the INSERT or UPDATE command, the DEFAULT keyword saves you from hard-coding the default value in your programs or querying the dictionary to find it.a. True

b. False

Answer: a

48