Les 08

  • Uploaded by: Makokhan
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Les 08 as PDF for free.

More details

  • Words: 1,757
  • Pages: 42
8

Oracle Database Extensions to DML and DDL Statements

Copyright © 2004, Oracle. All rights reserved.

Objectives After completing this lesson, you should be able to do the following: • Describe the features of multitable INSERT statements • Use multitable INSERT statements • Merge rows using the MERGE statement • Create and use external tables • Describe transaction processing

8-2

Copyright © 2004, Oracle. All rights reserved.

Using Multitable INSERT Statements •





The INSERT...SELECT statement can be used to insert rows into multiple tables as part of a single DML statement. Multitable INSERT statements can be used in data warehousing systems to transfer data from one or more operational sources to a set of target tables. They provide much better performance than: – Multiple INSERT...SELECT statements – A procedure to perform multiple insertions using IF...THEN syntax

8-3

Copyright © 2004, Oracle. All rights reserved.

8-4

Copyright © 2004, Oracle. All rights reserved.

Using Multitable INSERT Statements Oracle database introduces the following types of multitable INSERT statements: • Unconditional INSERT • Conditional ALL INSERT • Conditional FIRST INSERT • Pivoting INSERT

8-5

Copyright © 2004, Oracle. All rights reserved.

Using Multitable INSERT Statements Syntax: INSERT [ALL] [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]

8-6

Copyright © 2004, Oracle. All rights reserved.

8-7

Copyright © 2004, Oracle. All rights reserved.

Using Unconditional INSERT ALL •



Select the EMPLOYEE_ID, HIRE_DATE, SALARY, and MANAGER_ID values from the EMPLOYEES table for the employees whose employee IDs are greater than 200. Insert these values into the SAL_HISTORY and MGR_HISTORY tables by using a multitable INSERT statement.

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; 12 rows created. 8-8

Copyright © 2004, Oracle. All rights reserved.

Using Conditional INSERT ALL •

• •

8-9

Select the EMPLOYEE_ID, HIRE_DATE, SALARY and MANAGER_ID values from the EMPLOYEES table for those employees whose employee IDs are greater than 200. If the salary is greater than 10,000, insert these values into the SAL_HISTORY table by using a conditional multitable INSERT statement. If the manager ID is greater than 200, insert these values into the MGR_HISTORY table by using a conditional multitable INSERT statement.

Copyright © 2004, Oracle. All rights reserved.

Using Conditional INSERT ALL

INSERT ALL WHEN SAL > 10000 THEN INTO sal_history VALUES(EMPID,HIREDATE,SAL) WHEN MGR > 200 THEN 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; 4 rows created.

8-10

Copyright © 2004, Oracle. All rights reserved.

Using Conditional FIRST INSERT • •

• •

8-11

Select the DEPARTMENT_ID, SUM(SALARY) and MAX(HIRE_DATE) from the EMPLOYEES table. If the SUM(SALARY) is greater than $25,000 then insert these values into the SPECIAL_SAL table, using a conditional multitable FIRST INSERT statement. If the first WHEN clause is true, the subsequent WHEN clauses for this row should be skipped. For the rows that do not satisfy the first WHEN condition, insert the data into the HIREDATE_HISTORY_00, HIREDATE_HISTORY_99, or HIREDATE_HISTORY tables, based on the value in the HIREDATE column. Copyright © 2004, Oracle. All rights reserved.

Using Conditional FIRST INSERT

INSERT FIRST WHEN SAL > 25000 THEN INTO special_sal VALUES(DEPTID, SAL) WHEN HIREDATE like ('%00%') THEN INTO hiredate_history_00 VALUES(DEPTID,HIREDATE) WHEN HIREDATE like ('%99%') THEN INTO hiredate_history_99 VALUES(DEPTID, HIREDATE) ELSE INTO hiredate_history VALUES(DEPTID, HIREDATE) SELECT department_id DEPTID, SUM(salary) SAL, MAX(hire_date) HIREDATE FROM employees GROUP BY department_id; 12 rows created.

