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