Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introducing the Oracle Database 11g SQL and PL/SQL New Features

Similar presentations


Presentation on theme: "Introducing the Oracle Database 11g SQL and PL/SQL New Features"— Presentation transcript:

1 Introducing the Oracle Database 11g SQL and PL/SQL New Features

2 Oracle Database 11g: SQL and PL/SQL New Features 1 - 2
Objectives After completing this lesson, you should be able to: Describe the organization of the course Review the schemas that are used in this course Review the SQL*Plus environment that you can optionally use in this course Find additional information about Oracle Database 11g on the Oracle Technology Network Oracle Database 11g: SQL and PL/SQL New Features

3 Oracle Database 11g: SQL and PL/SQL New Features 1 - 3
Course Objectives After completing this course, you should be able to: Use the SQL Developer interface with the latest enhancements Write SQL statements that include the new functions added to enhance regular expression support functionality Monitor dependency tracking and change notification List the changes to locking that enable you to specify the maximum number of seconds the statement should wait to obtain a DML lock on the table Practice the performance improvements Oracle Database 11g: SQL and PL/SQL New Features

4 Oracle Database 11g: SQL and PL/SQL New Features 1 - 4
Course Objectives Use the enhancements added to native dynamic SQL and to DBMS_SQL, which enable more interoperability between the two methodologies Write compound triggers and use the enhancements made to the triggers Use SecureFile LOBS Write SQL and PL/SQL calls to sequences that are simpler Use the new CONTINUE statement to control loops Explore the data warehousing improvements Oracle Database 11g: SQL and PL/SQL New Features

5 Oracle Database 11g: SQL and PL/SQL New Features 1 - 5
Course Agenda Day 1: Introducing Oracle Database 11g SQL and PL/SQL enhancements Using the SQL Developer enhancements Using the language functionality enhancements Executing dynamic SQL in PL/SQL with the 11g enhancements Implementing the performance improvements Oracle Database 11g: SQL and PL/SQL New Features

6 Oracle Database 11g: SQL and PL/SQL New Features 1 - 6
Course Agenda Day 2: Practicing the language usability enhancements Developing triggers that utilize the new enhancements Administering SecureFile LOBs Using the data warehousing usability enhancements Oracle Database 11g: SQL and PL/SQL New Features

7 Oracle Database 11g: SQL and PL/SQL New Features 1 - 7
Lesson Agenda Appendixes and tables used in this course Overview of SQL*Plus Oracle Database 11g documentation and additional resources Oracle Database 11g: SQL and PL/SQL New Features

8 Appendixes Used in This Course
Appendix A: Practice Solutions Appendix B: Table Descriptions Appendix C: Using Oracle SQL Developer Appendix D: SQL*Plus Appendix E: Working with Collections Appendix F: Exploring the Data Warehousing Performance Enhancements Oracle Database 11g: SQL and PL/SQL New Features

9 Tables Used in This Course
The sample schemas that are used in this course are: The Order Entry (OE) schema The Sales History (SH) schema Tables Used in This Course The sample company portrayed by Oracle Database Sample Schemas operates worldwide to fulfill orders for several different products. The company has several divisions: The Order Entry division tracks product inventories and sales of the company’s products through various channels. The Sales History division tracks business statistics to facilitate business decisions. Each of these divisions is represented by a schema. All scripts necessary to create the OE schema reside in the $ORACLE_HOME/demo/schema/order_entry folder. All scripts necessary to create the SH schema reside in the $ORACLE_HOME/demo/schema/sales_history folder. Note Appendix B contains more information about the sample schemas. The code examples and the practices in this course specify the schema that needs to be used. Oracle Database 11g: SQL and PL/SQL New Features

10 Order Entry (OE) Schema
CUSTOMERS customer_id cust_first_name cust_ last_name cust_ address_typ phone_numbers nls_language nls_territory credit_limit cust_ account_mgr_id date_of_birth marital_status gender Income_level ORDER_ITEMS order_id line_item_id product_id unit_price quantity ORDERS order_id order_date order_mode customer_id order_status order_total sales_rep_id promotion_id street_address postal_code city state_province country_id PRODUCT_ INFORMATION product_id product_name product_description category_id weight_class warranty_period supplier_id product_status list_price min_price catalog_url PRODUCT_ DESCRIPTIONS product_id language_id product_name product_description Order Entry (OE) Schema: Description The company sells several categories of products, including computer hardware and software, music, clothing, and tools. The company maintains product information that includes product identification numbers, the category into which the product falls, the weight group (for shipping purposes), the warranty period if applicable, the supplier, the status of the product, a list price, a minimum price at which a product will be sold, and a URL for manufacturer information. INVENTORIES product_id warehouse_id quantity_on_hand WAREHOUSES warehouse_id warehouse_name location_id Oracle Database 11g: SQL and PL/SQL New Features

11 Sales History (SH) Schema
TIMES time_id day_name day_number_in_week day_number_in_month calendar_week_number fiscal_week_number week_ending_day week_ending_day_id calendar_month_number fiscal_month_number calendar_month_desc calendar_month_id fiscal_month_id days_in_cal_month days_in_fis_month end_of_cal_ month end_of_fis_month calendar _month _name fiscal _month _name calendar _quarter _desc calendar_quarter_id fiscal _quarter _desc fiscal _quarter _id days_in_cal_quarter days_in_fis_quarter end_of_cal_quarter end_of_fis_quarter calendar_quarter_number fiscal_quarter_number calendar_year calendar_year_id fiscal_year fiscal_year_id days_in_cal_year days_in_fis_year end_of_cal_year end_of_fis_year PROMOTIONS promo_id promo_name promo_subcategory promo_subcategory_id promo_category promo_category_id promo_cost promo_begin_date promo_end_date promo_total promo_total_id COSTS prod_id time_id promo_id channel_id unit_cost unit_price SALES prod_id cust_id time_id channel_id promo_id quantity_sold amount_sold CHANNELS channel_id channel_desc channel_class channel_class_id channel_total channel_total_id Sales History (SH) Schema: Description The company does a high volume of business, so it runs business statistics reports to aid in decision support. Many of these reports are time based and nonvolatile. That is, they analyze past data trends. The company loads data into its data warehouse regularly to gather statistics for the reports. The reports include annual, quarterly, monthly, and weekly sales figures by product. The company also runs reports on the distribution channels through which its sales are delivered. When the company runs special promotions on its products, it analyzes the impact of the promotions on sales. It also analyzes sales by geographical area. PRODUCTS Oracle Database 11g: SQL and PL/SQL New Features

12 Sales History (SH) Schema
COSTS SALES SALES PRODUCTS prod_id prod_name prod_desc prod_subcategory prod_subcategory_id prod_subcategory_desc prod_category prod_category_id prod_category_desc prod_weight_class prod_unit_of_measure prod_pack_size supplier_id prod_status prod_list_price prod_min_price prod_total prod_total_id prod_src_id prod_eff_from prod_eff_to prod_valid CUSTOMERS cust_id cust_first_name cust_last_name cust_gender cust_year_of_birth cust_marital_status cust_street_address cust_postal_code cust_city cust_city_id cust_state_province cust_state_province_id country_id cust_main_phone_number cust_income_level cust_credit_limit cust_ cust_total cust_total_id cust_src_id cust_eff_from cust_eff_to cust_valid COUNTRIES country_id country_iso_code country_name country_subregion country_subregion_id country_region country_region_id country_total country_total_id Country_name_hist Oracle Database 11g: SQL and PL/SQL New Features

13 Class Account Information
Cloned OE account IDs are set up for you. Your account IDs are OE1 – OE20. The password matches your account ID. Each machine is assigned one account. All OE account IDs have SELECT status on the SH schema. The instructor has a separate ID. Oracle Database 11g: SQL and PL/SQL New Features

14 Oracle Database 11g: SQL and PL/SQL New Features 1 - 14
Lesson Agenda Appendixes and tables used in this course Overview of SQL*Plus Oracle Database 11g documentation and additional resources Oracle Database 11g: SQL and PL/SQL New Features

15 Overview of SQL*Plus Used in This Course
Logging in to SQL*Plus Describing the table structure Executing SQL from SQL*Plus Reviewing SQL*Plus file commands SQL*Plus SQL*Plus is an environment in which you can do the following: Execute SQL statements to retrieve, modify, add, and remove data from the database. Format, perform calculations on, store, and print query results in the form of reports. Create script files to store SQL statements for repeated use in the future. SQL*Plus commands can be divided into the following main categories: Oracle Database 11g: SQL and PL/SQL New Features

16 Oracle Database 11g: SQL and PL/SQL New Features 1 - 16
Logging In to SQL*Plus 1 sqlplus 2 Logging In to SQL*Plus Starting with Oracle Database 11g, the SQL*Plus command-line environment is the only SQL*Plus environment available. To log in from a command-line environment, perform the following steps: 1. Enter the SQL*Plus command and optionally enter your username, password (your password is visible if you enter it here), and the database connect string. 2. In a Windows environment, you can set up an icon for SQL*Plus or click Start > Run > oracle_home\product\11.1.0\client_1\BIN\sqlplus.exe. Note To ensure the integrity of your password, do not enter it at the operating system prompt. Instead, enter only your username. Enter your password at the password prompt. After you log in to SQL*Plus, you see the message shown in the screenshot in the slide (if you are using SQL*Plus version ). The “[ ]” indicates an optional parameter. Oracle Database 11g: SQL and PL/SQL New Features

17 Displaying Table Structure
DESCRIBE sh.customers Name Null? Type CUST_ID NOT NULL NUMBER CUST_FIRST_NAME NOT NULL VARCHAR2(20) CUST_LAST_NAME NOT NULL VARCHAR2(40) CUST_GENDER NOT NULL CHAR(1) CUST_YEAR_OF_BIRTH NOT NULL NUMBER(4) CUST_MARITAL_STATUS VARCHAR2(20) CUST_STREET_ADDRESS NOT NULL VARCHAR2(40) CUST_POSTAL_CODE NOT NULL VARCHAR2(10) CUST_CITY NOT NULL VARCHAR2(30) CUST_CITY_ID NOT NULL NUMBER CUST_STATE_PROVINCE NOT NULL VARCHAR2(40) CUST_STATE_PROVINCE_ID NOT NULL NUMBER COUNTRY_ID NOT NULL NUMBER CUST_MAIN_PHONE_NUMBER NOT NULL VARCHAR2(25) CUST_INCOME_LEVEL VARCHAR2(30) CUST_CREDIT_LIMIT NUMBER CUST_ VARCHAR2(30) CUST_TOTAL NOT NULL VARCHAR2(14) CUST_TOTAL_ID NOT NULL NUMBER ... Displaying Table Structure The example in the slide displays information about the structure of the DEPARTMENTS table. In the result: Null?: Specifies whether a column must contain data (NOT NULL indicates that a column must contain data.) Type: Displays the data type for a column The following table describes the basic data types: Oracle Database 11g: SQL and PL/SQL New Features

18 Executing SQL from SQL*Plus
SELECT cust_last_name, cust_gender, cust_income_level FROM sh.customers; CUST_LAST_NAME G INCOME_LEVEL Kinski M D: 70, ,999 Garcia F I: 170, ,999 Olin F F: 110, ,999 Altman F F: 110, ,999 de Funes F D: 70, ,999 Chapman F F: 110, ,999 Gielgud F E: 90, ,999 Prashant F C: 50, ,999 Welles M D: 70, ,999 Rampling M F: 110, ,999 ... 319 rows selected. Oracle Database 11g: SQL and PL/SQL New Features

19 SQL*Plus File Commands
SAVE filename GET filename START filename @filename EDIT filename SPOOL filename EXIT SQL*Plus File Commands Use the following commands to control the environment, format results, and manage files: Oracle Database 11g: SQL and PL/SQL New Features

20 Oracle Database 11g: SQL and PL/SQL New Features 1 - 20
Lesson Agenda Appendixes and tables used in this course Overview of SQL*Plus Overview of Oracle SQL Developer Oracle Database 11g documentation and additional resources Oracle Database 11g: SQL and PL/SQL New Features

21 Oracle Database 11g SQL and PL/SQL Documentation
Navigate to then click the Books tab: Oracle Database Advanced Application Developer’s Guide 11g, Release 1 (11.1) Oracle Database Concepts 11g, Release 1 (11.1) Oracle Database 2 Day Developer’s Guide 11g, Release 1 (11.1) Oracle Database Security Guide 11g, Release 1 (11.1) Oracle Database 11g: SQL and PL/SQL New Features

22 Oracle Database 11g SQL and PL/SQL Documentation
Oracle Database SQL Language Reference 11g, Release 1 Oracle Database PL/SQL Language Reference 11g, Release 1 Oracle Database PL/SQL Packages and Types Reference 11g, Release 1 Oracle Database Large Objects Developer’s Guide SQL*Plus User’s Guide and Reference Oracle Database SQL Developer User’s Guide, Release 1.2 Oracle Database 11g: SQL and PL/SQL New Features

23 Oracle Database 11g: SQL and PL/SQL New Features 1 - 23
Additional Resources For additional information about the new Oracle 11g SQL and PL/SQL new features, refer to the following: Oracle Database 11g: New Features eStudies Oracle by Example series (OBE): Oracle Database 11g 11gr1db.html What’s New in PL/SQL in Oracle Database 11g on the Oracle Technology Network (OTN): Oracle Database 11g: SQL and PL/SQL New Features

24 Oracle Database 11g: SQL and PL/SQL New Features 1 - 24
Summary In this lesson, you should have learned how to: Describe the organization of the course Review the schemas that are used in this course Review the SQL*Plus environment that you can optionally use in this course Find additional information about Oracle Database 11g from the Oracle Technology Network Summary This lesson provides you with an overview for the course. In this lesson, you learned about the: Course organization, objectives, and agenda Data used for the examples in the course Where to find additional information on Oracle Database 11g Oracle Database 11g: SQL and PL/SQL New Features

25 Practice 1 Overview: Getting Started
This practice covers the following topics: Reviewing the schemas for this course Using SQL*Plus Accessing Oracle Database 11g resources Practice 1 Overview: Getting Started In this practice, you learn about your user account and data used for the course. You also learn where and how to access the Oracle Database 11g resources. Note Two environments are available for this class: Oracle SQL*Plus and Oracle SQL Developer 1.2. In this course, and for the rest of the course, the examples are shown predominantly using the Oracle SQL Developer tool. However, it is up to you to decide which tool you want to use in this class. In the next lesson, you learn how to use SQL Developer 1.2. Oracle Database 11g: SQL and PL/SQL New Features

26 Oracle Database 11g: SQL and PL/SQL New Features 1 - 26
Practice 1 Initiate a SQL*Plus session using the user ID and password that are provided by the instructor. a. Double-click the SQL*Plus icon on your desktop. b. At the SQL> prompt, enter: CONNECT where both the n values and sid are provided to you by your instructor 2. Create a SQL script named customer_info.sql that describes and reports the customers’ first and last names from the CUSTOMERS table. Open Notepad or WordPad. Enter this statement: SELECT cust_first_name, cust_last_name FROM customers; Save the file to your D:\Labs\labs folder. Execute the file from SQL*Plus. 3. Exit SQL*Plus. 4. Access the Oracle Database 11g documentation Web page at: 5. Display the complete list of books available for Oracle Database 11g. 6. Make a note of the following books that you will use in this course as needed: a. Oracle Database SQL Language Reference 11g, Release 1 b. Oracle Database PL/SQL Language Reference 11g, Release 1 c. Oracle Database PL/SQL Packages and Types Reference 11g, Release 1 d. Oracle Database Advanced Application Developer’s Guide 11g, Release 1 e. Oracle Database Concepts 11g, Release 1 f. Oracle Database Large Objects Developer’s Guide g. SQL*Plus User’s Guide and Reference h. Oracle® Database SQL Developer User’s Guide, Release 1.2 7. Review Appendix B Table Descriptions and Data to become familiar with the data used in this course. Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

27 Using SQL Developer

28 Oracle Database 11g: SQL and PL/SQL New Features 1 - 28
Objectives After completing this lesson, you should be able to: List the key features of Oracle SQL Developer Install Oracle SQL Developer Create a database connection Navigate through the object navigator Use the SQL Worksheet Create, save, and use scripts Develop, compile, and debug PL/SQL Browse through the available search engines Change preferences Create reports Describe migration Objectives In this lesson, you learn how to use the Oracle SQL Developer tool. Oracle Database 11g: SQL and PL/SQL New Features

29 What Is Oracle SQL Developer?
Oracle SQL Developer is a free graphical tool that enhances productivity and simplifies database development tasks. You can connect to any target Oracle database schema using standard Oracle database authentication. What Is Oracle SQL Developer? Oracle SQL Developer is a free graphical tool designed to improve your productivity and simplify the development of everyday database tasks. With just a few clicks, you can easily create and debug stored procedures, test SQL statements, and view optimizer plans. SQL Developer, the visual tool for database development, simplifies the following tasks: Browsing and managing database objects Executing SQL statements and scripts Editing and debugging PL/SQL statements Creating reports You can connect to any target Oracle database schema using the standard Oracle database authentication. When connected, you can perform operations on objects in the database. SQL Developer Oracle Database 11g: SQL and PL/SQL New Features

30 Installing SQL Developer
Download the Oracle SQL Developer kit and unzip it into any directory on your machine. Installing SQL Developer Oracle SQL Developer does not require an installer. To install SQL Developer, you need an unzip tool. To install SQL Developer, perform the following steps: 1. Create a folder as <local drive>:\SQL Developer. 2. Download the SQL Developer kit from the Oracle SQL Developer Home page at: 3. Unzip the downloaded SQL Developer kit into the folder created in step 1. To start SQL Developer, go to <local drive>:\SQL Developer and double-click sqldeveloper.exe. Note: In addition to using the Oracle SQL Developer Home page to download the software for free, you can use this page to view the Oracle SQL Developer online Help, presentations, white papers, technical information, lessons, tutorials, and so on. Oracle Database 11g: SQL and PL/SQL New Features

31 Menus for SQL Developer
1 2 3 4 5 6 7 Menus for SQL Developer SQL Developer has two main navigation tabs: Connections: By using this tab, you can browse database objects and users to which you have access. Reports: By using this tab, you can run predefined reports or create and add your own reports. SQL Developer uses the left side for navigation to find and select objects, and the right side to display information about selected objects. You can customize many aspects of the appearance and behavior of SQL Developer by setting preferences. The menus at the top contain standard entries, plus entries for features specific to the tool. 1. View: Contains options that affect what is displayed in the SQL Developer interface 2. Navigate: Contains options for navigating to panes and for execution of subprograms 3. Run: Contains the Run File and Execution Profile options that are relevant when a function or procedure is selected 4. Debug: Contains options that are relevant when a function or procedure is selected 5. Source: Contains options for use when editing functions and procedures 6. Migration: Enables you to migrate from another database, such as Microsoft SQL Server and Microsoft Access, to an Oracle database 7. Tools: Invokes SQL Developer tools such as SQL*Plus, Preferences, and SQL Worksheet Oracle Database 11g: SQL and PL/SQL New Features

32 Creating a Database Connection
You must have at least one database connection to use SQL Developer. You can create and test connections for: Multiple databases Multiple schemas SQL Developer automatically reads any connections defined in the tnsnames.ora file on your system. You can export connections to an XML file. Each additional database connection created is listed in the Connections navigator hierarchy. Creating a Database Connection A connection is a SQL Developer object that specifies the necessary information for connecting to a specific database as a specific user of that database. To use SQL Developer, you must have at least one database connection, which may be existing, created, or read. You can create and test connections for multiple databases and for multiple schemas. By default, the tnsnames.ora file is located in the $ORACLE_HOME/network/admin directory. But, it can also be in the directory specified by the TNS_ADMIN environment variable or registry value. When you start SQL Developer and display the New/Select Database Connection window, SQL Developer automatically reads any connections defined in the tnsnames.ora file on your system. Note: On Windows systems, if the tnsnames.ora file exists, but its connections are not being used by SQL Developer, define TNS_ADMIN as a system environment variable. You can export connections to an XML file so that you can reuse it later. You can create additional connections as different users to the same database or to connect to the different databases. Oracle Database 11g: SQL and PL/SQL New Features

33 Creating a Database Connection
2 1 3 Creating a Database Connection (continued) To create a database connection, start SQL Developer and perform the following steps: 1. On the Connections tabbed page, right-click Connections and select New Connection. 2. Enter the connection name, username, password, host name, and system identifier (SID) or Service name for the database that you want to connect to. 3. Click Test to make sure that the connection has been set correctly. 4. Click Connect. On the Oracle tabbed page, at the bottom, enter the following options: Hostname: The host system for the Oracle database Note: In the classroom, enter the host name associated with your PC. Port: Listener port SID: Database name Service name: Network service name for a remote database connection If you select the Save Password check box, the password is saved to an XML file. Therefore, the next time you access the SQL Developer connection, you will not be prompted for the password. The other tabbed pages enable you to set up connections to non-Oracle databases. 4 Oracle Database 11g: SQL and PL/SQL New Features

34 Browsing Database Objects
Use the Database navigator to: Browse through many objects in a database schema Review the definitions of objects at a glance Browsing Database Objects After you have created a database connection, you can use the Database navigator to browse through many objects in a database schema including Tables, Views, Indexes, Packages, Procedures, Triggers, Types, and so on. You can see the definition of the objects broken into tabs of information that is pulled out of the data dictionary. For example, if you select a table in the Connections navigator, the details about columns, constraints, grants, statistics, triggers, and more, are displayed on an easy-to-read tabbed page. If you want to see the definition of the CUSTOMERS table as shown in the slide, perform the following steps: 1. Expand the Connections node in the Connections navigator. 2. Expand Tables. 3. Click CUSTOMERS. Using the Data tab, you can enter new rows, update data, and commit these changes to the database. Oracle Database 11g: SQL and PL/SQL New Features

35 Exporting Database Objects
Enter the file name destination, and select the Connection. Select the Objects to export. Click Apply. Exporting Database Objects You can export DDL and data using the Export utility. For a selected database connection, you can export some or all objects of one or more types of database objects to a file containing SQL data definition language (DDL) statements to create these objects. To specify options for the export operation, select the Tools > Export DDL (and Data) menu. Then, select the objects to export on the Export tabbed page. To specify the objects or types of objects to export, use the Filter Objects tab. Identify the name of the file that you want the data saved to, in the File field. You can click Browse to select a directory for this file. Click Apply to proceed with the export. Oracle Database 11g: SQL and PL/SQL New Features

36 Exporting Database Objects
The resulting file contains the object definitions you exported. Exporting Database Objects (continued) After the export is completed, you can examine the contents of the exported file. In the example shown, the data for tables, views, and indexes is exported to the export.sql file. The contents of the export.sql file are displayed. Oracle Database 11g: SQL and PL/SQL New Features

37 Exporting and Importing Data
You can export table data by using the Export submenu when you right-click the Tables object in the navigator. The export utility offers you wide flexibility in the different formats that you can export to. You can import data from an Excel spreadsheet using the same submenu. You can also use the Migration tool to export and import data from other data sources. The Migration tool is discussed later in this lesson. Oracle Database 11g: SQL and PL/SQL New Features

38 Oracle Database 11g: SQL and PL/SQL New Features 1 - 38
Using SQL Worksheet Use SQL Worksheet to enter and execute SQL, PL/SQL, and SQL*Plus statements. Specify any actions that can be processed by the database connection associated with the worksheet. Using SQL Worksheet When you connect to a database, a SQL Worksheet window for that connection is automatically opened. You can use SQL Worksheet to enter and execute SQL, PL/SQL, and SQL*Plus statements. SQL Worksheet supports SQL*Plus statements to a certain extent. SQL*Plus statements that are not supported by SQL Worksheet are ignored and not passed to the database. You can specify any actions that can be processed by the database connection associated with the worksheet, such as creating a table, inserting data, creating and editing a trigger, selecting data from a table, saving the selected data to a file, and saving and running SQL scripts. You can display a SQL worksheet by using any of the following two options: Select Tools > SQL Worksheet. Click the Open SQL Worksheet icon. You can set your preferences so that the SQL Worksheet is opened automatically when you have a database connection. Select Tools > Preferences. Click Database. Click Worksheet Parameters. Check Open a Worksheet on Connect. Oracle Database 11g: SQL and PL/SQL New Features

39 Oracle Database 11g: SQL and PL/SQL New Features 1 - 39
Using SQL Worksheet 2 4 6 8 1 3 5 7 9 Using SQL Worksheet (continued) You may want to use the shortcut keys or icons to perform certain tasks such as executing a SQL statement, running a script, and viewing the history of SQL statements that you have executed. You can use the SQL Worksheet toolbar that contains icons to perform the following tasks: 1. Execute Statement: Executes the statement at the cursor in the Enter SQL Statement box. You can use bind variables in the SQL statements but not substitution variables. 2. Run Script: Executes all statements in the Enter SQL Statement box using Script Runner. You can use substitution variables in the SQL statements but not bind variables. 3. Commit: Writes any changes to the database and ends the transaction 4. Rollback: Discards any changes to the database, without writing them to the database, and ends the transaction 5. Cancel: Stops the execution of any statements currently being executed 6. SQL History: Displays a dialog box with information about the SQL statements that you have executed 7. Execute Explain Plan: Generates the execution plan, which you can see by clicking the Explain tab 8. Autotrace: Generates trace information for the statement 9. Clear: Erases the statement or statements in the Enter SQL Statement box Oracle Database 11g: SQL and PL/SQL New Features

40 Executing SQL Statements
Use the Enter SQL Statement box to enter single or multiple SQL statements. View the results on the Script Output tabbed page. Executing SQL Statements In SQL Worksheet, you can use the Enter SQL Statement box to enter a single SQL statement or multiple SQL statements. For a single statement, the semicolon at the end is optional. When you enter the statement, the SQL keywords are automatically highlighted. To execute a SQL statement, ensure that your cursor is within the statement and click the Execute Statement icon. Alternatively, you can press the [F9] key. To execute multiple SQL statements and see the results, click the Run Script icon. Alternatively, you can press the F5 key. In the example in the slide, because there are multiple SQL statements, the first statement is terminated with a semicolon. The cursor is in the first statement and so when the statement is executed, results corresponding to the first statement are displayed in the Results box. Oracle Database 11g: SQL and PL/SQL New Features

41 Oracle Database 11g: SQL and PL/SQL New Features 1 - 41
Saving SQL Scripts Click the Save icon to save your SQL statement to a file. Enter a file name and identify a location to save the file in the Windows Save dialog box. The contents of the saved file are visible and editable in your SQL Worksheet window. Saving SQL Scripts You can save your SQL statements from the SQL Worksheet into a text file. To save the contents of the Enter SQL Statement text box, follow these steps: 1. Click the Save icon or use the File > Save menu item. 2. In the Windows Save dialog box, enter a file name and the location where you want the file saved. 3. Click Save. After you save the contents to a file, the Enter SQL Statement text box displays a tabbed page of your file contents. You can have multiple files open at once. Each file displays as a tabbed page. Script Pathing You can select a default path to look for scripts and to save scripts. Under Tools > Preferences > Database > Worksheet Parameters, enter a value in the Select default path to look for scripts field. Oracle Database 11g: SQL and PL/SQL New Features

42 Executing Saved SQL Scripts
Use command followed by the location and name of the file you want to execute. Then click the Run Script icon. The output from the script is displayed on the Script Output tabbed page. Running a Saved SQL Script To run a saved SQL script, follow these steps: 1. In the Enter SQL Statement window, use command, followed by the location and name of the file that you want to run. 2. Click the Run Script icon. The results from running the file are displayed on the Script Output tabbed page. You can also save the script output by clicking the Save icon on the Script Output tabbed page. The Windows File Save dialog box appears and you can identify a name and location for your file. Note: You can also right-click in the Enter SQL Statement area and select Open File from the shortcut menu. Oracle Database 11g: SQL and PL/SQL New Features

43 Using PL/SQL in SQL Developer
Right-click the Procedures node and select New Procedure. Using PL/SQL in SQL Developer You can create, execute, and debug procedures, functions, packages, and triggers with SQL Developer. To create a PL/SQL object, such as a procedure, right-click the PL/SQL object type in the Object navigator. For example, click the Procedure node in the Object navigator and select New Procedure. The Create PL/SQL Procedure dialog box appears. Enter the header information for the procedure in the Create PL/SQL Procedure dialog box. Then click OK. Oracle Database 11g: SQL and PL/SQL New Features

44 Using PL/SQL in SQL Developer
1 2 3 4 5 Enter your code. Enter the header information for the procedure, and then click OK. Using PL/SQL in SQL Developer (continued) At the top of the Code Editor window, you have tools to help you run, compile, and debug your PL/SQL code. The icons represent: 1. Find 2. Run 3. Run in debug mode 4. Compile 5. Compile for debug mode In the Code Editor window, your header information is entered. Enter the code for the body of the PL/SQL object. In the example shown in the slide, the code for the HELLOWORLD procedure is entered. Note: To display the line numbers in the Code Editor, select Tools > Preferences > Code Editor > Line Gutter and select the Show Line Numbers option. Oracle Database 11g: SQL and PL/SQL New Features

45 Using PL/SQL in SQL Developer
Click Compile. Compilation messages are displayed on Messages – Log. Compiling PL/SQL in SQL Developer To compile your code, click the Compile icon in the Code Editor window. If your code compiles successfully, you see the message “Compiled” on the Messages tabbed page. If there are errors or warnings, you see “Compiled – (with errors)” on the Messages tabbed page. The details for the errors are on the Compiler tabbed page. If you have any errors, fix the errors and then recompile. Oracle Database 11g: SQL and PL/SQL New Features

46 Using PL/SQL in SQL Developer
Click Run. The Run dialog box appears with a call to your code wrapped within an anonymous block. Enter the parameter values, and then click OK. Executing PL/SQL Code To execute your PL/SQL code, click the Run icon. A Run PL/SQL dialog box appears. Within it, the call to your named PL/SQL block is wrapped in an anonymous block of code. Enter any variable values (which are converted to your parameter values in your stored block of code), and then click OK. Oracle Database 11g: SQL and PL/SQL New Features

47 Using PL/SQL in SQL Developer
Enter the parameter values, and then click OK. The results are displayed on the Running – Log tabbed page. Executing PL/SQL Code (continued) The results from running your code are displayed on the Running-Log tabbed page. Debugging PL/SQL Code To use the PL/SQL debugger in SQL Developer, you must compile the code in debug mode. Details for using the debugger are covered in Appendix C. Oracle Database 11g: SQL and PL/SQL New Features

48 Browsing Through the Available Search Engines
Enter a search word, and then press [Enter]. The results are displayed in your browser. Select a search engine. Browsing Through the Available Search Engines In SQL Developer 1.2, you have links to popular search engines and discussion forums. You can use the following search engines: Ask Tom Google Metalink Docs 10.2 docs 9.2 docs search.oracle.com OTN Forums In the example shown in the slide, the OTN Forums are searched for the word SQL Developer. A resulting list is displayed in your browser window. Oracle Database 11g: SQL and PL/SQL New Features

49 Oracle Database 11g: SQL and PL/SQL New Features 1 - 49
Changing Preferences From the Tools menu, select Preferences. The Preferences dialog box appears. Changing Preferences You can customize many aspects of the SQL Developer interface and environment by modifying the SQL Developer preferences according to your preferences and needs. To modify SQL Developer preferences, select Tools, then Preferences. Most preferences are self-explanatory. Some preferences involve performance or system resource trade-offs (for example, enabling a feature that adds execution time), and other preferences involve only personal aesthetic taste. The preferences are grouped into categories. In the example shown in the slide, the Code Editor preferences are displayed. Note that you can toggle your line numbers on and off using the Show Line Numbers check box. Oracle Database 11g: SQL and PL/SQL New Features

50 Oracle Database 11g: SQL and PL/SQL New Features 1 - 50
Creating Reports SQL Developer provides you with a number of predefined reports about your database and objects. The reports are organized into categories. You can create your own customized reports too. Creating Reports SQL Developer provides many reports about the database and its objects. These reports are grouped into the following categories: About Your Database reports Object reports Application Express reports Charts Database Administration reports Data Dictionary reports Jobs reports PL/SQL reports Security reports Streams reports Table reports XML reports To display a report, click the Reports tab, and then select the report type. You can also create your own user-defined reports. Oracle Database 11g: SQL and PL/SQL New Features

51 Oracle Database 11g: SQL and PL/SQL New Features 1 - 51
Creating Reports Examining Charts The example in the slide shows an Object Distribution chart report. Oracle Database 11g: SQL and PL/SQL New Features

52 Creating PL/SQL Reports
Select a connection, and then click OK. Select the PL/SQL report type. Creating PL/SQL Reports You can generate reports about your PL/SQL code. You can find out information about arguments, search source code for object name or text strings, and find out the length of your PL/SQL routines. In the example shown in the slide, the Search Source Code report is run. This is a handy report that enables you to find either text strings or object names in your PL/SQL code. Oracle Database 11g: SQL and PL/SQL New Features

53 Oracle Database 11g: SQL and PL/SQL New Features 1 - 53
Searching PL/SQL Code Specify either an Object Name search or a Text Search. Enter the value to search and click Apply. The results are displayed. Searching PL/SQL Code In this example, the PL/SQL code is searched for the text string “hello.” The results show the owner, the PL/SQL object name, code type, line number, and the text on that line number. Oracle Database 11g: SQL and PL/SQL New Features

54 Creating a User-Defined Report
Create and save user-defined reports for repeated use. Creating a User-Defined Report User-defined reports are any reports that are created by SQL Developer users. To create a user-defined report, perform the following steps: 1. Right-click the User Defined Reports node under Reports, and select Add Report. 2. In the Create Report Dialog box, specify the report name and the SQL query to retrieve information for the report. Then click Apply. In the example in the slide, the report name is specified as ORDERSBYREP. An optional description is provided indicating that the report contains sales orders organized by sales representatives. The complete SQL statement for retrieving the information to be displayed in the user-defined report is specified in the SQL box. You can also include an optional tool tip to be displayed when the cursor stays briefly over the report name in the Reports navigator display. You can organize user-defined reports in folders, and you can create a hierarchy of folders and subfolders. To create a folder for user-defined reports, right-click the User Defined Reports node or any folder name under that node and select Add Folder. Information about user-defined reports, including any folders for these reports, is stored in a file named UserReports.xml under the directory for user-specific information. Organize reports in folders Oracle Database 11g: SQL and PL/SQL New Features

55 Oracle Database 11g: SQL and PL/SQL New Features 1 - 55
Using SQL*Plus You can invoke the SQL*Plus command-line interface from SQL Developer. Close all SQL Worksheets to enable the SQL*Plus menu option. Provide the location of the sqlplus.exe file only for the first time you invoke SQL*Plus. Using SQL*Plus The SQL Worksheet supports most of the SQL*Plus statements. SQL*Plus statements must be interpreted by SQL Worksheet before being passed to the database; any SQL*Plus statements that are not supported by the SQL Worksheet are ignored and not passed to the database. To display the SQL*Plus command window, from the Tools menu, select SQL*Plus. To use this feature, the system on which you are using SQL Developer must have an Oracle home directory or folder, with a SQL*Plus executable under that location. If the location of the SQL*Plus executable is not already stored in your SQL Developer preferences, you are asked to specify its location. For example, these are some of the SQL*Plus statements that are not supported by SQL Worksheet: append archive attribute For a complete list of SQL*Plus statements that are supported, and not supported by SQL Worksheet, refer to the SQL*Plus Statements Supported and Not Supported in SQL Worksheet topic in the SQL Developer online Help. Note: You must use the Oracle SID in your SQL Developer connection in order for the SQL*Plus menu item to be enabled. Oracle Database 11g: SQL and PL/SQL New Features

56 Introducing Migration Through SQL Developer
Reduces the effort and risks involved in a migration project Enables you to migrate an entire third-party database, including triggers and stored procedures Enables you to see and compare the captured model and the converted model, and to customize each Provides feedback about the migration through reports Using SQL Developer for Migration You can migrate from a database, such as Microsoft SQL Server and Microsoft Access, to an Oracle database to take advantage of Oracle’s scalability, reliability, increased performance, and better security. Using SQL Developer to migrate a third-party database to an Oracle database provides the following benefits: Reduces the effort and risks involved in a migration project Enables you to migrate an entire third-party database, including triggers and stored procedures Enables you to see and compare the captured model and the converted model, and to customize each if you want, so that you can control how much automation there is in the migration process Provides feedback about the migration through reports Oracle Oracle Database 11g: SQL and PL/SQL New Features

57 Migration in SQL Developer
Destination Oracle schema SQL Developer Source data Captured model Converted Migration repository Using SQL Developer for Migration (continued) SQL Developer enables you to simplify the process of migrating a third-party database to an Oracle database. SQL Developer captures information from the source database and displays it in the captured model, which is a representation of the structure of the source database. This representation is stored in a migration repository, which is a collection of schema objects that SQL Developer uses to store migration information. The information in the repository is used to generate the converted model, which is a representation of the structure of the destination database as it will be implemented in the Oracle database. You can then use the information in the captured model and the converted model to compare database objects, identify conflicts with Oracle reserved words, and manage the migration progress. When you are ready to migrate, you generate the Oracle schema objects, and then migrate the data. SQL Developer contains logic to extract data from the data dictionary of the source database, create the captured model, and convert the captured model to the converted model. Oracle Database 11g: SQL and PL/SQL New Features

58 Oracle Database 11g: SQL and PL/SQL New Features 1 - 58
Summary In this lesson, you should have learned how to: List the key features of Oracle SQL Developer Install Oracle SQL Developer Create a database connection Navigate through the object navigator Use the SQL Worksheet Create, save, and use scripts Develop, compile, and debug PL/SQL Browse through the available search engines Change preferences Create reports Describe migration Summary In this lesson, you learned about the SQL Developer environment. You learned how to connect to an Oracle database, manage database objects, issue SQL statements, run scripts, develop PL/SQL code, change preferences, create reports, and describe the migration process. Oracle Database 11g: SQL and PL/SQL New Features

59 Practice 2 Overview: Using SQL Developer
This practice covers the following topics: Starting SQL Developer Creating a database connection in SQL Developer Executing SQL statements Setting up your script pathing preference Creating, compiling, and debugging a procedure Examining exporting Creating a SQL report Setting up and accessing SQL*Plus Practice 2: Using SQL Developer In this practice, you practice writing SQL and PL/SQL in the SQL Developer environment. Oracle Database 11g: SQL and PL/SQL New Features

60 Oracle Database 11g: SQL and PL/SQL New Features 1 - 60
Practice 2 1. Start SQL Developer. 2. Create a database connection using the following information: Connection Name: mydbconnection. Username: oen (where n is your assigned number) Password: oen Hostname: Enter the host name for your PC. Port: 1521 SID: ORCL (or the value provided to you by the instructor) 3. Test the new connection. If the Status is Success, connect to the database using this new connection. a. Click the Test button in the New/Select Database Connection window. b. If the status is Success, click the Connect button. 4. Browse the CUSTOMERS table and display its data. a. Expand the mydbconnection connection by clicking the plus sign next to it. b. Expand the Tables icon by clicking the plus sign next to it. c. Display the structure of the CUSTOMERS table. Click the CUSTOMERS table. The Columns tab displays the columns in the CUSTOMERS table as follows: Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

61 Oracle Database 11g: SQL and PL/SQL New Features 1 - 61
Practice 2 (continued) d. To display the customers’ data, click the Data tab. The CUSTOMERS table data is displayed as follows: 5. Use the SQL Worksheet to select the information for all line item orders whose ordered quantity is greater than 200. a. Display the SQL worksheet using any of the following two methods: i) Select Tools > SQL Worksheet or click the Open SQL Worksheet icon. The Select Connection window is displayed. ii) Select the new mydbconnection from the Connection drop-down list (if not already selected). b. Enter the following in the Enter SQL Statement box. SELECT order_id, line_item_id, product_id, unit_price, quantity FROM order_items WHERE quantity > 200 c. Click the Execute Statement icon (or [F9]). The results of the SQL statement are displayed in the Results region as follows: Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

62 Oracle Database 11g: SQL and PL/SQL New Features 1 - 62
Practice 2 (continued) 6. Set your script pathing preference to D:\Labs: a. Select Tools > Preferences > Database > Worksheet Parameters. b. Enter the value in the Select default path to look for scripts field. 7. Save the SQL statement to a script file using the File > Save menu item. a. Name the file ItemsGT200.sql. b. Place the file in your D:\Labs\labs folder. 8. Open and run the ItemsProdGT200.sql file from your D:\Labs\labs folder. Your output should match the following: 9. Create a stored procedure named PRINTIT. The procedure will print out a text string value that you pass into the program as an argument. The procedure will accept a second argument to hold the number of times that you want the text string value printed. CREATE OR REPLACE PROCEDURE printit (p_string VARCHAR2, p_int NUMBER) IS BEGIN FOR i IN 1..p_int LOOP DBMS_OUTPUT.PUT_LINE(p_string); END LOOP; END; / a. Create the procedure through the Object navigator. b. Compile the procedure. c. Run the procedure. Enter the parameter values 'hello world' for p_string and 3 for p_int in the Run PL/SQL dialog box. Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

63 Oracle Database 11g: SQL and PL/SQL New Features 1 - 63
Practice 2 (continued) d. If you have time, run the procedure in debug mode. Note: In the Oracle classroom, you must set your PL/SQL Debug port to Click Tools menu, select Preferences, and select Debugger. Select the Debugging Port Range check box. Enter 4000 for both the Minimum and Maximum fields. After this preference is set, you can use the PL/SQL Debugger. 10. Create a report named CUSTBYACCTMGR and save it to a folder named CUSTOMERREPORTS. a. Create a new folder named CUSTOMERREPORTS. On the Reports tabbed page, right-click the User Defined Reports node and select Add Folder. b. Enter the name CUSTOMERREPORTS. Add a description. c. Right-click the CUSTOMERREPORTS node and select Add Report. d. Name the report CUSTBYACCTMGR. e. Enter the following query: SELECT COUNT(*), account_mgr_id FROM customers GROUP BY account_mgr_id 11. Exit SQL Developer. Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

64 Notes Page Only

65 Using the Language Functionality Enhancements

66 Oracle Database 11g: SQL and PL/SQL New Features 1 - 66
Objectives After completing this lesson, you should be able to: Use the new regular expression support functions Track dependencies at the element level Find and fix exception handlers that do not pass the exception upward Dispatch an overridable object type method Learn about Data Change Notification (DCN) result-set-change notification Use the lock enhancements: Use the LOCK TABLE … WAIT new syntax Set the DDL_LOCK_TIMEOUT parameter Objectives In this lesson, you learn about some of the language functionality enhancements added to Oracle Database 11g. After completing this lesson, you should be able to: Use the new regular expression support functions to find subpatterns and count the occurrences of patterns Track dependencies at the element level Find and fix exception handlers that do not pass the exception upward to the calling program or environment Dispatch an overridable object type method using the ANSI SQL 2003 standard for supertypes Use Data Change Notification to publish result-set-change notifications, which result from data manipulation language (DML) or data definition language (DDL) changes to the result set associated with the registered queries Use the WAIT option for DDL statements Use the LOCK TABLE new syntax that enables you to specify the maximum number of seconds the statement should wait to obtain a DML lock on the table Set the DDL_LOCK_TIMEOUT parameter to identify a time limit for how long DDL statements will wait in a DML lock queue Oracle Database 11g: SQL and PL/SQL New Features

67 Oracle Database 11g: SQL and PL/SQL New Features 1 - 67
Lesson Agenda Using the new regular expression support functions Tracking dependencies at the element level Fixing exception handlers that do not pass the exception upward Dispatching an overridable object type method Learning about Data Change Notification (DCN) result-set-change notification Utilizing the lock enhancements Using the LOCK TABLE … WAIT new syntax Setting the DDL_LOCK_TIMEOUT parameter Oracle Database 11g: SQL and PL/SQL New Features

68 Regular Expression Enhancements in SQL and PL/SQL
Features added: Access to the n-th subexpression in the REGEXP_INSTR and REGEXP_SUBSTR functions Return the number of times a pattern match is found in an input string using the new REGEXP_COUNT function Benefits: Extends the current regular expression functionality based on customer feedback Decreases the number of calls to the regular expression functions in order to get related information Enhancements in Regular Expression Support Regular expression support was introduced in Oracle Database 10g. Its functionality is popular and heavily used, generating ideas for further enhancements. Customers have requested these enhancements to extend the regular expression functionality: Access to the n-th subexpression Count of the number of times a pattern is found Oracle Database 11g: SQL and PL/SQL New Features

69 Understanding Subexpressions
Examine this expression: The subexpressions are: (1 2 3)(4(5 6)(7 8)) (1 2 3)(4(5 6)(7 8)) 3 4 1 2 Understanding Subexpressions Before using the new regular expression support parameter to access a subexpression, it is important to understand how the subexpressions are interpreted. In the example shown in the slide, a string of digits is shown. The parentheses identify the subexpressions within the string of digits. Reading from left to right, and from inner parenthesis to the outer parenthesis, the subexpressions in the string of digits are: 1. 123 3. 56 4. 78 You can search for any of those subexpressions with the REGEXP_INSTR and REGEXP_SUBSTR functions. Oracle Database 11g: SQL and PL/SQL New Features

70 Using Subexpressions with Regular Expression Support
SELECT REGEXP_INSTR (' ', source char or search value '(123)(4(56)(78))', -- regular expression patterns 1, position to start searching 1, occurrence 0, return option 'i', match option (case insensitive) 1) subexpression on which to search "Position" FROM dual; Position 2 1 2 3 4 5 6 7 Understanding Subexpressions in Regular Expression Support In the example shown in the slide, you may want to search for the first subexpression pattern in your list of subexpressions. The example shown identifies several parameters for the REGEXP_INSTR function. 1. The string you are searching is identified. 2. The subexpressions are identified. The first subexpression is “123.” The second subexpression is “45678,” the third is “56,” and the fourth is “78.” 3. The third parameter identifies from which position to start searching. 4. The fourth parameter identifies the occurrence of the pattern that you want to find; “1” means find the first occurrence. 5. The fifth parameter is the return option. This is the position of the first character of the occurrence. (If you specify 1, the position of the character following the occurrence is returned.) 6. The sixth parameter identifies whether your search should be case sensitive or not. 7. The last parameter is the new parameter added in Oracle Database 11g. This parameter specifies the subexpression that you want to find. In the example shown, you are searching for the first subexpression, which is “123.” REGEXP_INSTR and REGEXP_SUBSTR now have an optional subexpr parameter that lets you target a particular substring of the regular expression being evaluated. Oracle Database 11g: SQL and PL/SQL New Features

71 Using Subexpressions with Regular Expression Support: More Examples
SELECT REGEXP_INSTR(' ', '(123)(4(56)(78))', 1, 1, 0, 'i', 2) "Position" FROM dual; Position 5 4) "Position" 8 1 2 More Examples In the first example shown in the slide, the second subexpression is searched for in the string. The results show that this subexpression is found in the fifth position. In the second example, the fourth subexpression is searched for in the string. The results show that this subexpression is found in the eighth position. Oracle Database 11g: SQL and PL/SQL New Features

72 Why Access the n-th Subexpression?
A more realistic use: DNA sequencing You may need to find a specific subpattern that identifies a protein needed for immunity in mouse DNA. CREATE OR REPLACE FUNCTION get_instrsubexp_pos (p_subexp NUMBER)RETURN NUMBER IS v_dna CLOB; v_location NUMBER; BEGIN v_dna := 'ccacctttccctccactcctcacgttctcacctgtaaagcgtccctccctcatccccatgcccccttac cctgcagggtagagtaggctagaaaccagagagctccaagctccatctgtggagaggtgccatccttgggctgcagagagag gagaatttgccccaaagctgcctgcagagcttcaccacccttagtctcacaaagccttgagttcatagcatttctt gagttttcaccctgcccagcaggacactgcagcacccaaagggcttcccaggagtagggttgccctcaagaggctc ttgggtctgatggccacatcctggaattgttttcaagttgatggtcacagccctgaggcatgtaggggcgtgggga tgcgctctgctctgctctcctctcctgaacccctgaaccctctggctaccccagagcacttagagccag'; v_location := REGEXP_INSTR(v_dna, '(gtc(tcac)(aaag))', 1, 1, 0, 'i', p_subexp); RETURN (v_location); END; Example of the New Argument to the REGEXP_INSTR Function In life sciences, you may need to extract the offsets of subexpression matches from a DNA sequence for further processing. For example, you may need to find a specific protein sequence, such as the begin offset for the DNA sequence preceded by gtc, followed by tcac and by aaag. To accomplish this goal, you can use the REGEXP_INSTR function, which returns the position where a match is found. This enhancement lets you target a particular substring of the regular expression being evaluated. Oracle Database 11g: SQL and PL/SQL New Features

73 REGEXP_INSTR: Examples
SQL> EXECUTE dbms_output.put_line(get_instrsubexp_pos(1)); 197 PL/SQL procedure successfully completed. SQL> EXECUTE dbms_output.put_line(get_instrsubexp_pos(2)); 200 SQL> EXECUTE dbms_output.put_line(get_instrsubexp_pos(3)); 204 1 2 3 REGEXP_INSTR: Examples In the first example, the position of the first subexpression (gtc) of the expression (gtc(tcac)(aaag)) is returned. gtc appears starting in position 197 of the DNA string. In the second example, the position of the second subexpression (tcac) of the expression (gtc(tcac)(aaag)) is returned. tcac appears starting in position 200 of the DNA string. In the third example, the position of the third subexpression (aaag) of the expression (gtc(tcac)(aaag)) is returned. aaag appears starting in position 204 of the DNA string. Oracle Database 11g: SQL and PL/SQL New Features

74 REGEXP_SUBSTR: Example
REGEXP_SUBSTR searches for a regular expression pattern within a given string and returns the matched string. SELECT REGEXP_SUBSTR ('acgctgcactgca', -- source char or search value 'acg(.*)gca', regular expression pattern 1, position to start searching 1, occurrence 'i', match option (case insensitive) 1) subexpression “Value" FROM dual; Value ------ ctgact 1 2 3 4 5 6 REGEXP_SUBSTR: Example The extended functionality in the REGEXP_SUBSTR function enables you to search a string for a regular expression pattern with the support of the additional subexpression parameter. This works in the same manner as the enhancement to the REGEXP_INSTR described previously. A new parameter identifies which subexpression in the pattern is returned by the call. In the example shown in the slide: 1. acgctgcactgca is the source to be searched. 2. acg(.*)gca is the pattern to be searched. Find acg followed by gca with potential characters between the acg and the gca. 3. Start searching at the first character of the source. 4. Search for the first occurrence of the pattern. 5. Use case-insensitive matching on the source. 6. A nonnegative integer value that identifies the n-th subexpression to be targeted. This is the new subexpression parameter. In this example, 1 indicates the first subexpression. You can use a value from 0-9. A zero means that no subexpression is targeted. The default value for this parameter is 0. Oracle Database 11g: SQL and PL/SQL New Features

75 REGEXP_COUNT Function
Returns the number of times a pattern appears in a string CREATE OR REPLACE FUNCTION get_subexp_count (p_subexp VARCHAR2)RETURN NUMBER IS v_dna CLOB; v_count NUMBER; BEGIN v_dna := 'ccacctttccctccactcctcacgttctcacctgtaaagcgtccctccctcatccccatgcccccttaccctgcag ggtagagtaggctagaaaccagagagctccaagctccatctgtggagaggtgccatccttgggctgcagagagaggagaat ttgccccaaagctgcctgcagagcttcaccacccttagtctcacaaagccttgagttcatagcatttcttgagttttcacc ctgcccagcaggacactgcagcacccaaagggcttcccaggagtagggttgccctcaagaggctcttgggtctgatggcca catcctggaattgttttcaagttgatggtcacagccctgaggcatgtaggggcgtggggatgcgctctgctctgctctcct ctcctgaacccctgaaccctctggctaccccagagcacttagagccag'; v_count := REGEXP_COUNT(v_dna, p_subexp); RETURN (v_count); END; Example of REGEXP_COUNT REGEXP_COUNT is a new function that counts the number of times a pattern appears in a string. In the following examples, the number of occurrences for different DNA substrings is determined: EXECUTE dbms_output.put_line(get_subexp_count('gtctcacaaag')) 1 PL/SQL procedure successfully completed. EXECUTE dbms_output.put_line(get_subexp_count('gtc')) 4 EXECUTE dbms_output.put_line(get_subexp_count('tcac')) 6 EXECUTE dbms_output.put_line(get_subexp_count('aaag')) Oracle Database 11g: SQL and PL/SQL New Features

76 Oracle Database 11g: SQL and PL/SQL New Features 1 - 76
Lesson Agenda Using the new regular expression support functions Tracking dependencies at the element level Fixing exception handlers that do not pass the exception upward Dispatching an overridable object type method Learning about Data Change Notification (DCN) result-set-change notification Utilizing the lock enhancements Using the LOCK TABLE … WAIT new syntax Setting the DDL_LOCK_TIMEOUT parameter Oracle Database 11g: SQL and PL/SQL New Features

77 More Precise Dependency Metadata
Earlier releases recorded dependency metadata. Oracle Database 11g records additional, finer-grained dependency management. Prior to Oracle Database 11g, adding column D to table T invalidated the dependent objects. Starting in Oracle Database 11g, adding column D to table T does not impact view V and does not invalidate the dependent objects. Fine-Grained Dependencies Starting with Oracle Database 11g, you have access to records that describe more precise dependency metadata. This is called fine-grain dependencies and it enables you to see when dependent objects are not invalidated without logical requirement. Earlier Oracle Database releases recorded dependency metadata—for example, that PL/SQL unit P depends on PL/SQL unit F, or that view V depends on table T—with the precision of the whole object. This means that dependent objects are sometimes invalidated without logical requirement. For example, if view V depends only on columns A and B in table T, and column D is added to table T, the validity of view V is not logically affected. Nevertheless, before Oracle Database 11g, Release 11.1, view V was invalidated by the addition of column D to table T. With Oracle Database 11g, Release 11.1, adding column D to table T does not invalidate view V. Similarly, if procedure P depends only on elements E1 and E2 within a package, adding element E99 to the package does not invalidate procedure P. Reducing the invalidation of dependent objects in response to changes to the objects on which they depend increases application availability, both in the development environment and during online application upgrade. Table T Column A Column B View V Column A Column B Procedure P Function F Add Column D Oracle Database 11g: SQL and PL/SQL New Features

78 Fine-Grain Dependency Management
In Oracle Database 11g, dependencies are tracked at the level of element within unit. Element-based dependency tracking covers the following: Dependency of a single-table view on its base table Dependency of a PL/SQL program unit (package specification, package body, or subprogram) on the following: Other PL/SQL program units Tables Views Oracle Database 11g: SQL and PL/SQL New Features

79 SQL Fine-Grain Dependency Management: Example
1 CREATE TABLE t (col_a NUMBER, col_b NUMBER, col_c NUMBER); CREATE VIEW v AS SELECT col_a, col_b FROM T; SELECT ud.name, ud.type, ud.referenced_name, ud.referenced_type, uo.status FROM user_dependencies ud, user_objects uo WHERE ud.name = uo.object_name AND ud.name = 'V'; NAME TYPE REFERENCED_NAME REFERENCED_TYPE STATUS V VIEW T TABLE VALID ALTER TABLE t ADD (col_d VARCHAR2(20)); SELECT ud.name, ud.type, ud.referenced_name, ud.referenced_type, uo.status FROM user_dependencies ud, user_objects uo WHERE ud.name = uo.object_name AND ud.name = 'V'; NAME TYPE REFERENCED_NAME REFERENCED_TYPE STATUS V VIEW T TABLE VALID 2 Example of Dependency of a Single-Table View on Its Base Table In the first example in the slide, table T is created with three columns, COL_A, COL_B, and COL_C. A view named V is created based on columns COL_A and COL_B of table T. The dictionary views are queried and the view V is dependent on table T and its status is valid. In the second example, table T is altered. A new column named COL_D is added. The dictionary views still report that the view V is dependent because element-based dependency tracking realizes that the columns COL_A and COL_B are not modified and, therefore, the view does not need to be invalidated. Oracle Database 11g: SQL and PL/SQL New Features

80 SQL Fine-Grain Dependency Management: Example
CREATE TABLE t (col_a NUMBER, col_b NUMBER, col_c NUMBER); CREATE OR REPLACE VIEW v AS SELECT col_a, col_b FROM T; SELECT ud.name, ud.referenced_name, ud.referenced_type, uo.status FROM user_dependencies ud, user_objects uo WHERE ud.name = uo.object_name AND ud.name = 'V'; NAME REFERENCED_NAME REFERENCED_TYPE STATUS V VIEW T TABLE VALID ALTER TABLE t MODIFY (col_a VARCHAR2(20)); V VIEW T TABLE INVALID Example of Dependency of a Single-Table View on Its Base Table (continued) Examine the scenario shown in the slide. In this example, the view is invalidated because its element (COL_A) is modified in the table on which the view is dependent. Oracle Database 11g: SQL and PL/SQL New Features

81 PL/SQL Fine-Grain Dependency Management: Example
CREATE PACKAGE sample_pkg IS PROCEDURE p1; END sample_pkg; / CREATE PROCEDURE my_proc BEGIN sample_pkg.p1(); END my_proc ; CREATE OR REPLACE PACKAGE sample_pkg PROCEDURE unheard_of; SELECT status FROM user_objects WHERE object_name = 'MY_PROC'; STATUS VALID Example of Dependency of a PL/SQL Program Unit on Other PL/SQL Program Units Fine-grain dependency management reduces the need to recompile referencing subprograms when a package specification changes. Prior to Oracle Database 11g, changes to the package body did not require recompilation of dependent constructs, whereas changes to the package specification required the recompilation of every stored subprogram that references the package. Oracle Database 11g reduces this dependency. Dependencies are now tracked at the level of element within unit. In the example shown in the slide, you create a package named SAMPLE_PKG that has a subroutine procedure named P1. Another procedure named MY_PROC invokes SAMPLE_PKG.P1. The definition of the package SAMPLE_PKG is modified and another subroutine is added to the package declaration. When you query the USER_OBJECTS dictionary view for the status of the MY_PROC procedure, it is still valid because the element you added to the definition of SAMPLE_PKG is not referenced through the MY_PROC procedure. Oracle Database 11g: SQL and PL/SQL New Features

82 Oracle Database 11g: SQL and PL/SQL New Features 1 - 82
Lesson Agenda Using the new regular expression support functions Tracking dependencies at the element level Fixing exception handlers that do not pass the exception upward Dispatching an overridable object type method Learning about Data Change Notification (DCN) result-set-change notification Utilizing the lock enhancements Using the LOCK TABLE … WAIT new syntax Setting the DDL_LOCK_TIMEOUT parameter Oracle Database 11g: SQL and PL/SQL New Features

83 Oracle Database 11g: SQL and PL/SQL New Features 1 - 83
PLW Warning A new PLW warning is available to you. This warning means that the OTHERS handler of your PL/SQL subroutine can exit without executing some form of RAISE or a call to the standard RAISE_APPLICATION_ERROR procedure. Good programming practices suggest that the OTHERS handler must always pass an exception upward. Tips for Exception Handling As good programming practice, you should have your OTHERS exception handler pass the exception upward to the calling subroutine. If you fail to add this functionality, you run the risk of having exceptions go unnoticed. To avoid this flaw in your code, you can turn on warnings for your session and recompile the code that you want to verify. If the OTHERS handler does not handle the exception, the PLW warning informs you. Oracle Database 11g: SQL and PL/SQL New Features

84 Oracle Database 11g: SQL and PL/SQL New Features 1 - 84
PLW Warning: Example CREATE OR REPLACE PROCEDURE p (i IN VARCHAR2) IS BEGIN INSERT INTO t(col_a) VALUES (i); EXCEPTION WHEN OTHERS THEN null; END p; / ALTER PROCEDURE P COMPILE PLSQL_warnings = 'enable:all' REUSE SETTINGS; SP2-0805: Procedure altered with compilation warnings SQL> SHOW ERRORS Errors for PROCEDURE P: LINE/COL ERROR 6/ PLW-06009: procedure "P" OTHERS handler does not end in RAISE or RAISE_APPLICATION_ERROR PLW Warning: Example In the example shown in the slide, the procedure P contains an exception handler with a WHEN OTHERS clause. The WHEN OTHERS clause catches for all exceptions in this example and the exception is not passed upward. You can turn on warnings by issuing: ALTER SESSION SET PLSQL_WARNINGS = 'enable:all'; If warnings are enabled, you receive the PLW warning for procedure P when you compile it. If you are using SQL*Plus, the SHOW ERRORS reports the warning. If you are using SQL Developer, you see the warnings when you compile the code. To avoid running into this warning with the code shown in the slide, you can add the RAISE APPLICATION ERROR function into the exception handler. CREATE OR REPLACE PROCEDURE p(i IN VARCHAR2) IS BEGIN INSERT INTO t(col_a) VALUES (i); EXCEPTION WHEN OTHERS THEN RAISE_APPLICATION_ERROR (20001, 'Error in proc P.'); END p; Oracle Database 11g: SQL and PL/SQL New Features

85 Oracle Database 11g: SQL and PL/SQL New Features 1 - 85
Lesson Agenda Using the new regular expression support functions Tracking dependencies at the element level Fixing exception handlers that do not pass the exception upward Dispatching an overridable object type method Learning about Data Change Notification (DCN) result-set-change notification Utilizing the lock enhancements Using the LOCK TABLE … WAIT new syntax Setting the DDL_LOCK_TIMEOUT parameter Oracle Database 11g: SQL and PL/SQL New Features

86 Support for Generalized Invocation
Provides the ability to statically dispatch an overridable object type method Is compliant with ANSI SQL 2003 Adds new syntax to PL/SQL to support this feature A Supertype B Subtype of A Supertype of C D Subtype of A Support for Supertype Starting with Oracle Database 11g, you can invoke the method of the supertype or parent, rather than the specific member function. This support for supertypes is an enhancement that follows the ANSI SQL 2003 standard for supertypes. C Subtype of B Oracle Database 11g: SQL and PL/SQL New Features

87 Support for Generalized Invocation: Example
The Employee_t supertype has an overridable show_data() member function that displays generic employee information: CREATE OR REPLACE TYPE employee_t AS OBJECT( emp_id NUMBER, emp_last_name VARCHAR2(100), emp_salary NUMBER, MEMBER PROCEDURE show_data) NOT FINAL NOT INSTANTIABLE / CREATE OR REPLACE TYPE BODY employee_t IS MEMBER PROCEDURE show_data IS BEGIN DBMS_OUTPUT.PUT_LINE('Employee ID: ' || emp_id || ' Last name: ' || emp_last_name || ' Salary: ' || emp_salary); END show_data; END; Support for Supertypes: Example Scenario You want to set up an object-oriented employee model. You need a noninstantiable EMPLOYEE_T supertype. It is not instantiable because an actual employed person is always some specialized kind, such as a sales person or a developer. The supertype includes common attributes amongst the different employees, such as ID, name, and salary. The supertype has a method called SHOW_DATA to show these common attributes. You have two subtypes under EMPLOYEE_T: SALESPERSON_T and DEVELOPER_T. Each needs its own specific attributes. The SALESPERSON_T type requires a territory to identify his or her sales area. The DEVELOPER_T type requires a product specialty that he or she manages. In your program, you want to step through a list of employees and print the common characteristics as well as the specialization for each type. The code in the slide shows the implementation of the EMPLOYEE_T supertype. Oracle Database 11g: SQL and PL/SQL New Features

88 Support for Generalized Invocation: Example
The Salesperson_t subtype specializes the show_data() method to acknowledge notions such as territory covered: CREATE OR REPLACE TYPE salesperson_t UNDER employee_t ( territory VARCHAR2(100), OVERRIDING MEMBER PROCEDURE show_data) FINAL / CREATE OR REPLACE TYPE BODY salesperson_t IS OVERRIDING MEMBER PROCEDURE show_data IS BEGIN -- First form of the new syntax. show_data((SELF AS employee_t)); DBMS_OUTPUT.PUT_LINE('Sales Territory: ' || territory); END show_data; END; Support for Supertypes: Example (continued) In the example shown in the slide, the SALESPERSON_T type is created with a TERRITORY attribute and a customized SHOW_DATA method. The SHOW_DATA method of the SALESPERSON_T subtype performs the common action as defined in the SHOW_DATA method of the EMPLOYEE_T object. You are calling the supertype from its subtype specification. This new syntax is introduced in Oracle Database 11g: (show_data (SELF AS employee_t)) Oracle Database 11g: SQL and PL/SQL New Features

89 Support for Generalized Invocation: Example
The Developer_t subtype specializes the show_data() method to acknowledge notions such as product area of responsibility: CREATE OR REPLACE TYPE developer_t UNDER employee_t( product_line VARCHAR2(100), OVERRIDING MEMBER PROCEDURE show_data) FINAL / CREATE OR REPLACE TYPE BODY developer_t IS OVERRIDING MEMBER PROCEDURE show_data IS BEGIN -- Second form (and more natural form) of the new syntax. (SELF AS employee_t).show_data(); DBMS_OUTPUT.PUT_LINE('Product Area: '|| product_line); END show_data; END; Support for Supertypes: Example (continued) You can use an external form of generalized invocation to execute methods of a specific type in the type hierarchy, rather than executing the most specific member methods from code that is outside the type hierarchy methods. In the example shown in the slide, the DEVELOPER_T type is created with a PRODUCT_LINE attribute and a customized SHOW_DATA method. The invocation to the SHOW_DATA method in the code shown identifies the method of the specific type of EMPLOYEE_T in the hierarchy: (SELF AS employee_t).show_data(); Oracle Database 11g: SQL and PL/SQL New Features

90 Support for Generalized Invocation: Example
Create a tester program to try out the new functionality: CREATE OR REPLACE PROCEDURE Test_It IS TYPE employee_List_t IS TABLE OF employee_t INDEX BY PLS_INTEGER; emp_Objects employee_List_t; BEGIN -- create some sample data for this example: emp_objects(1) := salesperson_t(35, 'Patel', 90000, 'India' ); emp_Objects(2) := salesperson_t(30, 'Jones', 92000, 'UK'); emp_objects(3) := developer_t(25, 'Cline', 96000, '11g XML'); emp_Objects(4) := developer_t(20, 'Smith', 97000, '11g PL/SQL'); FOR j IN 1..emp_objects.Count() LOOP DBMS_OUTPUT.PUT_LINE('Employee Report for: '); emp_Objects(j).show_data(); DBMS_OUTPUT.PUT_LINE(chr(10)); END LOOP; END; / BEGIN Test_It(); END; Support for Supertypes: Example (continued) The results from executing the code shown in the slide are: Employee Report for: Employee ID: 35 Last name: Patel Salary: 90000 Sales Territory: India Employee ID: 30 Last name: Jones Salary: 92000 Sales Territory: UK Employee ID: 25 Last name: Cline Salary: 96000 Product Area: 11g XML Employee ID: 20 Last name: Smith Salary: 97000 Product Area: 11g PL/SQL PL/SQL procedure successfully completed. Oracle Database 11g: SQL and PL/SQL New Features

91 Oracle Database 11g: SQL and PL/SQL New Features 1 - 91
Lesson Agenda Using the new regular expression support functions Tracking dependencies at the element level Fixing exception handlers that do not pass the exception upward Dispatching an overridable object type method Learning about Data Change Notification (DCN) result-set-change notification Utilizing the lock enhancements Using the LOCK TABLE … WAIT new syntax Setting the DDL_LOCK_TIMEOUT parameter Oracle Database 11g: SQL and PL/SQL New Features

92 Overview of Data Change Notification Enhancements
Pre-Oracle Database 11g, Release 11.1: Only object-change notifications, which result from DML or DDL changes to the objects associated with the registered queries are published. Starting with Oracle Database 11g, Release 11.1: Result-set-change notifications, which result from DML or DDL changes to the result set associated with the registered queries are published. New static data dictionary views allow you to see which queries are registered for result-set-change notifications. Data Change Notification for Result Sets Continuous Query Notification (CQN) enables you to set up client applications to register queries with the database and receive either object-change notifications or result-set-change notifications. Object-change notifications (the default) result from DML or DDL changes to the objects associated with the queries. Result-set-change notifications result from DML or DDL changes to the result set associated with the queries. The database publishes the notifications when the DML or DDL transaction commits. This is useful for an application that caches query result sets on mostly read-only objects in the middle tier to avoid network round trips to the database. Such an application can create a registration on the queries it is interested in caching using the change notification service. On changes to objects referenced inside those queries, the database publishes a change notification when the underlying transaction commits. In response to the notification, the application can refresh its cache by reexecuting the queries. Oracle Database 11g: SQL and PL/SQL New Features

93 Oracle Database 11g: SQL and PL/SQL New Features 1 - 93
Data Change Notification for Result Sets (continued) For example, if you have a Web forum application, your users might not need to view new content as soon as it is inserted into the back-end database. This type of an application is intrinsically tolerant of slightly out-of-date data, and can benefit from caching in the middle tier. CQN helps in this scenario in keeping the cache updated with the back-end database. In order to use CQN, you need the CHANGE NOTIFICATION privilege, the EXECUTE ON DBMS_CQ_NOTIFICATION privilege, and you need to enable job queue processes to receive notifications. For details on how CQN is implemented using PL/SQL and the DBMS_CQ_NOTIFICATION package, refer to Oracle Database PL/SQL Packages and Types Reference 11g, Release 1 (11.1). Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

94 Data Change Notification Process
Middle Tier Oracle Database Web cache 2 Registration via PL/SQL or OCI 8 1 Data Dictionary User objects 9 5 DML 3 Invalidation queue Job Queue process Client application 4 6 Data Change Notification Process 1. In this example, assume that the application has cached the result set of a query on OE.ORDERS. You create a registration for the query on OE.ORDERS using the CQN PL/SQL interface. In addition, you create a stored PL/SQL procedure to process notifications and supply the server-side PL/SQL procedure as the notification handler. 2. The database populates the registration information in the data dictionary. 3. A user modifies one of the registered objects with DML statements and commits the transaction. For example, a user updates a row in the OE.ORDERS table on the back-end database. The data for OE.ORDERS cached in the middle tier is now stale. 4. Oracle Database adds a message that describes the change to an internal queue. 5. A JOBQ background process is notified of a new change notification message. 6. The JOBQ process executes the stored procedure specified by the client application. In this example, JOBQ passes the data to a server-side PL/SQL procedure. The implementation of the PL/SQL callback procedure determines how the notification is handled. 7. Inside the server-side PL/SQL procedure, you can implement logic to notify the middle-tier client application of the changes to the registered objects. For example, it notifies the application of the ROWID of the changed row in OE.ORDERS. 8. The middle-tier application queries the back-end database to retrieve the changed data. 9. The client application updates the cache with the new data. 7 PL/SQL Client notification Oracle Database 11g: SQL and PL/SQL New Features

95 Data Change Notification
SELECT order_id, order_total FROM orders WHERE sales_rep_id = 158; With object change notification: DNS generates an object-change notification for this query for any DML or DDL change to the ORDERS table, even if the changed row or rows did not satisfy the query predicate (for example, if sales_rep_id = 160) With result-set-change notification: DNS generates a result-set-change notification only if the query result set itself changed and both of the following are true: The changed row or rows satisfy the query predicate (sales_rep_id = 158) either before or after the change. The change affected at least one of the columns in the SELECT list (order_id or order_total), as the result of either an UPDATE or an INSERT. Data Change Notification Examine this query to see an example of the difference between object-change notifications and result-set-change notifications. SELECT customer_id, cust_first_name, cust_last_name FROM customers WHERE credit_limit = 1400; Domain Name Service (DNS), which is a system for naming computers and network services that is organized into a hierarchy of domains, generates an object-change notification for this query for any DML or DDL change to the CUSTOMERS table, even if the changed row or rows did not satisfy the query predicate (for example, if credit_limit = 1200). DNS generates a result-set-change notification for this query only if the query result set itself changed; which means that both of the following are true: The changed row or rows satisfy the query predicate (credit_limit = 1400) either before or after the change. The change affected at least one of the columns in the SELECT list (customer_id or cust_first_name or cust_last_name), as the result of either an UPDATE or an INSERT statement. Oracle Database 11g: SQL and PL/SQL New Features

96 Data Dictionary Views for CQN
To see top-level information about all registrations: DBA_CHANGE_NOTIFICATION_REGS USER_CHANGE_NOTIFICATION_REGS To see which queries are registered for result-set-change notifications: DBA_CQ_NOTIFICATION_QUERIES USER_CQ_NOTIFICATION_QUERIES Dictionary Views for CQN If your application uses CQN, Oracle Database can publish a notification when a change occurs to registered objects with details on what changed. In response to the notification, your application can refresh cached data by fetching it from the back-end database. There are several dictionary views that you can query to see the status of CQN. Two dictionary views are added in Oracle Database 11g to support result-set-change notifications. These dictionary views are: DBA_CQ_NOTIFICATION_QUERIES USER_CQ_NOTIFICATION_QUERIES These views contain the query ID, query text, and registration ID values. Oracle Database 11g: SQL and PL/SQL New Features

97 Oracle Database 11g: SQL and PL/SQL New Features 1 - 97
Lesson Agenda Using the new regular expression support functions Tracking dependencies at the element level Fixing exception handlers that do not pass the exception upward Dispatching an overridable object type method Learning about Data Change Notification (DCN) result-set-change notification Utilizing the lock enhancements Using the LOCK TABLE … WAIT new syntax Setting the DDL_LOCK_TIMEOUT parameter Oracle Database 11g: SQL and PL/SQL New Features

98 Using the LOCK TABLE Statement with the WAIT Option
Use the WAIT option to identify the maximum number of seconds a statement should wait to obtain a DML lock on the table. There is no limit on the number of seconds. A message is returned indicating that the object is already locked. LOCK TABLE … WAIT The LOCK TABLE statement has new syntax that enables you to specify the maximum number of seconds a statement should wait to obtain a DML lock on a table. The new syntax is the WAIT clause. When you specify the WAIT clause, you identify the number of seconds your table should wait to have a DML lock on it. By default, the option is to wait indefinitely. However, if you specify the NOWAIT option, you are returned control immediately if the table is already locked. You receive a message indicating that the table is already locked by another user. Oracle Database 11g: SQL and PL/SQL New Features

99 Using the LOCK TABLE Statement with the WAIT Option: Example
In session #1: Time In session #2: LOCK TABLE orders IN EXCLUSIVE MODE; 01.01 01.05 01.15 01.20 01.25 01.30 01.35 01.40 01.45 01.50 01.55 02.00 02.05 02.10 02.15 02.20 02.25 02.30 02.35 LOCK TABLE orders IN EXCLUSIVE MODE WAIT 60; ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired LOCK TABLE … WAIT: Example In the example shown in the slide, the ORDERS table is locked in session 1. In session 2, a user tries to put a lock on the same ORDERS table, but specifies to wait 60 seconds. This means that if the table is already locked by another user, session 2 will wait 60 seconds for the lock. If the lock in the other session is not released after 60 seconds, an appropriate error message is returned to the session 2 user. Oracle Database 11g: SQL and PL/SQL New Features

100 Setting the DDL_LOCK_TIMEOUT Parameter
Use the DDL_LOCK_TIMEOUT parameter to specify a DDL lock timeout. The permissible range of values for DDL_LOCK_TIMEOUT is 0 through 1,000,000 (in seconds). The default is 0. You can set DDL_LOCK_TIMEOUT at the: System level Session level (with an ALTER SESSION statement) DDL_LOCK_TIMEOUT Parameter When you execute DDL statements, these statements require exclusive locks on internal structures. If these locks are unavailable when a DDL statement runs, the DDL statement fails, although it might have succeeded if it had been executed subseconds later. To enable DDL statements to wait for locks, you can specify a DDL lock timeout. This parameter controls the number of seconds that a DDL command waits for its required locks before failing. Oracle Database 11g: SQL and PL/SQL New Features

101 Setting the DDL_LOCK_TIMEOUT Parameter: Example
System level: Session level: DDL_LOCK_TIMEOUT = 50000 ALTER SESSION SET DDL_LOCK_TIMEOUT = 50000; DDL_LOCK_TIMEOUT: Example The default value of zero indicates a status of NOWAIT. The maximum value of 1,000,000 seconds results in the DDL statement waiting forever to acquire a DML lock. If a lock is not acquired before the timeout period expires, the default behavior is for an error to be returned. However, you can change that behavior at the session level (but not systemwide) so that all sessions holding the lock the DDL is waiting for are killed. Oracle Database 11g: SQL and PL/SQL New Features

102 Oracle Database 11g: SQL and PL/SQL New Features 1 - 102
Summary In this lesson, you should have learned how to: Use the new regular expression support functions Track dependencies at the element level Find and fix exception handlers that do not pass the exception upward Dispatch an overridable object type method Learn about Data Change Notification (DCN) result-set-change notification Use the lock enhancements Summary In this lesson, you learned about the language functionality enhancements added to the Oracle Database 11g release. You have more options with regular expressions. You can track dependencies at a finer-grained level. You can identify exception handlers that do not pass exceptions upward. You can use the ANSI SQL 2003 standard for supertypes. You can view the DCN notifications, and you can specify a timeout on requesting locks. Oracle Database 11g: SQL and PL/SQL New Features

103 Practice 3 Overview: Using the Language Functionality Enhancements
This practice covers the following topics: Using the regular expression support new functions for find and count patterns Tracking dependencies at the element level Writing code that causes the new PLW warning and then fix the code Trying the WAIT option for DDL statements Practice 3 Overview: Using the Language Functionality Enhancements In this practice, you write code that uses various language functionality enhancements added to Oracle Database 11g. This practice uses the OE schema. Oracle Database 11g: SQL and PL/SQL New Features

104 Oracle Database 11g: SQL and PL/SQL New Features 1 - 104
Practice 3 1. Start SQL Developer and connect to the database using your mydbconnection created in the last practice. 2. Use the new Oracle Database 11g regular expression support enhancements to find out information in the PRODUCT_INFORMATION table. a. Examine the structure of the PRODUCT_INFORMATION table. b. Create a report to find the number of occurrences of the string “RAM” in the PRODUCT_DESCRIPTION column. Alias the column as PRODUCT_NAME. Ignore case sensitivity. Your output should look similar to the following: Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

105 Oracle Database 11g: SQL and PL/SQL New Features 1 - 105
Practice 3 (continued) c. Create a report that queries the PRODUCT_INFORMATION table and finds the position of the occurrences of the second subexpression in the string “(SD)(RAM)” in the PRODUCT_DESCRIPTION column. Alias the column as PRODUCT_NAME Ignore case sensitivity. Return only the product names where the subexpression is found. 3. Use fine-grained dependency to examine dependencies at the element level. a. Create a view based on the CUSTOMERS table to store the data for the customers with the highest credit limits. (You can execute the lab_03_03_a.sql file). CREATE view best_customers AS SELECT customer_id, cust_first_name, cust_last_name, cust_ , credit_limit FROM customers WHERE credit_limit > 2500; b. Create a function named GET_BEST_CUSTOMER_COUNT that returns the number of highest credit rating customers. (You can execute the lab_03_03_b.sql file). CREATE OR REPLACE FUNCTION get_best_customers (p_credit_limit NUMBER DEFAULT 2500) RETURN NUMBER IS v_highest_amount NUMBER := 0; BEGIN SELECT COUNT(*) INTO v_highest_amount FROM best_customers WHERE credit_limit >= p_credit_limit; RETURN v_highest_amount; END get_best_customers; c. View the status of your BEST_CUSTOMERS view and GET_BEST_CUSTOMERS function by querying USER_OBJECTS. SELECT object_name, object_type, status FROM user_objects WHERE object_name LIKE '%BEST_CUST%'); Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

106 Oracle Database 11g: SQL and PL/SQL New Features 1 - 106
Practice 3 (continued) d. Add a column called INCOME to the CUSTOMERS table. ALTER TABLE customers ADD (income NUMBER); e. View the status of your BEST_CUSTOMERS view and GET_BEST_CUSTOMERS function by querying USER_OBJECTS. SELECT object_name, object_type, status FROM user_objects WHERE object_name LIKE '%BEST_CUST%'); f. Modify the view so that it includes the INCOME column. CREATE OR REPLACE VIEW best_customers AS SELECT customer_id, cust_first_name, cust_last_name, cust_ , credit_limit, income FROM customers WHERE credit_limit > 2500; g. View the status of your BEST_CUSTOMERS view and GET_BEST_CUSTOMERS function by querying USER_OBJECTS. h. Explain the results of the status. Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

107 Oracle Database 11g: SQL and PL/SQL New Features 1 - 107
Practice 3 (continued) 4. Coding to handle the PLW warning. a. Alter your session to enable compiler warnings. ALTER SESSION SET PLSQL_Warnings = 'enable:all'; b. Add an exception handler to your GET_BEST_CUSTOMERS function to catch for all exceptions. CREATE OR REPLACE FUNCTION get_best_customers (p_credit_limit NUMBER DEFAULT 2500) RETURN NUMBER IS v_highest_amount NUMBER := 0; BEGIN SELECT COUNT(*) INTO v_highest_amount FROM best_customers WHERE credit_limit >= p_credit_limit; RETURN v_highest_amount; EXCEPTION WHEN OTHERS THEN RETURN 0; END get_best_customers; c. Compile the function and note the warning. d. Fix the warning by modifying the exception handler. RAISE_APPLICATION_ERROR(-20001, 'Error occurred in get_best_customers.'); Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

108 Oracle Database 11g: SQL and PL/SQL New Features 1 - 108
Practice 3 (continued) 5. In this practice, you need two sessions. Use SQL Developer for one session and SQL*Plus for the second session. You will experiment with using the WAIT option on table locks. a. In SQL Developer, issue the following statement: LOCK TABLE customers IN EXCLUSIVE MODE b. Start SQL*Plus, and issue the following statement: LOCK TABLE customers IN EXLUSIVE MODE WAIT 60; c. Observe the error returned after 60 seconds. d. In SQL*Plus, issue the following statement: UPDATE customers SET income = 50000; e. What happens? f. In SQL Developer, issue the following statement: COMMIT; What happens in the SQL*Plus session? Rollback the data. Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

109 Executing Dynamic SQL in PL/SQL with the 11g Enhancements

110 Oracle Database 11g: SQL and PL/SQL New Features 1 - 110
Objectives After completing this lesson, you should be able to: Write PL/SQL code that uses dynamic SQL and allows for SQL statements larger than 32 KB Use the DBMS_SQL.PARSE() function that is overloaded for CLOBs Convert a REF CURSOR to a DBMS_SQL cursor and vice versa to support interoperability Program using the enhancements to DBMS_SQL that include supporting the full range of data types (including collections and object types) Objectives In this lesson, you learn about some of the language functionality enhancements added to Oracle Database 11g that pertain to dynamic SQL. After completing this lesson, you should be able to: List the new features added to dynamic SQL Describe when to use DBMS_SQL versus native dynamic SQL Oracle Database 11g: SQL and PL/SQL New Features

111 Oracle Database 11g: SQL and PL/SQL New Features 1 - 111
Lesson Agenda Overview of native dynamic SQL and DBMS_SQL Introduction Previous limitations Dynamic SQL support for CLOBs Native dynamic SQL support for CLOBs DBMS_SQL.PARSE() for CLOBs Converting between a REF CURSOR and a DBMS_SQL cursor DBMS_SQL.TO_REF_CURSOR DBMS_SQL.TO_CURSOR_NUMBER DBMS_SQL support for abstract data types Oracle Database 11g: SQL and PL/SQL New Features

112 Native Dynamic SQL and DBMS_SQL: Overview
Native dynamic SQL and the DBMS_SQL package are two ways to implement a dynamic SQL statement programmatically. You use native dynamic SQL on a single operation to bind any arguments in the dynamic SQL statement and execute the statement. You use the DBMS_SQL package to execute a dynamic SQL statement that has an unknown number of input or output variables. Overview of Native Dynamic SQL and DBMS_SQL A dynamic SQL statement is a string literal, string variable, or string expression. The full text of the dynamic SQL statement might be unknown until run time. Your program may build the SQL statement based on different scenarios within an application. Because the full statement is not known until run time, its syntax is checked at run time rather than at compile time. With dynamic SQL, you can make your PL/SQL programs more general and flexible because you do not need to know the full text of the dynamic SQL statement until run time. Because the PL/SQL compiler does not check their syntax, dynamic SQL statements can be SQL statements that are not part of the PL/SQL language. Your PL/SQL program can use dynamic SQL with either native dynamic SQL or the DBMS_SQL package. Native dynamic SQL uses a single operation to bind any arguments in the dynamic SQL statement and execute the statement. The DBMS_SQL package defines a basic data type called a SQL cursor number. Because the SQL cursor number is a basic data type, you can pass it across call boundaries and store it. You can also use the SQL cursor number to obtain information about the SQL statement that you are executing. Oracle Database 11g: SQL and PL/SQL New Features

113 Native Dynamic SQL and DBMS_SQL: Overview
Use native dynamic SQL when: The dynamic SQL statement retrieves rows into records You want to use the %FOUND, %ISOPEN, %NOTFOUND, or %ROWCOUNT SQL cursor attributes after issuing a dynamic SQL statement that is an INSERT, UPDATE, DELETE, or single-row SELECT statement Use DBMS_SQL when: You do not know the SELECT list at compile time You do not know how many columns a SELECT statement will return, or what their data types are Overview of Native Dynamic SQL and DBMS_SQL (continued) When generating dynamic SQL, you can either use the DBMS_SQL supplied package or you can use native dynamic SQL. Different situations may require you to use one method over the other. It is recommended that you use native dynamic SQL except when you cannot. For example, the DBMS_SQL package defines a basic data type called a SQL cursor number. Because the SQL cursor number is a basic data type, you can pass it across call boundaries and store it. You can also use the SQL cursor number to obtain information about the SQL statement that you are executing. Oracle Database 11g: SQL and PL/SQL New Features

114 Dynamic SQL Functional Completeness
Currently have two flavors of dynamic SQL within PL/SQL: DBMS_SQL and native dynamic SQL For functional completeness, interoperability between native dynamic SQL and DBMS_SQL is supported: SQL statements larger than 32 KB are allowed in native dynamic SQL. DBMS_SQL.PARSE() is overloaded for CLOBs. A REF CURSOR can be converted to a DBMS_SQL cursor and vice versa to support interoperability. DBMS_SQL supports the full range of data types (including collections and object types). Dynamic SQL Functional Completeness Prior to the Oracle Database 11g release, each of these methods had functional limitations. In Oracle Database 11g, functionality is added to both methods to make them more complete. Oracle Database 11g: SQL and PL/SQL New Features

115 Oracle Database 11g: SQL and PL/SQL New Features 1 - 115
Lesson Agenda Overview of native dynamic SQL and DBMS_SQL Introduction Previous limitations Dynamic SQL support for CLOBs Native dynamic SQL support for CLOBs DBMS_SQL.PARSE() for CLOBs Converting between a REF CURSOR and a DBMS_SQL cursor DBMS_SQL.TO_REF_CURSOR DBMS_SQL.TO_CURSOR_NUMBER DBMS_SQL support for abstract data types Oracle Database 11g: SQL and PL/SQL New Features

116 Dynamic SQL Support for CLOBs
Native dynamic SQL support: CREATE OR REPLACE PROCEDURE gen_pl (p_pgm CLOB) IS dynamic_pl CLOB := p_pgm; BEGIN EXECUTE IMMEDIATE dynamic_pl; -- next line is for learning purposes only DBMS_OUTPUT.PUT_LINE ('Just executed the following code: ' || dynamic_pl); END gen_pl; 1 EXECUTE gen_pl('begin dbms_output.put_line – (''put any code here''); end;') put any code here Just executed the following code: begin dbms_output.put_line('put any code here'); end; PL/SQL procedure successfully completed. 2 Native Dynamic SQL Support for CLOBs: Example You can now use the CLOB data type when specifying dynamic SQL statements. This removes the previous length constraint of 32 KB. In the example shown, the DYNAMIC_PL variable is set up with the CLOB data type. It is assigned a string that is passed in, which can now hold more than 32 KB. This is useful if you are dynamically building a PL/SQL block that might be very large. It is powerful because any valid code can be passed to the procedure and executed. In the examples shown in the slide: 1. With the code shown, you can pass any PL/SQL block as a parameter to the procedure. 2. This is an example of passing a call to the DBMS_OUTPUT procedure. You can pass in any string to represent a PL/SQL block. Oracle Database 11g: SQL and PL/SQL New Features

117 Dynamic SQL Support for CLOBs
More native dynamic SQL examples: For symmetry, DBMS_SQL.PARSE now accepts a CLOB too. EXECUTE gen_pl('begin null; end;') Just executed the following code: begin null; end; PL/SQL procedure successfully completed. 3 EXECUTE gen_pl('begin dbms_output.put_line(''hello world''); end;') hello world! Just executed the following code: begin dbms_output.put_line('hello world!'); end; PL/SQL procedure successfully completed. 4 Native Dynamic SQL Support for CLOBs: Example (continued) In the examples shown in the slide: 3. An anonymous block is passed to the GEN_PL procedure. 4. The “Hello World” program is passed to the GEN_PL procedure. DBMS_SQL Support for CLOBs The DBMS_SQL package is extended so that it accepts CLOBs too. PROCEDURE parse (c IN INTEGER, statement IN CLOB, language_flag IN INTEGER); Oracle Database 11g: SQL and PL/SQL New Features

118 Oracle Database 11g: SQL and PL/SQL New Features 1 - 118
Lesson Agenda Overview of native dynamic SQL and DBMS_SQL Introduction Previous limitations Dynamic SQL support for CLOBs Native dynamic SQL support for CLOBs DBMS_SQL.PARSE() for CLOBs Converting between a REF CURSOR and a DBMS_SQL cursor DBMS_SQL.TO_REF_CURSOR DBMS_SQL.TO_CURSOR_NUMBER DBMS_SQL support for abstract data types Oracle Database 11g: SQL and PL/SQL New Features

119 Transforming a DBMS_SQL Cursor into a REF CURSOR
To add interoperability between native dynamic SQL and DBMS_SQL, you can transform a DBMS_SQL cursor into a PL/SQL REF CURSOR and vice versa. Two new functions are added into the DBMS_SQL package to support this feature: DBMS_SQL.TO_REFCURSOR (cursor_number IN INTEGER) RETURN SYS_REFCURSOR; DBMS_SQL.TO_CURSOR_NUMBER (rc IN OUT SYS_REFCURSOR) RETURN INTEGER; Interoperability Between Native Dynamic SQL and DBMS_SQL You can switch between the DBMS_SQL package and native dynamic SQL by using the two new functions that are added into the DBMS_SQL package as of Oracle Database 11g. The DBMS_SQL.TO_REFCURSOR function converts a SQL cursor number to a weakly typed variable of the PL/SQL data type REF CURSOR. You can use the REF CURSOR variable in native dynamic SQL statements. The DBMS_SQL.TO_CURSOR function converts a REF CURSOR variable (either strongly or weakly typed) to a SQL cursor number. You can pass the SQL cursor number to DBMS_SQL subprograms. This fulfills a functionality hole that was present in prior releases. It is best practice to avoid having any client programs use SQL directly. You should implement the SQL via PL/SQL routines. If you are doing a query with an unbounded result, you need a REF CURSOR. If the WHERE clause is not known until run time, you can use DBMS_SQL for processing and then switch to do BULK COLLECT. Oracle Database 11g: SQL and PL/SQL New Features

120 Transforming a DBMS_SQL Cursor into a REF CURSOR: Example
CREATE OR REPLACE PROCEDURE do_query (rep_id NUMBER) IS TYPE num_list IS TABLE OF NUMBER INDEX BY BINARY_INTEGER; TYPE cur_type IS REF CURSOR; src_cur cur_type; c_hndl NUMBER; cust_nos num_list; crdt_nos num_list; ret INTEGER; sql_stmt CLOB; BEGIN c_hndl := DBMS_SQL.OPEN_CURSOR; sql_stmt := 'SELECT customer_id, credit_limit FROM customers WHERE account_mgr_id = :b1'; DBMS_SQL.PARSE(c_hndl, sql_stmt, DBMS_SQL.NATIVE); DBMS_SQL.BIND_VARIABLE(c_hndl, 'b1', rep_id); ret := DBMS_SQL.EXECUTE(c_hndl); -- continued on next page DBMS_SQL Cursor: Example In the example shown in the slide, a DBMS_SQL cursor is created, opened, parsed, and executed. In the following slide, the cursor is transformed into a PL/SQL REF CURSOR that is consumed by native dynamic SQL. Oracle Database 11g: SQL and PL/SQL New Features

121 Transforming a DBMS_SQL Cursor into a REF CURSOR: Example
-- continued from previous page -- switch from dbms_sql to native dynamic SQL src_cur := DBMS_SQL.TO_REFCURSOR(c_hndl); -- fetch with native dynamic SQL FETCH src_cur BULK COLLECT INTO cust_nos, crdt_nos; IF cust_nos.COUNT > 0 THEN DBMS_OUTPUT.PUT_LINE ('Customer Credit Limit'); DBMS_OUTPUT.PUT_LINE (' '); FOR i IN 1 .. cust_nos.COUNT LOOP DBMS_OUTPUT.PUT_LINE(cust_nos(i) || ' ' || crdt_nos(i)); END LOOP; END IF; CLOSE src_cur; END do_query; / DBMS_SQL Cursor: Example (continued) In the example shown in the slide, the cursor is switched to native dynamic SQL and the fetch is performed with native dynamic SQL. Oracle Database 11g: SQL and PL/SQL New Features

122 Transforming a DBMS_SQL Cursor into a REF CURSOR: Example
EXECUTE do_query(145) Customer Credit Limit ... PL/SQL procedure successfully completed. DBMS_SQL Cursor: Example (continued) Execution of the DO_QUERY procedure is shown in the slide. When using the DBMS_SQL.TO_REFCURSOR function: The cursor passed in by the cursor number parameter must be opened, parsed, and executed After the cursor number is transformed into a REF CURSOR, the cursor_number value is no longer accessible by any DBMS_SQL operations After the cursor number is transformed into a REF CURSOR, you cannot use DBMS_SQL.ISOPEN to check to see if the cursor number is still open Toggling between a REF CURSOR and a DBMS_SQL cursor number after starting to fetch is not allowed Oracle Database 11g: SQL and PL/SQL New Features

123 Transforming a REF CURSOR into a DBMS_SQL Cursor: Example
CREATE OR REPLACE PROCEDURE do_query2 (sql_stmt VARCHAR2, rep_id NUMBER) IS TYPE cur_type IS REF CURSOR; src_cur cur_type; c_hndl NUMBER; desctab DBMS_SQL.DESC_TAB; colcnt NUMBER; custid NUMBER; crdvar NUMBER; BEGIN OPEN src_cur FOR sql_stmt USING rep_id; -- switch from native dynamic SQL to DBMS_SQL: c_hndl := DBMS_SQL.TO_CURSOR_NUMBER(src_cur); DBMS_SQL.DESCRIBE_COLUMNS(c_hndl, colcnt, desctab); -- define columns FOR i in 1 .. colcnt LOOP IF desctab(i).col_type=1 THEN DBMS_SQL.DEFINE_COLUMN(c_hndl, i, custid); ELSIF desctab(i).col_type = 2 THEN DBMS_SQL.DEFINE_COLUMN(c_hndl, i, crdvar); END IF; END LOOP; -- continued on next page Transforming a REF CURSOR: Example You can use the DBMS_SQL.TO_CURSOR_NUMBER function to transform a REF CURSOR into a DBMS_SQL cursor number. In the example shown in the slide, a REF CURSOR is opened and transformed into a DBMS_SQL cursor. When using the DBMS_SQL.TO_CURSOR_NUMBER function: The REF CURSOR passed in must be opened first After the REF CURSOR is transformed into a DBMS_SQL cursor number, the REF CURSOR is no longer accessible by any native dynamic SQL operations You cannot toggle between a REF CURSOR and a DBMS_SQL cursor number after fetching is started Oracle Database 11g: SQL and PL/SQL New Features

124 Transforming a REF CURSOR into a DBMS_SQL Cursor: Example
-- continued from previous page -- fetch rows WHILE DBMS_SQL.FETCH_ROWS(c_hndl) > 0 LOOP FOR i IN 1 .. colcnt LOOP IF desctab(i).col_type=1 THEN DBMS_SQL.COLUMN_VALUE(c_hndl, i, custid); ELSIF desctab(i).col_type = 2 THEN DBMS_SQL.COLUMN_VALUE(c_hndl, i, crdvar); END IF; END LOOP; -- could do more processing... DBMS_SQL.CLOSE_CURSOR(c_hndl); END do_query2; / Transforming a REF CURSOR: Example (continued) In the example shown in the slide, the DO_QUERY2 procedure is created. It is then executed with a SQL statement passed to it as the first parameter. This SQL statement is opened as a REF CURSOR. The procedure converts the REF CURSOR into a DBMS_SQL cursor. The rest of the processing for this procedure uses DBMS_SQL. EXECUTE do_query2('SELECT customer_id, credit_limit FROM customers - WHERE account_mgr_id = :b1', 148) PL/SQL procedure successfully completed. Oracle Database 11g: SQL and PL/SQL New Features

125 Oracle Database 11g: SQL and PL/SQL New Features 1 - 125
Lesson Agenda Overview of native dynamic SQL and DBMS_SQL Introduction Previous limitations Dynamic SQL support for CLOBs Native dynamic SQL support for CLOBs DBMS_SQL.PARSE() for CLOBs Converting between a REF CURSOR and a DBMS_SQL cursor DBMS_SQL.TO_REF_CURSOR DBMS_SQL.TO_CURSOR_NUMBER DBMS_SQL support for abstract data types Oracle Database 11g: SQL and PL/SQL New Features

126 DBMS_SQL Support for Abstract Data Types (ADTs)
Now allows: Collections Varrays Nested tables REFs Opaque types DBMS_SQL Support for Abstract Data Types DBMS_SQL supports abstract data types. You can use varrays, nested tables, REFs, and opaque types with the DBMS_SQL package. Opaque Types Opaque types are abstract data types. With data implemented as simply a series of bytes, the internal representation is not exposed. Often opaque types are provided by Oracle’s supplied packages rather than being implemented by you. Opaque types are similar in some basic ways to object types, with similar concepts of static methods, instances, and instance methods. Typically, only the methods supplied with an opaque type allow you to manipulate the state and internal byte representation. For example, XMLType, provided with Oracle Database 11g, facilitates handling XML data natively in the database. Note: PL/SQL data types, such as INDEX-BY tables, Booleans, and records are not supported as bind and define variable data types in DBMS_SQL. However, DBMS_SQL supports all SQL data types. Oracle Database 11g: SQL and PL/SQL New Features

127 DBMS_SQL Support for ADTs: Example
CREATE OR REPLACE PROCEDURE update_phone_nos (p_new_nos phone_list_typ, p_cust_id customers.customer_id%TYPE) IS some_phone_nos phone_list_typ; c_hndl NUMBER; r NUMBER; sql_stmt CLOB := 'UPDATE customers SET phone_numbers = :b1 WHERE customer_id = :b2 RETURNING phone_numbers INTO :b3'; BEGIN c_hndl := DBMS_SQL.OPEN_CURSOR; DBMS_SQL.PARSE(c_hndl, sql_stmt, dbms_sql.native); DBMS_SQL.BIND_VARIABLE (c_hndl, 'b1', p_new_nos); DBMS_SQL.BIND_VARIABLE (c_hndl, 'b2', p_cust_id); DBMS_SQL.BIND_VARIABLE (c_hndl, 'b3', some_phone_nos); r := DBMS_SQL.EXECUTE (c_hndl); DBMS_SQL.VARIABLE_VALUE(c_hndl, 'b3', some_phone_nos); DBMS_SQL.CLOSE_CURSOR(c_hndl); -- continued on next page DBMS_SQL for Abstract Data Types: Example In this example, DBMS_SQL is used with the varray column in the CUSTOMERS table. The PHONE_LIST_TYP object is defined in the Order Entry sample schema as: DESCRIBE phone_list_typ Phone_list_typ VARRAY(5) OF VARCHAR2(25) The UPDATE_PHONE_NOS procedure binds the PHONE_LIST_TYP object, and then assigns a value to the SOME_PHONE_NOS varray variable defined in the PL/SQL block. Oracle Database 11g: SQL and PL/SQL New Features

128 DBMS_SQL Support for ADTs: Example
-- continued from previous page -- select the phones nos sql_stmt := 'SELECT phone_numbers FROM customers WHERE customer_id = :b2'; c_hndl := DBMS_SQL.OPEN_CURSOR; DBMS_SQL.PARSE(c_hndl, sql_stmt, dbms_sql.native); DBMS_SQL.DEFINE_COLUMN(c_hndl, 1, some_phone_nos); DBMS_SQL.BIND_VARIABLE(c_hndl, 'b2', p_cust_id); r := DBMS_SQL.EXECUTE_AND_FETCH(c_hndl); DBMS_SQL.COLUMN_VALUE(c_hndl, 1, some_phone_nos); DBMS_SQL.CLOSE_CURSOR(c_hndl); FOR i IN some_phone_nos.FIRST .. some_phone_nos.LAST LOOP DBMS_OUTPUT.PUT_LINE('Phone number = ' || some_phone_nos(i) || ' updated.'); END LOOP; END update_phone_nos; / Oracle Database 11g: SQL and PL/SQL New Features

129 DBMS_SQL Support for ADTs: Example
Execute the UPDATE_PHONE_NOS procedure: DECLARE new_phone_nos phone_list_typ; BEGIN new_phone_nos := phone_list_typ (' ', ' ', ' ', ' '); update_phone_nos(new_phone_nos, 980); END; / Phone number = updated. Phone number = updated. Phone number = updated. Phone number = updated. PL/SQL successfully completed. DBMS_SQL Support for ADTs: Example (continued) When the UPDATE_PHONE_NOS procedure is called, the new phone numbers for a specific customer are passed as parameters. The result shows that the varray abstract data type is updated. Oracle Database 11g: SQL and PL/SQL New Features

130 Oracle Database 11g: SQL and PL/SQL New Features 1 - 130
Summary In this lesson, you should have learned how to use the Oracle Database 11g enhancements to dynamic SQL: Write PL/SQL code that uses dynamic SQL and allows for SQL statements larger than 32 KB Convert a REF CURSOR to a DBMS_SQL cursor and vice versa to support interoperability Program using the enhancements to DBMS_SQL that include supporting collections and object types Create user-defined collection types and bulk-bind them using DBMS_SQL Summary In this lesson, you should have learned: When to use DBMS_SQL versus native dynamic SQL How to use the Oracle Database 11g enhancements to dynamic SQL Oracle Database 11g: SQL and PL/SQL New Features

131 Practice 4 Overview: Using the New Dynamic SQL Enhancements
This practice covers the following topics: Writing code that uses both DBMS_SQL.PARSE and native dynamic SQL to accept SQL statements larger than 32 KB. Using DBMS_SQL with abstract data types and perform bulk binding with them. Practice 4 Overview: Using the New Dynamic SQL Enhancements In this practice, you use the new enhancements to dynamic SQL. This practice uses the OE schema. Oracle Database 11g: SQL and PL/SQL New Features

132 Oracle Database 11g: SQL and PL/SQL New Features 1 - 132
Practice 4 1. Start SQL Developer and connect to the database using your mydbconnection created earlier. 2. Create a procedure that adds truck capacity information into the WAREHOUSES table. Name the procedure UPDATE_TRUCK_INFO. a. Examine the structure of the WAREHOUSES table. b. Create a new VARRAY type called TRUCK_CAPACITY_TYP to store the truck capacities for trucks at a given warehouse. It is estimated that a warehouse will have no more than 10 trucks. The capacity information is a variable length string, no more than 40 characters in length. CREATE TYPE truck_capacity_typ IS varray (10) OF VARCHAR2(40); c. Add a new column called TRUCKS to the WAREHOUSES table. The TRUCKS column is of type TRUCK_CAPACITY_TYP. ALTER TABLE warehouses ADD (trucks truck_capacity_typ); Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

133 Oracle Database 11g: SQL and PL/SQL New Features 1 - 133
Practice 4 (continued) d. Create the UPDATE_TRUCK_INFO procedure. This procedure accepts two arguments: the warehouse ID for which you are updating the trucking information, and the trucking information. The trucking information is the capacity for each truck at a warehouse. Below is the skeleton code for the procedure: (You can use the lab_04_02_d.sql file). CREATE OR REPLACE PROCEDURE update_truck_info (p_new_truck truck_capacity_typ, p_wh_id NUMBER) IS some_truck_info truck_capacity_typ; c_hndl NUMBER; r NUMBER; sql_stmt CLOB := 'UPDATE warehouses SET trucks = :b1 WHERE warehouse_id = :b2 RETURNING trucks INTO :b3'; BEGIN c_hndl := DBMS_SQL.OPEN_CURSOR; ... DBMS_OUTPUT.PUT_line('Truck information for warehouse ' || p_wh_id || ' is: '); FOR i IN some_truck_info.FIRST .. some_truck_info.LAST LOOP DBMS_OUTPUT.PUT_LINE(some_truck_info(i)); END LOOP; END update_truck_info; e. Call your procedure through an anonymous block. For example, the following code adds the trucking capacity information for warehouse 1. DECLARE new_truck_info truck_capacity_typ; new_truck_info := truck_capacity_typ ('1 ton', '2 ton', '1 ton', '5 ton'); update_truck_info(new_truck_info, 1); END; Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

134 Oracle Database 11g: SQL and PL/SQL New Features 1 - 134
Practice 4 (continued) 3. Create a dynamic PL/SQL routine that will build the PRINTIT routine you created in lesson 2. a. Examine this PRINTIT routine. CREATE OR REPLACE PROCEDURE printit (p_string IN VARCHAR2, p_int IN NUMBER) IS BEGIN FOR i IN 1..p_int LOOP DBMS_OUTPUT.PUT_LINE(p_string); END LOOP; END; b. If you completed the earlier practices, you need to remove the PRINIT procedure. DROP PROCEDURE printit; c. Create a procedure name CODE_GENERATE that runs any PL/SQL block. The goal is to pass the definition of the PRINTIT routine shown above, and have the CODE_GENERATE procedure create the PRINTIT procedure. d. After successfully executing the CODE_GENERATE procedure and passing to the procedure the definition of the PRINTIT procedure, you should have the PRINTIT procedure redefined as one of your database objects. Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

135 Implementing the Performance Improvements

136 Oracle Database 11g: SQL and PL/SQL New Features 1 - 136
Objectives After completing this lesson, you should be able to: List the compiler changes and explain how the changes impact native compilation Use the new SIMPLE_INTEGER data type Describe the process of inlining Use caching for optimization Use flashback to store and track all transactional changes to a record Objectives In this lesson, you learn about the SQL and PL/SQL performance improvements. This includes compiler changes, a new SIMPLE_INTEGER data type that takes advantage of native compilation, and caching for SQL and PL/SQL results. Additionally, you learn about inlining, which is an optimization process that replaces procedure calls with a copy of the body of the procedure to be called, and two flashback enhancements that enable developers to 1) store and track all transactional changes to a record over its lifetime; 2) roll back a transaction and its dependent transactions while the database remains online. Oracle Database 11g: SQL and PL/SQL New Features

137 Oracle Database 11g: SQL and PL/SQL New Features 1 - 137
Lesson Agenda Compiler changes and how the changes impact native compilation The SIMPLE_INTEGER data type Inlining Caching SQL result cache PL/SQL function result cache Flashback enhancements To store and track all transactional changes to a record over its lifetime Oracle Database 11g: SQL and PL/SQL New Features

138 Real Native Compilation
The compiler translates PL/SQL source directly to the dynamic-link library (DLL) for the current hardware. The compiler does the linking and loading so that the file system directories are no longer needed. The PL/SQL native compilation works out of the box, without requiring a C compiler on a production box. The PLSQL_CODE_TYPE parameter is the on/off switch. And, real native compilation is faster than the C native compilation. Real Native Compilation In Oracle Database 11g, the compiler translates PL/SQL source directly to the DLL for the current hardware. It also does the linking and loading so that the file system directories are no longer needed. This means that a C compiler is not needed and that PL/SQL native compilation will work out of the box. The parameter that controls the compilation type is PLSQL_CODE_TYPE. You can set this to either native or interpreted with the ALTER SESSION, ALTER SYSTEM, and ALTER PROCEDURE (for a specific PL/SQL subprogram) statements. Note: In Oracle Database 10g, Release 1, the configuration of initialization parameters and the command setup for native compilation were simplified. The only parameter required was PLSQL_NATIVE_LIBRARY_DIR. The parameters related to the compiler, linker, and make utility are obsolete (PLSQL_NATIVE_C_COMPILER, PLSQL_NATIVE_LINKER, PLSQL_NATIVE_MAKE_FILE_NAME, PLSQL_NATIVE_MAKE_UTILITY). Native compilation is turned on and off by a separate initialization parameter, PLSQL_CODE_TYPE, rather than being one of several options in the PLSQL_COMPILER_FLAGS parameter, which is now deprecated. The spnc_commands file, located in your ORACLE_HOME/plsql directory, contains the information for compiling and linking, rather than a makefile. Oracle Database 11g: SQL and PL/SQL New Features

139 Oracle Database 11g: SQL and PL/SQL New Features 1 - 139
Lesson Agenda Compiler changes and how the changes impact native compilation The SIMPLE_INTEGER data type Inlining Caching SQL result cache PL/SQL function result cache Flashback enhancements To store and track all transactional changes to a record over its lifetime Oracle Database 11g: SQL and PL/SQL New Features

140 SIMPLE_INTEGER Data Type
Definition: Is a predefined subtype Has the range – Does not include a null value Is allowed anywhere in PL/SQL where the PLS_INTEGER data type is allowed Benefits: Eliminates the overhead of overflow checking Is estimated to be 2–10 times faster when compared with the PLS_INTEGER type with native PL/SQL compilation SIMPLE_INTEGER Data Type The SIMPLE_INTEGER data type is a predefined subtype of the BINARY_INTEGER (or PLS_INTEGER) data type that has the same numeric range as BINARY_INTEGER. It differs significantly from PLS_INTEGER in its overflow semantics. Incrementing the largest SIMPLE_INTEGER value by one silently produces the smallest value; and decrementing the smallest value by one silently produces the largest value. These “wrap around” semantics conform to the IEEE standard for 32-bit integer arithmetic. The key features of the SIMPLE_INTEGER predefined subtype are the following: Includes the range of – Has a not null constraint Wraps rather than overflows Is faster than PLS_INTEGER Without the overhead of overflow checking and nullness checking, the SIMPLE_INTEGER data type provides significantly better performance than PLS_INTEGER when the parameter PLSQL_CODE_TYPE is set to native because arithmetic operations on the former are performed directly in the machine’s hardware. The performance difference is less noticeable when parameter PLSQL_CODE_TYPE is set to interpreted, but even with this setting, the SIMPLE_INTEGER type is faster than the PLS_INTEGER type. Oracle Database 11g: SQL and PL/SQL New Features

141 SIMPLE_INTEGER Data Type: Example
CREATE OR REPLACE PROCEDURE p IS t NUMBER :=0; t NUMBER :=0; $IF $$Simple $THEN SUBTYPE My_Integer_t IS SIMPLE_INTEGER; My_Integer_t_Name CONSTANT VARCHAR2(30) := 'SIMPLE_INTEGER'; $ELSE SUBTYPE My_Integer_t IS PLS_INTEGER; My_Integer_t_Name CONSTANT VARCHAR2(30) := 'PLS_INTEGER'; $END v00 My_Integer_t := 0; v01 My_Integer_t := 0; v02 My_Integer_t := 0; v03 My_Integer_t := 0; v04 My_Integer_t := 0; v05 My_Integer_t := 0; two CONSTANT My_Integer_t := 2; lmt CONSTANT My_Integer_t := ; -- continued on next page SIMPLE_INTEGER Data Type: Example In this example, a test procedure named p is created, which utilizes conditional compilation. By using conditional compilation, this procedure can easily be run with the test case of the SIMPLE_INTEGER or the test case of the PLS_INTEGER. Note the use of the subtype in the conditional compilation statement (prefixed with the $ symbol). Before running this procedure, you set the conditional compilation flag named SIMPLE to either true or false. By using a subtype, you can improve the readability of the code and reduce potential typographic errors. This example code is continued in the graphic in the following slide. Note: There is no difference between using the PLS_INTEGER data type and the BINARY_INTEGER data type. Starting in Oracle Database 10g, they are exactly the same. Oracle Database 11g: SQL and PL/SQL New Features

142 SIMPLE_INTEGER Data Type: Example
-- continued from previous page BEGIN t0 := DBMS_UTILITY.GET_CPU_TIME(); WHILE v01 < lmt LOOP v00 := v00 + Two; v01 := v01 + Two; v02 := v02 + Two; v03 := v03 + Two; v04 := v04 + Two; v05 := v05 + Two; END LOOP; IF v01 <> lmt OR v01 IS NULL THEN RAISE Program_Error; END IF; t1 := DBMS_UTILITY.GET_CPU_TIME(); DBMS_OUTPUT.PUT_LINE( RPAD(LOWER($$PLSQL_Code_Type), 15)|| RPAD(LOWER(My_Integer_t_Name), 15)|| TO_CHAR((t1-t0), '9999')||' centiseconds'); END p; / SIMPLE_INTEGER Data Type: Example (continued) This example code is continued from the graphic in the previous slide. The main processing of this code performs a loop where simple mathematical computations take place. The loop is timed using DBMS_UTILITY.GET_CPU_TIME. Note: If you use the SIMPLE_INTEGER in a mixed operation with any other numeric types, or pass it as a parameter or a bind, or define it where a PLS_INTEGER is expected, a compiler warning is issued. If you violate a limitation, a compiler error is raised. Oracle Database 11g: SQL and PL/SQL New Features

143 SIMPLE_INTEGER Data Type: Example
ALTER PROCEDURE p COMPILE PLSQL_Code_Type = NATIVE PLSQL_CCFlags = 'simple:true' REUSE SETTINGS; Procedure altered. EXECUTE p() native simple_integer centiseconds PL/SQL procedure successfully completed. PLSQL_Code_Type = native PLSQL_CCFlags = 'simple:false' native pls_integer centiseconds 1 2 3 SIMPLE_INTEGER Data Type: Example (continued) The procedure p is executed under two different conditions: native compilation with the SIMPLE_INTEGER data type and native compilation with the PLS_INTEGER data type. 1. The procedure is natively compiled and the code type is set to use the SIMPLE_INTEGER. 2. The results show the timing for the code running with the SIMPLE_INTEGER data type. 3. The procedure is natively compiled and the code type is set to NOT use the SIMPLE_INTEGER. 4. The results show the timing for the code running with the PLS_INTEGER data type. 4 Oracle Database 11g: SQL and PL/SQL New Features

144 Oracle Database 11g: SQL and PL/SQL New Features 1 - 144
Lesson Agenda Compiler changes and how the changes impact native compilation The SIMPLE_INTEGER data type Inlining Caching SQL result cache PL/SQL function result cache Flashback enhancements To store and track all transactional changes to a record over its lifetime Oracle Database 11g: SQL and PL/SQL New Features

145 Oracle Database 11g: SQL and PL/SQL New Features 1 - 145
Intra Unit Inlining Definition: Inlining is defined as the replacement of a call to subroutine with a copy of the body of the subroutine that is called. The copied procedure generally runs faster than the original. The PL/SQL compiler can automatically find the calls that should be inlined. Benefits: Inlining can provide large performance gains when applied judiciously by a factor of 2–10 times. Introducing Inlining Procedure inlining is an optimization process that replaces procedure calls with a copy of the body of the procedure to be called. The copied procedure almost always runs faster than the original call because: The need to create and initialize the stack frame for the called procedure is eliminated The optimization can be applied over the combined text of the call context and the copied procedure body Propagation of constant actual arguments often causes the copied body to collapse under optimization When inlining is achieved, you can see performance gains of 2–10 times. With Oracle Database 11g, the PL/SQL compiler can automatically find calls that should be inlined and can do that inlining correctly and quickly. There are some controls to specify where and when the compiler should do this work (using the PLSQL_OPTIMIZATION_LEVEL database parameter), but usually, a general request is sufficient. Oracle Database 11g: SQL and PL/SQL New Features

146 Oracle Database 11g: SQL and PL/SQL New Features 1 - 146
Use of Inlining Influence implementing inlining via two methods: Oracle parameter PLSQL_OPTIMIZE_LEVEL PRAGMA INLINE Recommend that you: Inline small programs Inline programs that are frequently executed Use performance tools to identify hot spots suitable for inline applications: plstimer Use of Inlining When implementing inlining, it is recommended that the process should be applied to smaller programs, and/or programs that execute frequently. For example, you may want to inline small helper programs. To help you identify which programs to inline, you can use the plstimer PL/SQL performance tool. This tool specifically analyzes program performance in terms of time spent in procedures and time spent from particular call sites. It is important that you identify the procedure calls that may benefit from inlining. There are two ways to use inlining: 1. Set the PLSQL_OPTIMIZE_LEVEL parameter to 3. When this parameter is set to 3, the PL/SQL compiler searches for calls that might profit by inlining and inlines the most profitable calls. Profitability is measured by those calls that will help the program speed up the most and keep the compiled object program as short as possible. ALTER SESSION SET plsql_optimize_level = 3; 2. Use PRAGMA INLINE in your PL/SQL code. This identifies whether a specific call should be inlined or not. Setting this pragma to “YES” will have an effect only if the optimize level is set to two or higher. Oracle Database 11g: SQL and PL/SQL New Features

147 Oracle Database 11g: SQL and PL/SQL New Features 1 - 147
Inlining Concepts Noninlined program: CREATE OR REPLACE PROCEDURE small_pgm IS a NUMBER; b NUMBER; PROCEDURE touch(x IN OUT NUMBER, y NUMBER) BEGIN IF y > 0 THEN x := x*x; END IF; END; a := b; FOR I IN LOOP touch(a, -17); a := a*b; END LOOP; END small_pgm; Inlining Concepts The example shown in the slide will be expanded to show you how a procedure is inlined. The a:=a*b assignment at the end of the loop looks like it could be moved before the loop. However, it cannot be because a is passed as an IN OUT parameter to the TOUCH procedure. The compiler cannot be certain what the procedure does to its parameters. This results in the multiplication and assignment being completed 10 times instead of only once, even though multiple executions are not necessary. Oracle Database 11g: SQL and PL/SQL New Features

148 Oracle Database 11g: SQL and PL/SQL New Features 1 - 148
Inlining Concepts Examine the loop after inlining: ... BEGIN a := b; FOR i IN LOOP IF –17 > 0 THEN a := a*a; END IF; a := a*b; END LOOP; END small_pgm; Inlining Concepts (continued) The code in the slide shows what happens to the loop after inlining. Oracle Database 11g: SQL and PL/SQL New Features

149 Oracle Database 11g: SQL and PL/SQL New Features 1 - 149
Inlining Concepts The loop is transformed in several steps: a := b; FOR i IN LOOP ... IF false THEN a := a*a; END IF; a := a*b; END LOOP; a := b*b; Inlining Concepts (continued) Because the insides of the procedure are now visible to the compiler, it can transform the loop in several steps, as shown in the slide. Instead of 11 assignments (one outside of the loop) and 10 multiplications, only one assignment and one multiplication are performed. If the loop ran a million times (instead of 10), the savings would be a million assignments. For code that contains deep loops that are executed frequently, inlining offers tremendous savings. Oracle Database 11g: SQL and PL/SQL New Features

150 Oracle Database 11g: SQL and PL/SQL New Features 1 - 150
Inlining: Example Set the PLSQL_OPTIMIZE_LEVEL session-level parameter to a value of 2 or 3: Setting it to 2 means no automatic inlining is attempted. Setting it to 3 means automatic inlining is attempted and no pragmas are necessary. Within a PL/SQL subroutine, use PRAGMA INLINE NO means no inlining occurs regardless of the level and regardless of the YES pragmas. YES means inline at level 2 of a particular call and increase the priority of inlining at level 3 for the call. ALTER PROCEDURE small_pgm COMPILE PLSQL_OPTIMIZE_LEVEL = 3 REUSE SETTINGS; Inlining Concepts (continued) To influence the optimizer to use inlining, you can set the PLSQL_OPTIMIZE_LEVEL parameter to a value of 2 or higher. By setting this parameter, you are making a request that inlining be used. It is up to the compiler to analyze the code and determine whether inlining is appropriate. When the optimize level is set to 3, the PL/SQL compiler searches for calls that might profit by inlining and inlines the most profitable calls. Within a PL/SQL subroutine, you can use PRAGMA INLINE to suggest that a specific call be inlined. Oracle Database 11g: SQL and PL/SQL New Features

151 Oracle Database 11g: SQL and PL/SQL New Features 1 - 151
Inlining: Example After setting the PLSQL_OPTIMIZE_LEVEL parameter, use a pragma: CREATE OR REPLACE PROCEDURE small_pgm IS a PLS_INTEGER; FUNCTION add_it(a PLS_INTEGER, b PLS_INTEGER) RETURN PLS_INTEGER BEGIN RETURN a + b; END; pragma INLINE (small_pgm, 'YES'); a := add_it(3, 4) + 6; END small_pgm; Inlining Concepts (continued) Within a PL/SQL subroutine, you can use PRAGMA INLINE to suggest that a specific call be inlined. When using PRAGMA INLINE, the first argument is the simple name of a subroutine, a function name, a procedure name, or a method name. The second argument is either the constant string ‘NO’ or ‘YES’. The pragma can go before any statement or declaration. If you put it in the wrong place, you receive a syntax error message from the compiler. To identify that a specific call should not be inlined, use: PRAGMA INLINE (function_name, 'NO'); Setting the PRAGMA INLINE to ‘NO’ always works, regardless of any other pragmas that might also apply to the same statement. The pragma also applies at all optimization levels, and it applies no matter how badly the compiler would like to inline a particular call. Setting the PRAGMA INLINE to ‘YES’ strongly encourages the compiler to inline the call. The compiler keeps track of the resources used during inlining and makes the decision to stop inlining when the cost becomes too high. Oracle Database 11g: SQL and PL/SQL New Features

152 Oracle Database 11g: SQL and PL/SQL New Features 1 - 152
Inlining: Guidelines Pragmas apply only to calls in the next statement following the pragma. Programs that make use of smaller helper subroutines are good candidates for inlining. Only local subroutines can be inlined. You cannot inline an external subroutine. Cursor functions should not be inlined. Inlining can increase the size of a unit. Be careful about suggesting to inline functions that are deterministic. Inlining: Guidelines The compiler inlines code automatically, provided that you are using native compilation and have set the PLSQL_OPTIMIZE_LEVEL to 3. If you have set PLSQL_Warnings = 'enable:all', using the SQL*Plus SHOW ERRORS command displays the name of the code that is inlined. The PLW compiler message tells you that a PRAGMA INLINE(, 'YES') referring to the named procedure was found. The compiler will, if possible, inline this call. The PLW compiler message tells you the name of the code that is inlined. Alternatively, you can query the USER/ALL/DBA_ERRORS dictionary view. Deterministic functions compute the same outputs for the same inputs every time it is invoked and have no side effects. In Oracle Database 11g, the PL/SQL compiler can figure out whether a function is deterministic; it may not find all of the ones that truly are, but it will find many of them. It will never mistake a nondeterministic function for a deterministic function. Oracle Database 11g: SQL and PL/SQL New Features

153 Oracle Database 11g: SQL and PL/SQL New Features 1 - 153
Lesson Agenda Compiler changes and how the changes impact native compilation The SIMPLE_INTEGER data type Inlining Caching SQL result cache PL/SQL function result cache Flashback enhancements To store and track all transactional changes to a record over its lifetime Oracle Database 11g: SQL and PL/SQL New Features

154 Oracle Database 11g: SQL and PL/SQL New Features 1 - 154
SQL Query Result Cache Definition: Cache the results of the current query or query fragment in memory and then use the cached results in future executions of the query or query fragments. Cached results reside in the result cache memory portion of the SGA. Benefits: Improved performance SQL Query Result Cache You can improve the performance of your queries by caching the results of a query in memory and then using the cached results in future executions of the query or query fragments. The cached results reside in the result cache memory portion of the SGA. This feature is designed to speed up query execution on systems with large memories. Oracle Database 11g: SQL and PL/SQL New Features

155 Oracle Database 11g: SQL and PL/SQL New Features 1 - 155
SQL Query Result Cache Scenario: You need to find the greatest average value of credit limit grouped by state over the whole population. The query results in a huge number of rows analyzed to yield a few or one row. In your query, the data changes fairly slowly (say every hour) but the query is repeated fairly often (say every second). Solution: Use the new optimizer hint /*+ result_cache */ in your query: SELECT /*+ result_cache */ AVG(cust_credit_limit), cust_state_province FROM sh.customers GROUP BY cust_state_province; Usage SQL result caching is useful when your queries need to analyze a large number of rows to return a small number of rows or a single row. Two new optimizer hints are available to turn on and turn off SQL result caching: /*+ result_cache */ /*+ no_result_cache */ These hints let you override settings of the RESULT_CACHE_MODE initialization parameter. You can execute DBMS_RESULT_CACHE.MEMORY_REPORT to produce a memory usage report of the result cache. Oracle Database 11g: SQL and PL/SQL New Features

156 PL/SQL Function Result Cache
Definition: Enables data that is stored in cache to be shared across sessions Stores the function result cache in a shared global area (SGA), making it available to any session that runs your application Benefits: Improved performance Improved scalability PL/SQL Function Result Cache Starting in Oracle Database 11g, you can use the PL/SQL cross-section function result caching mechanism. This caching mechanism provides you with a language-supported and system-managed means for storing the results of PL/SQL functions in a shared global area (SGA), which is available to every session that runs your application. The caching mechanism is both efficient and easy to use, and it relieves you of the burden of designing and developing your own caches and cache-management policies. Oracle Database 11g: SQL and PL/SQL New Features

157 PL/SQL Function Result Cache
Scenario: You need a PL/SQL function that derives a complex metric. The data that your function calculates changes slowly, but the function is frequently called. Solution: Use the new result_cache clause in your function definition. PL/SQL Function Result Cache (continued) To enable result caching for a function, use the RESULT_CACHE clause in your PL/SQL function. Using this clause results in the following: If a result-cached function is called, the system checks the cache. If the cache contains the result from a previous call to the function with the same parameter values, the system returns the cached result to the caller and does not re-execute the function body. If the cache does not contain the result, the system executes the function body and adds the result (for these parameter values) to the cache before returning control to the caller. The cache can accumulate many results—one result for every unique combination of parameter values with which each result-cached function has been called. If the system needs more memory, it ages out (deletes) one or more cached results. You can specify the database objects that are used to compute a cached result, so that if any of them are updated, the cached result becomes invalid and must be recomputed. The best candidates for result caching are functions that are called frequently but depend on information that changes infrequently or never. Oracle Database 11g: SQL and PL/SQL New Features

158 Enabling Result Caching
Include the RESULT_CACHE option in the function definition. Optionally, include the RELIES_ON clause. CREATE OR REPLACE FUNCTION productName (prod_id NUMBER, lang_id VARCHAR2) RETURN NVARCHAR2 RESULT_CACHE RELIES_ON (product_descriptions) IS result VARCHAR2(50); BEGIN SELECT translated_name INTO result FROM product_descriptions WHERE product_id = prod_id AND language_id = lang_id; RETURN result; END; Example of PL/SQL Result Cache When writing code for the PL/SQL result cache option, you need to: Include the RESULT_CACHE option in the function declaration section of a package Include the RESULT_CACHE option in the function definition Optionally include the RELIES_ON clause to specify any tables or views on which the function results depend In the example shown in the slide, the productName function has result caching enabled through the RESULT_CACHE option in the function declaration. In this example, the RELIES_ON clause is used to identify the PRODUCT_DESCRIPTIONS table on which the function results depend. Execute the function as: EXECUTE dbms_output.put_line(productname(1792, 'US')) Industrial 600/DQ You can also run the DBMS_RESULT_CACHE.MEMORY_REPORT to view the result cache memory results. For more information about result caching in Oracle Database 11g, review the Oracle by Example tutorial available at: Oracle Database 11g: SQL and PL/SQL New Features

159 Oracle Database 11g: SQL and PL/SQL New Features 1 - 159
Lesson Agenda Compiler changes and how the changes impact native compilation The SIMPLE_INTEGER data type Inlining Caching SQL result cache PL/SQL function result cache Flashback enhancements To store and track all transactional changes to a record over its lifetime Oracle Database 11g: SQL and PL/SQL New Features

160 Flashback Data Archives
Provide the ability to store and track all transactional changes to a record over its lifetime Save development resources because you no longer need to build this intelligence into your application Are useful for compliance with record stage policies and audit reports Flashback Data Archives: Overview With Flashback Data Archives, you have the ability to track and store all transactional changes to a table over its lifetime. With this feature, you no longer need to manually build this functionality into your applications. Furthermore, Flashback Data Archive is useful for compliance with record stage policies and audit reports. Oracle Database 11g: SQL and PL/SQL New Features

161 Flashback Data Archive Process
1. Create the Flashback Data Archive. 2. Specify the default Flashback Data Archive. 3. Enable the Flashback Data Archive. 4. View Flashback Data Archive data. Flashback Data Archive Process The first step is to create a Flashback Data Archive. A Flashback Data Archive consists of one or more tablespaces. You can have multiple Flashback Data Archives. In the second step, you can specify a default Flashback Data Archive for the system. A Flashback Data Archive is configured with retention time. Data archived in the Flashback Data Archive is retained for the retention time. In the third step, you can enable flashback archiving (and then disable it again) for a table. Although flashback archiving is enabled for a table, some DDL statements are not allowed on that table. By default, flashback archiving is off for any table. Finally, you can examine the Flashback Data Archives. There are static data dictionary views that you can query for information about Flashback Data Archives. Oracle Database 11g: SQL and PL/SQL New Features

162 Flashback Data Archive Scenario
Using Flashback Data Archive to access historical data: CONNECT AS sysdba -- create the Flashback Data Archive CREATE FLASHBACK ARCHIVE DEFAULT fla1 TABLESPACE example QUOTA 10G RETENTION 5 YEAR; 1 -- Specify the default Flashback Data Archive ALTER FLASHBACK ARCHIVE fla1 SET DEFAULT; 2 -- Enable Flashback Data Archive ALTER TABLE oe1.inventories FLASHBACK ARCHIVE; ALTER TABLE oe1.warehouses FLASHBACK ARCHIVE; 3 Flashback Data Archive Scenario You create a Flashback Data Archive with the CREATE FLASHBACK ARCHIVE statement. You need the privileges to do so. Typically, steps 1–3 shown in the slide are issued by the DBA. You can optionally specify whether this is the default Flashback Data Archive for the system. If you omit this option, you can still make this Flashback Data Archive the default later. You must provide the name of the Flashback Data Archive. You must provide the name of the first tablespace of the Flashback Data Archive. You can optionally identify the Maximum amount of space that the Flashback Data Archive can use in the first tablespace. The default is unlimited. Unless your space quota on the first tablespace is also unlimited, you must specify this value; otherwise, you receive the ORA error. You must provide the retention time (number of days that Flashback Data Archive data for the table is guaranteed to be stored). In the first step in the example shown in the slide, a default Flashback Data Archive named fla1 is created that uses up to 10 GB of the tbs1 tablespace, whose data will be retained for five years. Oracle Database 11g: SQL and PL/SQL New Features

163 Oracle Database 11g: SQL and PL/SQL New Features 1 - 163
Flashback Data Archive Scenario (continued) In the second step, the default Flashback Data Archive is specified. By default, the system has no Flashback Data Archive. You can set it in one of two ways: 1. Specify the name of an existing Flashback Data Archive in the SET DEFAULT clause of the ALTER FLASHBACK ARCHIVE statement. 2. Include DEFAULT in the CREATE FLASHBACK ARCHIVE statement when you create a Flashback Data Archive. In the third step shown in the previous slide, Flashback Data Archive is enabled. By default, flashback archiving is disabled. At any time, you can enable flashback archiving for a table. Note: If Automatic Undo Management is disabled, you receive the ORA error when you try to modify the table. To enable flashback archiving for a table, include the FLASHBACK ARCHIVE clause in either the CREATE TABLE or ALTER TABLE statement. In the FLASHBACK ARCHIVE clause, you can specify the Flashback Data Archive where the historical data for the table will be stored. The default is the default Flashback Data Archive for the system. If a table already has flashback archiving enabled, and you try to enable it again with a different Flashback Data Archive, an error occurs. To disable flashback archiving for a table, specify NO FLASHBACK ARCHIVE in the ALTER TABLE statement. Oracle Database 11g: SQL and PL/SQL New Features

164 Flashback Data Archive Scenario
Using Flashback Data Archive to access historical data: Examine the data: Change the data: Examine the flashback data: SELECT product_id, warehouse_id, quantity_on_hand FROM oe1.inventories WHERE product_id = 3108; 1 UPDATE oe1.inventories SET quantity_on_hand = 300 WHERE product_id = 3108; 2 SELECT product_id, warehouse_id, quantity_on_hand FROM oe1.inventories AS OF TIMESTAMP TO_TIMESTAMP (' :00:00', 'YYYY-MM-DD HH24:MI:SS') WHERE product_id = 3108; Flashback Data Archive Scenario (continued) The last statement in the previous slide shows how to retrieve the inventories of product 3108 as of June 26, 2007. Before the UPDATE statement, the values retrieved from the first statement shown are: PRODUCT_ID WAREHOUSE_ID QUANTITY_ON_HAND 3 Oracle Database 11g: SQL and PL/SQL New Features

165 Oracle Database 11g: SQL and PL/SQL New Features 1 - 165
Flashback Data Archive Scenario (continued) After you execute the second statement, the QUANTITY_ON_HAND values for product 3108 are updated to 300. SELECT product_id, warehouse_id, quantity_on_hand FROM oe1.inventories WHERE product_id = 3108; PRODUCT_ID WAREHOUSE_ID QUANTITY_ON_HAND After the update occurs, you can still see the flashback data. These are the values retrieved from the third statement shown. Oracle Database 11g: SQL and PL/SQL New Features

166 Flashback Data Archive Dictionary Views
Viewing the results: View Name Description *_FLASHBACK_ARCHIVE Displays information about Flashback Data Archives *_FLASHBACK_ARCHIVE_TS Displays tablespaces of Flashback Data Archives *_FLASHBACK_ARCHIVE_TABLES Displays information about tables that are enabled for flashback archiving Dictionary View for Flashback Archives You can view information about your flashback archives from the dictionary views. Oracle Database 11g: SQL and PL/SQL New Features

167 Flashback Data Archive Dictionary Views
Viewing information about tables that are enabled for flashback archiving: DESCRIBE dba_flashback_archive_tables Name Null? Type TABLE_NAME NOT NULL VARCHAR2(30) OWNER_NAME NOT NULL VARCHAR2(30) FLASHBACK_ARCHIVE_NAME NOT NULL VARCHAR2(255) ARCHIVE_TABLE_NAME VARCHAR2(53) SELECT * FROM dba_flashback_archive_tables; TABLE_NAME OWNER_NAME FLASHBACK_ARCHIVE_NAME ARCHIVE_TABLE_NAME INVENTORIES OE FLA SYS_FBA_HIST_70355 WAREHOUSES OE FLA SYS_FBA_HIST_70336 Flashback Dictionary Information DESCRIBE dba_flashback_archive Name Null? Type FLASHBACK_ARCHIVE_NAME NOT NULL VARCHAR2(255) FLASHBACK_ARCHIVE# NOT NULL NUMBER RETENTION_IN_DAYS NOT NULL NUMBER CREATE_TIME TIMESTAMP(9) LAST_PURGE_TIME TIMESTAMP(9) STATUS VARCHAR2(7) SELECT * FROM dba_flashback_archive; FLASHBACK_ARCHIVE_NA FLASHBACK_ARCHIVE# RETENTION_IN_DAYS CREATE_TIME LAST_PURGE_TIME STATUS FLA 27-JUN PM 27-JUN PM DEFAULT Oracle Database 11g: SQL and PL/SQL New Features

168 Flashback Data Archive DDL Restrictions
Using any of the following DDL statements on a table enabled for Flashback Data Archive causes the error ORA-55610: Invalid DDL statement on history-tracked table ALTER TABLE statement that does any of the following: Drops, renames, or modifies a column Performs partition or subpartition operations Converts a LONG column to a LOB column Includes an UPGRADE TABLE clause, with or without an INCLUDING DATA clause DROP TABLE statement RENAME TABLE statement TRUNCATE TABLE statement Guidelines You can use the DBMS_FLASHBACK.ENABLE and DBMS_FLASHBACK.DISABLE procedures to enable and disable the Flashback Data Archives, respectively. Use Flashback Query, Flashback Version Query, or Flashback Transaction Query for the SQL code that you write, for convenience. To obtain an SCN to use later with a flashback feature, you can use the DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER function. To compute or retrieve a past time to use in a query, use a function return value as a time stamp or SCN argument. For example, add or subtract an INTERVAL value to the value of the SYSTIMESTAMP function. To ensure database consistency, always perform a COMMIT or ROLLBACK operation before querying past data. Remember that all flashback processing uses the current session settings, such as national language and character set, not the settings that were in effect at the time being queried. To query past data at a precise time, use an SCN. If you use a time stamp, the actual time queried might be up to 3 seconds earlier than the time you specify. Oracle Database uses SCNs internally and maps them to time stamps at a granularity of 3 seconds. You cannot retrieve past data from a dynamic performance (V$) view. A query on such a view always returns current data. However, you can perform queries on past data in static data dictionary views, such as *_TABLES. Oracle Database 11g: SQL and PL/SQL New Features

169 Oracle Database 11g: SQL and PL/SQL New Features 1 - 169
Summary In this lesson, you should have learned how to: List the compiler changes and explain how the changes impact native compilation Use the new SIMPLE_INTEGER data type Describe the process of inlining Use caching for optimization Use flashback to store and track all transactional changes to a record Summary In this lesson, you learned about the Oracle Database 11g performance improvements for SQL and PL/SQL. Oracle Database 11g: SQL and PL/SQL New Features

170 Practice 5 Overview: Implementing Performance Improvements
This practice covers the following topics: Testing the performance of the SIMPLE_INTEGER data type Writing code to use SQL caching Writing code to use PL/SQL caching Examining inlined code and practicing how to influence inlining Practice 5 Overview: Implementing the Performance Improvements In this practice, you examine the new performance improvements. This practice uses the OE schema. Oracle Database 11g: SQL and PL/SQL New Features

171 Oracle Database 11g: SQL and PL/SQL New Features 1 - 171
Practice 5 1. Start SQL Developer and connect to the database using your mydbconnection created earlier. 2. In this example, you examine the performance of the SIMPLE_INTEGER data type. a. Query the v$parameter dictionary view, and find the name and value for the PLSQL_CODE_TYPE parameter. SELECT name, value FROM v$parameter WHERE name like 'plsql%'; b. Execute the following code and note the results. (You can use the lab_05_02_b.sql file). CREATE OR REPLACE PROCEDURE test_simple_integer IS sim_counter SIMPLE_INTEGER :=0 ; t_start SIMPLE_INTEGER :=0 ; t_end SIMPLE_INTEGER :=0 ; t_max SIMPLE_INTEGER := ; BEGIN t_start := DBMS_UTILITY.GET_CPU_TIME(); WHILE sim_counter < t_max LOOP sim_counter := sim_counter + 1; END LOOP; t_end := DBMS_UTILITY.GET_CPU_TIME(); DBMS_OUTPUT.PUT_LINE((t_end-t_start)||' centiseconds with Simple counter'); END test_simple_integer ; / Results:________________________________________ c. Change your session so that native compilation is used. ALTER SESSION SET PLSQL_CODE_TYPE = 'NATIVE'; Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

172 Oracle Database 11g: SQL and PL/SQL New Features 1 - 172
Practice 5 (continued) d. Recompile the code and then run the procedure. Note the results: CREATE OR REPLACE PROCEDURE test_simple_integer IS sim_counter SIMPLE_INTEGER :=0 ; t_start SIMPLE_INTEGER :=0 ; t_end SIMPLE_INTEGER :=0 ; t_max SIMPLE_INTEGER := ; BEGIN t_start := DBMS_UTILITY.GET_CPU_TIME(); WHILE sim_counter < t_max LOOP sim_counter := sim_counter + 1; END LOOP; t_end := DBMS_UTILITY.GET_CPU_TIME(); DBMS_OUTPUT.PUT_LINE((t_end-t_start)||' centiseconds with Simple counter'); END test_simple_integer ; / Results:________________________________________ Explain the results. e. What is wrong with this code? (You can use the lab_05_02_f.sql file). CREATE OR REPLACE PROCEDURE loopy (p_counter SIMPLE_INTEGER) IS v_counter SIMPLE_INTEGER; v_counter := p_counter; FOR i IN 1..v_counter LOOP dbms_output.put_line('hello world'); END loopy; Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

173 Oracle Database 11g: SQL and PL/SQL New Features 1 - 173
Practice 5 (continued) 3. Examine SQL and PL/SQL Result Caching. a. Examine the Explain Plan for this query found in the lab_05_03_a.sql lab file. You can see the Explain Plan by clicking the Explain Plan icon in the Code Editor window. SELECT count(*), round(avg(quantity_on_hand)) AVG_AMT, product_id, product_name FROM inventories natural join product_information GROUP BY product_id, product_name; Results:________________________________________ b. Add the RESULT_CACHE hint to the query and re-examine the Explain Plan results. SELECT /*+ result_cache */ count(*), c. The following code is used to generate a list of warehouse names for pick lists in applications. The WAREHOUSES table is fairly stable and is not modified often. Execute this code (You can use the lab_05_03_c.sql file): Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

174 Oracle Database 11g: SQL and PL/SQL New Features 1 - 174
Practice 5 (continued) CREATE OR REPLACE TYPE list_typ IS TABLE OF VARCHAR2(35); / CREATE OR REPLACE FUNCTION get_warehouse_names RETURN list_typ IS v_count BINARY_INTEGER; v_wh_names list_typ; BEGIN SELECT count(*) INTO v_count FROM warehouses; FOR i in 1..v_count LOOP SELECT warehouse_name INTO v_wh_names(i) END LOOP; RETURN v_wh_names; END get_warehouse_names; d. Because the function is called frequently and because the content of the data returned does not frequently change, this code is a good candidate for PL/SQL result caching. Modify the code so that PL/SQL result caching is turned on. Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

175 Oracle Database 11g: SQL and PL/SQL New Features 1 - 175
Practice 5 (continued) 4. Inlining replaces procedure calls with a copy of the body of the procedure to be called. Which statements are true? a. You can influence inlining. b. You can control inlining by setting PLSQL_OPTIMIZE_LEVEL and PRAGMA INLINE. Explain the following: c. Explain the settings for PLSQL_OPTMIZE_LEVEL. d. Explain the valid values for PRAGMA INLINE. e. What is the warning message returned to you when inlining occurs? Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

176 Notes Page Only

177 Practicing the Language Usability Enhancements

178 Oracle Database 11g: SQL and PL/SQL New Features 1 - 178
Objectives After completing this lesson, you should be able to: Implement the sequence calls to NEXTVAL and CURRVAL without using a SQL statement to retrieve the values Use the new CONTINUE statement to control the next loop iteration or to leave a loop Use both named and mixed notation calls to functions from a SQL statement Use the ALTER TABLE statement to change tables to read-only status Objectives In this lesson, you learn about the language usability enhancements added in Oracle Database 11g. Oracle Database 11g: SQL and PL/SQL New Features

179 Oracle Database 11g: SQL and PL/SQL New Features 1 - 179
Lesson Agenda Changes to sequence calls The new CONTINUE statement Named and mixed notation calls Read-only tables Oracle Database 11g: SQL and PL/SQL New Features

180 Sequence Enhancement in PL/SQL Expressions
Prior to Oracle Database 11g release: References to sequences were permitted only through SQL statements Use of CURRVAL and NEXTVAL pseudocolumns was not allowed in PL/SQL unless embedded in a SQL statement Using sequences in PL/SQL was cumbersome and required an additional SQL statement in a PL/SQL subroutine Sequence Enhancement Prior to Oracle Database 11g, you were forced to write a SQL statement in order to use a sequence object value in a PL/SQL subroutine. Typically, you would write a SELECT statement to reference the pseudocolumns of NEXTVAL and CURRVAL to obtain a sequence number. This method created a usability problem. 1 2 3 4 5 6 7 Oracle Database 11g: SQL and PL/SQL New Features

181 Sequence Enhancement in PL/SQL Expressions
Enhancements in Oracle Database 11g: You can use the CURRVAL and NEXTVAL pseudocolumns, qualified by a sequence name, directly in a PL/SQL expression. Sequence usability is improved. Less typing is required by the developer. The resulting code is clearer. 1 2 3 4 5 6 7 Sequence Enhancement (continued) In Oracle Database 11g, the limitation of forcing you to write a SQL statement to retrieve a sequence value is lifted. With the sequence enhancement feature: Sequence usability is improved Less typing is required by the developer The resulting code is clearer Oracle Database 11g: SQL and PL/SQL New Features

182 Using Sequences in PL/SQL Expressions
Pre-Oracle Database 11g: Starting in Oracle Database 11g: declare v_new_id NUMBER; BEGIN SELECT my_seq.NEXTVAL INTO v_new_id FROM Dual; END; / DECLARE v_new_id NUMBER; BEGIN v_new_id := my_seq.NEXTVAL; END; / Accessing Sequence Values in Oracle Database 11g In Oracle Database 11g, you can use the NEXTVAL and CURRVAL pseudocolumns in any PL/SQL context where an expression of NUMBER data type may legally appear. The code shown in the slide assumes that you have a sequence named MY_SEQ created: CREATE SEQUENCE my_seq; Oracle Database 11g: SQL and PL/SQL New Features

183 Using Sequences in PL/SQL Expressions
Try to avoid using the old syntax anymore: SELECT my_seq.NEXTVAL INTO v_new_id FROM dual; Oracle Database 11g: SQL and PL/SQL New Features

184 Oracle Database 11g: SQL and PL/SQL New Features 1 - 184
Lesson Agenda Changes to sequence calls The new CONTINUE statement Named and mixed notation calls Read-only tables Oracle Database 11g: SQL and PL/SQL New Features

185 PL/SQL CONTINUE Statement
Definition: Adds the functionality to begin the next loop iteration Provides programmers with the ability to transfer control to the next iteration of a loop Uses parallel structure and semantics to the EXIT statement Benefits: Eases the programming process May see a small performance improvement over the previous programming workarounds to simulate the CONTINUE statement New PL/SQL CONTINUE Statement The CONTINUE statement that is added to PL/SQL enables you to transfer control within a loop back to a new iteration or to leave the loop. Many other programming languages have this functionality. With Oracle Database 11g, PL/SQL also offers this functionality. Prior to Oracle Database 11g, you could code a workaround using Boolean variables and conditional statements to simulate the CONTINUE programmatic functionality. In some cases, the workarounds are less efficient. Oracle Database 11g: SQL and PL/SQL New Features

186 PL/SQL CONTINUE Statement: Usage
Offers you a simplified means to control loop iterations Can be more efficient than previous coding workarounds Is commonly used to filter data inside a loop body before the main processing begins CONTINUE Statement: Usage The CONTINUE statement offers you a simplified means to control loop iterations. It may be more efficient than previous coding workarounds. The CONTINUE statement is commonly used to filter data inside a loop body before the main processing begins. Oracle Database 11g: SQL and PL/SQL New Features

187 PL/SQL CONTINUE Statement: Example
Total is: 1 End of Loop Total is: 2 Total is: 4 6 Total is: 9 12 Total is: 16 20 Total is: 25 30 Total is: 36 Total is: 43 Total is: 51 Total is: 60 Total is: 70 PL/SQL procedure successfully completed. DECLARE v_total SIMPLE_INTEGER := 0; BEGIN FOR i IN LOOP v_total := v_total + i; dbms_output.put_line ('Total is: '|| v_total); CONTINUE WHEN i > 5; ('End of Loop Total is: '|| v_total); END LOOP; END; / 1 2 PL/SQL CONTINUE Statement: Example The first TOTAL assignment is executed for each of the 10 iterations of the loop. The second TOTAL assignment is executed for the first five iterations of the loop. The CONTINUE statement transfers control within a loop back to a new iteration, so for the last five iterations of the loop, the second TOTAL assignment is not executed. The end result of the TOTAL variable is 70. Oracle Database 11g: SQL and PL/SQL New Features

188 PL/SQL CONTINUE Statement: Example
CREATE OR REPLACE PROCEDURE two_loop IS v_total NUMBER := 0; BEGIN <<BeforeTopLoop>> FOR i IN LOOP v_total := v_total + 1; dbms_output.put_line ('Total is: ' || v_total); FOR j IN LOOP CONTINUE BeforeTopLoop WHEN i + j > 5; END LOOP; END two_loop; Procedure created. --RESULTS: EXECUTE two_loop Total is: 1 Total is: 6 Total is: 10 Total is: 13 Total is: 15 Total is: 16 Total is: 17 Total is: 18 Total is: 19 Total is: 20 PL/SQL procedure successfully completed. PL/SQL CONTINUE Statement: Example (continued) You can use the CONTINUE statement to jump to the next iteration of an outer loop. Give the outer loop a label to identify where the CONTINUE statement should go. The CONTINUE statement in the innermost loop terminates that loop whenever the WHEN condition is true (just like the keyword EXIT). After the innermost loop is terminated by the CONTINUE statement, control transfers to the next iteration of the outermost loop, labeled BeforeTopLoop in this example. When this pair of loops completes, the value of the TOTAL variable is 20. You can also use the CONTINUE statement inside an inner block of code that does not contain a loop as long as the block is nested inside an appropriate outer loop. Oracle Database 11g: SQL and PL/SQL New Features

189 CONTINUE Statement: Guidelines
The CONTINUE statement offers you the functionality to transfer control within a loop back to a new iteration or to leave the loop. The CONTINUE statement cannot appear outside a loop at all; this generates a compiler error. You cannot use the CONTINUE statement to pass through a procedure, function, or method boundary; this generates a compiler error. CONTINUE Statement: Guidelines The CONTINUE statement gives you greater programming functionality. However, there are some limitations for its use. Be careful not to use the CONTINUE statement outside of a loop or to pass through a routine boundary. Oracle Database 11g: SQL and PL/SQL New Features

190 Oracle Database 11g: SQL and PL/SQL New Features 1 - 190
Lesson Agenda Changes to sequence calls The new CONTINUE statement Named and mixed notation calls Read-only tables Oracle Database 11g: SQL and PL/SQL New Features

191 Named and Mixed Notation from SQL
Definition: PL/SQL allows arguments in a subroutine call to be specified using positional, named, or mixed notation. Before Oracle Database 11g, only the positional notation was supported in calls from SQL. Starting in Oracle Database 11g, named and mixed notation can be used for specifying arguments in calls to PL/SQL subroutines from SQL statements. Benefits: For long parameter lists, with most having default values, you can omit values from the optional parameters. You can avoid duplicating the default value of the optional parameter at each call site. Oracle Database 11g: SQL and PL/SQL New Features

192 Named and Mixed Notation from SQL: Example
CREATE OR REPLACE FUNCTION f ( p1 IN NUMBER DEFAULT 1, p5 IN NUMBER DEFAULT 5) RETURN NUMBER IS v number; BEGIN v:= p1 + (p5 * 2); RETURN v; END f; / Function created. SELECT f(p5 => 10) FROM DUAL; F(P5=>10) 21 Named and Mixed Notation: Example In the example shown in the slide, the call to the function f within the SELECT SQL statement uses the named notation. Prior to Oracle Database 11g, you could not use the named or mixed notation when passing parameters to a function from within a SQL statement. Prior to Oracle Database 11g, you received the following error: SELECT f(p5 => 10) FROM DUAL; ORA-00907: missing right parenthesis Oracle Database 11g: SQL and PL/SQL New Features

193 Oracle Database 11g: SQL and PL/SQL New Features 1 - 193
Lesson Agenda Changes to sequence calls The new CONTINUE statement Named and mixed notation calls Read-only tables Oracle Database 11g: SQL and PL/SQL New Features

194 Oracle Database 11g: SQL and PL/SQL New Features 1 - 194
Read-Only Tables Use the ALTER TABLE syntax to put a table into read-only mode: Prevents DDL or DML changes during table maintenance Changes it back into read/write mode ALTER TABLE customers READ ONLY; -- perform table maintenance and then -- return table back to read/write mode ALTER TABLE customers READ WRITE; Read-Only Tables You can specify READ ONLY to place a table in read-only mode. When the table is in read-only mode, you cannot issue any DML statements that affect the table or any SELECT ... FOR UPDATE statements. You can issue DDL statements as long as they do not modify any table data. Operations on indexes associated with the table are allowed when the table is in read-only mode. Specify READ WRITE to return a read-only table to read/write mode. Note: You can drop a table that is in read-only mode. The DROP command is executed only in the data dictionary, so access to the table contents is not required. The space used by the table will not be reclaimed, until the tablespace is made read/write again and the required changes can be made to the block segment headers, and so on. Oracle Database 11g: SQL and PL/SQL New Features

195 Oracle Database 11g: SQL and PL/SQL New Features 1 - 195
Summary In this lesson, you should have learned how to: Implement the sequence calls to NEXTVAL and CURRVAL without using a SQL statement to retrieve the values. Use the new CONTINUE statement to control the next loop iteration or to leave a loop. Use both named and mixed notation calls to functions from a SQL statement. Use the ALTER TABLE statement to change tables to read-only status. Summary In this lesson, you learned about a variety of topics pertaining to SQL and PL/SQL language usability. Sequences are easier to use in PL/SQL. PL/SQL has a new CONTINUE statement to help you control loop logic. You can use named, positional, and a mix of the two calls when accessing functions from a SQL statement. The ALTER TABLE syntax now lets you change your tables to read-only status and then change them back to read/write status. Oracle Database 11g: SQL and PL/SQL New Features

196 Practice 6 Overview: Using the New SQL and PL/SQL Usability Features
This practice covers the following topics: Trying the new syntax for sequences Controlling loop iteration with the CONTINUE statement Using the named and mixed notation calls to functions from a SQL statement Changing the status of a table to read-only Practice 6 Overview: Using the New SQL and PL/SQL Usability Enhancements In this practice, you try out the various usability features introduced in Oracle Database 11g. This practice uses the OE schema. Oracle Database 11g: SQL and PL/SQL New Features

197 Oracle Database 11g: SQL and PL/SQL New Features 1 - 197
Practice 6 1. Start SQL Developer and connect to the database using your mydbconnection created earlier. 2. In this practice, you create a sequence, examine code that uses the sequence, and then modify the code so that it uses the new method of invoking a sequence in PL/SQL. a. Create this sequence: CREATE SEQUENCE customer_seq START WITH 1000; / b. Examine this code (This code is found in the lab_06_02_b.sql file.): CREATE OR REPLACE PROCEDURE add_customer (p_last_name customers.cust_last_name%TYPE, p_first_name customers.cust_first_name%TYPE) IS v_id NUMBER; BEGIN SELECT customer_seq.nextval INTO v_id FROM dual; INSERT INTO customers(customer_id,cust_last_name, cust_first_name) VALUES (v_id, p_last_name, p_first_name); END add_customer; c. Modify the code so that you can call the sequence directly and eliminate the SELECT statement. d. Test your procedure. Execute the ADD_CUSTOMERS procedure and pass ‘Smith’ for the last name parameter, and ‘John’ for the first name parameter. e. Verify that the row is inserted. Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

198 Oracle Database 11g: SQL and PL/SQL New Features 1 - 198
Practice 6 (continued) 3. In this practice, you practice using the mixed notation calls with PL/SQL. a. Create a function named CALC_CHARGES that calculates the surcharge on a product based on its weight class. (You can use the lab_06_03_a.sql file.) CREATE OR REPLACE FUNCTION calc_charges (p_id NUMBER DEFAULT 0, p_weight_class NUMBER DEFAULT 1, p_list_price NUMBER DEFAULT 0) RETURN NUMBER IS v_surcharge NUMBER; BEGIN IF p_id = 0 THEN RETURN 0; END IF; v_surcharge := CASE p_weight_class WHEN 1 THEN 1.005 WHEN 2 THEN 1.01 WHEN 3 THEN 1.015 WHEN 4 THEN 1.02 WHEN 5 THEN 1.025 ELSE 1 END; RETURN v_surcharge * p_list_price; END calc_charges; / b. Sample the data with the following query: SELECT product_id, list_price, weight_class, calc_charges(product_id, weight_class, list_price) AS new_price FROM product_information WHERE product_id BETWEEN 1725 and 1740; c. Execute the query again, however, this time, pass in values only for the WEIGHT_CLASS and LIST_PRICE parameters. d. Execute the query again, however, this time, pass in values only for the PRODUCT_ID and LIST_PRICE parameters. Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

199 Oracle Database 11g: SQL and PL/SQL New Features 1 - 199
Practice 6 (continued) 4. Use the CONTINUE statement to control loop iterations. a. Examine this code. What is wrong with it? (Do not execute this code.) -- what is wrong with this code (do not execute this!) CREATE OR REPLACE PROCEDURE continue_loop (p_looper NUMBER) IS v_index SIMPLE_INTEGER := 0; BEGIN LOOP CONTINUE WHEN v_index < p_looper; v_index := v_index + 1; dbms_output.put_line ('In the loop, index value is: ' || v_index); EXIT WHEN v_index = p_looper; END LOOP; dbms_output.put_line ('Out of the loop, index value is: ' || v_index); END continue_loop; / b. Examine this code. Create the procedure in SQL Developer. (You can use the lab_06_04_b.sql file.) dbms_output.put_line ('In the loop before CONTINUE, index value is: ' || v_index); IF v_index < 3 THEN CONTINUE; END IF; dbms_output.put_line ('In the loop after CONTINUE, dbms_output.put_line ('Out of the loop, Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

200 Oracle Database 11g: SQL and PL/SQL New Features 1 - 200
Practice 6 (continued) c. What are the results when you execute the CONTINUE_LOOP procedure and pass to it the value of 5? Try to figure out the results of the v_index variable without running the procedure. d. Run the procedure to verify your results. 5. In this practice, you modify a table to be read-only. Then you alter the status back to read/write. a. Change the CUSTOMERS table so that it has read-only access. b. Execute the ADD_CUSTOMER procedure that you created in practice 6, question 2. Pass your first name and last name to the procedure. c. What are the results? d. Change the table status to read/write. e. Execute the ADD_CUSTOMER procedure that you created in practice 6, question 2. Pass your first name and last name to the procedure. f. Examine the results. Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

201 Developing Triggers that Utilize the New Enhancements

202 Oracle Database 11g: SQL and PL/SQL New Features 1 - 202
Objectives After completing this lesson, you should be able to: Describe compound triggers Create compound triggers Create disabled triggers Use the ENABLE clause with a trigger Control trigger order with the FOLLOWS clause Objectives In this lesson, you learn about the new trigger type and the trigger enhancements. Oracle Database 11g added a few enhancements to triggers. The first enhancement is a new trigger type called a compound trigger. A compound trigger is a single trigger on a table that allows you to specify actions for each of the four triggering timing points. In addition to the new trigger type, the CREATE TRIGGER SQL statement now includes the ENABLE, DISABLE, and FOLLOWS clauses that give you more control over triggers. The DISABLE clause allows you to create a trigger in a disabled state to ensure that the code compiles successfully before enabling the trigger. The ENABLE clause enables the trigger. The FOLLOWS clause allows you to specify that the trigger fires after certain other triggers. Oracle Database 11g: SQL and PL/SQL New Features

203 Compound Trigger: Overview
Definition: It is a single trigger on a table that allows you to specify actions for each of four timing points of the trigger. The trigger body supports a common PL/SQL state that the code for each timing point can access. You can avoid the mutating table error by allowing rows destined for a second table to accumulate and then bulk-inserting them. Benefits: Improved usability for the PL/SQL programmer Improved run-time performance and scalability Overview of Compound Triggers Starting with Oracle Database 11g, you can use a compound trigger. With a compound trigger, you create a single trigger on a table that allows you to specify actions for each of the four triggering timing points. The compound trigger body supports a common PL/SQL state that the code for each timing point can access. The common state is automatically destroyed when the firing statement completes, even when the firing statement causes an error. Your applications can avoid the mutating table error by allowing rows destined for a second table (such as a history table or an audit table) to accumulate and then bulk-inserting them. Before Oracle Database 11g, Release 1 (11.1), you needed to model the common state with an ancillary package. This approach was both cumbersome to program and subject to memory leak when the firing statement causes an error and the after-statement trigger does not fire. Compound triggers make PL/SQL easier to use, and improve run-time performance and scalability. Oracle Database 11g: SQL and PL/SQL New Features

204 Compound Trigger: Overview
Can be used on tables or views A single trigger with four timing points: 1. Before the firing statement 2. Before each row that the firing statement affects 3. After each row that the firing statement affects 4. After the firing statement INSERT UPDATE DELETE Before statement Before row After row After statement Overview of Compound Triggers (continued) A compound trigger is a single trigger on a table that allows you to specify actions for each of the four triggering timing points: Before the firing statement Before each row that the firing statement affects After each row that the firing statement affects After the firing statement If multiple compound triggers are specified on a table, all BEFORE statement sections will be executed at the BEFORE statement timing point, BEFORE EACH ROW sections will be executed at the BEFORE EACH ROW timing point, and so forth. If trigger execution order has been specified using the FOLLOWS clause, order of execution of the compound trigger sections will be determined by the FOLLOWS clause. If FOLLOWS is specified only for some triggers but not all triggers, the order of execution of triggers is guaranteed only for those that are related using the FOLLOWS clause. Oracle Database 11g: SQL and PL/SQL New Features

205 Compound Trigger: Structure
For tables: CREATE OR REPLACE TRIGGER schema.trigger FOR dml_event_clause ON schema.table COMPOUND TRIGGER -- Initial section -- Declarations -- Subprograms -- Optional section BEFORE STATEMENT IS ...; BEFORE EACH ROW IS ...; AFTER EACH ROW IS ...; AFTER STATEMENT IS ...; 1 2 Compound Trigger: Structure For Tables A compound trigger has two main sections: 1. An initial section where variables and subprograms are declared. The code in this section executes before any of the code in the optional section. 2. An optional section that defines the code for each possible trigger point. Depending on whether you are defining a compound trigger for a table or for a view, these triggering points are different and are listed in the image shown in the slide and on the following page. The code for the triggering points must follow the order shown in the slide. Oracle Database 11g: SQL and PL/SQL New Features

206 Compound Trigger: Structure
For views: CREATE OR REPLACE TRIGGER schema.trigger FOR dml_event_clause ON schema.view COMPOUND TRIGGER -- Initial section -- Declarations -- Subprograms -- Optional section (exclusive) INSTEAD OF EACH ROW IS ...; Compound Trigger: Structure (continued) For Views With views, an INSTEAD OF EACH ROW clause takes the place of the BEFORE EACH ROW and AFTER EACH ROW clauses. Oracle Database 11g: SQL and PL/SQL New Features

207 Oracle Database 11g: SQL and PL/SQL New Features 1 - 207
Compound Trigger: Structure (continued) Usage Notes A compound trigger must be implemented in PL/SQL. It must be either a PL/SQL block or a PL/SQL procedure. It cannot be a C or Java procedure, and it cannot call C or Java procedures. The optional section cannot be enclosed in a PL/SQL block. If a compound trigger has the INSTEAD OF EACH ROW section, that must be its only section. Any section can include the Inserting(), Updating(), Deleting(), and Applying() functions. The triggering statement of a compound trigger must be a DML statement. If the triggering statement affects no rows, and the compound trigger has neither a BEFORE STATEMENT section nor an AFTER STATEMENT section, the trigger never fires. If the triggering statement of a compound trigger is within a FORALL statement, each execution of the triggering statement fires the compound trigger anew. Specify the FOR clause if you are creating a compound trigger. You cannot specify the FOR EACH ROW clause for a compound trigger. A compound trigger must be associated with either a table or a view. The initial section cannot include PRAGMA AUTONOMOUS_TRANSACTION. A compound trigger body cannot have an initialization block; therefore, it cannot have an exception section. This is not a problem, however, because the BEFORE STATEMENT section is always executed exactly once before any other optional sections are executed. An exception that occurs in one section must be handled in that section. You cannot transfer control to another section. If a section includes a GOTO statement, the target of the GOTO statement must be in the same section. :OLD, :NEW, and :PARENT cannot appear in the initial section, the BEFORE STATEMENT section, or the AFTER STATEMENT section. Only the BEFORE EACH ROW section can change the value of :NEW. If, after the compound trigger is fired, the triggering statement rolls back due to a DML exception: - Local variables declared in the compound trigger sections are reinitialized, and any values computed thus far are lost - Side effects from firing the compound trigger are not rolled back The firing order of compound triggers is not guaranteed. Their firing can be interleaved with the firing of conventional triggers. If compound triggers are ordered using the FOLLOWS option, and if the target of FOLLOWS does not contain the corresponding section as source code, the ordering is ignored. Oracle Database 11g: SQL and PL/SQL New Features

208 Compound Trigger: Scenario
Track changes on the ORDER_TOTAL column in the ORDERS table to an audit table: INSERT UPDATE ORDERTOTALS_AUDIT table ORDERS table Compound Trigger: Scenario On the following pages, you see how a compound trigger is used to: Populate an audit table that records the value of the order total, for any inserted or updated rows in the ORDERS table (when the update changes a value in the ORDER_TOTAL column), the value of the order total, the time stamp of when the change was made, and the user ID Bulk insert records into the audit table to improve performance Assume that many rows are updated by a single UPDATE statement. Oracle Database 11g: SQL and PL/SQL New Features

209 Supporting Structures for Compound Trigger: Example
CREATE SEQUENCE ordertotals_audit_seq START WITH 2500; CREATE OR REPLACE TRIGGER gen_ordertotals_audit_id_trg BEFORE INSERT ON orders FOR EACH ROW BEGIN :NEW.order_id := ordertotals_audit_seq.NEXTVAL; END gen_ordertotals_audit_id_trg; CREATE TABLE ordertotals_audit( order_id NUMBER NOT NULL, change_date DATE NOT NULL, user_id VARCHAR2(30), old_total NUMBER(8, 2) NOT NULL, new_total NUMBER(8, 2) NOT NULL, CONSTRAINT order_total_PK PRIMARY KEY (order_id, change_date), CONSTRAINT orders_FK FOREIGN KEY (order_id) REFERENCES orders(order_id) ON DELETE CASCADE); Compound Trigger: Example Before creating the trigger shown in the slide, you should have the following object definitions: A sequence A BEFORE INSERT trigger on the ORDERS table that is used to generate a new order ID, using the sequence defined above The history audit table, which is a detail table for the ORDERS table with a composite primary key of ORDER_ID and CHANGE_DATE. The purpose of this table is to track changes to the ORDER_TOTAL column in the ORDERS table. This table will capture both the old order total (for order updates) and the new order total (for order updates and order inserts). Your session is set up as follows (this will enable inlining for your session): ALTER SESSION SET PLSQL_Warnings = 'enable:all'; ALTER SESSION SET PLSQL_Optimize_Level = 3; ALTER SESSION SET PLSQL_Code_Type = native; Oracle Database 11g: SQL and PL/SQL New Features

210 Compound Trigger: Example
CREATE OR REPLACE TRIGGER maintain_ordertotals_audit_trg FOR INSERT OR UPDATE OF order_total ON orders COMPOUND TRIGGER --Initial section begins --Declarations threshhold CONSTANT SIMPLE_INTEGER := 7; TYPE order_totals_t IS TABLE OF ordertotals_audit%rowtype INDEX BY PLS_INTEGER; o_totals order_totals_t; idx SIMPLE_INTEGER := 0; -- subprogram PROCEDURE Flush_Array IS n CONSTANT SIMPLE_INTEGER := o_totals.Count(); BEGIN FORALL j IN 1..n INSERT INTO ordertotals_audit VALUES o_totals(j); o_totals.Delete(); idx := 0; DBMS_Output.Put_Line('Flushed '||n||' rows'); END Flush_Array; -- Initial section ends Compound Trigger: Example (continued) This trigger fires on an INSERT or UPDATE of the ORDER_TOTAL column in the ORDERS table. The key points are the following: A THRESHOLD constant value is set to 7. A collection called O_TOTALS is defined to hold the values of the order ID and order totals being changed in the ORDERS table. The date of the change and user are also stored in this record structure. When a row is inserted into the ORDERS table or ORDER_TOTALS are updated in the ORDERS table, the BEFORE STATEMENT code runs and empties the O_TOTALS collection, and initializes an index to zero. For each row that is either inserted or updated in the ORDERS table, the AFTER EACH ROW code runs and builds the O_TOTALS collection so that it holds the values of the order ID and order total being changed in the ORDERS table. When the number of records in the O_TOTALS collection reaches 7, the FLUSH_ARRAY subroutine is called. The FLUSH_ARRAY subroutine performs a bulk insert into the ORDERTOTALS_AUDIT table. Note that not more than 7 records are bulk-inserted into the table because this is what the threshold is set to. Oracle Database 11g: SQL and PL/SQL New Features

211 Compound Trigger: Example
-- Optional section BEFORE STATEMENT IS BEGIN o_totals.Delete(); idx := 0; END BEFORE STATEMENT; AFTER EACH ROW IS idx := idx + 1; o_totals(idx).order_ID := :New.order_ID; o_totals(idx).Change_Date := SYSDATE(); o_totals(idx).user_id := sys_context('userenv', 'session_user'); o_totals(idx).old_total := :OLD.order_total; o_totals(idx).new_total := :NEW.order_total; IF idx >= Threshhold THEN -- PLW-06005: inlining... done Flush_Array(); END IF; END AFTER EACH ROW; AFTER STATEMENT IS -- PLW-06005: inlining... done END AFTER STATEMENT; END maintain_ordertotals_audit_trg; Compound Trigger: Example (continued) Finally, the AFTER STATEMENT code is run that calls the FLUSH_ARRAY subroutine. If there are less than 7 records being modified in the ORDERS table, this code ensures that these records are recorded into the ORDERTOTALS_AUDIT table. Oracle Database 11g: SQL and PL/SQL New Features

212 Compiling the Compound Trigger
The session settings are enabled for inlining and viewing all compiler messages. The warning message tells you that inlining is performed. The trigger is successfully compiled; this is only an informational warning. Compiling the Compound Trigger Because the session settings are enabled for inlining and viewing all compiler messages, when creating this trigger, you are able to see the message that inlining has been performed. In SQL*Plus, you see the following after issuing the SHOW ERRORS command: SP2-0814: Trigger created with compilation warnings SHOW ERRORS Errors for TRIGGER MAINTAIN_ORDERTOTALS_AUDIT: LINE/COL ERROR 32/7 PLW-06005: inlining of call of procedure 'FLUSH_ARRAY' was done 39/5 PLW-06005: inlining of call of procedure Oracle Database 11g: SQL and PL/SQL New Features

213 Firing the Compound Trigger
Execute a statement to force the trigger to fire: Examine the results in the audit table: UPDATE orders SET order_total = order_total * 1.05 WHERE order_status = 10; 1 SELECT * FROM ordertotals_audit; ORDER_ID CHANGE_DATE USER_ID OLD_TOTAL NEW_TOTAL JUL OE JUL OE JUL OE JUL OE JUL OE JUL OE 2 Firing the Compound Trigger In this scenario, all orders with an order status of 10 are surcharged a 5% tax. An UPDATE statement is executed to accomplish this. 1. The order total values for rows in the ORDERS table are changed. 2. This results in 6 rows being recorded into the ORDERTOTALS_AUDIT table. Oracle Database 11g: SQL and PL/SQL New Features

214 Oracle Database 11g: SQL and PL/SQL New Features 1 - 214
Other Trigger Changes More control over triggers CREATE TRIGGER now includes the ENABLE, DISABLE, and FOLLOWS clauses that give you more control over triggers. The DISABLE clause lets you create a trigger in a disabled state so that you can ensure that your code compiles successfully before you enable the trigger. The ENABLE clause enables the trigger. The FOLLOWS clause allows you to specify that the trigger you are creating fires after certain other triggers. Oracle Database 11g: SQL and PL/SQL New Features

215 Creating a Disabled Trigger
Prior to Oracle Database 11g, if you created a trigger whose body has a PL/SQL compilation error, DML to the table fails with “ORA-04098: trigger 'TRG' is invalid and failed re-validation.” It is safer to create it as disabled, and then, to enable it only when you know it will be compiled without an error. CREATE OR REPLACE TRIGGER gen_cust_id BEFORE INSERT ON customers FOR EACH ROW DISABLE BEGIN :NEW.customer_id := customer_seq.Nextval; END; / Creating a Disabled Trigger Starting with Oracle Database 11g, you can create a trigger in a disabled mode and later enable it with the ALTER TRIGGER statement. This gives you greater flexibility in that you can create a trigger and not use it until the data is ready. ALTER TRIGGER gen_cust_id ENABLE; Prior to Oracle Database 11g, you had the ability to compile a valid trigger and then disable it. You never had the ability to compile a trigger before all of the underlining data structures existed. This feature enhances the development process by removing the constraint that developers could not stage any triggers before all the data structures were in place with proper access. Oracle Database 11g: SQL and PL/SQL New Features

216 Oracle Database 11g: SQL and PL/SQL New Features 1 - 216
FOLLOWS Clause To ensure that a trigger fires after certain other triggers defined on the same object, use the FOLLOWS clause when you create the first trigger. Trig1 Trig2 Trig3 Trig4 FOLLOWS Clause: Overview To ensure that a trigger fires after certain other triggers defined on the same object, you can use the FOLLOWS clause when you create the first trigger. If two or more triggers are defined with the same timing point, and the order in which they fire is important, you can control the firing order using the FOLLOWS clause. Without the FOLLOWS clause, you are not guaranteed a firing order when two or more triggers of the same type are created on an object. If trigger execution order is specified by using the FOLLOWS clause, the order of execution of compound trigger sections is determined by the FOLLOWS clause. If FOLLOWS is specified only for some triggers but not all triggers, the order of execution of triggers is guaranteed only for those that are related using the FOLLOWS clause. FOLLOWS FOLLOWS FOLLOWS Oracle Database 11g: SQL and PL/SQL New Features

217 Oracle Database 11g: SQL and PL/SQL New Features 1 - 217
FOLLOWS Clause Applies to both compound and simple triggers Lets you order the executions of multiple triggers relative to each other Can be placed: In the definition of a simple trigger with a compound trigger target In the definition of a compound trigger with a simple trigger target Applies only to the section of the compound trigger with the same timing point as the simple trigger: If the compound trigger has no such timing point, FOLLOWS is quietly ignored. FOLLOWS Clause: Overview (continued) When defining triggers that contain the FOLLOWS clause, the specified triggers must already exist, they must be defined on the same table as the trigger being created, and they must have been successfully compiled. You do not need to have them enabled. Note: If it is practical, you should consider replacing the set of individual triggers for a particular timing point with a single compound trigger that explicitly codes the actions in the order you intend. Oracle Database 11g: SQL and PL/SQL New Features

218 FOLLOWS Clause: Scenario
COMPUTE_TOTAL CHANGE_PRODUCT FOLLOWS CREATE OR REPLACE TRIGGER change_product AFTER UPDATE of product_id ON order_items FOR EACH ROW FOLLOWS oe1.compute_total BEGIN dbms_output.put_line ('Do processing here…'); END; FOLLOWS Clause: Scenario For example, consider two AFTER ROW ... FOR UPDATE triggers defined on the same table. One trigger needs to reference the :OLD value and the other trigger needs to change the :OLD value. In this case, you can use the FOLLOWS clause to order the firing sequence. Specify FOLLOWS to indicate that the trigger being created should fire after the specified triggers. Oracle Database 11g: SQL and PL/SQL New Features

219 FOLLOWS Clause: Example
CREATE OR REPLACE TRIGGER compute_total AFTER UPDATE OR INSERT OR DELETE of unit_price, quantity ON order_items FOR EACH ROW BEGIN IF UPDATING THEN UPDATE orders SET order_total = order_total – (:old.unit_price * :old.quantity) + (:new.quantity * :new.unit_price) WHERE order_id = :old.order_id; ELSIF DELETING THEN ELSE --inserting UPDATE orders SET order_total = order_total + (:new.quantity * :new.unit_price) END IF; END; FOLLOWS Clause: Example In the example shown in the slide, the COMPUTE_TOTAL trigger is created. Its purpose is to update an order total whenever a price or quantity of a product in the ORDER_ITEMS table changes. To verify the data, examine the following information: SELECT * FROM order_items WHERE order_id = 2412; ORDER_ID LINE_ITEM_ID PRODUCT_ID UNIT_PRICE QUANTITY 9 rows selected. Oracle Database 11g: SQL and PL/SQL New Features

220 FOLLOWS Clause: Example
UPDATE order_items SET quantity = 100 WHERE order_id = 2412 AND line_item_id = 9; 1 row updated. 1 SELECT order_id, order_date, customer_id, order_status, order_total FROM orders WHERE customer_id = 170; ORDER_ID ORDER_DAT CUSTOMER_ID ORDER_STATUS ORDER_TOTAL MAR 2 UPDATE order_items SET product_id=3165 WHERE order_id = 2412 AND line_item_id = 8; Do processing here... 1 row updated. 3 FOLLOWS Clause: Example (continued) 1. In the examples shown in the slide, the first statement changes the quantity of an item ordered. This causes the COMPUTE_TOTAL trigger to fire and the order total is updated. 2. The value of the updated ORDER_TOTAL is displayed. 3. The value for a product ID is changed in the ORDER_ITEMS table. This causes the CHANGE_PRODUCT trigger to fire. Because the CHANGE_PRODUCT trigger has the FOLLOWS clause, it fires following the execution of the COMPUTE_TOTAL trigger. Oracle Database 11g: SQL and PL/SQL New Features

221 Oracle Database 11g: SQL and PL/SQL New Features 1 - 221
Summary In this lesson, you should have learned how to: Describe compound triggers Create compound triggers Create disabled triggers Use the ENABLE clause with a trigger Control trigger order with the FOLLOWS clause Summary In this lesson, you learned about the trigger enhancements made in Oracle Database 11g. These enhancements include a new trigger type called a compound trigger, and two new clauses. With the DISABLE clause, you can create a trigger in a disabled state. Previously, you could change only the state of a trigger after it was created by using the ALTER TRIGGER statement. Now that the DISABLE clause is added to the CREATE TRIGGER statement, you can create a trigger in a disabled state. The FOLLOWS clause enables you to control the order of how your triggers fire. Oracle Database 11g: SQL and PL/SQL New Features

222 Practice 7 Overview: Using the New Trigger Enhancements
This practice covers the following topics: Creating a disabled trigger and then enabling it Writing a compound trigger Controlling the trigger firing order with the new FOLLOWS clause Practice 7 Overview: Using the New Trigger Enhancements In this practice, you use the new trigger enhancements. Using the OE schema, you create a compound trigger. You practice controlling the firing order with the new trigger clauses. Oracle Database 11g: SQL and PL/SQL New Features

223 Oracle Database 11g: SQL and PL/SQL New Features 1 - 223
Practice 7 1. Start SQL Developer and connect to the database using your mydbconnection created earlier. 2. Alter your session so that you can view the compiler warnings. ALTER SESSION SET PLSQL_Warnings = 'enable:all'; ALTER SESSION SET PLSQL_Optimize_Level = 3; ALTER SESSION SET PLSQL_Code_Type = native; 3. Create a compound trigger to track any changes in the MIN_PRICE and LIST_PRICE columns in the PRODUCT_INFORMATION table. Whenever a price is modified, capture the date, user, product ID, old prices, and new prices. a. Create an audit table to store the information. Your audit table should look similar to the definition shown below (You can run the lab_07_03_a.sql script): CREATE TABLE productprice_audit( product_id NUMBER NOT NULL, change_date DATE NOT NULL, user_id VARCHAR2(30), old_min_price NUMBER, new_min_price NUMBER, old_list_price NUMBER, new_list_price NUMBER CONSTRAINT product_change_PK PRIMARY KEY (product_id, change_date), CONSTRAINT products_FK FOREIGN KEY (product_id) REFERENCES product_information(product_id) ON DELETE CASCADE); b. Create the compound trigger. Name the trigger MAINTAIN_PRICES_AUDIT_TRG. Build the trigger so that it checks for updates of the MIN_PRICE and LIST_PRICE columns in the PRODUCT_INFORMATION table. (You can use the code in the lab_07_03_b.sql script file.) CREATE OR REPLACE TRIGGER maintain_prices_audit_trg FOR UPDATE OF min_price, list_price ON product_information COMPOUND TRIGGER -- Initial section begins -- Enter your code here... Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

224 Oracle Database 11g: SQL and PL/SQL New Features 1 - 224
Practice 7 (continued) c. Execute the following query to analyze the data for supplier SELECT product_id, supplier_id, min_price, list_price FROM product_information WHERE supplier_id = ; d. Supplier is increasing its prices by 5 percent. Issue the UPDATE statement to update the list prices and minimum prices for supplier UPDATE product_information SET list_price = list_price * 1.05, min_list_price = min_list_price * 1.05 Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

225 Oracle Database 11g: SQL and PL/SQL New Features 1 - 225
Practice 7 (continued) e. Verify that the data has changed. f. Examine the contents of the PRODUCTPRICE_AUDIT table. Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

226 Oracle Database 11g: SQL and PL/SQL New Features 1 - 226
Practice 7 (continued) 4. In this practice, you create a disabled trigger, then execute a statement that would fire the trigger if it was not disabled. Then you enable the trigger and observe the results. This trigger uses the FOLLOWS clause to ensure that its firing order occurs after the MAINTAIN_PRICES_AUDIT_TRG created previously. a. Create a trigger named INFORM_LIST_PRICE in a disabled state. This trigger fires after the LIST_PRICE is updated and following MAINTAIN_PRICES_AUDIT_TRG. Have the trigger display the message “Warning – new list price is unknown for product: xyz,” where xyz is the product number. (You can use the code in the lab_07_04_a.sql script file.) b. Execute the following UPDATE statement and observe whether your trigger is fired. UPDATE product_information SET list_price = list_price * 1.05, min_list_price = min_list_price * 1.05 WHERE supplier_id = ; c. Enable the trigger. ALTER TRIGGER inform_list_price ENABLE; d. Execute the following UPDATE statement and observe whether your trigger is fired. You should see the following messages displayed: Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

227 Implementing SecureFile LOBs

228 Oracle Database 11g: SQL and PL/SQL New Features 1 - 228
Objectives After completing this lesson, you should be able to: Describe SecureFile LOB features Enable SecureFile LOB deduplication, compression, and encryption Migrate BasicFile LOBs to the SecureFile LOB format Analyze the performance of LOBs Objectives In this lesson, you learn to migrate the pre-Oracle Database 11g LOB storage format (called BasicFile LOB format) to the SecureFile LOB format. You also compare the performance of LOBs stored in the BasicFile format versus the SecureFile format. Finally, you learn how to enable SecureFile LOB deduplication (storage sharing), compression, and encryption. Oracle Database 11g: SQL and PL/SQL New Features

229 Oracle Database 11g: SQL and PL/SQL New Features 1 - 229
Lesson Agenda SecureFile LOB features Deduplication, compression, and encryption Migration of BasicFile LOBs to the SecureFile LOB format Performance of LOBs Oracle Database 11g: SQL and PL/SQL New Features

230 Introducing SecureFile LOBs
Oracle Database 11g offers a reengineered large object (LOB) data type that: Improves performance Eases manageability Simplifies application development Offers advanced, next-generation functionality such as intelligent compression and transparent encryption Introduction to SecureFile LOBs With SecureFile LOBs, the LOB data type is completely reengineered with dramatically improved performance, manageability, and ease of application development. This new implementation also offers advanced, next-generation functionality such as intelligent compression and transparent encryption. This feature significantly strengthens the native content management capabilities of Oracle Database. SecureFile LOBs are introduced to supplement the original BasicFile LOBs implementation that is identified by the BASICFILE SQL parameter. Oracle Database 11g: SQL and PL/SQL New Features

231 Storage of SecureFile LOBs
Oracle Database 11g implements a new storage paradigm for LOB storage: If the SECUREFILE storage keyword appears in the CREATE TABLE statement, the new storage is used. If the BASICFILE storage keyword appears in the CREATE TABLE statement, the old storage paradigm is used. By default, the storage is BASICFILE, unless you modify the setting for the DB_SECUREFILE parameter in the init.ora file. Storage of SecureFile LOBs Starting with Oracle Database 11g, you have the option of using the new SecureFile storage paradigm for LOBs. You can specify to use the new paradigm by using the SECUREFILE keyword in the CREATE TABLE statement. If that keyword is left out, the old storage paradigm for basic file LOBs is used. This is the default behavior. You can modify the init.ora file and change the default behavior for the storage of LOBs by setting the DB_SECUREFILE initialization parameter. The values allowed are: ALWAYS – Attempts to create all LOB files as SECUREFILES, but creates any LOBs not in ASSM tablespaces as BASICFILE LOBs FORCE – All LOBs created in the system are created as SECUREFILE LOBs PERMITTED – Is the default; allows SECUREFILES to be created when specified with the SECUREFILE keyword in the CREATE TABLE statement NEVER – Creates any LOBs that are specified as SECUREFILE LOBs as BASICFILE LOBs IGNORE – Ignores the SECUREFILE keyword and all SECUREFILE options Oracle Database 11g: SQL and PL/SQL New Features

232 Setting Up SecureFile LOBs
Create a tablespace for the LOB data: Create a table to hold the LOB data: -- have your dba do this: CREATE TABLESPACE sf_tbs1 DATAFILE 'sf_tbs1.dbf' SIZE 1500M REUSE AUTOEXTEND ON NEXT 200M MAXSIZE 3000M SEGMENT SPACE MANAGEMENT AUTO; 1 CONNECT CREATE TABLE customer_profiles (id NUMBER, first_name VARCHAR2 (40), last_name VARCHAR2 (80), profile_info BLOB) LOB(profile_info) STORE AS SECUREFILE (TABLESPACE sf_tbs1); 2 Setting Up SecureFile LOBs To create a column to hold an LOB that is a SecureFile, you need to: Create a tablespace to hold the data Define a table that contains an LOB column data type that is used to store the data in the SecureFile format In the example shown in the slide: 1. The sf_tbs1 tablespace is defined. This tablespace stores the LOB data in the SecureFile format. When you define a column to hold SecureFile data, you must have Automatic Segment Space Management (ASSM) enabled for the tablespace in order to support SecureFiles. 2. The CUSTOMER_PROFILES table is created. The column PROFILE_INFO will hold the LOB data in the SecureFile format because the storage clause identifies the format. Oracle Database 11g: SQL and PL/SQL New Features

233 Writing Data to the SecureFile LOB
Create the procedure to read the MS Word files and load them into the LOB column. Call this procedure from the WRITE_LOB procedure (shown on the next page). CREATE OR REPLACE PROCEDURE loadLOBFromBFILE_proc (dest_loc IN OUT BLOB, file_name IN VARCHAR2) IS src_loc BFILE := BFILENAME('CWD', file_name); amount INTEGER := 4000; BEGIN DBMS_LOB.OPEN(src_loc, DBMS_LOB.LOB_READONLY); amount := DBMS_LOB.GETLENGTH(src_loc); DBMS_LOB.LOADFROMFILE(dest_loc, src_loc, amount); DBMS_LOB.CLOSE(src_loc); END loadLOBFromBFILE_proc; Writing Data to the SecureFile LOB The procedure shown in the slide is used to load data into the LOB column. Before running the LOADLOBFROMBFILE_PROC procedure, you need to set a directory object that identifies where the LOB files are stored externally. In this example, the Microsoft Word documents are stored in a directory that is external to the database. Assume that the .doc files are placed in a folder called SECUREFILES: -- note: replace hostname with your host name. CREATE OR REPLACE DIRECTORY cwd AS '/hostname/oracle/SECUREFILES'; The LOADLOBFROMBFILE_PROC procedure is used to read the LOB data into the PROFILE_INFO column in the CUSTOMER_PROFILES table. In this example: DBMS_LOB.OPEN is used to open an external LOB in read-only mode DBMS_LOB.GETLENGTH is used to find the length of the LOB value DBMS_LOB.LOADFROMFILE is used to load the BFILE data into an internal LOB DBMS_LOB.CLOSE is used to close the external LOB Note: The LOADLOBFROMBFILE_PROC procedure shown in the slide can be used to read both SecureFile and BasicFile formats. Oracle Database 11g: SQL and PL/SQL New Features

234 Writing Data to the SecureFile LOB
Create the procedure to insert LOBs into the table: CREATE OR REPLACE PROCEDURE write_lob (p_file IN VARCHAR2) IS i NUMBER; v_fn VARCHAR2(15); v_ln VARCHAR2(40); v_b BLOB; BEGIN DBMS_OUTPUT.ENABLE; DBMS_OUTPUT.PUT_LINE('Begin inserting rows...'); FOR i IN LOOP v_fn:=SUBSTR(p_file,1,INSTR(p_file,'.')-1); v_ln:=SUBSTR(p_file,INSTR(p_file,'.')+1,LENGTH(p_file)- INSTR(p_file,'.')-4); INSERT INTO customer_profiles VALUES i, v_fn, v_ln, EMPTY_BLOB()) RETURNING profile_info INTO v_b; loadLOBFromBFILE_proc(v_b,p_file); DBMS_OUTPUT.PUT_LINE('Row '|| i ||' inserted.'); END LOOP; COMMIT; END write_lob; Writing Data to the SecureFile LOB (continued) Before you write data to the LOB column, you need to make the LOB column non-NULL. The LOB column must contain a locator that points to an empty or populated LOB value. You can initialize a BLOB column value by using the EMPTY_BLOB() function as a default predicate. The code shown in the slide uses the INSERT statement to initialize the locator. The LOADLOBFROMBFILE routine is then called and the LOB column value is inserted. The write and read performance statistics for LOB storage is captured through output messages. Oracle Database 11g: SQL and PL/SQL New Features

235 Writing Data to the SecureFile LOB
1 set serveroutput on set verify on set term on set linesize 200 timing start load_data execute write_lob('karl.brimmer.doc'); execute write_lob('monica.petera.doc'); execute write_lob('david.sloan.doc'); timing stop 2 Writing Data to the SecureFile LOB (continued) 1. The Microsoft Word files are stored in the SECUREFILES directory. 2. To read them into the PROFILE_INFO column in the CUSTOMER_PROFILES table, the WRITE_LOB procedure is called and the name of the .doc files is passed as a parameter. Note: This script is run in SQL*Plus because TIMING is a SQL*Plus option and is not available in SQL Developer. Oracle Database 11g: SQL and PL/SQL New Features

236 Important Instructions
Writing Data to the SecureFile LOB (continued) The output is similar to the following: timing start load_data execute write_lob('karl.brimmer.doc'); Begin inserting rows... Row 1 inserted. ... PL/SQL procedure successfully completed. execute write_lob('monica.petera.doc'); execute write_lob('david.sloan.doc'); timing stop timing for: load_data Elapsed: 00:00:00.96 Important Instructions Oracle Database 11g: SQL and PL/SQL New Features

237 Reading LOBs from the Table
Create the procedure to read LOBs from the table: CREATE OR REPLACE PROCEDURE read_lob IS lob_loc BLOB; CURSOR profiles_cur IS SELECT id, first_name, last_name, profile_info FROM customer_profiles; profiles_rec customer_profiles%ROWTYPE; BEGIN OPEN profiles_cur; LOOP FETCH profiles_cur INTO profiles_rec; lob_loc := profiles_rec.profile_info; DBMS_OUTPUT.PUT_LINE('The length is: '|| DBMS_LOB.GETLENGTH(lob_loc)); DBMS_OUTPUT.PUT_LINE('The ID is: '|| profiles_rec.id); DBMS_OUTPUT.PUT_LINE('The blob is read: '|| UTL_RAW.CAST_TO_VARCHAR2(DBMS_LOB.SUBSTR(lob_loc,200,1))); EXIT WHEN profiles_cur%NOTFOUND; END LOOP; CLOSE profiles_cur; END; Reading LOBs from the Table To retrieve the records that were inserted, you can call the READ_LOB procedure: set serveroutput on set verify on set term on set linesize 200 timing start read_data execute read_lob; timing stop The commands shown in the slide read back the 90 records from the CUSTOMER_PROFILES table. For each record, the size of the LOB value plus the first 200 characters of the LOB are displayed on screen. A SQL*Plus timer is started to capture the total elapsed time for the retrieval. Oracle Database 11g: SQL and PL/SQL New Features

238 Important Instructions
Reading LOBs from the Table (continued) The output is similar to the following: The ID is: 1 The blob is read: ¿¿¿ >  ¿¿   x  z  ¿¿¿¿ w ¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿ The length is: 64000 The ID is: 2 ... The length is: 37376 The ID is: 30 The blob is read: ¿¿¿ >  ¿¿   D  F  ¿¿¿¿ C PL/SQL procedure successfully completed. timing stop timing for: read_data Elapsed: 00:00:01.09 Note: The text shown on this page is intentional. The text appears as garbage because it is a binary file. Important Instructions Oracle Database 11g: SQL and PL/SQL New Features

239 Oracle Database 11g: SQL and PL/SQL New Features 1 - 239
Lesson Agenda SecureFile LOB features Deduplication, compression, and encryption Migration of BasicFile LOBs to the SecureFile LOB format Performance of LOBs Oracle Database 11g: SQL and PL/SQL New Features

240 Enabling Deduplication and Compression
To enable deduplication and compression, use the ALTER TABLE statement with the DEDUPLICATE and COMPRESS options. By enabling deduplication with SecureFiles, duplicate LOB data is automatically detected and space is conserved by storing only one copy. Enabling compression turns on LOB compression. ALTER TABLE tblname MODIFY LOB lobcolname (DEDUPLICATE option COMPRESS option) Enabling Deduplication and Compression with the ALTER TABLE Statement You can enable deduplication and compression of SecureFiles with the ALTER TABLE statement and the DEDUPLICATE and COMPRESS options. The DEDUPLICATE option allows you to specify that LOB data, which is identical in two or more rows in an LOB column, should all share the same data blocks. The opposite of this option is KEEP_DUPLICATES. Using a secure hash index to detect duplication, the database combines LOBs with identical content into a single copy, reducing storage and simplifying storage management. You can also use DBMS_LOB.SETOPTIONS to enable or disable deduplication on individual LOBs. The options for the COMPRESS clause are: COMPRESS HIGH – Provides the best compression but incurs the most work COMPRESS MEDIUM – Is the default value NOCOMPRESS – Disables compression You can also use DBMS_LOB.SETOPTIONS to enable or disable compression on individual LOBs. Oracle Database 11g: SQL and PL/SQL New Features

241 Enabling Deduplication and Compression: Example
1. Check the space being used by the CUSTOMER_PROFILES table. 2. Enable deduplication and compression on the PROFILE_INFO LOB column with the ALTER TABLE statement. 3. Recheck the space being used by the CUSTOMER_PROFILES table. 4. Reclaim the space. Deduplication and Compression: Example To show you how efficient deduplication and compression are on SecureFiles, the example will follow the set of steps outlined in the slide. In the first step, you will see the space being used by the CUSTOMER_PROFILES table. In the second step, you will enable deduplication and compression for the PROFILE_INFO LOB column in the CUSTOMER_PROFILES table. In the third step, you will examine the space being used after deduplication and compression are enabled. Finally, in the fourth step, you reclaim the space and examine the results. Oracle Database 11g: SQL and PL/SQL New Features

242 Important Instructions
Deduplication and Compression: Example (continued) Checking LOB Space Usage This procedure checks for LOB space usage. CREATE OR REPLACE PROCEDURE check_space IS l_fs1_bytes NUMBER; l_fs2_bytes NUMBER; l_fs3_bytes NUMBER; l_fs4_bytes NUMBER; l_fs1_blocks NUMBER; l_fs2_blocks NUMBER; l_fs3_blocks NUMBER; l_fs4_blocks NUMBER; l_full_bytes NUMBER; l_full_blocks NUMBER; l_unformatted_bytes NUMBER; l_unformatted_blocks NUMBER; BEGIN DBMS_SPACE.SPACE_USAGE( segment_owner => 'OE1', segment_name => 'CUSTOMER_PROFILES', segment_type => 'TABLE', fs1_bytes => l_fs1_bytes, fs1_blocks => l_fs1_blocks, fs2_bytes => l_fs2_bytes, fs2_blocks => l_fs2_blocks, fs3_bytes => l_fs3_bytes, fs3_blocks => l_fs3_blocks, fs4_bytes => l_fs4_bytes, fs4_blocks => l_fs4_blocks, full_bytes => l_full_bytes, full_blocks => l_full_blocks, unformatted_blocks => l_unformatted_blocks, unformatted_bytes => l_unformatted_bytes ); -- continued on next page Important Instructions Oracle Database 11g: SQL and PL/SQL New Features

243 Important Instructions
Deduplication and Compression: Example (continued) Checking LOB Space Usage (continued) -- continued from previous page DBMS_OUTPUT.ENABLE; DBMS_OUTPUT.PUT_LINE(' FS1 Blocks = '||l_fs1_blocks||' Bytes = '||l_fs1_bytes); DBMS_OUTPUT.PUT_LINE(' FS2 Blocks = '||l_fs2_blocks||' Bytes = '||l_fs2_bytes); DBMS_OUTPUT.PUT_LINE(' FS3 Blocks = '||l_fs3_blocks||' Bytes = '||l_fs3_bytes); DBMS_OUTPUT.PUT_LINE(' FS4 Blocks = '||l_fs4_blocks||' Bytes = '||l_fs4_bytes); DBMS_OUTPUT.PUT_LINE('Full Blocks = '||l_full_blocks||' Bytes = '||l_full_bytes); DBMS_OUTPUT.PUT_LINE('==================================== ========='); DBMS_OUTPUT.PUT_LINE('Total Blocks = '||to_char(l_fs1_blocks + l_fs2_blocks + l_fs3_blocks + l_fs4_blocks + l_full_blocks)|| ' || Total Bytes = '|| to_char(l_fs1_bytes + l_fs2_bytes + l_fs3_bytes + l_fs4_bytes + l_full_bytes)); END; / Important Instructions Oracle Database 11g: SQL and PL/SQL New Features

244 Enabling Deduplication and Compression: Example
Checking LOB space usage: execute check_space anonymous block completed FS1 Blocks = 0 Bytes = 0 FS2 Blocks = 1 Bytes = 8192 FS3 Blocks = 0 Bytes = 0 FS4 Blocks = 4 Bytes = 32768 Full Blocks = 0 Bytes = 0 ============================================= Total Blocks = 5 || Total Bytes = 40960 Checking LOB Space Usage Before Deduplication and Compression Before enabling deduplication and compression, you are shown the space usage. The amount shown in the slide will be used as a baseline for comparison over the next few steps. Oracle Database 11g: SQL and PL/SQL New Features

245 Enabling Deduplication and Compression: Example
ALTER TABLE customer_profiles MODIFY LOB (profile_info) (DEDUPLICATE LOB COMPRESS HIGH); Table altered. Enabling Deduplication and Compression To enable deduplication and compression, run the ALTER TABLE statement with the appropriate options. In this example, deduplication is turned on and the compression rate is set to HIGH. Oracle Database 11g: SQL and PL/SQL New Features

246 Enabling Deduplication and Compression: Example
Rechecking LOB space usage: execute check_space anonymous block completed FS1 Blocks = 0 Bytes = 0 FS2 Blocks = 1 Bytes = 8192 FS3 Blocks = 0 Bytes = 0 FS4 Blocks = 4 Bytes = 32768 Full Blocks = 0 Bytes = 0 ============================================= Total Blocks = 5 || Total Bytes = 40960 Rechecking LOB Space Usage The total space used appears to be the same as before deduplication and compression were enabled. This is because the free space needs to be reclaimed before it is usable again. Oracle Database 11g: SQL and PL/SQL New Features

247 Enabling Deduplication and Compression: Example
Reclaiming the free space: ALTER TABLE customer_profiles ENABLE ROW MOVEMENT; Table altered. ALTER TABLE customer_profiles SHRINK SPACE COMPACT; ALTER TABLE customer_profiles SHRINK SPACE; 1 2 3 Reclaiming the Free Space 1. The first statement enables row movement so that the data can be shifted to save space. Compacting the segment requires row movement. 2. The second statement (ALTER TABLE resumes SHRINK SPACE COMPACT) redistributes the rows inside the blocks resulting in more free blocks under the High Water Mark (HWM)—but the HWM itself is not disturbed. 3. The third statement (ALTER TABLE resumes SHRINK SPACE) will return unused blocks to the database and reset the HWM, moving it to a lower position. Lowering the HWM should result in better full table scan reads. Oracle Database 11g: SQL and PL/SQL New Features

248 Enabling Deduplication and Compression: Example
Examining the space after reclaiming: execute check_space anonymous block completed FS1 Blocks = 0 Bytes = 0 FS2 Blocks = 1 Bytes = 8192 FS3 Blocks = 0 Bytes = 0 FS4 Blocks = 0 Bytes = 0 Full Blocks = 0 Bytes = 8192 ============================================= Total Blocks = 1 || Total Bytes = 16384 Examining the Space After Reclaiming Notice that the amount of space used is about 65% less than before deduplication and compression were enabled. Oracle Database 11g: SQL and PL/SQL New Features

249 Oracle Database 11g: SQL and PL/SQL New Features 1 - 249
Encryption The encryption option enables you to turn on or off the LOB encryption, and optionally select an encryption algorithm. Encryption is performed at the block level. You can specify the encryption algorithm: 3DES168 AES128 AES192 (default) AES256 The column encryption key is derived from PASSWORD. All LOBs in the LOB column will be encrypted. DECRYPT keeps the LOBs in cleartext. LOBs can be encrypted on a per-column or per-partition basis. Using Encryption You can create a table or alter a table with encryption enabled or disabled on an LOB column. The current Transparent Data Encryption (TDE) syntax is used for extending encryption to LOB data types. Oracle Database 11g: SQL and PL/SQL New Features

250 Oracle Database 11g: SQL and PL/SQL New Features 1 - 250
Using Encryption You need a directory to store the Transparent Data Encryption (TDE) wallet. This is required for the SecureFiles LOB encryption. Edit the $ORACLE_HOME/network/admin/sqlnet.ora file to indicate the location of the TDE wallet. mkdir $ORACLE_HOME/wallet ENCRYPTION_WALLET_LOCATION= (SOURCE=(METHOD=FILE)(METHOD_DATA= (DIRECTORY =/u01/app/oracle/product/11.1.0/db_1/wallet))) Using Encryption (continued) Transparent data encryption (TDE) enables you to encrypt sensitive data in database columns as it is stored in the operating system files. Transparent data encryption is a key-based access control system that enforces authorization by encrypting data with a key that is kept secret. There can be only one key for each database table that contains encrypted columns regardless of the number of encrypted columns in a given table. Each table’s column encryption key is, in turn, encrypted with the database server’s master key. No keys are stored in the database. Instead, they are stored in an Oracle wallet, which is part of the external security module. To enable TDE, you need to create a directory to store the TDE wallet. You also need to modify the sqlnet.ora file to identify the location of the TDE wallet. Oracle Database 11g: SQL and PL/SQL New Features

251 Using Encryption: Example
Enabling encryption: Verify that the LOB is encrypted: ALTER TABLE customer_profiles MODIFY (profile_info ENCRYPT USING 'AES192'); Table altered. SELECT * FROM user_encrypted_columns; TABLE_NAME COLUMN_NAME ENCRYPTION_ALG SAL CUSTOMER_PROFILES PROFILE_INFO AES 192 bits key YES Using Encryption: Example In the example shown in the slide, the CUSTOMER_PROFILES table is modified so that the PROFILE_INFO column uses encryption. You can query the USER_ENCRYPTED_COLUMNS dictionary view to see the status of encrypted columns. Oracle Database 11g: SQL and PL/SQL New Features

252 Oracle Database 11g: SQL and PL/SQL New Features 1 - 252
Lesson Agenda SecureFile LOB features Deduplication, compression, and encryption Migration of BasicFile LOBs to the SecureFile LOB format Performance of LOBs Oracle Database 11g: SQL and PL/SQL New Features

253 Migrating from BasicFile to SecureFile Format
Scenario: You have data that is stored in the BasicFile format. You want to migrate it to the SecureFile format. connect CREATE TABLESPACE bf_tbs1 DATAFILE 'bf_tbs1.dbf' SIZE 800M REUSE EXTENT MANAGEMENT LOCAL UNIFORM SIZE 64M SEGMENT SPACE MANAGEMENT AUTO; connect CREATE TABLE customer_profiles (id NUMBER, first_name VARCHAR2 (40), last_name VARCHAR2 (80), profile_info BLOB) LOB(profile_info) STORE AS BASICFILE (TABLESPACE bf_tbs1); Migrating from BasicFile to SecureFile Format You may have LOB data in tables that were created before Oracle Database 11g. You can migrate the LOB data from the BasicFile format to the SecureFile format. In the example in the slide, you may have previously created a table with an LOB column stored in the BasicFile format (the default and only choice before Oracle Database 11g). On the next few pages, you see the BasicFile format migrated to the SecureFile format through a series of steps. Note: For this example, you need to drop the CUSTOMER_PROFILES table created earlier in the lesson, and re-create it with the code shown in the slide. DROP TABLE customer_profiles; Oracle Database 11g: SQL and PL/SQL New Features

254 Migrating from BasicFile to SecureFile Format
Load the data to the BasicFile LOB: set serveroutput on set verify on set term on set linesize 200 timing start load_data execute write_lob('karl.brimmer.doc'); execute write_lob('monica.petera.doc'); execute write_lob('david.sloan.doc'); timing stop PL/SQL procedure successfully completed. timing for: load_data Elapsed: 00:00:01.68 Migrating from BasicFile to SecureFile Format (continued) Loading the Data to the BasicFile LOB Column In the example in the slide, data is loaded into the PROFILE_INFO BLOB column in the CUSTOMER_PROFILES table. The WRITE_LOB procedure is defined earlier in this lesson. The purpose of the example shown is to build and populate the BasicFile LOB format column so that it can be migrated to the SecureFile format. Note that the elapsed time is much longer than loading the data in the SecureFile format shown earlier. Oracle Database 11g: SQL and PL/SQL New Features

255 Migrating from BasicFile to SecureFile Format
Check the timing on reading data in BasicFile format: set serveroutput on set verify on set term on set lines 200 timing start read_data execute read_lob; timing stop PL/SQL procedure successfully completed. timing for: read_data Elapsed: 00:00:01.15 Migrating from BasicFile to SecureFile Format (continued) Reading the Data from the BasicFile LOB Column The commands shown in the slide read back the 90 records from the CUSTOMER_PROFILES table. For each record, the size of the LOB value plus the first 200 characters of the LOB are displayed on screen. A SQL*Plus timer is started to capture the total elapsed time for the retrieval. Later, you use this timing information to compare the performance between the BasicFile format and SecureFile format LOBs. Oracle Database 11g: SQL and PL/SQL New Features

256 Migrating from BasicFile to SecureFile Format
Check the LOB segment subtype name for BasicFile format: col segment_name format a30 col segment_type format 13 SELECT segment_name, segment_type, segment_subtype FROM dba_segments WHERE tablespace_name = 'BF_TBS1' AND segment_type = 'LOBSEGMENT'; SEGMENT_NAME SEGMENT_TYPE SEGME SYS_LOB C00004$$ LOBSEGMENT ASSM LOB Segment Type for BasicFile Format By querying the DBA_SEGMENTS view, you can see that the LOB segment subtype name for BasicFile LOB storage is ASSM. Oracle Database 11g: SQL and PL/SQL New Features

257 Migrating from BasicFile to SecureFile Format
The migration from BasicFile to SecureFiles LOB storage format is performed online. This means that the CUSTOMERS_PROFILES table will continue to be accessible during the migration. This type of operation is called online redefinition. CREATE TABLE customer_profiles_interim (id NUMBER, first_name VARCHAR2 (40), last_name VARCHAR2 (80), profile_info BLOB) LOB(profile_info) STORE AS SECUREFILE (TABLESPACE sf_tbs1); Create an Interim Table Online redefinition requires an interim table for data storage. In this step, the interim table is defined with the SecureFiles LOB storage format, and the LOB is stored in the sf_tbs1 tablespace. After the migration is completed, the PROFILE_INFO LOB is stored in the sf_tbs1 tablespace. Oracle Database 11g: SQL and PL/SQL New Features

258 Migrating from BasicFile to SecureFile Format
Call the DBMS_REDEFINITION package to perform the online redefinition operation: connect DECLARE error_count PLS_INTEGER := 0; BEGIN DBMS_REDEFINITION.START_REDEF_TABLE ('OE1', 'customer_profiles', 'customer_profiles_interim', 'id id, first_name first_name, last_name last_name, profile_info profile_info', OPTIONS_FLAG => DBMS_REDEFINITION.CONS_USE_ROWID); DBMS_REDEFINITION.COPY_TABLE_DEPENDENTS 1, true,true,true,false, error_count); DBMS_OUTPUT.PUT_LINE('Errors := ' || TO_CHAR(error_count)); DBMS_REDEFINITION.FINISH_REDEF_TABLE ('OE1', 'customer_profiles', 'customer_profiles_interim'); END; Use DBMS_REDEFINITION to Perform Redefinition After running the code shown in the slide and completing the redefinition operation, you can drop the interim table: connect DROP TABLE customer_profiles_interim; Now, check the segment type of the migrated LOB. Note that the segment subtype for SecureFile LOB storage is SECUREFILE; for BasicFile format, it is ASSM. SELECT segment_name, segment_type, segment_subtype FROM dba_segments WHERE tablespace_name = 'SF_TBS1' AND segment_type = 'LOBSEGMENT' / SEGMENT_NAME SEGMENT_TYPE SEGMENT_SU SYS_LOB C00004$$ LOBSEGMENT SECUREFILE Oracle Database 11g: SQL and PL/SQL New Features

259 Oracle Database 11g: SQL and PL/SQL New Features 1 - 259
Lesson Agenda SecureFile LOB features Deduplication, compression, and encryption Migration of BasicFile LOBs to the SecureFile LOB format Performance of LOBs Oracle Database 11g: SQL and PL/SQL New Features

260 Comparing Performance
Compare the performance on loading and reading LOB columns in the SecureFile and BasicFile formats: Performance Comparison Loading Data Reading Data SecureFile format 00:00:00.96 00:00:01.09 BasicFile format 00:00:01.68 00:00:01.15 Performance In the examples shown in this lesson, the performance on loading and reading data in the LOB column of the SecureFile format LOB is faster than that of the BasicFile format LOB. Oracle Database 11g: SQL and PL/SQL New Features

261 Oracle Database 11g: SQL and PL/SQL New Features 1 - 261
Summary In this lesson, you should have learned how to: Describe SecureFile LOB features Enable SecureFile LOB deduplication, compression, and encryption Migrate BasicFile LOBs to the SecureFile LOB format Analyze the performance of LOBs Summary In this lesson, you learned about the new SecureFile format for LOBs. You learned that the SecureFile format offers features such as deduplication, compression, and encryption. You learned how to migrate the older version BasicFile format to the SecureFile format and also learned that the performance of SecureFile format LOBs is faster than the BasicFile format LOBs. Oracle Database 11g: SQL and PL/SQL New Features

262 Practice 8 Overview: Using SecureFile Format LOBs
This practice covers the following topics: Setting up the environment for LOBs Migrating BasicFile LOBs to SecureFile LOBs Enabling deduplication and compression Practice 8 Overview: Using SecureFile Format LOBs In this lesson, you practice using the features of SecureFile format LOBs. Oracle Database 11g: SQL and PL/SQL New Features

263 Oracle Database 11g: SQL and PL/SQL New Features 1 - 263
Practice 8 1. Start SQL Developer and connect to the database using your mydbconnection created earlier. 2. In this practice, you migrate a BasicFile format LOB to a SecureFile format LOB. You need to set up several supporting structures: a. Drop your existing PRODUCT_DESCRIPTIONS table and create a new one: DROP TABLE product_descriptions; CREATE TABLE product_descriptions (product_id NUMBER); b. Alter the table and add a BLOB column of BASICFILE storage type. ALTER TABLE product_descriptions ADD (detailed_product_info BLOB ) LOB (detailed_product_info) STORE AS BASICFILE (tablespace bf_tbs2); Create a directory object that identifies the location of your LOBs. In the Oracle classroom, the location is under the Oracle home/SECUREFILES folder. You can run the lab_08_02_c.sql script. --you need to replace the vx0115 with your server name. CREATE OR REPLACE DIRECTORY cwd AS '/vx0115/oracle/SECUREFILES'; d. Create the procedure to load the LOB data into the column (You can run the lab_08_02_d.sql script): CREATE OR REPLACE PROCEDURE loadLOBFromBFILE_proc (dest_loc IN OUT BLOB, file_name IN VARCHAR2) IS src_loc BFILE := BFILENAME('CWD', file_name); amount INTEGER := 4000; BEGIN DBMS_LOB.OPEN(src_loc, DBMS_LOB.LOB_READONLY); amount := DBMS_LOB.GETLENGTH(src_loc); DBMS_LOB.LOADFROMFILE(dest_loc, src_loc, amount); DBMS_LOB.CLOSE(src_loc); END loadLOBFromBFILE_proc; / Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

264 Oracle Database 11g: SQL and PL/SQL New Features 1 - 264
Practice 8 (continued) e. Create the procedure to write the LOB data (You can run the lab_08_02_e.sql script): CREATE OR REPLACE PROCEDURE write_lob (p_file IN VARCHAR2) IS i NUMBER; v_id NUMBER; v_b BLOB; BEGIN DBMS_OUTPUT.ENABLE; DBMS_OUTPUT.PUT_LINE('Begin inserting rows...'); FOR i IN LOOP v_id:=SUBSTR(p_file, 1, 4); INSERT INTO product_descriptions VALUES (v_id, EMPTY_BLOB()) RETURNING detailed_product_info INTO v_b; loadLOBFromBFILE_proc(v_b,p_file); DBMS_OUTPUT.PUT_LINE('Row '|| i ||' inserted.'); END LOOP; COMMIT; END write_lob; / f. Execute the procedures to load the data. If you are using SQL*Plus, you can set the timing on to observe the time. (You can run the lab_08_02_f.sql script). Note: If you are using SQL Developer, issue only the EXECUTE statements listed as follows. In SQL Developer, some of the SQL*Plus commands are ignored. It is recommended that you use SQL*Plus for this question. set serveroutput on set verify on set term on set lines 200 timing start load_data execute write_lob('1726_LCD.doc'); execute write_lob('1734_RS232.doc'); execute write_lob('1739_SDRAM.doc'); timing stop g. Check the segment type in the data dictionary. (You can run the lab_08_02_g.sql script). SELECT segment_name, segment_type, segment_subtype FROM dba_segments WHERE tablespace_name = 'BF_TBS1' AND segment_type = 'LOBSEGMENT'; Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

265 Oracle Database 11g: SQL and PL/SQL New Features 1 - 265
Practice 8 (continued) h. Create an interim table. (You can run the lab_08_02_h.sql script). CREATE TABLE product_descriptions_interim (product_id NUMBER, detailed_product_info BLOB) LOB(detailed_product_info) STORE AS SECUREFILE (TABLESPACE sf_tbs1); i. Start a SQL*Plus session. Connect as system and run the redefinition script. (You can run the lab_08_02_i.sql script). -- replace orcl with your sid connect -- replace OE1 with your ID in 3 places below: DECLARE error_count PLS_INTEGER := 0; BEGIN DBMS_REDEFINITION.START_REDEF_TABLE ('OE1', 'product_descriptions', 'product_descriptions_interim', 'product_id product_id, detailed_product_info detailed_product_info', OPTIONS_FLAG => DBMS_REDEFINITION.CONS_USE_ROWID); DBMS_REDEFINITION.COPY_TABLE_DEPENDENTS 1, true,true,true,false, error_count); DBMS_OUTPUT.PUT_LINE('Errors := ' || TO_CHAR(error_count)); DBMS_REDEFINITION.FINISH_REDEF_TABLE ('OE1', 'product_descriptions', 'product_descriptions_interim'); END; / j. In SQL Developer, using your OEn User ID, remove the interim table. DROP TABLE product_descriptions_interim; k. Check the segment type in the data dictionary. SELECT segment_name, segment_type, segment_subtype FROM dba_segments WHERE tablespace_name = 'SF_TBS2' AND segment_type = 'LOBSEGMENT'; Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

266 Oracle Database 11g: SQL and PL/SQL New Features 1 - 266
Practice 8 (continued) 3. In this practice, you turn on compression and deduplication for your PRODUCT_DESCRIPTIONS table. a. Modify the table to enable deduplication and compression. ALTER TABLE product_descriptions MODIFY LOB (detailed_product_info) (DEDUPLICATE LOB COMPRESS HIGH); Alter the table to reclaim the free space. ALTER TABLE product_descriptions ENABLE ROW MOVEMENT; ALTER TABLE product_descriptions SHRINK SPACE COMPACT; ALTER TABLE product_descriptions SHRINK SPACE; Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

267 Using the Data Warehousing Usability Enhancements

268 Oracle Database 11g: SQL and PL/SQL New Features 1 - 268
Objectives After completing this lesson, you should be able to: Identify the benefits of pivoting and unpivoting operations Write cross-tab queries to pivot (rotate) column values into new columns and to unpivot (rotate) columns into column values Pivot and unpivot with multiple columns and multiple aggregates Use wildcards and aliases with pivoting operations Objectives Pivoting is a key technique in business intelligence (BI) queries where you transform multiple rows of input into fewer rows, generally with more columns. With Oracle Database 11g, you can perform pivots on the server side. In this lesson, you learn how to use the PIVOT and UNPIVOT operations to improve performance, processing speed, and reducing the network load. Oracle Database 11g: SQL and PL/SQL New Features

269 Benefits of Using Pivoting Operations
Data returned by business intelligence (BI) queries is more useful if presented in a cross-tabular format. Pivoting enables you to transform multiple rows of input into fewer rows, generally with more columns. When pivoting, an aggregation operator is applied, enabling the query to condense large data sets into smaller, more readable results. Performing pivots on the server side can: Enhance processing speed Reduce network load Benefits By performing pivots on the server side: Processing burden is removed from client applications, simplifying client-side development and potentially enhancing processing speed Network load is reduced because only aggregated pivot results need to traverse the network and not the detail data Data that was originally on multiple rows can be transformed into a single row of output, enabling intra-row calculations without a SQL Join operation. Oracle Database 11g: SQL and PL/SQL New Features

270 PIVOT and UNPIVOT Clauses of the SELECT Statement
You can use the PIVOT operator of the SELECT statement to write cross-tabulation queries that rotate the column values into new columns, aggregating data in the process. You can use the UNPIVOT operator of the SELECT statement to rotate columns into values of a column. PIVOT UNPIVOT PIVOT and UNPIVOT Clauses Note: An UNPIVOT operation does not reverse a PIVOT operation; instead, it rotates data from columns into rows. Oracle Database 11g: SQL and PL/SQL New Features

271 Pivoting on the QUARTER Column: Conceptual Example
30,000 40,000 60,000 20,000 AMOUNT_ SOLD 2,500 Q1 I USA Kids Jeans 2,000 Q2 C Japan Q3 S Shorts P CHANNEL PRODUCT 1,000 Germany 1,500 Q4 QUARTER Poland QUANTITY_ SOLD COUNTRY 2,000 Q3 Kids Jeans Shorts PRODUCT 3,500 Q2 1,500 2,500 Q4 Q1 Pivoting: Conceptual Example The first example in the slide displays the columns of a table. The table in the second example displays the results of pivoting on the QUARTER column. The values of the QUARTER column, Q1, Q2, Q3, and Q4 are rotated into new columns. The quantity sold is grouped by products and quarters. To accomplish the desired result, you can use the PIVOT clause to PIVOT the QUARTER column; that is, turn the values of this column into separate columns and aggregate data using a group function such as SUM on the QUANTITY_SOLD along the way for each PRODUCT. In the second example, the QUARTER column is pivoted. The values of the QUARTER column namely Q1, Q2, Q3, and Q4 are rotated into columns. In addition, the quantity sold is calculated for each quarter for ALL products, ALL channels, and ALL countries. For example, the total number of shorts sold for all channels, all countries, for quarter 2 is The total number of Kids Jeans sold for all channels, all countries, for quarter 2 is 2000. Note that the first example contains six rows before the pivoting operation. In the second example and after pivoting the QUARTER column, only two rows are displayed. Pivoting transforms multiple rows of input into fewer and generally wider rows. Oracle Database 11g: SQL and PL/SQL New Features

272 Oracle Database 11g: SQL and PL/SQL New Features 1 - 272
PIVOT Clause Syntax table_reference PIVOT [ XML ] ( aggregate_function ( expr ) [[AS] alias ] [, aggregate_function ( expr ) [[AS] alias ] ]... pivot_for_clause pivot_in_clause ) -- Specify the column(s) to pivot whose values are to -- be pivoted into columns. pivot_for_clause = FOR { column |( column [, column]... ) } -- Specify the pivot column values from the columns you -- specified in the pivot_for_clause. pivot_in_clause = IN ( { { { expr | ( expr [, expr]... ) } [ [ AS] alias] }... | subquery | { ANY | ANY [, ANY]...} } ) PIVOT Clause Syntax You can use the PIVOT clause to write cross-tabulation queries that rotate rows into columns, aggregating the data in the process of rotation. The XML keyword is required when you use either a subquery or the wildcard ANY in the pivot_in_clause to specify pivot values. You cannot specify XML when you specify explicit pivot values using expressions in the pivot_in_clause. If the XML keyword is used, the output will include grouping columns and one column of XMLType rather than a series of pivoted columns. The optional AS alias allows you to specify an alias for each measure. Note that the aggregate function has an implicit GROUP BY based on the columns in the source data. The aggregate_function operates on the table’s data, and the result of the computation appears in the cross-tab report. The expr argument for the aggregate function is the measure to be pivoted. It must be a column or expression of the query_table_expression on which the PIVOT clause is operating. The optional AS alias allows you to specify an alias for each measure. In the pivot_for_clause, specify one or more columns whose values are to be pivoted into columns. In the pivot_in_clause, specify the pivot column values from the columns you specified in the pivot_for_clause. Oracle Database 11g: SQL and PL/SQL New Features

273 Oracle Database 11g: SQL and PL/SQL New Features 1 - 273
PIVOT Clause Syntax (continued) For expr, specify a constant value of a pivot column. You can optionally provide an alias for each pivot column value. Use a subquery to extract the pivot column values by way of a nested subquery. If you specify ANY, all values of the pivot columns are pivoted into columns. Subqueries and wildcards are useful if you do not know the specific values in the pivot columns. However, you will need to do further processing to convert the XML output into a tabular format. The values evaluated by the pivot_in_clause become the columns in the pivoted data. Oracle Database 11g: SQL and PL/SQL New Features

274 Creating a New View: Example
CREATE OR REPLACE VIEW sales_view AS SELECT prod_name AS product, country_name AS country, channel_id AS channel, SUBSTR(calendar_quarter_desc, 6,2) AS quarter, SUM(amount_sold) AS amount_sold, SUM(quantity_sold) AS quantity_sold FROM sh.sales, sh.times, sh.customers, sh.countries, sh.products WHERE sales.time_id = times.time_id AND sales.prod_id = products.prod_id AND sales.cust_id = customers.cust_id AND customers.country_id = countries.country_id GROUP BY prod_name, country_name, channel_id, SUBSTR(calendar_quarter_desc, 6, 2); Pivot: Example Supporting Structure In this lesson, you use the newly created view, sales_view. The definition of sales_view is displayed as follows: DESCRIBE sales_view Name Null? Type PRODUCT NOT NULL VARCHAR2(50) COUNTRY NOT NULL VARCHAR2(40) CHANNEL NOT NULL NUMBER QUARTER VARCHAR2(2) AMOUNT_SOLD NUMBER QUANTITY_SOLD NUMBER Oracle Database 11g: SQL and PL/SQL New Features

275 Oracle Database 11g: SQL and PL/SQL New Features 1 - 275
Pivot: Example (continued) The calendar_quarter_desc column values in the SH schema are as follows: SELECT DISTINCT calendar_quarter_desc FROM sh.times; CALENDAR_QUARTER_DESC 20 rows selected. Note: The two-character quarter value is extracted from the calendar_quarter_desc column starting at position 6 as shown in the syntax in the example above. Oracle Database 11g: SQL and PL/SQL New Features

276 Selecting the SALES_VIEW Data
SELECT product, country, channel, quarter, quantity_sold FROM sales_view; PRODUCT COUNTRY CHANNEL QUARTER QUANTITY_SOLD Y Box Italy Y Box Italy Y Box Italy . . . Y Box Japan Y Box Japan Y Box Japan Y Box Japan Y Box Japan Bounce Italy Bounce Italy 9502 rows selected. Viewing the Sample Data The two boxes around QUARTER and “9502 rows selected” displayed in the second example in the slide, highlight the two key changes that are performed by the PIVOT operator: The QUARTER column becomes multiple columns, each holding one quarter. The row count will drop to just 71 from 9502, representing the distinct products in the schema. The following statement displays the distinct channel_id and channel_desc column values from the CHANNEL table: SELECT DISTINCT channel_id, channel_desc FROM sh.channels ORDER BY channel_id; CHANNEL_ID CHANNEL_DESC 2 Partners 3 Direct Sales 4 Internet 5 Catalog 9 Tele Sales The valid quarter values are 01, 02, 03, and 04. Oracle Database 11g: SQL and PL/SQL New Features

277 Pivoting on the QUARTER Column in the SALES_VIEW Data: Example
SELECT * FROM (SELECT product, quarter, quantity_sold FROM sales_view) PIVOT (sum(quantity_sold) FOR quarter IN ('01', '02', '03', '04')) ORDER BY product DESC; PRODUCT '01' '02' '03' '04' Y Box Xtend Memory Unix/Windows 1-user Standard Mouse Smash up Boxing . . . 71 rows selected. Pivoting on the QUARTER Column: Example The example in the slide uses a subquery (inline view) in the FROM clause. This is required because when you issue a SELECT * directly from the sales view, the query outputs rows for each row of sales_view. The result set shows the product column followed by a column for each value of the quarter specified in the IN clause. The numbers shown in the pivoted output are the sum of quantity_sold for each product at each quarter. If you also specify an alias for each measure, the column name is a concatenation of the pivot column value or alias, an underscore (_), and the measure alias. You can use aliases in the example in the slide as shown below: SELECT * FROM (SELECT product, quarter, quantity_sold FROM sales_view) PIVOT (sum(quantity_sold) FOR quarter in ('01' AS Q1, '02' AS Q2, '03' AS Q3, '04' AS Q4)) ORDER by PRODUCT DESC; Oracle Database 11g: SQL and PL/SQL New Features

278 Oracle Database 11g: SQL and PL/SQL New Features 1 - 278
Pivoting on the QUARTER Column: Example (continued) PRODUCT Q Q Q Q4 Y Box Xtend Memory Unix/Windows Standard Mouse . . . 71 rows selected. Prior to Oracle Database 11g, the above example could be accomplished using the CASE expression syntax: SELECT product, SUM(CASE when quarter = '01' THEN quantity_sold ELSE NULL END) Q1, SUM(CASE when quarter = '02' THEN quantity_sold ELSE NULL END) Q2, SUM(CASE when quarter = '03' THEN quantity_sold ELSE NULL END) Q3, SUM(CASE when quarter = '04' THEN quantity_sold ELSE NULL END) Q4 FROM ( SELECT product, quarter, quantity_sold FROM SALES_VIEW ) GROUP BY product; The advantage of the new syntax over the syntax used prior to Oracle Database 11g is that it enables greater query optimization by the Oracle database. The query optimizer recognizes the PIVOT keyword and as a result, uses algorithms optimized to process it efficiently. Oracle Database 11g: SQL and PL/SQL New Features

279 Pivoting on the ORDER_MODE Column in the OE Schema: Example
CREATE TABLE pivot_table AS SELECT * FROM (SELECT EXTRACT(YEAR FROM order_date) year, order_mode, order_total FROM orders) PIVOT (SUM(order_total) FOR order_mode IN ('direct' AS Store, 'online' AS Internet)) ORDER BY year; SELECT * FROM pivot_table ORDER BY year; YEAR STORE INTERNET 6 rows selected. Pivoting on the ORDER_MODE Column: Example The example in the slide uses the Order Entry (OE) schema. The oe.orders table contains information about when an order was placed (order_date), how it was placed (order_mode), and the total amount of the order (order_total), as well as other information. The example shows how to use the PIVOT clause to pivot order_mode values into columns, aggregating order_total data in the process, to get yearly totals by order mode. This example creates a new table called pivot_table by using a subquery. The rows inserted into the table are generated by the subquery. Note the use of the aliases for the direct and online pivot column values. Oracle Database 11g: SQL and PL/SQL New Features

280 Pivoting on Multiple Columns
To pivot on more than one column: A pivoting column is required to be a column of the table reference on which the pivot is operating The pivoting column cannot be an arbitrary expression If you need to pivot on an expression, you should alias the expression in a view before the pivot operation. Pivoting on Multiple Columns You can pivot on multiple columns. Oracle Database 11g: SQL and PL/SQL New Features

281 Pivoting on Multiple Columns
SELECT * FROM (SELECT product, channel, quarter, quantity_sold FROM sales_view) PIVOT (sum(quantity_sold) FOR (channel, quarter) IN ((3, '01') AS Direct_Sales_Q1, (4, '01') AS Internet_Sales_Q1)) ORDER BY product DESC; PRODUCT DIRECT_SALES_Q1 INTERNET_SALES_Q1 Y Box Xtend Memory Unix/Windows 1-user pack Standard Mouse Smash up Boxing . . . 71 rows selected. Pivoting on Multiple Columns (continued) The example in the slide pivots on both the CHANNEL and QUARTER columns. The example uses only CHANNEL values 3 (Direct Sales) and 4 (Internet), and only the Q1 value for the QUARTER column. The example below specifies more values for the QUARTER column: SELECT * FROM (SELECT product, channel, quarter, quantity_sold FROM sales_view ) PIVOT (sum(quantity_sold) FOR (channel, quarter) IN ((3, '01') AS Direct_Sales_Q1, (3, '02') AS Direct_Sales_Q2, (3, '03') AS Direct_Sales_Q3, (3, '04') AS Direct_Sales_Q4, (4, '01') AS Internet_Sales_Q1, (4, '02') AS Internet_Sales_Q2, (4, '03') AS Internet_Sales_Q3, (4, '04') AS Internet_Sales_Q4)) ORDER BY product DESC; Oracle Database 11g: SQL and PL/SQL New Features

282 Pivoting Using Multiple Aggregations
SELECT * FROM (SELECT product, channel, amount_sold, quantity_sold FROM sales_view) PIVOT (SUM(amount_sold) AS sums, SUM(quantity_sold) as sumq FOR channel IN (3 AS Dir_Sales, 4 AS Int_Sales)) ORDER BY product DESC; PRODUCT DIR_SALES_SUMS DIR_SALES_SUMQ INT_SALES_SUMS INT_SALES_SUMQ Y Box Xtend Memory Unix/Windows Standard Mouse Smash up Boxing ... 71 rows selected. Pivoting Using Multiple Aggregations The query in the example in the slide pivots SALES_VIEW on the CHANNEL column. The amount_sold and quantity_sold measures are pivoted. Note that the query creates column headings by concatenating the pivot columns with the aliases of the aggregate functions, plus an underscore. When you use multiple aggregation, you can omit the alias for only one aggregation. If you omit an alias, the corresponding result column name is the pivot value (or the alias for the pivot value). Oracle Database 11g: SQL and PL/SQL New Features

283 Distinguishing PIVOT-Generated Nulls from Nulls in the Source Data
SELECT * FROM sales2; PROD_ID QTR AMOUNT_SOLD Q1 10 Q1 20 Q2 Q1 50 SELECT * FROM ( SELECT prod_id, qtr, amount_sold FROM sales2) PIVOT (SUM(amount_sold), COUNT(*) AS count_total FOR qtr IN ('Q1', 'Q2') ) ORDER BY prod_id DESC; PROD_ID Q1 Q1_COUNT_TOTAL Q Q2_COUNT_TOTAL Pivoting and Nulls You can distinguish between NULL values that are generated from the use of PIVOT and those that exist in the source data. The following example illustrates NULL that PIVOT generates. The first code example in the slide assumes an existing table named sales2. The query in the second code example returns prod_id rows and the resulting pivot columns: Q1, Q1_COUNT_TOTAL, Q2, and Q2_COUNT_TOTAL. For each unique value of prod_id, Q1_COUNT_TOTAL, the query returns the total number of rows whose QTR value is Q1. The unique value of Q2_COUNT_TOTAL returns the total number of rows whose QTR value is Q2. From the result set shown in the second example in the slide, there are two sales rows for prod_id 100 for quarter Q1, and one sales row for prod_id 100 and quarter Q2. For prod_id 200, there is one sales row for quarter Q1 and no sales row for quarter Q2. Using Q2_COUNT_TOTAL, you can identify that the NULL for PROD_ID 100 in Q2 is the result of a row in the original table whose measure is of NULL value. The NULL for PROD_ID 200 in Q2 is due to no row being present in the original table for prod_id 200 in quarter Q2. Oracle Database 11g: SQL and PL/SQL New Features

284 Using the XML Keyword to Specify Pivot Values: Two Methods
ANY The XML string for each row includes only the pivot values found in the input data for that row. Use the ANY keyword, or XML The XML string includes all pivot values found by the subquery even if there are no aggregate values. If you use the XML keyword with the PIVOT syntax Using XML Keyword with Pivoting Using the XML keyword in the PIVOT syntax requires including either the ANY keyword or a subquery. Each output row will include following: The implicit Group By columns A single column of XMLType containing an XML string for all value and measure pairs The XML string for each row will hold aggregated data corresponding to the row’s implicit GROUP BY value. The values of the pivot column are evaluated at execution time. Use a subquery Oracle Database 11g: SQL and PL/SQL New Features

285 Specifying PIVOT Values: Using the ANY Keyword
SET LONG 1024; SELECT * FROM (SELECT product, channel, quantity_sold FROM sales_view ) PIVOT XML (SUM(quantity_sold) FOR channel IN (ANY) ) ORDER BY product DESC; PRODUCT CHANNEL_XML . . . 1.44MB External 3.5" Diskette <PivotSet> <item><column name = "CHANNEL">3</column><column name = "SUM(QUANTITY_SOLD)">14189</column></item> <item><column name = "CHANNEL">2</column><column name = "SUM(QUANTITY_SOLD)">6455</column></item> <item><column name = "CHANNEL">4</column><column name = "SUM(QUANTITY_SOLD)">2464</column></item></PivotSet> 71 rows selected. Using the ANY Keyword with Pivoting The ANY keyword acts as a wildcard. If you specify ANY, all values found in the pivot column will be used for pivoting. Note that when using the ANY keyword, the ANY string for each output row will include only the pivot values found in the input data corresponding to that row. The example in the slide uses the ANY wildcard keyword. The XML output includes all channel values in the sales_view view created earlier in this lesson. The ANY keyword is available only in PIVOT operations as part of an XML operation. This output includes data for cases where the channel exists in the data set. You can use wildcards or subqueries to specify the pivot IN list members when the values of the pivot column are not known. Oracle Database 11g: SQL and PL/SQL New Features

286 Specifying PIVOT Values: Using Subqueries
SELECT * FROM (SELECT product, channel, quantity_sold FROM sales_view ) PIVOT XML(SUM(quantity_sold) FOR channel IN (SELECT distinct channel_id FROM sh.channels)); PRODUCT CHANNEL_XML . . . Y Box <PivotSet> <item><column name = "CHANNEL">9</column><column name = "SUM(QUANTITY_SOLD)">1</column></item> <item><column name = "CHANNEL">2</column><column name = "SUM(QUANTITY_SOLD)">2037</column></item> <item><column name = "CHANNEL">5</column><column name = "SUM(QUANTITY_SOLD)"></column></item> <item><column name = "CHANNEL">3</column><column name = "SUM(QUANTITY_SOLD)">3552</column></item> Using Subqueries with Pivoting The example in the slide illustrates using a subquery. The XML output includes all channel values and the sales data corresponding to each channel and for each product. Subquery-based pivots give results different from those of the ANY wildcard. In the example, when you use a subquery, the XMLType column will show value and measure pairs for all channels for each product even if the input data has no such product/channel combination. For example, the XML string in the slide shows the highlighted Channel 5, although it has no value for the SUM(QUANTITY_SOLD)column. Pivots that use a subquery will, therefore, often have longer output than queries based on the ANY keyword. Depending on how you process the query results, subquery-style output may be more convenient to work with than the results derived from ANY. Note: The results displayed in the slide are not complete due to space limitations. Oracle Database 11g: SQL and PL/SQL New Features

287 Unpivoting on the QUARTER Column: Conceptual Example
PRODUCT Q1 Q2 Q3 Q4 Shorts 3,500 2,000 Kids Jeans 2,500 2,000 1,500 2,500 Q1 Kids Jeans 2,000 Q2 3,500 Shorts 1,500 Q4 Q3 QUARTER SUM_OF_QUANTITY PRODUCT Unpivoting: Conceptual Example The example in the slide unpivots the QUARTER column, which turns the quarters columns into the values of the QUARTER column. An UNPIVOT operation does not reverse a PIVOT operation; instead, it rotates data found in multiple columns of a single row into multiple rows of a single column. If you are working with pivoted data, an UNPIVOT operation cannot reverse any aggregations that have been made by PIVOT or any other means. Note that the first example contains two rows before the unpivoting operation. In the second example, the unpivoting operation on the QUARTER column displays five rows. Unpivoting transforms fewer rows of input into generally more rows. Oracle Database 11g: SQL and PL/SQL New Features

288 Using the UNPIVOT Operator
An UNPIVOT does not reverse a PIVOT operation; instead, it rotates data from columns into rows. If you are working with pivoted data, an UNPIVOT operation cannot reverse any aggregations that have been made by PIVOT or any other means. UNPIVOT Using UNPIVOT Data from sources such as spreadsheets and flat files is often in pivoted form. For instance, sales data will often be stored in a separate column for each time period. UNPIVOT can normalize such data, transforming multiple columns into a single column. When the data is normalized with UNPIVOT, it is much more accessible to relational database processing with SQL. By placing data in a normalized layout, queries can readily apply SQL aggregate and analytic functions, enabling powerful analysis. Similarly, it is more efficient to specify the WHERE clause predicates on normalized data. Oracle Database 11g: SQL and PL/SQL New Features

289 Using the UNPIVOT Clause
The UNPIVOT clause rotates columns from a previously pivoted table or a regular table into rows. You specify: The measure columns to be unpivoted The names for the columns that will result from the unpivot operation The columns that will be unpivoted back into values of the column specified in the unpivot_for_clause You can use an alias to map the column name to another value. Oracle Database 11g: SQL and PL/SQL New Features

290 Data Types of the Value Columns in an UNPIVOT Operation
Data Type for the Value Columns Resulting Unpivoted Column Data Type If ALL the value columns are CHAR CHAR If ANY value column is VARCHAR2 VARCHAR2 If ALL the value columns are NUMBER NUMBER If ANY value column is BINARY_DOUBLE BINARY_DOUBLE If NO value column is BINARY_DOUBLE but ANY value column is BINARY_FLOAT BINARY_FLOAT Data Types for Unpivoting The UNPIVOT operation turns a set of value columns into one column. The data types of all the value columns must be of the same data type, such as numeric or character. If all the value columns are CHAR, the unpivoted column is CHAR. If any value column is VARCHAR2, the unpivoted column is VARCHAR2. If all the value columns are NUMBER, the unpivoted column is NUMBER. If any value column is BINARY_DOUBLE, the unpivoted column is BINARY_DOUBLE. If no value column is BINARY_DOUBLE, but any value column is BINARY_FLOAT, the unpivoted column is BINARY_FLOAT. Oracle Database 11g: SQL and PL/SQL New Features

291 Oracle Database 11g: SQL and PL/SQL New Features 1 - 291
Unpivot Clause Syntax table_reference UNPIVOT [{INCLUDE|EXCLUDE} NULLS] -- specify the measure column(s) to be unpivoted. ( { column | ( column [, column]... ) } unpivot_for_clause unpivot_in_clause ) -- Specify one or more names for the columns that will -- result from the unpivot operation. unpivot_for_clause = FOR { column | ( column [, column]... ) } -- Specify the columns that will be unpivoted into values of -- the column specified in the pivot_for_clause. unpivot_in_clause = ( { column | ( column [, column]... ) } [ AS { constant | ( constant [, constant]... ) } ] [, { column | ( column [, column]... ) } [ AS { constant | ( constant [, constant]...) } ] ]...) UNPIVOT Clause Syntax The UNPIVOT clause rotates columns into rows. The [INCLUDE] | [EXCLUDE] [NULLS] clause gives you the option of including or excluding null-valued rows. [INCLUDE] [NULLS] causes the unpivot operation to include null-valued rows; [EXCLUDE] [NULLS] eliminates null-valued rows from the return set. If you omit this clause, the unpivot operation excludes nulls. For column, specify a name for each output column that will hold measure values, such as sales_quantity. In pivot_for_clause, specify a name for each output column that will hold descriptor values, such as quarter or product. In the unpivot_in_clause, specify the input data columns whose names will become values in the output columns of the unpivot_for_clause. These input data columns have names specifying a category value, such as Q1, Q2, Q3, and Q4. The optional alias enables you to map the column name to any desired value. Oracle Database 11g: SQL and PL/SQL New Features

292 Creating a New Pivot Table: Example
CREATE TABLE pivotedtable AS SELECT * FROM (SELECT product, quarter, quantity_sold FROM sales_view) PIVOT (sum(quantity_sold) FOR quarter IN ('01' AS Q1, '02' AS Q2, '03' AS Q3, '04' AS Q4)); Table created. SELECT * FROM pivotedtable ORDER BY product DESC; PRODUCT Q Q Q Q4 Y Box Xtend Memory . . . 71 rows selected. Pivot: Example Supporting Structure The example in the slide creates a new table named pivotedtable. Oracle Database 11g: SQL and PL/SQL New Features

293 Unpivoting on the QUARTER Column in the SH Schema: Example
SELECT * FROM pivotedtable UNPIVOT (quantity_sold For Quarter IN (Q1, Q2, Q3, Q4)) ORDER BY product DESC, quarter; PRODUCT QUARTER QUANTITY_SOLD Y Box Q Y Box Q Y Box Q Y Box Q Xtend Memory Q Xtend Memory Q Xtend Memory Q Xtend Memory Q Unix/Windows 1-user pack Q . . . 284 rows selected. Unpivoting on the QUARTER Column: Example The QUARTER column in the example in the slide was formatted using the following command: COLUMN quarter FORMAT A7 Prior to Oracle Database 11g, you could simulate the UNPIVOT syntax in the example in the slide by using existing SQL as shown below: SELECT product, 'Q1' as quarter, Q1 as quantity_sold FROM pivotedTable WHERE Q1 is not NULL union all SELECT product, 'Q2' as quarter, Q2 as quantity_sold FROM pivotedTable WHERE Q2 is not NULL SELECT product, 'Q3' as quarter, Q3 as quantity_sold FROM pivotedTable WHERE Q3 is not NULL SELECT product, 'Q4' as quarter, Q4 as quantity_sold FROM pivotedTable WHERE Q4 is not NULL; As noted for PIVOT earlier, the UNPIVOT syntax enables more efficient query processing. The UNPIVOT keyword alerts the query optimizer to the desired behavior. As a result, the optimizer calls highly efficient algorithms. Oracle Database 11g: SQL and PL/SQL New Features

294 Unpivoting the ORDER_MODE Column in the OE Schema: Example
SELECT * FROM pivot_table UNPIVOT (yearly_total FOR order_mode IN (store AS 'direct', internet AS 'online')) ORDER BY year, order_mode; YEAR ORDER_MODE YEARLY_TOTAL direct direct direct direct online direct online direct online 9 rows selected. Using the UNPIVOT Clause The UNPIVOT clause enables you to restore a pivoted table, or a table with similar structure, so that selected columns are pivoted into values in a single column. Note the use of column aliases in the example in the slide. Oracle Database 11g: SQL and PL/SQL New Features

295 Unpivoting on Multiple Columns in the SH Schema: Example
CREATE TABLE multi_col_pivot AS SELECT * FROM (SELECT product, channel, quarter, quantity_sold FROM sales_view) PIVOT (sum(quantity_sold) FOR (channel, quarter) IN ((3, '01') AS Direct_Sales_Q1, (4, '01') AS Internet_Sales_Q1)) ORDER BY product DESC; Table created. SELECT * FROM multi_col_pivot; PRODUCT DIRECT_SALES_Q1 INTERNET_SALES_Q1 Y Box Xtend Memory . . . 71 rows selected. Unpivoting on Multiple Columns The example in the slide creates a pivot table using the CHANNEL and QUARTER columns in the SH schema. This is similar to the example provided in the section on “Pivoting on Multiple Columns.” The example uses only the CHANNEL values 3 (Direct Sales) and 4 (Internet), and only the Q1 value for the QUARTER column. However, you can use other values for both columns. The structure of the newly created table is shown below: DESCRIBE multi_col_pivot Name Null? Type PRODUCT NOT NULL VARCHAR2(50) DIRECT_SALES_Q NUMBER INTERNET_SALES_Q NUMBER The query in the example in the slide returns 71 rows. Oracle Database 11g: SQL and PL/SQL New Features

296 Unpivoting on Multiple Columns in the SH Schema: Example
-- Provide explicit values for the unpivot columns SELECT * FROM multi_col_pivot UNPIVOT (quantity_sold FOR (channel, quarter) IN ( Direct_Sales_Q1 AS ('Direct', 'Q1'), Internet_Sales_Q1 AS ('Internet', 'Q1') ) ) ORDER BY product DESC, quarter; PRODUCT CHANNEL QUARTER QUANTITY_SOLD Y Box Internet Q Y Box Direct Q Xtend Memory Internet Q Xtend Memory Direct Q . . . 142 rows selected. Unpivoting on Multiple Columns (continued) The example in the slide unpivots the CHANNEL and QUARTER columns using the multi_col_pivot table created on the previous page. Note that the example uses explicit values for the unpivoted CHANNEL and QUARTER columns. The example below demonstrates unpivoting on the CHANNEL and QUARTER columns without using aliases as explicit values for the unpivoted columns. In this case, each unpivoted column uses the column name as its value. The query in the example in the slide returns 142 rows. SELECT * FROM multi_col_pivot UNPIVOT (quantity_sold FOR (channel, quarter) IN (Direct_Sales_Q1, Internet_Sales_Q1 ) ); PRODUCT CHANNEL QUARTER QUANTITY_SOLD Y Box DIRECT_SALES_Q1 DIRECT_SALES_Q Y Box INTERNET_SALES_Q1 INTERNET_SALES_Q Xtend Memory DIRECT_SALES_Q1 DIRECT_SALES_Q ... 142 rows selected. Oracle Database 11g: SQL and PL/SQL New Features

297 Unpivoting on Multiple Aggregations in the SH Schema: Example
CREATE TABLE multi_agg_pivot AS SELECT * FROM (SELECT product, channel, quarter, quantity_sold, amount_sold FROM sales_view) PIVOT (sum(quantity_sold) sumq, sum(amount_sold) suma FOR channel IN (3 AS Direct, 4 AS Internet) ) ORDER BY product DESC; Table created. SELECT * FROM multi_agg_pivot; PRODUCT QUARTER DIRECT_SUMQ DIRECT_SUMA INTERNET_SUMQ INTERNET_SUMA . . . Bounce Bounce Bounce Bounce 283 row selected. Unpivoting on Multiple Aggregations The example shown in the slide creates the multi_agg_pivot table using the CHANNEL column, and the amount_sold and quantity_sold measures in the SH schema. This is similar to the example provided in the section on “Pivoting Using Multiple Aggregations.” The example uses only the CHANNEL values 3 (Direct Sales) and 4 (Internet). You can use other values for the CHANNEL column. Note that the query creates column headings by concatenating the pivot columns with the aliases of the aggregate functions, plus an underscore. When you use multiple aggregation, you can omit the alias for only one aggregation. If you omit an alias, the corresponding result column name is the pivot value (or the alias for the pivot value). The structure of the newly created table is shown below: DESCIBE multi_agg_pivot Name Null? Type PRODUCT NOT NULL VARCHAR2(50) QUARTER VARCHAR2(8) DIRECT_SUMQ NUMBER DIRECT_SUMA NUMBER INTERNET_SUMQ NUMBER INTERNET_SUMA NUMBER Oracle Database 11g: SQL and PL/SQL New Features

298 Unpivoting on Multiple Aggregations in the SH Schema: Example
SELECT * FROM multi_agg_pivot UNPIVOT ((total_amount_sold, total_quantity_sold) FOR channel IN ((Direct_sumq, Direct_suma) AS 3, (Internet_sumq, Internet_suma) AS 4 )) ORDER BY product DESC, quarter, channel; PRODUCT QUARTER CHANNEL TOTAL_AMOUNT_SOLD TOTAL_QUANTITY_SOLD Bounce Bounce Bounce Bounce Bounce Bounce Bounce Bounce . . . 566 rows selected. Unpivoting on Multiple Aggregations (continued) The unpivot example shown in the slide uses the newly created multi_agg_pivot table. The measures amount_sold and quantity_sold are unpivoted. Channels are mapped to the value “3” for Direct_sumq and Direct_suma, and to the value “4” for Internet_sumq and Internet_suma. The channel mapping is consistent with the values used in the pivot operation that created the multi_agg_pivot table. However, any values could have been used for the channel mappings. Oracle Database 11g: SQL and PL/SQL New Features

299 Oracle Database 11g: SQL and PL/SQL New Features 1 - 299
Summary In this lesson, you should have learned how to: Identify the benefits of pivoting and unpivoting operations Write cross-tab queries to pivot (rotate) column values into new columns and to unpivot (rotate) columns into column values Pivot and unpivot with multiple columns and multiple aggregates Use wildcards and aliases with pivoting operations Oracle Database 11g: SQL and PL/SQL New Features

300 Practice 9 Overview: Pivoting
This practice covers the following topics: Using the PIVOT clause with the SELECT statement. Examining special pivoting cases Practice 9 Overview: Pivoting In this practice, you practice with the PIVOT operator. You work with data in the SH schema. Oracle Database 11g: SQL and PL/SQL New Features

301 Oracle Database 11g: SQL and PL/SQL New Features 1 - 301
Practice 9 1. Start SQL Developer and connect to the database using your mydbconnection created earlier. 2. In this practice, you practice creating reports with the PIVOT operator. This practice uses the SH schema. You need to set up a view based on the SH schema (You can run the lab_09_02_a.sql script): a. Create the MY_SALES_VIEW. CREATE OR REPLACE VIEW my_sales_view AS SELECT prod_name AS product, country_name AS country, channel_id AS channel, substr(calendar_month_desc, 1, 4) + 6 || substr(calendar_month_desc, 5, 3) AS MONTH_YEAR, SUM(amount_sold) AS amount_sold, SUM(quantity_sold) AS quantity_sold FROM sh.sales, sh.times, sh.customers, sh.countries, sh.products WHERE sales.time_id = times.time_id AND sales.prod_id = products.prod_id AND sales.cust_id = customers.cust_id AND customers.country_id = countries.country_id AND substr(calendar_month_desc, 1, 4) = '2001' GROUP BY prod_name, country_name, channel_id, substr(calendar_month_desc, 5, 3); b. Describe MY_SALES_VIEW. DESCRIBE my_sales_view c. View the distinct values in the MONTH_YEAR column. You will use this column to pivot. SELECT DISTINCT month_year FROM my_sales_view; Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

302 Oracle Database 11g: SQL and PL/SQL New Features 1 - 302
Practice 9 (continued) Create a report that pivots on the MONTH_YEAR column. The output is shown below. The numbers in the pivoted output are the sum of quantity_sold for each product for the first and fourth months in Note: 70 rows are returned. Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

303 Oracle Database 11g: SQL and PL/SQL New Features 1 - 303
Practice 9 (continued) 3. Create a report that pivots on both the CHANNEL and MONTH_YEAR columns for the quantity sold. a. In your report, use only the CHANNEL values 3 (Direct Sales) and 4 (Internet), and only the 2007-M1 value for the MONTH_YEAR column. b. Modify the query and display the amount sold too. Use only the CHANNEL values 3 (Direct Sales) and 4 (Internet). Notes Page Only Oracle Database 11g: SQL and PL/SQL New Features

304 Table Descriptions and Data

305 Oracle Database 11g: SQL and PL/SQL New Features 1 - 305
Schema Descriptions Overall Description The Oracle Database sample schemas portray a sample company that operates worldwide to fill orders for different products. The company has three divisions: Human Resources: Tracks information about the employees and facilities Order Entry: Tracks product inventories and sales through various channels Sales History: Tracks business statistics to facilitate business decisions Each of these divisions is represented by a schema. In this course, you have access to the objects in all the schemas. However, the emphasis of the examples, demonstrations, and practices is on the Sales History (SH) schema. All scripts necessary to create the sample schemas reside in the $ORACLE_HOME/demo/schema/ folder. Oracle Database 11g: SQL and PL/SQL New Features

306 Oracle Database 11g: SQL and PL/SQL New Features 1 - 306
Schema Descriptions (continued) Order Entry (OE) The company sells several categories of products, including computer hardware and software, music, clothing, and tools. The company maintains product information that includes product identification numbers, the category into which the product falls, the weight group (for shipping purposes), the warranty period if applicable, the supplier, the status of the product, a list price, a minimum price at which a product will be sold, and a URL for manufacturer information. Inventory information is also recorded for all products, including the warehouse where the product is available and the quantity on hand. Because products are sold worldwide, the company maintains the names of the products and their descriptions in different languages. The company maintains warehouses in several locations to facilitate filling customer orders. Each warehouse has a warehouse identification number, name, and location identification number. Customer information is tracked in some detail. Each customer is assigned an identification number. Customer records include name, street address, city or province, country, phone numbers (up to five phone numbers for each customer), and postal code. Some customers order through the Internet, so addresses are also recorded. Because of language differences among customers, the company records the NLS language and territory of each customer. The company places a credit limit on its customers to limit the amount for which they can purchase at one time. Some customers have account managers, whom the company monitors. The company keeps track of a customer’s phone number. These days, it is difficult to tell how many phone numbers a customer might have, but the company tries to keep track of all of them. Because of the language differences of the customers, the company identifies the language and territory of each customer. When a customer places an order, the company tracks the date of the order, the mode of the order, status, shipping mode, total amount of the order, and the sales representative who helped place the order. This may be the same individual as the account manager for a customer, it may be different, or, in the case of an order over the Internet, the sales representative is not recorded. In addition to the order information, it also tracks the number of items ordered, the unit price, and the products ordered. For each country in which it does business, the company records the country name, currency symbol, currency name, and the region where the country is located geographically. This data is useful to interact with customers living in different geographical regions around the world. Oracle Database 11g: SQL and PL/SQL New Features

307 OE Order Entry (OE) (continued)
Schema Descriptions (continued) OE Order Entry (OE) (continued) CUSTOMERS customer_id cust_first_name cust_ last_name cust_ address_typ phone_numbers nls_language nls_territory credit_limit cust_ account_mgr_id date_of_birth marital_status gender Income_level ORDER_ITEMS order_id line_item_id product_id unit_price quantity ORDERS order_id order_date order_mode customer_id order_status order_total sales_rep_id promotion_id street_address postal_code city state_province country_id PRODUCT_ INFORMATION product_id product_name product_description category_id weight_class warranty_period supplier_id product_status list_price min_price catalog_url PRODUCT_ DESCRIPTIONS product_id language_id product_name product_description INVENTORIES product_id warehouse_id quantity_on_hand WAREHOUSES warehouse_id warehouse_name location_id Oracle Database 11g: SQL and PL/SQL New Features

308 Oracle Database 11g: SQL and PL/SQL New Features 1 - 308
Schema Descriptions (continued) Order Entry (OE) Row Counts SELECT COUNT(*) FROM customers; COUNT(*) 319 SELECT COUNT(*) FROM inventories; 1112 SELECT COUNT(*) FROM orders; 105 SELECT COUNT(*) FROM order_items; 665 SELECT COUNT(*) FROM product_descriptions; 8640 SELECT COUNT(*) FROM product_information; 288 SELECT COUNT(*) FROM warehouses; 9 Oracle Database 11g: SQL and PL/SQL New Features

309 Oracle Database 11g: SQL and PL/SQL New Features 1 - 309
Schema Descriptions (continued) Print Media (PM) The company stores multimedia and print information about its products in the database. Print Media (PM) schema is used to store such data. Examples of such information are: Promotional audio and video clips Product images and thumbnails for Web publishing Press release texts Print media advertisements Other promotion texts and translations Oracle Database 11g: SQL and PL/SQL New Features

310 PM Schema Descriptions (continued) Print Media (PM)
PRODUCT_ INFORMATION product_id product_name product_description category_id weight_class warranty_period supplier_id product_status list_price min_price catalog_url OE Schema ONLINE_MEDIA product_id product_photo product_photo_signature product_thumbnail product_video product_audio product_text product_testimonials PRINT_MEDIA product_id first_name ad_id ad_composite ad_sourcetext ad_finaltext ad_fltexttn ad_textdocs_ntab ad_photo ad_graphic ad_header press_release ADHEADER_TYP header_name creation_date header_text logo TEXTDOC_TYP document_typ formatted_doc Oracle Database 11g: SQL and PL/SQL New Features

311 Oracle Database 11g: SQL and PL/SQL New Features 1 - 311
Schema Descriptions (continued) Print Media (PM) Row Counts/Descriptions SELECT COUNT(*) FROM print_media; COUNT(*) 5 SELECT COUNT(*) FROM online_media; 9 DESCRIBE adheader_typ Name Null? Type HEADER_NAME VARCHAR2(256) CREATION_DATE DATE HEADER_TEXT VARCHAR2(1024) LOGO BLOB DESCRIBE textdoc_typ; Name Null? Type DOCUMENT_TYP VARCHAR2(32) FORMATTED_DOC BLOB Oracle Database 11g: SQL and PL/SQL New Features

312 Oracle Database 11g: SQL and PL/SQL New Features 1 - 312
Schema Descriptions (continued) Sales History (SH) The company does a high volume of business, so it runs business statistics reports to aid in decision support. Many of these reports are time based and nonvolatile. That is, they analyze past data trends. The company loads data into its data warehouse regularly to gather statistics for the reports. The reports include annual, quarterly, monthly, and weekly sales figures by product. The company also runs reports on the distribution channels through which its sales are delivered. When the company runs special promotions on its products, it analyzes the impact of the promotions on sales. It also analyzes sales by geographical area. Oracle Database 11g: SQL and PL/SQL New Features

313 Oracle Database 11g: SQL and PL/SQL New Features 1 - 313
Schema Descriptions (continued) SH PROMOTIONS promo_id promo_name promo_subcategory promo_subcategory_id promo_category promo_category_id promo_cost promo_begin_date promo_end_date promo_total promo_total_id TIMES time_id day_name day_number_in_week day_number_in_month calendar_week_number fiscal_week_number week_ending_day week_ending_day_id calendar_month_number fiscal_month_number calendar_month_desc calendar_month_id fiscal_month_id days_in_cal_month days_in_fis_month end_of_cal_ month end_of_fis_month calendar _month _name fiscal _month _name calendar _quarter _desc calendar_quarter_id fiscal _quarter _desc fiscal _quarter _id days_in_cal_quarter days_in_fis_quarter end_of_cal_quarter end_of_fis_quarter calendar_quarter_number fiscal_quarter_number calendar_year calendar_year_id fiscal_year fiscal_year_id days_in_cal_year days_in_fis_year end_of_cal_year end_of_fis_year SALES prod_id cust_id time_id channel_id promo_id quantity_sold amount_sold COSTS prod_id time_id promo_id channel_id unit_cost unit_price CHANNELS channel_id channel_desc channel_class channel_class_id channel_total channel_total_id PRODUCTS prod_id prod_name prod_desc prod_subcategory prod_subcategory_id prod_subcategory_desc prod_category prod_category_id prod_category_desc prod_weight_class prod_unit_of_measure prod_pack_size supplier_id prod_status prod_list_price prod_min_price prod_total prod_total_id prod_src_id prod_eff_from prod_eff_to prod_valid CUSTOMERS cust_id cust_first_name cust_last_name cust_gender cust_year_of_birth cust_marital_status cust_street_address cust_postal_code cust_city cust_city_id cust_state_province cust_state_province_id country_id cust_main_phone_number cust_income_level cust_credit_limit cust_ cust_total cust_total_id cust_src_id cust_eff_from cust_eff_to cust_valid COUNTRIES country_id country_iso_code country_name country_subregion country_subregion_id country_region country_region_id country_total country_total_id Country_name_hist Oracle Database 11g: SQL and PL/SQL New Features

314 Oracle Database 11g: SQL and PL/SQL New Features 1 - 314
Schema Descriptions (continued) Sales History (SH) Row Counts SELECT COUNT(*) FROM channels; COUNT(*) 5 SELECT COUNT(*) FROM costs; 82112 SELECT COUNT(*) FROM countries; 23 SELECT COUNT(*) FROM customers; 55500 SELECT COUNT(*) FROM products; 72 SELECT COUNT(*) FROM promotions; 503 SELECT COUNT(*) FROM sales; 918843 SELECT COUNT(*) FROM times; 1826 Oracle Database 11g: SQL and PL/SQL New Features

315 JOB_HISTORY employee_id
Schema Descriptions (continued) Human Resources (HR) In the Human Resource (HR) records, each employee has an identification number, address, job identification code, salary, and manager. Some employees earn commissions in addition to their salary. The company also tracks information about the jobs within the organization. Each job has an identification code, job title, and a minimum and maximum salary range for the job. Some employees have been with the company for a long time and have held different positions within the company. When an employee resigns, the duration the employee was working, the job identification number, and the department are recorded. The sample company is regionally diverse, so it tracks the locations of its warehouses and departments. Each employee is assigned to a department, and each department is identified either by a unique department number or a short name. Each department is associated with one location, and each location has a full address that includes the street name, postal code, city, state or province, and the country code. In places where the departments and warehouses are located, the company records details such as the country name, currency symbol, currency name, and the region where the country is located geographically. The HR Entity Relationship Diagram HR DEPARTMENTS department_id department_name manager_id location_id LOCATIONS street_address postal_code city state_province country_id COUNTRIES country_name region_id REGIONS region_name EMPLOYEES employee_id first_name last_name phone_number hire_date job_id salary commission_pct JOBS job_id job_title min_salary max_salary JOB_HISTORY employee_id start_date end_date Oracle Database 11g: SQL and PL/SQL New Features

316 Oracle Database 11g: SQL and PL/SQL New Features 1 - 316
Schema Descriptions (continued) Human Resources (HR) Row Counts SELECT COUNT(*) FROM regions; COUNT(*) 4 SELECT COUNT(*) FROM countries; 25 SELECT COUNT(*) FROM locations; 23 SELECT COUNT(*) FROM departments; 27 SELECT COUNT(*) FROM jobs; 19 SELECT COUNT(*) FROM employees; 107 SELECT COUNT(*) FROM job_history; 10 Oracle Database 11g: SQL and PL/SQL New Features

317 Using the PL/SQL Debugger in SQL Developer

318 Oracle Database 11g: SQL and PL/SQL New Features 1 - 318
Objectives After completing this lesson, you should be able to do the following: Use the PL/SQL Debugger tool in SQL Developer Step through PL/SQL code line by line Step through called PL/SQL subroutines Analyze data as the routine is running Change variable values at run time Control the execution of a PL/SQL block Lesson Aim In this lesson, you learn how to use the PL/SQL Debugger within the SQL Developer tool. The PL/SQL Debugger is a powerful debugging tool that enables you to step through your code line by line and analyze the contents of variables, arguments, loops, and branch statements. Oracle Database 11g: SQL and PL/SQL New Features

319 Debugging PL/SQL Subprograms Using the SQL Developer Debugger
You can use the debugger to control the execution of your PL/SQL program. To debug a PL/SQL subprogram, a security administrator needs to grant the following privileges to the application developer: DEBUG ANY PROCEDURE DEBUG CONNECT SESSION Moving Through Code While Debugging The SQL Developer debugger enables you to control the execution of your program. You can control whether your program executes a single line of code, an entire subprogram (procedure or function), or an entire program block. By manually controlling when the program should run and when it should pause, you can quickly move over the sections that you know work correctly and concentrate on the sections that are causing problems. Oracle Database 11g: SQL and PL/SQL New Features

320 Debugging a Subprogram: Overview
1. Edit procedure 2. Add breakpoints 3. Compile for Debug 6. Choose debugging tool, and monitor data 5. Enter parameter value(s) 4. Debug Oracle Database 11g: SQL and PL/SQL New Features

321 The Procedure or Function Code Editing Tab
The Procedure or Function Code Tab This tab displays a toolbar and the text of the subprogram, which you can edit. You can set and unset breakpoints for debugging by clicking to the left of the thin vertical line beside each statement with which you want to associate a breakpoint. (When a breakpoint is set, a red circle is displayed.) Oracle Database 11g: SQL and PL/SQL New Features

322 The Procedure or Function Tab Toolbar
1 3 4 2 Icon Description 1. Run Starts normal execution of the function or procedure, and displays the results on the Running - Log tabbed page 2. Debug Starts execution of the subprogram in debug mode, and displays the Debugging - Log tabbed page, which includes the debugging toolbar for controlling the execution 3. Compile Performs a PL/SQL compilation of the subprogram 4. Compile for Debug Performs a PL/SQL compilation so that it can be debugged Oracle Database 11g: SQL and PL/SQL New Features

323 The Debugging – Log Tab Toolbar
1 2 3 4 5 Icon Description 1. Find Execution Point Goes to the next execution point 2. Resume Continues execution 3. Step Over Bypasses the next subprogram and goes to the next statement after the subprogram 4. Step Into Executes a single program statement at a time. If the execution point is located on a call to a subprogram, it steps into the first statement in that subprogram. 5. Step Out Leaves the current subprogram and goes to the next statement with a breakpoint The Debugging – Log Tab Toolbar Step Over The Step Over command bypasses the next subprogram and goes to the next statement after the subprogram provided that the subprogram does not have any breakpoint elsewhere. Step Into The Step Into command executes a single program statement at a time. If the execution point is located on a call to a subprogram, the Step Into command steps into that subprogram and places the execution point on the subprogram’s first statement. Oracle Database 11g: SQL and PL/SQL New Features

324 The Debugging – Log Tab Toolbar
7 Displays breakpoints 6 8 Icon Description 6. Step to End of Method Goes to last statement in the current subprogram (or to the next breakpoint if there are any in the current procedure) 7. Pause Halts execution but does not exit 8. Terminate Halts and exits the execution Oracle Database 11g: SQL and PL/SQL New Features

325 Oracle Database 11g: SQL and PL/SQL New Features 1 - 325
Additional Tabs Tab Description Data Located under the code text area, displays information about all variables Watches Located under the code text area, displays information about watchpoints Note If you cannot see some of the debugging tabs described in this lesson, you can redisplay such tabs using the View > Debugger menu option. Oracle Database 11g: SQL and PL/SQL New Features

326 Debugging a Procedure Example: Creating a New emp_list Procedure
The emp_list procedure gathers employee information such as the employee’s department name, ID, name, salary, and commission percent. The procedure creates a record to store the employee’s information. The procedure also creates a table that can hold multiple records of employees. “i” is a counter. The code opens the cursor and fetches the employee’s records. The code also checks whether or not there are more records to fetch, or whether the number of records fetched so far is less than the number of records that you specify. The code eventually prints out the employee’s information. The procedure also calls the get_location function, which returns the name of the city in which an employee works. Note: Make sure that you are displaying the procedure code in edit mode. To edit the procedure code, click the Edit icon on the procedure’s toolbar. Oracle Database 11g: SQL and PL/SQL New Features

327 Debugging a Procedure Example: Creating a New get_location Function
This function returns the city in which an employee works. This function is called from the emp_list procedure. Oracle Database 11g: SQL and PL/SQL New Features

328 Setting Breakpoints and Compiling emp_list in Debug Mode
Debugging a Procedure Example: Setting Breakpoints in emp_list In the example in the slide, the emp_list procedure is displayed in edit mode, and four breakpoints are added to various locations in the code. To compile the procedure for debugging, right-click the code, and then select Compile for Debug from the shortcut menu. The Messages – Log tab displays the message that the procedure was compiled. Oracle Database 11g: SQL and PL/SQL New Features

329 Compiling the get_location Function for Debug Mode
In the example in the slide, the get_location function is displayed in edit mode. To compile the function for debugging, right-click the code, and then select Compile for Debug from the shortcut menu. The Messages – Log tab displays the message that the function was compiled. Oracle Database 11g: SQL and PL/SQL New Features

330 Debugging emp_list and Enter Value for PMAXROWS Parameter
Enter the procedure’s Parameter value using the Anonymous block. Debugging emp_list and Enter Value for PMAXROWS Parameter The next step in the debugging process is to debug the procedure using any of the several available methods mentioned earlier, such as clicking the Debug icon on the procedure’s toolbar. An anonymous block is displayed, where you are prompted to enter the parameters for this procedure. emp_list has one parameter, PMAXROWS, which specifies the number of records to return. Replace the second PMAXROWS with a number such as 2, and then click OK. Oracle Database 11g: SQL and PL/SQL New Features

331 Debugging emp_list: Step Into the Code
1 Program control stops at first breakpoint. The Step Into Debugging Tool The Step Into command executes a single program statement at a time. If the execution point is located on a call to a subprogram, the Step Into command steps into that subprogram and places the execution point on the subprogram’s first statement. If the execution point is located on the last statement of a subprogram, choosing Step Into causes the debugger to return from the subprogram, placing the execution point on the line of code that follows the call to the subprogram you are returning from. The term single stepping refers to using Step Into to run successively through the statements in your program code. You can step into a subprogram in any of the following ways: Select Debug > Step Into, press [F7], or click the Step Into icon on the Debugging – Log toolbar. In the example in the slide, the program control is stopped at the first breakpoint in the code. The arrow next to the breakpoint indicates that this is the line of code that will be executed next. Note the various tabs at the bottom of the window. Note: The Step Into and Step Over commands offer the simplest way of moving through your program code. Although the two commands are very similar, they each offer a different way to control code execution. 2 Oracle Database 11g: SQL and PL/SQL New Features

332 Debugging emp_list: Step Into the Code
1 Step Into [F7]: Steps into and executes the cursor code. Control is transferred to Cursor definition 2 Debugging emp_list: Step Into the Code Selecting the Step Into command executes a single program statement at a time. If the execution point is located on a call to a subprogram, the Step Into command steps into that subprogram and places the execution at the first statement in the subprogram. In the example in the slide, pressing [F7] executes the line of code at the first breakpoint. In this case, program control is transferred to the section where the cursor is defined. Oracle Database 11g: SQL and PL/SQL New Features

333 Oracle Database 11g: SQL and PL/SQL New Features 1 - 333
Viewing the Data Viewing the Data While you are debugging your code, you can use the Data tab to display and modify the variables. You can also set watches to monitor a subset of the variables displayed in the Data tab. To display or hide the Data, Smart Data, and Watch tabs: Select View > Debugger, and then select the tabs that you want to display or hide. Oracle Database 11g: SQL and PL/SQL New Features

334 Modifying the Variables While Debugging the Code
To modify the value of a variable in the Data tab, right-click the variable name, and then select Modify Value from the shortcut menu. The Modify Value window is displayed. The current value for the variable is displayed. You can enter a new value in the second text box, and then click OK. Oracle Database 11g: SQL and PL/SQL New Features

335 Debugging emp_list: Step Over the Code
Step Over [F8]: Executes the Cursor (same as [F7]) But control is not transferred to Open Cursor code 1 2 The Step Over Debugging Tool The Step Over command, like Step Into, enables you to execute program statements one at a time. However, if you issue the Step Over command when the execution point is located on a subprogram call, the debugger runs that subprogram without stopping (instead of stepping into it), and then positions the execution point on the statement that follows the subprogram call. If the execution point is located on the last statement of a subprogram, choosing Step Over causes the debugger to return from the subprogram, placing the execution point on the line of code that follows the call to the subprogram you are returning from. You can step over a subprogram in any of the following ways: Select Debug > Step Over, press [F8], or click the Step Over icon on the Debugging – Log toolbar. In the example in the slide, stepping over will execute the open cursor line without transferring program control to the cursor definition as was the case with the Step Into option example. Oracle Database 11g: SQL and PL/SQL New Features

336 Debugging emp_list: Step Out of the Code ([SHIFT]+[F7])
2 1 3 5 4 Debugging emp_list: Step Out of the Code ([SHIFT]+[F7]) The Step Out of the code option leaves the current subprogram and goes to the next statement subprogram. In the example in the slide, starting with step 3, if you press [SHIFT] + [F7], the program control leaves the procedure and goes to the next statement in the anonymous block. Continuing to press [SHIFT] + [F7], will take you to the next anonymous block that prints the contents of the SQL buffer. 6 Oracle Database 11g: SQL and PL/SQL New Features

337 Debugging emp_list: Run to Cursor [F4]
Run to Cursor (F4): Run to your cursor location without having to single step or set a breakpoint. Running to the Cursor Location When stepping through your application code in the debugger, you may want to run to a particular location without having to single step or set a breakpoint. To run to a specific program location, in a subprogram editor, position your text cursor on the line of code where you want the debugger to stop. You can run to the cursor location using any of the following procedures: In the procedure editor, right-click and choose Run to Cursor, choose the Debug > Run to Cursor option from the main menu, or press [F4]. Any of the following conditions may result: When you run to the cursor, your program executes without stopping, until the execution reaches the location marked by the text cursor in the source editor. If your program never actually executes the line of code where the text cursor is, the Run to Cursor command will cause your program to run until it encounters a breakpoint or when your program finishes. Oracle Database 11g: SQL and PL/SQL New Features

338 Debugging emp_list: Step to End of Method
Loops until i <> PMAXROWS Debugging emp_list: Step to End of Method Step to End of Method goes to the last statement in the current subprogram or to the next breakpoint if there are any in the current subprogram. In the example in the slide, the Step to End of Method debugging tool is used. Because there is a second breakpoint, selecting Step to End of Method will transfer control to that breakpoint. Selecting Step to End of Method again will go through the iterations of the while loop first, and then it will transfer the program control to the next executable statement in the anonymous block. Oracle Database 11g: SQL and PL/SQL New Features

339 Oracle Database 11g: SQL and PL/SQL New Features 1 - 339
Summary In this lesson, you should have learned how to debug procedures and functions. Summary In this lesson, you learned how to use the PL/SQL Debugger in the SQL Developer tool. You learned how to set up break points, step through code line by line, analyze the contents of variables and parameters, and follow progress through looping and branching constructs. Oracle Database 11g: SQL and PL/SQL New Features

340

341 Using SQL*Plus

342 Oracle Database 11g: SQL and PL/SQL New Features 1 - 342
Objectives After completing this appendix, you should be able to do the following: Log in to SQL*Plus Edit SQL commands Format output using SQL*Plus commands Interact with script files Objectives You might want to create SELECT statements that can be used again and again. This appendix also covers the use of SQL*Plus commands to execute SQL statements. You learn how to format output using SQL*Plus commands, edit SQL commands, and save scripts in SQL*Plus. Oracle Database 11g: SQL and PL/SQL New Features

343 SQL and SQL*Plus Interaction
SQL statements Server SQL*Plus Query results Buffer SQL and SQL*Plus SQL is a command language for communication with the Oracle Server from any tool or application. Oracle SQL contains many extensions. When you enter a SQL statement, it is stored in a part of memory called the SQL buffer and remains there until you enter a new SQL statement. SQL*Plus is an Oracle tool that recognizes and submits SQL statements to Oracle Database 11g for execution. It contains its own command language. Features of SQL Can be used by a range of users, including those with little or no programming experience Is a nonprocedural language Reduces the amount of time required for creating and maintaining systems Is an English-like language Features of SQL*Plus Accepts ad hoc entry of statements Accepts SQL input from files Provides a line editor for modifying SQL statements Controls environmental settings Formats query results into basic reports Accesses local and remote databases SQL scripts Oracle Database 11g: SQL and PL/SQL New Features

344 SQL Statements Versus SQL*Plus Commands
A language ANSI-standard Keywords cannot be abbreviated Statements manipulate data and table definitions in the database An environment Oracle-proprietary Keywords can be abbreviated Commands do not allow manipulation of values in the database SQL statements SQL buffer SQL*Plus commands SQL*Plus buffer SQL and SQL*Plus (continued) The following table compares SQL and SQL*Plus: Oracle Database 11g: SQL and PL/SQL New Features

345 Oracle Database 11g: SQL and PL/SQL New Features 1 - 345
Overview of SQL*Plus Log in to SQL*Plus Describe the table structure Edit your SQL statement Execute SQL from SQL*Plus Save SQL statements to files and append SQL statements to files Execute saved files Load commands from file to buffer to edit SQL*Plus SQL*Plus is an environment in which you can do the following: Execute SQL statements to retrieve, modify, add, and remove data from the database Format, perform calculations on, store, and print query results in the form of reports Create script files to store SQL statements for repeated use in the future SQL*Plus commands can be divided into the following main categories: Oracle Database 11g: SQL and PL/SQL New Features

346 Oracle Database 11g: SQL and PL/SQL New Features 1 - 346
Logging In to SQL*Plus 1 sqlplus Logging In to SQL*Plus How you invoke SQL*Plus depends on the type of operating system or Windows environment that you are running. To log in from a Windows environment: 1. Select Start > Programs > Oracle > Application Development > SQL*Plus 2. Enter the username, password, and database name. To log in from a command-line environment: 1. Log on to your machine. 2. Enter the sqlplus command shown in the slide. In the syntax: username Your database username password Your database password (Your password is visible if you enter it here.) @database The database connect string Note: To ensure the integrity of your password, do not enter it at the operating system prompt. Instead, enter only your username. Enter your password at the password prompt. 2 Oracle Database 11g: SQL and PL/SQL New Features

347 Changing the Settings of SQL*Plus Environment
You can optionally change the look of the SQL*Plus environment by using the SQL*Plus Properties dialog box. In the SQL*Plus window, right-click the title bar and in the context menu that appears, select Properties. You can then use the Colors tab of the SQL*Plus Properties dialog box to set Screen Background and Screen Text. Oracle Database 11g: SQL and PL/SQL New Features

348 Displaying Table Structure
Use the SQL*Plus DESCRIBE command to display the structure of a table: DESC[RIBE] tablename Displaying Table Structure In SQL*Plus, you can display the structure of a table using the DESCRIBE command. The result of the command is a display of column names and data types, as well as an indication whether a column must contain data. In the syntax: tablename The name of any existing table, view, or synonym that is accessible to the user To describe the DEPARTMENTS table, use this command: SQL> DESCRIBE DEPARTMENTS Name Null? Type DEPARTMENT_ID NOT NULL NUMBER(4) DEPARTMENT_NAME NOT NULL VARCHAR2(30) MANAGER_ID NUMBER(6) LOCATION_ID NUMBER(4) Oracle Database 11g: SQL and PL/SQL New Features

349 Displaying Table Structure
DESCRIBE departments Name Null? Type DEPARTMENT_ID NOT NULL NUMBER(4) DEPARTMENT_NAME NOT NULL VARCHAR2(30) MANAGER_ID NUMBER(6) LOCATION_ID NUMBER(4) Displaying Table Structure (continued) The example in the slide displays the information about the structure of the DEPARTMENTS table. In the result: Null?: Specifies whether a column must contain data (NOT NULL indicates that a column must contain data.) Type: Displays the data type for a column Oracle Database 11g: SQL and PL/SQL New Features

350 SQL*Plus Editing Commands
A[PPEND] text C[HANGE] / old / new C[HANGE] / text / CL[EAR] BUFF[ER] DEL DEL n DEL m n SQL*Plus Editing Commands SQL*Plus commands are entered one line at a time and are not stored in the SQL buffer. Guidelines If you press [Enter] before completing a command, SQL*Plus prompts you with a line number. You terminate the SQL buffer either by entering one of the terminator characters (semicolon or slash) or by pressing [Enter] twice. The SQL prompt appears. Oracle Database 11g: SQL and PL/SQL New Features

351 SQL*Plus Editing Commands
I[NPUT] I[NPUT] text L[IST] L[IST] n L[IST] m n R[UN] n n text 0 text SQL*Plus Editing Commands (continued) Note: You can enter only one SQL*Plus command for each SQL prompt. SQL*Plus commands are not stored in the buffer. To continue a SQL*Plus command on the next line, end the first line with a hyphen (-). Oracle Database 11g: SQL and PL/SQL New Features

352 Oracle Database 11g: SQL and PL/SQL New Features 1 - 352
Using LIST, n, and APPEND LIST 1 SELECT last_name 2* FROM employees 1 1* SELECT last_name A , job_id 1* SELECT last_name, job_id Using LIST, n, and APPEND Use the L[IST] command to display the contents of the SQL buffer. The asterisk (*) beside line 2 in the buffer indicates that line 2 is the current line. Any edits that you made apply to the current line. Change the number of the current line by entering the number (n) of the line that you want to edit. The new current line is displayed. Use the A[PPEND] command to add text to the current line. The newly edited line is displayed. Verify the new contents of the buffer by using the LIST command. Note: Many SQL*Plus commands, including LIST and APPEND, can be abbreviated to just their first letter. LIST can be abbreviated to L; APPEND can be abbreviated to A. LIST 1 SELECT last_name, job_id 2* FROM employees Oracle Database 11g: SQL and PL/SQL New Features

353 Using the CHANGE Command
LIST 1* SELECT * from employees c/employees/departments 1* SELECT * from departments LIST 1* SELECT * from departments Using the CHANGE Command Use L[IST] to display the contents of the buffer. Use the C[HANGE] command to alter the contents of the current line in the SQL buffer. In this case, replace the employees table with the departments table. The new current line is displayed. Use the L[IST] command to verify the new contents of the buffer. Oracle Database 11g: SQL and PL/SQL New Features

354 SQL*Plus File Commands
SAVE filename GET filename START filename @ filename EDIT filename SPOOL filename EXIT SQL*Plus File Commands SQL statements communicate with the Oracle server. SQL*Plus commands control the environment, format query results, and manage files. You can use the commands described in the following table: Oracle Database 11g: SQL and PL/SQL New Features

355 Using the SAVE, START, and EDIT Commands
LIST 1 SELECT last_name, manager_id, department_id 2* FROM employees SAVE my_query Created file my_query START my_query LAST_NAME MANAGER_ID DEPARTMENT_ID King Kochhar ... 107 rows selected. Using the SAVE, START, and EDIT Commands SAVE Use the SAVE command to store the current contents of the buffer in a file. In this way, you can store frequently used scripts for use in the future. START Use the START command to run a script in SQL*Plus. Alternatively, you can also use the symbol to run a script. @my_query Oracle Database 11g: SQL and PL/SQL New Features

356 Using the SAVE, START, and EDIT Commands
EDIT my_query Using the SAVE, START, and EDIT Commands (continued) EDIT Use the EDIT command to edit an existing script. This opens an editor with the script file in it. When you have made the changes, quit the editor to return to the SQL*Plus command line. Note: The “/” is a delimiter that signifies the end of the statement. When encountered in a file, SQL*Plus runs the statement prior to this delimiter. The delimiter must be the first character of a new line immediately following the statement. Oracle Database 11g: SQL and PL/SQL New Features

357 Oracle Database 11g: SQL and PL/SQL New Features 1 - 357
SERVEROUTPUT Command Use the SET SERVEROUT[PUT] command to control whether to display the output of stored procedures or PL/SQL blocks in SQL*Plus. The DBMS_OUTPUT line length limit is increased from 255 bytes to 32,767 bytes. The default size is now unlimited. Resources are not preallocated when SERVEROUTPUT is set. Because there is no performance penalty, use UNLIMITED unless you want to conserve physical memory. SERVEROUTPUT Command Most of the PL/SQL programs perform input and output through SQL statements, to store data in database tables or query those tables. All other PL/SQL I/O is performed through APIs that interact with other programs. For example, the DBMS_OUTPUT package has procedures such as PUT_LINE. To see the result outside of PL/SQL requires another program, such as SQL*Plus, to read and display the data passed to DBMS_OUTPUT. SQL*Plus does not display DBMS_OUTPUT data unless you first issue the SQL*Plus command, SET SERVEROUTPUT ON, as follows: SET SERVEROUTPUT ON Note SIZE sets the number of bytes of the output that can be buffered within the Oracle Database server. The default is UNLIMITED. n cannot be less than 2000 or greater than 1,000,000. For additional information about SERVEROUTPUT, see the Oracle Database PL/SQL User’s Guide and Reference 11g. SET SERVEROUT[PUT] {ON | OFF} [SIZE {n | UNL[IMITED]}] [FOR[MAT] {WRA[PPED] | WOR[D_WRAPPED] | TRU[NCATED]}] Oracle Database 11g: SQL and PL/SQL New Features

358 Using the SQL*Plus SPOOL Command
SPO[OL] [file_name[.ext] [CRE[ATE] | REP[LACE] | APP[END]] | OFF | OUT] Option Description file_name[.ext] Spools output to the specified file name CRE[ATE] Creates a new file with the name specified REP[LACE] Replaces the contents of an existing file. If the file does not exist, REPLACE creates the file. APP[END] Adds the contents of the buffer to the end of the file that you specify OFF Stops spooling OUT Stops spooling and sends the file to your computer’s standard (default) printer Using the SQL*Plus SPOOL Command The SPOOL command stores query results in a file, or optionally sends the file to a printer. The SPOOL command has been enhanced. You can now append to, or replace an existing file, where previously you could only use SPOOL to create (and replace) a file. REPLACE is the default. To spool the output generated by commands in a script without displaying the output on the screen, use SET TERMOUT OFF. SET TERMOUT OFF does not affect the output from commands that run interactively. You must use quotes around file names containing white space. To create a valid HTML file using SPOOL APPEND commands, you must use PROMPT or a similar command to create the HTML page header and footer. The SPOOL APPEND command does not parse HTML tags. SET SQLPLUSCOMPAT[IBILITY] to 9.2 or earlier to disable the CREATE, APPEND, and SAVE parameters. Oracle Database 11g: SQL and PL/SQL New Features

359 Using the AUTOTRACE Command
It displays a report after the successful execution of SQL DML statements, such as SELECT, INSERT, UPDATE, or DELETE. The report can now include execution statistics and the query execution path. SET AUTOT[RACE] {ON | OFF | TRACE[ONLY]} [EXP[LAIN]] [STAT[ISTICS]] SET AUTOTRACE ON -- The AUTOTRACE report includes both the optimizer -- execution path and the SQL statement execution -- statistics. Using the AUTOTRACE Command EXPLAIN shows the query execution path by performing an EXPLAIN PLAN. STATISTICS displays SQL statement statistics. The formatting of your AUTOTRACE report may vary depending on the version of the server to which you are connected and the configuration of the server. The DBMS_XPLAN package provides an easy way to display the output of the EXPLAIN PLAN command in several, predefined formats. Note For additional information about the package and subprograms, see the Oracle Database PL/SQL Packages and Types Reference 11g. For additional information about EXPLAIN PLAN, see the Oracle Database SQL Reference 11g. For additional information about Execution Plans and the statistics, see the Oracle Database Performance Tuning Guide 11g. Oracle Database 11g: SQL and PL/SQL New Features

360 Oracle Database 11g: SQL and PL/SQL New Features 1 - 360
Summary In this appendix, you should have learned how to use SQL*Plus as an environment to do the following: Execute SQL statements Edit SQL statements Format output Interact with script files Summary SQL*Plus is an execution environment that you can use to send SQL commands to the database server, and to edit and save SQL commands. You can execute commands from the SQL prompt or from a script file. Oracle Database 11g: SQL and PL/SQL New Features

361 Working with Collections

362 Oracle Database 11g: SQL and PL/SQL New Features 1 - 362
Objectives After completing this lesson, you should be able to do the following: Describe an object type Create an object type specification Implement the constructor method on objects Create collections Nested table collections Varray collections Use collections methods Manipulate collections Objectives In this lesson, you are introduced to PL/SQL programming using collections, including user-defined object types and constructor methods. Oracle object types are user-defined data types that make it possible to model complex real-world entities such as customers and purchase orders as unitary entities—objects—in the database. A collection is an ordered group of elements, all of the same type (for example, phone numbers for each customer). Each element has a unique subscript that determines its position in the collection. Collections work like the set, queue, stack, and hash table data structures found in most third-generation programming languages. Collections can store instances of an object type and can also be attributes of an object type. Collections can be passed as parameters. So, you can use them to move columns of data into and out of database tables or between client-side applications and stored subprograms. You can define collection types in a PL/SQL package, and then use the same types across many applications. Oracle Database 11g: SQL and PL/SQL New Features

363 Understanding Collections
A collection is a group of elements, all of the same type. Collections work like arrays. Collections can store instances of an object type and, conversely, can be attributes of an object type. Types of collections in PL/SQL: Nested tables Varrays Associative arrays String-indexed collections INDEX BY pls_integer Collections A collection is a group of elements, all of the same type. Each element has a unique subscript that determines its position in the collection. Collections work like the arrays found in most third-generation programming languages. They can store instances of an object type and, conversely, can be attributes of an object type. Collections can also be passed as parameters. You can use them to move columns of data into and out of database tables or between client-side applications and stored subprograms. Object types are used not only to create object relational tables, but also to define collections. You can use any of the three categories of collections: Nested tables: Can have any number of elements Varrays: An ordered collection of elements Associative arrays (known as “index-by tables” in earlier Oracle releases): Sets of key-value pairs, where each key is unique and is used to locate a corresponding value in the array. The key can be an integer or a string. Note: Associative arrays will not be emphasized in this Appendix. Oracle Database 11g: SQL and PL/SQL New Features

364 Describing the Collection Types
Nested table: Varray: Collections (continued) PL/SQL offers two collection types: Nested Tables A nested table holds a set of values. That is, it is a table within a table. Nested tables are unbounded, meaning the size of the table can increase dynamically. Nested tables are available in both PL/SQL and the database. Within PL/SQL, nested tables are like one-dimensional arrays whose size can increase dynamically. Within the database, nested tables are column types that hold sets of values. The Oracle database stores the rows of a nested table in no particular order. When you retrieve a nested table from the database into a PL/SQL variable, the rows are given consecutive subscripts starting at 1. This gives you array-like access to individual rows. Nested tables are initially dense but they can become sparse through deletions and, therefore, have nonconsecutive subscripts. Varrays Variable-size arrays, or varrays, are also collections of homogeneous elements that hold a fixed number of elements (although you can change the number of elements at run time). They use sequential numbers as subscripts. You can define equivalent SQL types, allowing varrays to be stored in database tables. They can be stored and retrieved through SQL, but with less flexibility than nested tables. You can reference the individual elements for array operations, or manipulate the collection as a whole. Oracle Database 11g: SQL and PL/SQL New Features

365 Oracle Database 11g: SQL and PL/SQL New Features 1 - 365
Collections (continued) Varrays (continued) Varrays are always bounded and never sparse. You can specify the maximum size of the varray in its type definition. Its index has a fixed lower bound of 1 and an extensible upper bound. A varray can contain a varying number of elements—from zero (when empty) to the maximum specified in its type definition. To reference an element, you can use the standard subscripting syntax. Oracle Database 11g: SQL and PL/SQL New Features

366 Listing Characteristics for Collections
PL/SQL Nested Tables DB Nested Tables PL/SQL Varrays Maximum size No Yes Sparsity Can be Dense Storage N/A Stored out of line Stored inline (if < 4,000 bytes) Ordering Does not retain ordering and subscripts Retains ordering and subscripts Choosing a PL/SQL Collection Type If you already have code or business logic that uses some other language, you can usually translate that language’s array and set types directly to PL/SQL collection types. Arrays in other languages become varrays in PL/SQL. Sets and bags in other languages become nested tables in PL/SQL. Hash tables and other kinds of unordered lookup tables in other languages become associative arrays in PL/SQL. If you are writing original code or designing business logic from the start, consider the strengths of each collection type and decide which is appropriate. Choosing Between Nested Tables and Varrays Use varrays when: The number of elements is known in advance The elements are usually all accessed in sequence Use nested tables when: The index values are not consecutive There is no predefined upper bound for index values You need to delete or update some elements, but not all the elements at once You would usually create a separate lookup table, with multiple entries for each row of the main table, and access it through join queries Oracle Database 11g: SQL and PL/SQL New Features

367 Using Collections Effectively
Varrays involve fewer disk accesses and are more efficient. Use nested tables for storing large amounts of data. Use varrays to preserve the order of elements in the collection column. If you do not have a requirement to delete elements in the middle of a collection, favor varrays. Varrays do not allow piecewise updates. Guidelines for Using Collections Effectively Because varray data is stored inline (in the same tablespace), retrieving and storing varrays involves fewer disk accesses. Varrays are thus more efficient than nested tables. To store large amounts of persistent data in a column collection, use nested tables. This way the Oracle server can use a separate table to hold the collection data, which can grow over time. For example, when a collection for a particular row could contain 1 to 1,000,000 elements, a nested table is simpler to use. If your data set is not very large and it is important to preserve the order of elements in a collection column, use varrays. For example, if you know that in each row the collection will not contain more than 10 elements, you can use a varray with a limit of 10. If you do not want to deal with deletions in the middle of the data set, use varrays. If you expect to retrieve the entire collection simultaneously, use varrays. Varrays do not allow piecewise updates. Oracle Database 11g: SQL and PL/SQL New Features

368 Creating Collection Types
Nested table in the database: Nested table in PL/SQL: Varray in the database: Varray in PL/SQL: CREATE [OR REPLACE] TYPE type_name AS TABLE OF Element_datatype [NOT NULL]; TYPE type_name IS TABLE OF element_datatype [NOT NULL]; CREATE [OR REPLACE] TYPE type_name AS VARRAY (max_elements) OF element_datatype [NOT NULL]; TYPE type_name IS VARRAY (max_elements) OF element_datatype [NOT NULL]; Creating Collection Types To create a collection, you first define a collection type, and then declare collections of that type. The slide shows the syntax for defining nested table and varray collection types in both the database (persistent) and in PL/SQL (transient), and defining a string-indexed collection in PL/SQL. Creating Collections in the Database You can create a nested table or a varray data type in the database, which makes the data type available to use in places such as columns in database tables, variables in PL/SQL programs, and attributes of object types. Before you can define a database table containing a nested table or varray, you must first create the data type for the collection in the database. Use the syntax shown in the slide to create collection types in the database. Creating Collections in PL/SQL You can also create a nested table or a varray in PL/SQL. Use the syntax shown in the slide to create collection types in PL/SQL. Note: Collections can be nested. Oracle Database 11g: SQL and PL/SQL New Features

369 Declaring Collections: Nested Table
First, define an object type: Second, declare a column of that collection type: CREATE TYPE typ_item AS OBJECT --create object (prodid NUMBER(5), price NUMBER(7,2) ) / CREATE TYPE typ_item_nst -- define nested table type AS TABLE OF typ_item 1 CREATE TABLE pOrder ( -- create database table ordid NUMBER(5), supplier NUMBER(5), requester NUMBER(4), ordered DATE, items typ_item_nst) NESTED TABLE items STORE AS item_stor_tab / 2 3 Declaring Collections: Nested Table To create a table based on a nested table, perform the following steps: 1. Create the typ_item type, which holds the information for a single line item. 2. Create the typ_item_nst type, which is created as a table of the typ_item type. Note: You must create the typ_item_nst nested table type based on the previously declared type because it is illegal to declare multiple data types in this nested table declaration. 3. Create the pOrder table and use the nested table type in a column declaration, which will include an arbitrary number of items based on the typ_item_nst type. Thus, each row of pOrder may contain a table of items. The NESTED TABLE STORE AS clause is required to indicate the name of the storage table in which the rows of all the values of the nested table reside. The storage table is created in the same schema and the same tablespace as the parent table. Note: The USER_COLL_TYPES dictionary view holds information about collections. Oracle Database 11g: SQL and PL/SQL New Features

370 Understanding Nested Table Storage
Nested tables are stored out-of-line in storage tables. pOrder nested table Supplier Requester Ordered Items 123 321 456 789 10-MAR-97 12-FEB-97 Storage table NESTED_TABLE_ID ProdID Price 901 879 $ $ Nested Table Storage The rows for all nested tables of a particular column are stored within the same segment. This segment is called the storage table. A storage table is a system-generated segment in the database that holds instances of nested tables within a column. You specify the name for the storage table by using the NESTED TABLE STORE AS clause in the CREATE TABLE statement. The storage table inherits storage options from the outermost table. To distinguish between nested table rows belonging to different parent table rows, a system-generated nested table identifier (unique for each outer row that encloses a nested table) is created. Operations on storage tables are performed implicitly by the system. You should not access or manipulate the storage table, except implicitly through its containing objects. Privileges of the column of the parent table are transferred to the nested table. 333 112 $ $300.00 Oracle Database 11g: SQL and PL/SQL New Features

371 Declaring Collections: Varray
First, define a collection type: Then, declare a collection of that type: CREATE TYPE typ_Project AS OBJECT( --create object project_no NUMBER(2), title VARCHAR2(35), cost NUMBER(7,2)) / CREATE TYPE typ_ProjectList AS VARRAY (50) OF typ_Project -- define VARRAY type 1 2 CREATE TABLE department ( -- create database table dept_id NUMBER(2), name VARCHAR2(15), budget NUMBER(11,2), projects typ_ProjectList) -- declare varray as column / 3 Example The example in the slide shows how to create a table based on a varray. 1. Create the typ_project type, which holds the information for a project. 2. Create the typ_ projectlist type, which is created as a varray of the project type. The varray contains a maximum of 50 elements. Create the DEPARTMENT table and use the varray type in a column declaration. Each element of the varray stores a project object. This example demonstrates how to create a varray of phone numbers, and then use it in a CUSTOMERS table (The OE sample schema uses this definition.): CREATE TYPE phone_list_typ AS VARRAY(5) OF VARCHAR2(25); / CREATE TABLE customers (customer_id NUMBER(6) ,cust_first_name VARCHAR2(50) ,cust_last_name VARCHAR2(50) ,cust_address cust_address_typ(100) ,phone_numbers phone_list_typ ... ); Oracle Database 11g: SQL and PL/SQL New Features

372 Working with Collections in PL/SQL
You can declare collections as formal parameters of procedures and functions. You can specify a collection type in the RETURN clause of a function specification. Collections follow the usual scoping and instantiation rules. CREATE OR REPLACE PACKAGE manage_dept_proj AS TYPE typ_proj_details IS TABLE OF typ_Project; ... PROCEDURE allocate_proj (propose_proj IN typ_proj_details); FUNCTION top_project (n NUMBER) RETURN typ_proj_details; Working with Collections There are several points about collections that you must know when working with them: You can declare collections as the formal parameters of functions and procedures. That way, you can pass collections to stored subprograms and from one subprogram to another. A function’s RETURN clause can be a collection type. Collections follow the usual scoping and instantiation rules. In a block or subprogram, collections are instantiated when you enter the block or subprogram and cease to exist when you exit. In a package, collections are instantiated when you first reference the package and cease to exist when you end the database session. In the example in the slide, a nested table is used as the formal parameter of a packaged procedure, the data type of an IN parameter for the procedure ALLOCATE_PROJ, and the return data type of the TOP_PROJECT function. Oracle Database 11g: SQL and PL/SQL New Features

373 Initializing Collections
Three ways to initialize: Use a constructor. Fetch from the database. Assign another collection variable directly. DECLARE this example uses a constructor v_accounting_project typ_ProjectList; BEGIN v_accounting_project := typ_ProjectList (typ_Project (1, 'Dsgn New Expense Rpt', 3250), typ_Project (2, 'Outsource Payroll', 12350), typ_Project (3, 'Audit Accounts Payable',1425)); INSERT INTO department VALUES(10, 'Accounting', 123, v_accounting_project); ... END; / Initializing Collections Until you initialize it, a collection is atomically null (that is, the collection itself is null, not its elements). To initialize a collection, you can use one of the following means: Use a constructor, which is a system-defined function with the same name as the collection type. A constructor allows the creation of an object from an object type. Invoking a constructor is a way to instantiate (create) an object. This function “constructs” collections from the elements passed to it. In the example shown in the slide, you pass three elements to the typ_ProjectList() constructor, which returns a varray containing those elements. Read an entire collection from the database using a fetch. Assign another collection variable directly. You can copy the entire contents of one collection to another as long as both are built from the same data type. Oracle Database 11g: SQL and PL/SQL New Features

374 Initializing Collections
DECLARE this example uses a fetch from the database v_accounting_project typ_ProjectList; BEGIN SELECT projects INTO v_accounting_project FROM department WHERE dept_id = 10; ... END; / 1 DECLARE -- this example assigns another collection -- variable directly v_accounting_project typ_ProjectList; v_backup_project typ_ProjectList; BEGIN SELECT projects INTO v_accounting_project FROM department WHERE dept_id = 10; v_backup_project := v_accounting_project; END; / 2 Initializing Collections (continued) In the first example shown in the slide, an entire collection from the database is fetched into the local PL/SQL collection variable. In the second example shown in the slide, the entire contents of one collection variable are assigned to another collection variable. Oracle Database 11g: SQL and PL/SQL New Features

375 Referencing Collection Elements
Use the collection name and a subscript to reference a collection element: Syntax: Example: To reference a field in a collection: collection_name(subscript) v_accounting_project(1) v_accounting_project(1).cost Referencing Collection Elements Every element reference includes a collection name and a subscript enclosed in parentheses. The subscript determines which element is processed. To reference an element, you can specify its subscript by using the following syntax: collection_name(subscript) In the preceding syntax, subscript is an expression that yields a positive integer. For nested tables, the integer must lie in the range 1 to For varrays, the integer must lie in the range from 1 to maximum_size that you provide. Oracle Database 11g: SQL and PL/SQL New Features

376 Using Collection Methods
EXISTS COUNT LIMIT FIRST and LAST PRIOR and NEXT EXTEND TRIM DELETE collection_name.method_name [(parameters)] Using Collection Methods You can use collection methods from procedural statements, but not from SQL statements. Oracle Database 11g: SQL and PL/SQL New Features

377 Using Collection Methods
Traverse collections with methods: DECLARE i INTEGER; v_accounting_project typ_ProjectList; BEGIN v_accounting_project := typ_ProjectList( typ_Project (1,'Dsgn New Expense Rpt', 3250), typ_Project (2, 'Outsource Payroll', 12350), typ_Project (3, 'Audit Accounts Payable',1425)); i := v_accounting_project.FIRST ; WHILE i IS NOT NULL LOOP IF v_accounting_project(i).cost > then DBMS_OUTPUT.PUT_LINE('Project too expensive: ' || v_accounting_project(i).title); END IF; i := v_accounting_project.NEXT (i); END LOOP; END; / Traversing Collections In the example shown, the FIRST method finds the smallest index number, the NEXT method traverses the collection starting at the first index. The output from this block of code shown above is: Project too expensive: Outsource Payroll You can use the PRIOR and NEXT methods to traverse collections indexed by any series of subscripts. In the example shown, the NEXT method is used to traverse a varray. PRIOR(n) returns the index number that precedes index n in a collection. NEXT(n) returns the index number that succeeds index n. If n has no predecessor, PRIOR(n) returns NULL. Likewise, if n has no successor, NEXT(n) returns NULL. PRIOR is the inverse of NEXT. PRIOR and NEXT do not wrap from one end of a collection to the other. When traversing elements, PRIOR and NEXT ignore deleted elements. Oracle Database 11g: SQL and PL/SQL New Features

378 Using Collection Methods
DECLARE v_my_projects typ_ProjectList; v_array_count INTEGER; v_last_element INTEGER; BEGIN SELECT projects INTO v_my_projects FROM department WHERE dept_id = 10; v_array_count := v_my_projects.COUNT ; dbms_output.put_line('The # of elements is: ' || v_array_count); v_my_projects.EXTEND ; --make room for new project v_last_element := v_my_projects.LAST ; dbms_output.put_line('The last element is: ' || v_last_element); IF v_my_projects.EXISTS(5) THEN dbms_output.put_line('Element 5 exists!'); ELSE dbms_output.put_line('Element 5 does not exist.'); END IF; END; / Example The block of code shown uses the COUNT, EXTEND, LAST, and EXISTS methods on the my_projects varray. The COUNT method reports that the projects collection holds three projects for department 10. The EXTEND method creates a fourth empty project. The LAST method reports that four projects exist. When testing for the existence of a fifth project, the program reports that it does not exist. The output from this block of code is as follows: The # of elements is: 3 The last element is: 4 Element 5 does not exist. PL/SQL procedure successfully completed. Oracle Database 11g: SQL and PL/SQL New Features

379 Manipulating Individual Elements
CREATE OR REPLACE PROCEDURE add_project ( p_deptno IN NUMBER, p_new_project IN typ_Project, p_position IN NUMBER ) IS v_my_projects typ_ProjectList; BEGIN SELECT projects INTO v_my_projects FROM department WHERE dept_id = p_deptno FOR UPDATE OF projects; v_my_projects.EXTEND; --make room for new project /* Move varray elements forward */ FOR i IN REVERSE p_position..v_my_projects.LAST - 1 LOOP v_my_projects(i + 1) := v_my_projects(i); END LOOP; v_my_projects(p_position) := p_new_project; -- add new -- project UPDATE department SET projects = v_my_projects WHERE dept_id = p_deptno; END add_project; / Manipulating Individual Elements You must use PL/SQL procedural statements to reference the individual elements of a varray in an INSERT, UPDATE, or DELETE statement. In the example shown in the slide, the stored procedure inserts a new project into a department’s projects at a given position. To execute the procedure, pass the department number to which you want to add a project, the project information, and the position where the project information is to be inserted. EXECUTE add_project(10, - typ_Project(4, 'Information Technology', 789), 4) SELECT * FROM department; DEPT_ID NAME BUDGET PROJECTS(PROJECT_NO, TITLE, COST) 10 Accounting PROJECTLIST(PROJECT(1, 'Dsgn New Expense Rpt', 3250), PROJECT(2, 'Outsource Payroll', 12350), PROJECT(3, 'Audit Accounts Payable', 1425), PROJECT(4, 'Information Technology', 789)) Oracle Database 11g: SQL and PL/SQL New Features

380 Avoiding Collection Exceptions
Common exceptions with collections: COLLECTION_IS_NULL NO_DATA_FOUND SUBSCRIPT_BEYOND_COUNT SUBSCRIPT_OUTSIDE_LIMIT VALUE_ERROR Avoiding Collection Exceptions In most cases, if you reference a nonexistent collection element, PL/SQL raises a predefined exception. Oracle Database 11g: SQL and PL/SQL New Features

381 Avoiding Collection Exceptions
Common exceptions with collections: DECLARE TYPE NumList IS TABLE OF NUMBER; nums NumList; atomically null BEGIN /* Assume execution continues despite the raised exceptions. */ nums(1) := 1; raises COLLECTION_IS_NULL nums := NumList(1,2); -- initialize table nums(NULL) := raises VALUE_ERROR nums(0) := 3; raises SUBSCRIPT_OUTSIDE_LIMIT nums(3) := 3; raises SUBSCRIPT_BEYOND_COUNT nums.DELETE(1); delete element 1 IF nums(1) = 1 THEN -- raises NO_DATA_FOUND ... Example In the first case, the nested table is atomically null. In the second case, the subscript is null. In the third case, the subscript is outside the legal range. In the fourth case, the subscript exceeds the number of elements in the table. In the fifth case, the subscript designates a deleted element. Oracle Database 11g: SQL and PL/SQL New Features

382 Oracle Database 11g: SQL and PL/SQL New Features 1 - 382
Summary In this lesson, you should have learned how to: Identify types of collections Nested tables Varrays Define nested tables and varrays in the database Define nested tables and varrays in PL/SQL Access collection elements Use collection methods in PL/SQL Identify raised exceptions with collections Decide which collection type is appropriate for each scenario Summary Collections are a grouping of elements, all of the same type. The types of collections are nested tables, varrays, and associative arrays. You can define nested tables in PL/SQL program units and in the database. Nested tables, varrays, and associative arrays can be used in a PL/SQL program. When using collections in PL/SQL programs, you can access collection elements, use predefined collection methods, and use exceptions that are commonly encountered with collections. There are guidelines for using collections effectively and to determine which collection type is appropriate under specific circumstances. Oracle Database 11g: SQL and PL/SQL New Features

383 Exploring the Data Warehousing Performance Enhancements

384 Oracle Database 11g: SQL and PL/SQL New Features 1 - 384
Objectives After completing this lesson, you should be able to: List the materialized view enhancements: Review the materialized view concepts List the new and updated MV catalog views Describe the updated columns in the MV catalog views Identify the refresh performance enhancements Review the benefits of using materialized views Objectives Two main topics are discussed in this lesson: 1. Materialized view enhancements 2. Query rewrite enhancements In this lesson, you learn about the new and updated materialized view catalogs. Using SQL Developer, you examine the changes to the materialized view catalogs. You learn how to improve data warehousing performance through summary management and query rewrite features. Oracle Database 11g: SQL and PL/SQL New Features

385 Oracle Database 11g: SQL and PL/SQL New Features 1 - 385
Objectives List the query rewrite enhancements: Review the benefits of query rewrite Use the query rewrite enhancement to support queries with inline views Identify the query rewrite enhancement that supports queries with remote tables Oracle Database 11g: SQL and PL/SQL New Features

386 Oracle Database 11g: SQL and PL/SQL New Features 1 - 386
Lesson Agenda Materialized view enhancements: Concepts New and updated MV catalog views Refresh performance enhancements Benefits of using materialized views Query rewrite enhancements: Query rewrite benefits Query rewrite enhancements with inline views Query rewrite enhancements with remote tables Oracle Database 11g: SQL and PL/SQL New Features

387 Materialized View: Overview
Materialized views are: Schema objects Used to summarize, compute, replicate, and distribute data Used in data warehouse, decision support, distributed or mobile computing environments Overview of Materialized Views Materialized views are schema objects that can be used to summarize, compute, replicate, and distribute data. You can use materialized views most appropriately in data warehousing, decision support, and distributed or mobile computing environments. In data warehouses, materialized views are used to compute and store aggregated data, such as sums and averages. Materialized views in these environments are typically referred to as summaries because they store summarized data. The optimizer can use materialized views to improve query performance by automatically recognizing when a materialized view can and should be used to satisfy a request. Queries are then directed to the materialized view and not to the underlying detail tables or views. In distributed environments, materialized views are used to replicate data at distributed sites and synchronize updates done at several sites with conflict resolution methods. Materialized views as replicas provide local access to data that otherwise has to be accessed from remote sites. In mobile computing environments, materialized views are used to download a subset of data from central servers to mobile clients, with periodic refreshes from the central servers and propagation of updates by clients back to the central servers. Materialized views are similar to indexes in that they consume storage space, need to be refreshed, improve performance of SQL for query rewrites, and are transparent. Oracle Database 11g: SQL and PL/SQL New Features

388 Materialized View: Overview
Sample materialized view (in the SH sample schema): DESCRIBE sh.FWEEK_PSCAT_SALES_MV Name Null Type WEEK_ENDING_DAY NOT NULL DATE PROD_SUBCATEGORY NOT NULL VARCHAR2(50) DOLLARS NUMBER CHANNEL_ID NOT NULL NUMBER PROMO_ID NOT NULL NUMBER Sample Materialized View (in the SH Sample Schema) The SH sample schema comes with a materialized view. In the next examples, this view is used. The materialized view is defined as: SELECT t.week_ending_day , p.prod_subcategory , sum(s.amount_sold) AS dollars , s.channel_id , s.promo_id FROM sales s , times t , products p WHERE s.time_id = t.time_id AND s.prod_id = p.prod_id GROUP BY t.week_ending_day , s.promo_id; Note: For information about materialized views, see the Oracle Database 10g: Implement and Administer a Data Warehouse Instructor Led Training or the Oracle Database Data Warehousing Guide 11g, Release 1 (11.1) documentation. Oracle Database 11g: SQL and PL/SQL New Features

389 New and Updated MV Catalog Views
New catalog views display the partition change tracking (PCT) information for a given materialized view. New catalog views display which sections of the materialized views data are fresh or stale. You can view the partition staleness information of the materialized view. It affects the usability and maintainability of the materialized view. New and Updated MV Catalog Views Oracle Database 11g introduces both new and enhanced catalog views for materialized views that enable you to track the materialized view freshness. Oracle Database 11g: SQL and PL/SQL New Features

390 PCT Catalog Views Showing Stalessness Corresponding to Base Partitions
USER/ALL/DBA_MVIEWS views (UPDATED) USER/ALL/DBA_MVIEW_DETAIL_RELATIONS views (UPDATED) New and Updated MV Catalog Views (continued) Partition Change Tracking (PCT) was introduced in Oracle Database 9i but its related information was not yet exposed to the user through catalog views. Therefore, users were unable to see this valuable information, which could help them make better decisions as to how their materialized views should be maintained. Oracle Database 11g exposes the MV freshness information corresponding to PCT base table partitions. This provides users with the information regarding which ranges of the MV data are fresh and which are not. The fresh part of MV is reliable and available for use. The USER/ALL/DBA_MVIEWS catalog view is extended where new columns are added to describe the number of PCT tables, and the number of fresh and stale PCT regions. The USER/ALL/DBA_MVIEW_DETAIL_RELATIONS catalog view is extended where new columns are added to indicate whether the detail table is PCT-enabled, and to show the numbers of fresh and stale PCT partitions. A new catalog view for PCT partition (USER/ALL/DBA_MVIEW_DETAIL_PARTITION) is created to describe the freshness of each PCT partition. A new catalog view for PCT subpartition (_MVIEW_DETAIL_SUBPARTITION) is created to describe the freshness of each PCT subpartition. USER/ALL/DBA_MVIEW_DETAIL_PARTITION views (NEW) USER/ALL/DBA_MVIEW_DETAIL_ SUBPARTITION views (NEW) Oracle Database 11g: SQL and PL/SQL New Features

391 Important Instructions
New and Updated MV Catalog Views (continued) The USER/ALL/DBA_MVIEWS extension describes all materialized views in the database. The USER/ALL/DBA_MVIEW_DETAIL_RELATIONS extension represents the named detail relations that are either in the FROM list of a materialized view, or that are indirectly referenced through views in the FROM list. The new USER/ALL/DBA_MVIEW_DETAIL_PARTITION displays the freshness information of the materialized views, with respect to a PCT detail partition. The new USER/ALL/DBA_MVIEW_DETAIL_SUBPARTITION displays freshness information for all materialized views in the database, with respect to a PCT detail subpartition. Important Instructions Oracle Database 11g: SQL and PL/SQL New Features

392 New Columns in the USER/ALL/DBA_MVIEWS Catalog View
NUM_PCT_TABLES: Specifies the number of PCT detail tables NUM_FRESH_PCT_REGIONS: Specifies the number of fresh PCT partition regions NUM_STALE_PCT_REGIONS: Specifies the number of stale PCT partition regions SELECT mview_name, num_pct_tables, num_fresh_pct_regions, num_stale_pct_regions FROM all_mviews WHERE owner = 'SH'; MVIEW_NAME NUM_PCT_TABLES NUM_FRESH_PCT_REGIONS NUM_STALE_PCT_REGIONS FWEEK_PSCAT_ SALES_MV New Columns in the _MVIEWS Catalog View This view describes materialized views. You can use this view to find the query definition of the view as well as other useful information such as whether it is updatable, rewrite enabled, and fresh. The syntax shown in the slide is the extended specification for the DBA/ALL/USER_MVIEWS catalog view. This catalog view is extended to show how many detail partitions support PCT. In addition, two extended columns show how many fresh and stale PCT regions are present in that MV. ALL_MVIEWS describes all the materialized views accessible to the current user. DBA_MVIEWS describes all the materialized views in the database. USER_MVIEWS describes all materialized views owned by the current user. Note: For the complete columns listing for the DBA/ALL/USER_MVIEWS catalog views, see the Oracle Database Reference 11g, Release 1 (11.1) guide. Oracle Database 11g: SQL and PL/SQL New Features

393 New Columns in USER/ALL/DBA_MVIEW_DETAIL_RELATIONS
DESCRIBE all_mview_detail_relations Name Null Type OWNER NOT NULL VARCHAR2(30) MVIEW_NAME NOT NULL VARCHAR2(30) DETAILOBJ_OWNER NOT NULL VARCHAR2(30) DETAILOBJ_NAME NOT NULL VARCHAR2(30) DETAILOBJ_TYPE VARCHAR2(9) DETAILOBJ_ALIAS VARCHAR2(30) DETAILOBJ_PCT VARCHAR2(1) NUM_FRESH_PCT_PARTITIONS NUMBER NUM_STALE_PCT_PARTITIONS NUMBER DETAILOBJ_PCT: Is the detail object PCT supported NUM_FRESH_PCT_PARTITIONS: Specifies the number of fresh PCT partitions NUM_STALE_PCT_PARTITIONS: Specifies the number of stale PCT partitions New Columns in the _MVIEW_DETAIL_RELATIONS Catalog View This view describes the named detail relations that are either specified in the FROM list of the subquery that defines a materialized view accessible to the current user, or that are indirectly referenced through views in that FROM list. Inline views in the materialized view definition are not represented in this view or the related views. Three new columns are added to this view in Oracle Database 11g. The views are extended to show whether the detail partition supports PCT with respect to a given MV. If the detail partition does support PCT, the catalog views display how many fresh and stale PCT partitions are present in that detail table. The columns outlined above are the new columns that are added to the DBA/ALL/USER_ MVIEW_DETAIL_RELATIONS catalog views. Oracle Database 11g: SQL and PL/SQL New Features

394 New USER/ALL/DBA_MVIEW_DETAIL_PARTITION Catalog View
SELECT detailobj_owner, detailobj_name, detail_partition_name, detail_partition_position POSITION, freshness FRESH FROM all_mview_detail_partition WHERE mview_name = 'FWEEK_PSCAT_SALES_MV'; DETAIL_OBJ_OWNER DETAIL_OBJ_NAME DETAIL_PARTITION_NAME POSITION FRESH SH SALES SALES_ FRESH SH SALES SALES_ FRESH SH SALES SALES_H1_ FRESH SH SALES SALES_H2_ FRESH SH SALES SALES_Q1_ FRESH ... SH SALES SALES_Q1_ FRESH SH SALES SALES_Q2_ FRESH SH SALES SALES_Q3_ FRESH SH SALES SALES_Q4_ FRESH 28 rows selected New _MVIEW_DETAIL_PARTITION Catalog View You can use this new view to find the freshness information about the materialized view with respect to a PCT detail partition. The columns of this view are defined as follows: OWNER – The name of the owner of the materialized view MVIEW_NAME – The name of the materialized view DETAILOBJ_OWNER – The name of the owner of the detail object DETAILOBJ_NAME – The detail object name (table or view) DETAIL_PARTITION_NAME – The name of the detail object partition DETAIL_PARTITION_POSITION – The position of the detail object partition FRESHNESS – The freshness state; either FRESH, STALE, UNKNOWN, or NA Oracle Database 11g: SQL and PL/SQL New Features

395 New Catalog View: USER/ALL/DBA_MVIEW_DETAIL_SUBPARTITION
DESCRIBE all_mview_detail_subpartition Name Null Type OWNER NOT NULL VARCHAR2(30) MVIEW_NAME NOT NULL VARCHAR2(30) DETAILOBJ_OWNER NOT NULL VARCHAR2(30) DETAILOBJ_NAME NOT NULL VARCHAR2(30) DETAIL_PARTITION_NAME VARCHAR2(30) DETAIL_SUBPARTITION_NAME VARCHAR2(30) DETAIL_SUBPARTITION_POSITION NUMBER FRESHNESS CHAR(5) New _MVIEW_DETAIL_SUBPARTITION Catalog View ALL_MVIEW_DETAIL_SUBPARTITION displays freshness information of the materialized views, with respect to a PCT detail subpartition, accessible to the current user. DBA_MVIEW_DETAIL_SUBPARTITION displays freshness information for all materialized views in the database, with respect to a PCT detail subpartition. USER_MVIEW_DETAIL_SUBPARTITION displays freshness information for all materialized views, with respect to a PCT detail subpartition, owned by the current user. Oracle Database 11g: SQL and PL/SQL New Features

396 Refresh Performance Improvements in Oracle Database 11g
Refresh operations on materialized views are now faster with the following improvements: Refresh statement combinations (merge and delete) Removal of unnecessary refresh hint Index creation for UNION ALL MV PCT refresh possible for UNION ALL MV Refresh Performance Improvements The refresh performance improvements reduce the time required to refresh materialized views. Query Rewrite Possible Using the Materialized View During Its Atomic Refresh Previously, when the materialized view was being refreshed, it was implicitly disabled for query rewrite even if its data was acceptable to the user. This was especially true when atomic refresh was in progress and the user saw the data in the materialized view in a transactional state of the past refresh. In Oracle Database 11g, when the materialized view is refreshed in the atomic mode, it is eligible for query rewrite if the rewrite integrity mode is set to “STALE_TOLERATED.” Partition-Based Refresh for UNION ALL MVs Previously, the fast refresh was extended to support MVs that have UNION ALL operators. However, the fast refresh of the UNION ALL MV did not apply partition-based (PCT) refresh. In Oracle Database 11g, PCT refresh is now allowed for UNION ALL MV fast refresh. Automatically Create Proper Index on MV (UNION ALL MV) The materialized view with UNION ALL operators is fast refreshable but unlike most of the fast refreshable MVs, it is not created with an index. Previously, in order to speed up refresh execution, you needed to manually create such index. Now it is automatic. Oracle Database 11g: SQL and PL/SQL New Features

397 Using Summaries to Improve Performance
Special types of aggregate views Improve query execution time by precalculating expensive joins and aggregation operations prior to execution and storing results in a database table Created with a materialized view Using Summaries to Improve Performance Summaries are aggregate views that are created to improve query execution times. In an Oracle Database, summaries are implemented with a materialized view. There are many well-known techniques that you can use to increase query performance. For example, you can create additional indexes, or you can partition your data. Many data warehouses are also using a technique called summaries. The basic process for a summary is to precompute the result of a long-running query and store this result in a database table called summary table, which is comparable to a CREATE TABLE AS SELECT (CTAS) statement. Instead of precomputing the same query result many times, the user can directly access the summary table. Although this approach has the benefit of enhancing query response time, it also has many drawbacks. The user needs to be aware of the summary table’s existence in order to rewrite the query to use that table instead. Also, the data contained in a summary table is frozen, and must be manually refreshed whenever modifications occur on the real tables. With Oracle Database summary management, the user no longer has to be aware of summaries that have been defined. The DBA creates materialized views that are automatically used by the system when rewriting SQL queries. Using MVs offers another advantage over manually creating summaries tables, in that the data can be refreshed automatically. Oracle Database 11g: SQL and PL/SQL New Features

398 Summary Management DBA creates materialized view (summary table)
End user queries tables and views Using Summaries to Improve Performance (continued) In a typical use of summary management, the database administrator creates the materialized view (summary table). When the end user queries tables and views, the query rewrite mechanism of the Oracle server automatically rewrites the SQL query to use the summary table. The use of the materialized view is transparent to the end user or application querying the data. Oracle server rewrites SQL query to use summary tables Oracle Database 11g: SQL and PL/SQL New Features

399 Summary Management Components
Mechanisms to define materialized views and dimensions Refresh mechanism to ensure materialized views contain the latest data Query rewrite capability to transparently rewrite a query to use a materialized view SQL Access Advisor: Recommends materialized views and indexes to be created DBMS_ADVISOR.TUNE_MVIEW procedure: Shows you how to make your materialized view fast refreshable and use general query rewrite Summary Management Components The implementation of summary management in Oracle Database includes the use of the components listed in the slide. Oracle Database 11g: SQL and PL/SQL New Features

400 Using Summary Management
1. Use the SQL Access Advisor to determine how you will use materialized views. 2. Create materialized views and design how queries will be rewritten. 3. Use DBMS_ADVISOR.TUNE_MVIEW to obtain an optimized materialized view as necessary. 4. View the CREATE output results by querying USER_TUNE_MVIEW or DBA_TUNE_MVIEW. DBMS_ADVISOR.TUNE_MVIEW ( name, 'CREATE MATERIALIZED VIEW my_mv_name REFRESH FAST AS SELECT_statement_goes_here); Using Summary Management After your data has been transformed, staged, and loaded into the detail tables, you invoke the summary management process as described above. The parameters for the DBMS_ADVISOR.TUNE_MVIEW are: name – The task name for looking up the results in a catalog view. If not specified, the system will generate a name and return. mv_create_stmt – The original materialized view creation statement. Oracle Database 11g: SQL and PL/SQL New Features

401 Oracle Database 11g: SQL and PL/SQL New Features 1 - 401
Lesson Agenda Materialized view enhancements: Concepts New and updated MV catalog views Refresh performance enhancements Benefits of using materialized views Query rewrite enhancements: Query rewrite benefits Query rewrite enhancements with inline views Query rewrite enhancements with remote tables Oracle Database 11g: SQL and PL/SQL New Features

402 Query Rewrite: Overview
Tries to use materialized views instead of base tables to return query results Can save orders of magnitude of CPU and elapsed time to return results as queries are precomputed Query does not necessarily have to be in the exact form of the materialized view query to rewrite Various requirements for query rewrite to occur Query rewrite Overview of Query Rewrite When base tables contain large amount of data, it is expensive and time consuming to compute the required aggregates or to compute joins between these tables. Because materialized views contain already precomputed aggregates and joins, you can use the process called query rewrite to quickly answer the query using materialized views. One of the major benefits of creating and maintaining materialized views is the ability to take advantage of query rewrite, which transforms a SQL statement expressed in terms of tables or views into a statement accessing one or more materialized views that are defined in the detail tables. The transformation is transparent to the end user or application, requiring no intervention and no reference to the materialized view in the SQL statement. Because query rewrite is transparent, materialized views can be added or dropped just like indexes without invalidating the SQL in the application code. A query undergoes several checks to determine whether it is a candidate for query rewrite. If the query fails any of the checks, then the query is applied to the detail tables rather than the materialized view. This can be costly in terms of response time and processing power. Oracle Database 11g: SQL and PL/SQL New Features

403 Important Instructions
Overview of Query Rewrite (continued) The optimizer uses two different methods to recognize when to rewrite a query in terms of a materialized view. One method is based on matching the SQL text of the query with the SQL text of the materialized view definition. If the above method fails, the optimizer uses the more general method in which it compares joins, selections, data columns, grouping columns, and aggregate functions between the query and materialized views. Dimensions, constraints, and rewrite integrity levels affect whether or not a given query is rewritten to use one or more materialized views. Additionally, query rewrite can be enabled or disabled by REWRITE and NOREWRITE hints, and the QUERY_REWRITE_ENABLED session parameter. The DBMS_MVIEW.EXPLAIN_REWRITE procedure advises whether query rewrite is possible on a query and, if so, which materialized views will be used. It also explains why a query cannot be rewritten. When Does Oracle Rewrite a Query? A query is rewritten only when a certain number of conditions are met: Query rewrite must be enabled for the session. A materialized view must be enabled for query rewrite. The rewrite integrity level should allow the use of the materialized view. For example, if a materialized view is not fresh and query rewrite integrity is set to ENFORCED, then the materialized view is not used. Either all or part of the results requested by the query must be obtainable from the precomputed result stored in the materialized view or views. To test these conditions, the optimizer may depend on some of the data relationships declared by the user using constraints and dimensions, among others, hierarchies, referential integrity, and uniqueness of key data, and so on. Important Instructions Oracle Database 11g: SQL and PL/SQL New Features

404 Cost-Based Query Rewrite Process
Generate plan End user queries tables and views Cost-Based Query Rewrite Process Query rewrite is available only with the cost-based optimizer. The Oracle database optimizes the input query with and without rewrite, and selects the least costly alternative. The optimizer rewrites a query by rewriting one or more query blocks, one at a time. If the rewrite logic has a choice between multiple materialized views to rewrite a query block, it selects the one that can result in reading the least amount of data. After a materialized view has been picked for a rewrite, the optimizer performs the rewrite and then tests whether the rewritten query can be rewritten further with another materialized view (this can be the case only when nested materialized views exist). This process continues until no further rewrites are possible. Query rewrite is attempted recursively to take advantage of nested materialized views. Generate plan Compare plan costs Pick best plan Oracle Database 11g: SQL and PL/SQL New Features

405 Oracle Database 11g: SQL and PL/SQL New Features 1 - 405
What Can Be Rewritten? Queries and subqueries in the following types of SQL statements: SELECT CREATE TABLE … AS SELECT INSERT INTO … SELECT Subqueries in DML statements: INSERT UPDATE DELETE Subqueries in the set operators: UNION UNION ALL INTERSECT MINUS Query Rewrite: What Can Be Rewritten? Query rewrite operates on queries and subqueries in the following types of SQL statements: SELECT CREATE TABLE … AS SELECT INSERT INTO … SELECT Query rewrite operates on subqueries in DML statements: INSERT, DELETE, and UPDATE. It also operates on subqueries in set operators: UNION, UNION ALL, INTERSECT, and MINUS. Oracle Database 11g: SQL and PL/SQL New Features

406 Query Rewrite Enhancement to Support Queries Containing Inline Views
Yes Query with inline view Query inline view’s (IV) text matches the MV’s IV text? No Rewrite query No Yes Query Rewrite Enhancements with Inline Views In Oracle Database 10g, general query rewrite was supported when the user’s query contained an inline view, or a subquery in the FROM list. Query rewrite matched inline views in the materialized view with inline views in the request query when the text of the two inline views exactly matches. In that case, Oracle Database 10g treated the matching inline view as it would a named view, and general rewrite processing was possible. The query rewrite in Oracle Database 11g supports queries containing inline views. More queries are now eligible for query rewrite, thus improving system throughput and performance. Oracle Database 11g supports query rewrite with inline views in two circumstances: When the text from the inline views in the materialized view exactly matches the text in the request query, or When the request query contains inline views that are equivalent to the inline views in the materialized view No query rewrite Query inline view’s (IV) text equivalent to the MV’s IV text? Oracle Database 11g: SQL and PL/SQL New Features

407 When Are Two Inline Views Equivalent?
Two inline views are considered equivalent when: The SELECT lists and GROUP BY lists are equivalent The FROM clauses contain the same or equivalent objects The join graphs, including all the selections in the WHERE clauses are equivalent The HAVING clauses are equivalent Oracle Database 11g: SQL and PL/SQL New Features

408 An MV Inline View Text Matches a Query’s Inline View Text: Example
CREATE MATERIALIZED VIEW SUM_SALES_MV ENABLE QUERY REWRITE AS SELECT mv_iv.prod_id, mv_iv.cust_id, sum(mv_iv.amount_sold) sum_amount_sold FROM (SELECT prod_id, cust_id, amount_sold FROM sales, products WHERE sales.prod_id = products.prod_id) MV_IV GROUP BY mv_iv.prod_id, mv_iv.cust_id; 1 -- The text of the IV matches exactly the text of the -- MV; therefore, the query is rewritten with the MV SELECT iv.prod_id, iv.cust_id, SUM(iv.amount_sold) sum_amount_sold FROM (SELECT prod_id, cust_id, amount_sold FROM sales, products WHERE sales.prod_id = products.prod.id) IV GROUP BY iv.prod_id, iv.cust_id; 2 An MV Inline View: Example 1. The first example shown above, displays a materialized view that contains an inline view. 2. The query in the second example shown in the slide has an inline view whose text matches exactly with that of the materialized view’s inline view; therefore, the query inline view will be internally replaced with the materialized view’s inline view so that the query can be rewritten. Oracle Database 11g: SQL and PL/SQL New Features

409 Oracle Database 11g: SQL and PL/SQL New Features 1 - 409
An MV Inline View Text is Equivalent to a Query’s Inline View Text: Example CREATE MATERIALIZED VIEW SUM_SALES_MV ENABLE QUERY REWRITE AS SELECT mv_iv.prod_id, mv_iv.cust_id, sum(mv_iv.amount_sold) sum_amount_sold FROM (SELECT prod_id, cust_id, amount_sold FROM sales, products WHERE sales.prod_id = products.prod_id) MV_IV GROUP BY mv_iv.prod_id, mv_iv.cust_id; 1 -- The text of the IV doesn’t match the text of the MV; -- however, they are equivalent SELECT iv.prod_id, iv.cust_id, SUM(iv.amount_sold) sum_amount_sold FROM (SELECT prod_id, cust_id, amount_sold FROM products, sales WHERE sales.prod_id = products.prod.id) IV GROUP BY iv.prod_id, iv.cust_id; 2 An MV Inline View: Example (continued) 1. The first example shown in the slide displays a materialized view that contains an inline view. 2. The second example displays a query that has an equivalent inline view to the inline view found in the materialized view example above. Note that their texts do not match. It receives the same object number and rewrite takes place. The first example shown in the slide has an inline view that does not have exact text match with the inline view in the preceding materialized view. Note that the join predicate in the query inline view is switched. Even though this query does not textually match with that of the materialized view’s inline view, query rewrite will identify the query’s inline view as equivalent to the materialized view’s inline view. As before, the query inline view will be internally replaced with the materialized view’s inline view so that the query can be rewritten. The two earlier queries are rewritten with SUM_SALES_MV as shown in the code example in the slide. Oracle Database 11g: SQL and PL/SQL New Features

410 Transforming and Rewriting the Query from the Two Previous Examples
SELECT iv.prod_id, iv.cust_id, SUM(iv.amount_sold) sum_amount_sold FROM (SELECT prod_id, cust_id, amount_sold FROM products, sales WHERE sales.prod_id = products.prod.id) IV GROUP BY iv.prod_id, iv.cust_id; SELECT iv.prod_id, iv.cust_id, SUM(iv.amount_sold) sum_amount_sold FROM (SELECT prod_id, cust_id, amount_sold FROM sales, products WHERE sales.prod_id = products.prod.id) IV GROUP BY iv.prod_id, iv.cust_id; -- Both above queries are first transformed as follows: SELECT prod_id, cust_id, sum(amount_sold) FROM MV_IV GROUP BY prod_id, cust_id; Transforming and Rewriting the Query The two previous query examples with the matching and equivalent inline view texts to that of the MV are first transformed as shown in the second example above to use the MV. Next the query inline view for both the examples will be internally replaced with the materialized view’s inline view so that the query can be rewritten. -- Both above queries are finally re-written as follows: SELECT p_id, c_id, sum_sold FROM SUM_SALES_MV; Oracle Database 11g: SQL and PL/SQL New Features

411 Did Query Rewrite Occur?
Use the EXPLAIN PLAN statement Execute the query Was the query rewritten? Examining Query Rewrite Because query rewrite occurs transparently, it is not always evident that it has taken place. The rewritten statement is not stored in the V$SQL view, nor can it be dumped in a trace file. Of course, if the query runs faster, rewrite should have occurred; but that is not proof. There are two ways to confirm that the query rewrite has occurred: Use the EXPLAIN PLAN statement and check whether the OBJECT_NAME column contains the name of a materialized view. Use the DBMS_MVIEW.EXPLAIN_REWRITE procedure to see whether a query will be rewritten or not. Note: For additional information about verifying whether or not the query was rewritten using the EXPLAIN PLAN statement and the DBMS_MVIEW.EXPLAIN_REWRITE procedure, see the Oracle Database 10g: Implement and Administer a Data Warehouse Instructor-Led Training or the Oracle Database Data Warehousing Guide 11g, Release 1 (11.1) documentation. Use the DBMS_MVIEW.EXPLAIN_REWRITE procedure Oracle Database 11g: SQL and PL/SQL New Features

412 Query Rewrite Using Remote Tables in Oracle Database 11g
Oracle supports query rewrite with MVs that reference tables at a single remote database site. The MV should be present at the site where the query is being issued. Because any remote table update cannot be propagated to the local site simultaneously, query rewrite will only work in the stale_tolerated mode. Whenever a query contains columns that are not found in the MV, a join back is used to rewrite the query. If the join back table is not found at the local site, query rewrite will not take place. Query Rewrite and Remote Tables Oracle Database 11g extends the current query rewrite capability. Query rewrite can reference remote objects using MVs, which reference the remote objects. This reduces or eliminates the data from network (network round trips), which is a costly operation. Note: Because the constraint information of the remote tables is not available at the remote site, query rewrite will not make use of any constraint information. Oracle Database 11g: SQL and PL/SQL New Features

413 Query Rewrite Using Remote Tables: Example
CREATE MATERIALIZED VIEW sum_sales_prod_week_mv ENABLE QUERY REWRITE AS SELECT p.prod_id, t.week_ending_day, s.cust_id, SUM(s.amount_sold) AS sum_amount_sold FROM p, t WHERE s.time_id=t.time_id AND s.prod_id=p.prod_id GROUP BY p.prod_id, t.week_ending_day, s.cust_id; SELECT p.prod_id, t.week_ending_day, s.cust_id, SUM(s.amount_sold) AS sum_amount_sold FROM s, p, t WHERE s.time_id=t.time_id AND s.prod_id=p.prod_id GROUP BY p.prod_id, t.week_ending_day, s.cust_id; Query Rewrite and Remote Tables: Example The materialized view shown in the first example above is present at the local site, but it references tables that are all found at the remote site. The query in the second example shown in the slide contains tables that are found at a single remote site. Even though the query references remote tables, it will be rewritten using the above materialized view as shown in the third example in the slide. SELECT prod_id, week_ending_day, cust_id, sum_amount_sold FROM sum_sales_prod_week_mv; Oracle Database 11g: SQL and PL/SQL New Features

414 Oracle Database 11g: SQL and PL/SQL New Features 1 - 414
Summary In this lesson, you should have learned how to use: The materialized view enhancements: List the new and updated MV catalog views Describe the updated columns in the MV catalog views Identify the refresh performance enhancements Review the benefits of using materialized views The query rewrite enhancements: Use the query rewrite enhancement to support queries with inline views Identify the query rewrite enhancement that supports queries with remote tables Summary This lesson covered two main topics: 1. The materialized view enhancements 2. The query rewrite enhancements Oracle Database 11g: SQL and PL/SQL New Features


Download ppt "Introducing the Oracle Database 11g SQL and PL/SQL New Features"

Similar presentations


Ads by Google