8-12

Copyright © 2004, Oracle. All rights reserved.

Using Pivoting INSERT •





8-13

Suppose you receive a set of sales records from a nonrelational database table, SALES_SOURCE_DATA, in the following format: EMPLOYEE_ID, WEEK_ID, SALES_MON, SALES_TUE, SALES_WED, SALES_THUR, SALES_FRI. You want to store these records in the SALES_INFO table in a more typical relational format: EMPLOYEE_ID, WEEK, SALES. Using a pivoting INSERT statement, convert the set of sales records from the nonrelational database table to the relational format.

Copyright © 2004, Oracle. All rights reserved.

Using 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; 5 rows created.

8-14

Copyright © 2004, Oracle. All rights reserved.

8-15

Copyright © 2004, Oracle. All rights reserved.

Merging Rows by Using the MERGE Statement •

Allows you to merge rows from one table into another:

MERGE INTO destination_table table_alias USING target_table table_alias ON ( condition ) WHEN MATCHED THEN UPDATE SET column1 = new_value1, column2 = new_value2, … WHEN NOT MATCHED THEN INSERT (column1, column2, …) VALUES (value1, value2, …)

8-16

Copyright © 2004, Oracle. All rights reserved.

Merging Rows by Using the MERGE Statement •

Example:

MERGE INTO JOBS j USING JOBS_NEW jnew ON (j.JOB_ID = jnew.JOB_ID) WHEN MATCHED THEN UPDATE SET j.MIN_SALARY = jnew.MIN_SALARY;

8-17

Copyright © 2004, Oracle. All rights reserved.

Merging Rows by Using the MERGE Statement •

Example of a conditional update:

MERGE INTO JOBS j USING JOBS_NEW jnew ON (j.JOB_ID = jnew.JOB_ID) WHEN MATCHED THEN UPDATE SET j.MIN_SALARY = jnew.MIN_SALARY WHERE j.JOB_TITLE NOT LIKE ‘Marketing %’;

8-18

Copyright © 2004, Oracle. All rights reserved.

Understanding External Tables • • • •

8-19

External tables are read-only tables that store data outside the database in flat files. The metadata for an external table is created using a CREATE TABLE statement. With the help of external tables, Oracle data can be stored or unloaded as flat files. The data can be queried using SQL, but you cannot use DML and no indexes can be created.

Copyright © 2004, Oracle. All rights reserved.

Creating an External Table

• • •

8-20

Use the external_table_clause along with the CREATE TABLE syntax to create an external table. Specify ORGANIZATION as EXTERNAL to indicate that the table is located outside the database. The external_table_clause consists of the access driver type, external_data_properties, and the REJECT LIMIT.

Copyright © 2004, Oracle. All rights reserved.

Creating an External Table



The external_data_properties consist of the following: – DEFAULT DIRECTORY – ACCESS PARAMETERS – LOCATION

8-21

Copyright © 2004, Oracle. All rights reserved.

Creating an External Table: Example Create a directory object that corresponds to the directory on the file system where the external data source resides. CREATE DIRECTORY emp_dir AS '/flat_files' ;

8-22

Copyright © 2004, Oracle. All rights reserved.

8-23

Copyright © 2004, Oracle. All rights reserved.

Creating an External Table: Example CREATE TABLE oldemp ( empno NUMBER, empname CHAR(20), birthdate DATE) ORGANIZATION EXTERNAL (TYPE ORACLE_LOADER DEFAULT DIRECTORY emp_dir ACCESS PARAMETERS (RECORDS DELIMITED BY NEWLINE BADFILE 'bad_emp' LOGFILE 'log_emp' FIELDS TERMINATED BY ',' (empno CHAR, empname CHAR, birthdate CHAR date_format date mask "dd-mon-yyyy")) LOCATION ('emp1.txt')) PARALLEL 5 REJECT LIMIT 200; Table created. 8-24

Copyright © 2004, Oracle. All rights reserved.

8-25

Copyright © 2004, Oracle. All rights reserved.

8-26

Copyright © 2004, Oracle. All rights reserved.

