Oracle 11g For Developers: What You Need To Know: Presented By: John Jay King
Oracle 11g For Developers: What You Need To Know: Presented By: John Jay King
Presented by: John Jay King King Training Resources - john@kingtraining.com Download this paper from: http://www.kingtraining.com
Copyright @ 2010, John Jay King 1
Session Objectives
Learn new Oracle 11g features that are geared to developers Know how existing database features have been improved in Oracle Become aware of some DBA-oriented features that impact developers
Oracle 11g R1
Environment changes XML enhancements New/improved SQL statements New features in PL/SQL SQL & PL/SQL Results Caches Java, JDBC, and SQLJ improvements Pro* and OCI enhancements
Oracle 11g R2
Results Cache Improvements New Analytic Functions XML Enhancements Java Enhancements Pro*C/Pro*COBOL Enhancements Edition-Based Redefinition (EBR)
Binary XML
Oracle continues its XML leadership in Oracle 11g Biggest change is the addition of a new binary XMLType binary xml is a third method for storing XML data in the database structured and unstructured XMLType still supported Oracle 11gs XML processors includes a binary XML encoder, decoder, and token manager XML 1.0 text may be parsed via SAX events with or without a corresponding schema into binary XML form binary XMLType allows optimization of some XML applications by reducing memory and CPU expense
XML Indexes
Replaces CTXSYS.CTXXPATH indexes XML-specific index type, indexes document XML structure Designed to improve indexing unstructured and hybrid XML Determines XPath expressions for a document's XML tags Indexes singleton (scalar) nodes and items that occur multiple times XMLIndex record document child, descendant, and attribute axes (hierarchy) information XMLIndex is be design general (like CTXXPATH) rather than specific like B-tree indexes XMLIndex applies to all possible XPath targeting of a document XMLIndex may be used for XMLQuery, XMLTable, XMLExists, XMLCast, extract, extractValue, and existsNode XMLIndex helps anywhere in the query, not just in the WHERE clause
9
Creating XMLIndex
The syntax to create an XMLIndex looks a little different from non-XML indexes; it is made up of three parts: Path index Indexes XML tags and identifies document fragments Order index Indexes the hierarchy of nodes Value index Values to match WHERE clauses (may be exact match or range) XMLIndex uses a Path Table to store the various node paths in an XML document; if not specified in the CREATE INDEX statement Oracle will generate a name for you
CREATE INDEX po_xmlindex_ix ON po_clob (OBJECT_VALUE) INDEXTYPE IS XDB.XMLIndex PARAMETERS ('PATH TABLE my_path_table');
10
11
13
SELECT PIVOT/UNPIVOT
Oracle joins other vendors by adding the PIVOT clause to the SELECT statement Adding a PIVOT clause to a SELECT allows rotation of rows into columns while performing aggregation to create cross-tabulation queries The PIVOT clause:
Computes aggregations (implicit GROUP BY of all columns not in PIVOT clause) Output of all implicit grouping columns followed by new columns generated by PIVOT
UNPIVOT performs the same activity but converts columns into ROWS (does not undo PIVOT) Clever developers have used PL/SQL and/or CASE to achieve PIVOT results before, but now it is part of Oracle's standard SQL
14
PIVOT Example
select * from (select job,deptno,income from newemp) query1 pivot (avg(income) for deptno in (10 AS ACCOUNTING, 20 AS RESEARCH, 30 AS SALES)) order by job; Job ACCOUNTING RESEARCH ANALYST 30000 CLERK 13000 9500 MANAGER 24500 29750 PRESIDENT 50000 SALESMAN 19500 SALES 9500 28500
15
UNPIVOT Example
select * from pivot_emp_table unpivot include nulls (avgpay for dept in (ACCOUNTING,RESEARCH,SALES)) order by job; JOB DEPT ANALYST ACCOUNTING ANALYST RESEARCH ANALYST SALES /*** more rows ***/ SALESMAN ACCOUNTING SALESMAN RESEARCH SALESMAN SALES AVGPAY 30000
19500
16
17
Results Caching
Caching is nothing new to Oracle; Oracle has cached data for a long time now Whats new is the caching of results This is similar to how a Materialized View works but is more-dynamic New result_cache hint asks Oracle to cache query results
18
This query was run three times in succession with timing turned on; resulting timings were
Elapsed: 00:00:00.67 Elapsed: 00:00:00.46 Elapsed: 00:00:00.37
19
This query was run three times in succession with timing turned on; resulting timings were
Elapsed: 00:00:00.23 Elapsed: 00:00:00.01 Elapsed: 00:00:00.03
20
21
22
23
PL/SQL Enhancements
Oracle 11gs changes to PL/SQL are very interesting to the developer: PL/SQL has been improved to include all of the XMLType, BLOB, Regular Expression, and other functionality added to SQL Improvements have been made to the compiler New PL/SQL data types Sequence number use is easier continue added for loop control CALL syntax has improved
24
Compiler Enhancement
In previous releases, the PL/SQL compiler required a standalone C compiler Oracle 11g now provides a native compiler for PL/SQL eliminating the need for a separate compiler CREATE COMPILE PLSQL_CODE_TYPE=NATIVE CREATE COMPILE PLSQL_CODE_TYPE=INTERPRETED
25
Compound Triggers
Compound triggers allow the same code to be shared across timing points (previously accomplished using packages most of the time) Compound triggers have unique declaration and code sections for timing point All parts of a compound trigger share a common state that is initiated when the triggering statement starts and is destroyed when the triggering statement completes (even if an error occurs)
26
27
TRIGGER FOLLOWS
Oracle 11g adds the FOLLOWS clause to trigger creation allowing control over the sequence of execution when multiple triggers share a timing point FOLLOWS indicates that including trigger should happen after the named trigger(s)
28
29
FOLLOWS Syntax
CREATE OR REPLACE TRIGGER myTrigger BEFORE/AFTER/INSTEAD OF someEvent FOR EACH ROW FOLLOWS someschema.otherTrigger WHEN (condition=true) /* trigger body */ FOLLOWS may specify a list (and designate sequence) FOLLOWS otherTrigger1, otherTrigger2, etc
30
31
Example SIMPLE_INTEGER
declare -- mytestvar pls_integer := 2147483645; mytestvar simple_integer := 2147483645; begin loop mytestvar := mytestvar + 1; dbms_output.put_line('Value of mytestvar is now || mytestvar); exit when mytestvar < 10; end loop; end; Results in: Value of mytestvar is now 2147483646 Value of mytestvar is now 2147483647 Value of mytestvar is now -2147483648
'
32
Without SIMPLE_INTEGER
If the mytestvar variable is switched to PLS_INTEGER, an ORA-1426 NUMERIC OVERFLOW exception occurs
Error report: ORA-01426: numeric overflow ORA-06512: at line 7 01426. 00000 - "numeric overflow" *Cause: Evaluation of an value expression causes an overflow/underflow. *Action: Reduce the operands. Value of mytestvar is now 2147483646 Value of mytestvar is now 2147483647
33
Sequences in PL/SQL
Sequence values NEXTVAL and CURRVAL may be use in PL/SQL assignment statement myvar := myseq.nextval;
34
CONTINUE
CONTINUE iterates a loop; branching over the rest of the code in the loop and returning to the loop control statement
begin dbms_output.put_line('Counting down to blastoff!'); for loopctr in reverse 1 .. ctr loop if loopctr in (4,2) then continue; end if; dbms_output.put_line(to_char(loopctr)); end loop; dbms_output.put_line('Blast Off!'); end; Counting down to blastoff! 6 5 <-Values 4 and 2 do not appear in the output 3 1 Blast Off!
35
REGEXP_COUNT
REGEXP_COUNT counts the number of times a pattern occurs in a source string
select ename,regexp_count(ename,'l',1,'i') from emp; SMITH 0 ALLEN 2 WARD 0 JONES 0 MARTIN 0 BLAKE 1 /** more rows ***/ MILLER 2 string expression and/or column to match pattern Regular Expression pattern Beginning position in the source string (default=1) Match parameters (i = case insensitive, c = case sensitive, m = multiple line source delimited by ^ or $, n = matches . newline characters (default no), and x = ignore whitespace characters (default is to match)
36
37
Non-PL/SQL Development
Pro*C++ and Pro*COBOL improvements include: Supports DB2-style array INSERT and SELECT syntax Client-Side Query Cache Use Oracle's Outline to fix execution plans Oracle 11g Java Enhancements include: Java SE 5 (JDK 1.5) is new base level JIT enabled by default; automatic native compile JDBC 4.0 supported Microsoft .NET and Visual Studio .NET 2005 PL/SQL Debugging in Visual Studio .NET 2005 Designer and integration using Data Windows via Visual Studio .NET 2005 DDEX Oracle Data Provider for .NET (ODP.NET) PHP Enhancements Zend Technologies collaboration; Zend Core for Oracle may be downloaded from OTN
38
39
LISTAGG (11gR2)
LISTAGG provides lists of lower-level columns after aggregation
select department_id, listagg(last_name, ', ') within group (order by last_name) dept_employees from hr.employees where department_id in (20,30) group by department_id order by department_id; DEPARTMENT_ID ------------20 30 DEPT_EMPLOYEES ----------------------------------------Fay, Hartstein Baida, Colmenares, Himuro, Khoo, Raphaely, Tobias
40
NTH_VALUE (11gR2)
NTH_VALUE simplifies the process of retrieving the n-th value
select distinct department_id ,first_value(salary) ignore nulls over (partition by department_id order by salary desc) "1st" ,nth_value(salary,2) ignore nulls over (partition by department_id order by salary desc) "2nd" ,nth_value(salary,3) ignore nulls over (partition by department_id order by salary desc) "3rd" from hr.employees where department_id = 80 order by department_id, "1st", "2nd", "3rd"; DEPARTMENT_ID 1st 2nd 3rd ------------- ---------- ---------- ---------80 14000 13500 12000 80 14000 13500 80 14000
41
Recursive Subquery
Oracles CONNECT BY has allowed definition of a hierarchical relationship for years; now an ISOstandard option is available:
with empConnect(last_name,employee_id,manager_id,lvl) as (select last_name, employee_id, manager_id, 1 lvl2 from hr.employees where manager_id is null union all select emp.last_name, emp.employee_id, emp.manager_id, ec.lvl+1 from hr.employees emp, empConnect ec where emp.manager_id = ec.employee_id) SEARCH DEPTH FIRST BY last_name SET order_by select lvl,lpad(' ' ,3*lvl, ' ')||last_name empname from empConnect order by order_by
42
43
44
45
46
47
How?
Oracle 11gR2 Edition-Based Redefinition adds a new non-schema "edition" of an application including all of the original edition's PL/SQL, views, and synonyms; the new edition may be modified as desired then tested and deployed without impacting the users of the original edition Once the new edition is ready for complete rollout it may be released This is accomplished by a combination of: Editioning Views Showing the data "as of" a specific edition Cross-Edition Triggers Triggers keeping "old" and "new" editions synchronized
48
49
Wrapping it all Up
Oracle 11g adds significant new functionality to the already robust database environment With the production release of Oracle 11g R2 its probably time for organizations to really get serious about moving off of earlier releases While an emphasis is sometimes placed on the features of Oracle that support the Data Base Administrator, this paper shows many Developer-oriented features of great usefulness
50
Collaborate 2010
52
53
http://www.kingtraining.com
54