Oracle_071_lesson_p19

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle_071_lesson_p19相关的知识,希望对你有一定的参考价值。

Manipulating Data 数据操作

you should be able to:
1、Specify explicit default values in the INSERT and UPDATE statements
2、Describe the features of multitable INSERTs
3、Use the following types of multitable INSERTs:
4、Unconditional INSERT
5、Conditional INSERT ALL
6、Conditional INSERT FIRST
7、Pivoting INSERT
8、Merge rows in a table
9、Perform flashback operations
10、Track the changes made to data over a period of time

default 值
1、default with insert:
INSERT INTO deptm3
(department_id, department_name, manager_id)
VALUES (300, ‘Engineering‘, DEFAULT);

2、default with update
UPDATE deptm3
SET manager_id = DEFAULT WHERE department_id = 10;

multitable insert statements 多表insert语句
INSERT…SELECT...

INSERT ALL
INTO target_a VALUES(…,…,…)
INTO target_b VALUES(…,…,…)
INTO target_c VALUES(…,…,…)
SELECT …
FROM sourcetab
WHERE …;

Unconditional INSERT ALL 无条件全插入
例:
create table sal_history as
select employee_id,hire_date,salary
from emp
where 1=0;

create table mgr_history as
select employee_id,manager_id,hire_date
from emp
where 1=0;

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;

Conditional INSERT ALL 有条件全插入 (会逐条匹配)
例:
INSERT ALL
WHEN HIREDATE < ‘01-JAN-05‘ 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;

Conditional INSERT FIRST 有条件INSERT FIRST (当找到满足条件则不再向下匹配)
例:
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;

Pivoting INSERT 旋转插入(行列转换)
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;

Merging rows in a table 可新表数据更新到旧表
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);

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,
...

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

TRUNCATE TABLE copy_emp3;
SELECT * FROM copy_emp3;
no rows selected

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,
...

DELETE WHERE (E.COMMISSION_PCT IS NOT NULL)
WHEN NOT MATCHED THEN
INSERT VALUES(e.employee_id, e.first_name, ...

SELECT * FROM copy_emp3;
107 rows selected.

flashback table 闪回表
system change number (SCN)
FLASHBACK TABLE [ schema. ] table [, [ schema. ] table ]... TO { { { SCN | TIMESTAMP } expr | RESTORE POINT restore_point } [ { ENABLE | DISABLE } TRIGGERS ] | BEFORE DROP [ RENAME TO table ] } ;
例:
DROP TABLE emp3;
SELECT original_name, operation, droptime FROM recyclebin;
FLASHBACK TABLE emp3 TO BEFORE DROP;

select current_timestamp from dual; 查询当前时间戳
flashback table emp to timestamp ( current_timestamp - interval ‘5‘ minute);当前时间减5分钟.

闪回查询:
例:
SELECT salary FROM employees3
WHERE last_name = ‘Chung‘;

UPDATE employees3 SET salary = 4000
WHERE last_name = ‘Chung‘;

SELECT salary FROM employees3
WHERE last_name = ‘Chung‘;

SELECT salary FROM employees3
AS OF TIMESTAMP (SYSTIMESTAMP - INTERVAL ‘1‘ MINUTE)
WHERE last_name = ‘Chung‘;

闪回版本查询:commit 后才会记录版本信息
select * from emp as of timestamp ( current_timestamp - interval ‘5‘ minute);
select version_starttime, version_endtime, salary
from emp versions between scn minvalue and maxvalue where emp_id=100;

例:
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;

例:
SELECT versions_starttime "START_DATE", versions_endtime "END_DATE",
salary FROM employees VERSIONS BETWEEN SCN MINVALUE
AND MAXVALUE WHERE last_name = ‘Lorentz‘;

SELECT salary FROM employees3
VERSIONS BETWEEN SCN MINVALUE AND MAXVALUE
WHERE employee_id = 107;

以上是关于Oracle_071_lesson_p19的主要内容,如果未能解决你的问题,请参考以下文章

Oracle_071_lesson_p17

Oracle_071_lesson_p21

Oracle_071_lesson_p21

Oracle_071_lesson_p5

Oracle_071_lesson_p13

Oracle_071_lesson_p16