Querying External Tables

SELECT * FROM oldemp

emp1.txt

8-27

Copyright © 2004, Oracle. All rights reserved.

Understanding Database Transactions •

Database transactions can consist of: – DML statements that make up one consistent change to the data (for example, INSERT or UPDATE) – One DDL statement (for example, CREATE or ALTER) – One DCL statement (for example, GRANT or REVOKE)

• •

The transaction begins when the first executable SQL statement is executed. It ends when one of the following occurs: – A COMMIT or ROLLBACK statement is issued – A DDL or DCL statement is executed (and automatically commited) – The user exits – The system crashes

8-28

Copyright © 2004, Oracle. All rights reserved.

Hidden Slide

8-29

Copyright © 2004, Oracle. All rights reserved.

Using the COMMIT and ROLLBACK Statements • •

COMMIT and ROLLBACK ensure data consistency. Users can preview data changes before making changes permanent. • Users can group logically related operations. Implicit transaction processing: • An automatic commit occurs under the following circumstances: – A DDL statement is issued, such as CREATE. – A DCL statement is issued, such as GRANT. – A normal exit from iSQL*Plus occurs without an explicitly issued COMMIT or ROLLBACK statement.



8-30

An automatic rollback occurs under an abnormal termination of iSQL*Plus or a system failure. Copyright © 2004, Oracle. All rights reserved.

Hidden Slide

8-31

Copyright © 2004, Oracle. All rights reserved.

Committing Changes • • • •

8-32

The previous state of the data can be recovered. The current user can review the results of the DML operations by using the SELECT statement. Other users cannot view the results of the DML statements by the current user. The affected rows are locked; other users cannot change the data within the affected rows.

Copyright © 2004, Oracle. All rights reserved.

Committing Changes •

• • •

Data changes are made permanent in the database and the previous state of the data is permanently lost. All users can view the results. Locks on the affected rows are released and are now available for other users to manipulate. All savepoints are erased.

UPDATE employee SET department_id = 10 WHERE employee_id = 129; 1 row updated. COMMIT; Commit complete. 8-33

Copyright © 2004, Oracle. All rights reserved.

8-34

Copyright © 2004, Oracle. All rights reserved.

Rolling Back Changes • • •

Pending data changes (changes since the last commit operation) are undone and discarded. The previous state of the data is restored. Locks on the affected rows are released.

DELETE FROM employee; 107 rows deleted. ROLLBACK; Rollback complete.

8-35

Copyright © 2004, Oracle. All rights reserved.

Understanding Statement-Level Rollbacks • • • •

8-36

If a single DML statement fails during execution, only that statement is rolled back. The Oracle server implements an implicit savepoint. All other changes are retained. The user should terminate transactions explicitly by executing a COMMIT or ROLLBACK statement.

Copyright © 2004, Oracle. All rights reserved.

Summary In this lesson, you should have learned how to: • Describe the features of multitable INSERT statements • Use multitable INSERT statements • Merge rows by using the MERGE statement • Create and use external tables • Describe transaction processing

8-37

Copyright © 2004, Oracle. All rights reserved.

Practice 8: Overview This practice covers the following topics: • Writing unconditional INSERT statements • Writing conditional ALL INSERT statements • Using pivoting INSERT statements • Creating indexes along with the CREATE TABLE command

8-38

Copyright © 2004, Oracle. All rights reserved.

8-39

Copyright © 2004, Oracle. All rights reserved.

8-40

Copyright © 2004, Oracle. All rights reserved.

8-41

Copyright © 2004, Oracle. All rights reserved.

8-42

Copyright © 2004, Oracle. All rights reserved.

Related Documents

Les 08
May 2020 6
Les 08
November 2019 4
Les 08
May 2020 2
08 Les Jacuzzi
November 2019 3
Les
May 2020 44
Les
June 2020 42

More Documents from ""

Les 05
May 2020 15
Les 07
May 2020 12
Less05 Storage Tb3
May 2020 16
Les 09
May 2020 13
Les 02
May 2020 1