75
SAGE Computing Services Customised Oracle Training Workshops and Consulting 11g New Features … of the SQL & PL/SQL Variety Scott Wesley Systems Consultant

Oracle 11g new features for developers

Embed Size (px)

DESCRIPTION

Abstract: There are a wealth of new features available in the 11g database release. This presentation touches on SQL & PL/SQL features I found of interest, and concentrates particularly on virtual columns. Relevant scripts found at my blog http://grassroots-oracle.com/2009/07/presentations.html#11gNewFeatures

Citation preview

Page 1: Oracle 11g new features for developers

SAGE Computing ServicesCustomised Oracle Training Workshops and

Consulting

11g New Features… of the SQL & PL/SQL Variety

Scott WesleySystems Consultant

Page 2: Oracle 11g new features for developers

Documentation

Passwords

Sequences

Triggers

SQLPL/SQL

Recursion

Read only tables

Virtual columns

11g

Page 3: Oracle 11g new features for developers

Documentation is your friend

Page 4: Oracle 11g new features for developers
Page 5: Oracle 11g new features for developers

Readme’s are still around

• Features Not Available or Restricted in This Release– Edition-based redefinition is not available in Oracle

Database 11g Release 1 (11.1). You cannot create an edition, an editioning view, or a crossedition trigger; nor can you use the ENABLE EDITIONS clause in the CREATE USER and ALTER USER commands. As a consequence, other related functionality (for example, the ALTER SESSION SET EDITION statement or the new overload of DBMS_Sql.Parse() that lets you specify an edition or a crossedition trigger) becomes uninteresting and attempting to use it will cause a semantic error.

Page 6: Oracle 11g new features for developers

Can you log in?

Page 7: Oracle 11g new features for developers

SAGE@sw11g> conn scott/tigerERROR:ORA-01017: invalid username/password; logon denied

Warning: You are no longer connected to ORACLE.

Page 8: Oracle 11g new features for developers

SAGE@sw11g> conn scott/TigerConnected.

Page 9: Oracle 11g new features for developers

SYS@sw11g> ALTER SYSTEM SET SEC_CASE_SENSITIVE_LOGON = false;

System altered.

Page 10: Oracle 11g new features for developers

SCOTT@sw11g> conn scott/tigerConnected.

Page 11: Oracle 11g new features for developers

SYS@sw11g> select username, password_versions from dba_users where username = 'SCOTT';

USERNAME PASSWORD--------------- --------SCOTT 10G 11G

1 row selected.

Page 12: Oracle 11g new features for developers

Password Complexity

Page 13: Oracle 11g new features for developers

SYS@sw11g> @$ORACLE_HOME/RDBMS/ADMIN/utlpwdmg.sql

SYS@sw11g> ALTER PROFILE DEFAULT LIMIT PASSWORD_VERIFY_FUNCTION verify_function_11g;

Profile altered.

Page 14: Oracle 11g new features for developers

SCOTT@sw11g> passwordChanging password for SCOTTOld password:New password:Retype new password: sagesageERROR:ORA-28003: password verification for the specified password failedORA-20006: Password too simple

Password unchanged

Page 15: Oracle 11g new features for developers

SYS@sw11g> ALTER PROFILE DEFAULT LIMIT PASSWORD_VERIFY_FUNCTION NULL;

Profile altered.

Page 16: Oracle 11g new features for developers

Quick win: SQL*Plus supports BLOBs

Page 17: Oracle 11g new features for developers
Page 18: Oracle 11g new features for developers

SQL*Plus

Free

Available at every site

Supported by Oracle

Thin

Fast

Can do pie charts

Page 19: Oracle 11g new features for developers
Page 20: Oracle 11g new features for developers

Let’s get our hands dirty...

Page 21: Oracle 11g new features for developers

Allow Sequence in PL/SQL expressions

Page 22: Oracle 11g new features for developers

SCOTT@sw11g> create table T ( id number, value number );

Table created.

SCOTT@sw11g> create sequence id_seq;

Sequence created.

SCOTT@sw11g> create or replace 2 trigger populate_id 3 before insert on T 4 for each row 5 begin 6 -- dbms_db_version.ver_le_10 7 -- select id_seq.nextval into from dual; 8 9 -- dbms_db_version.ver_le_11 10 :new.id := id_seq.nextval; 11 end; 12 /

Trigger created.

Page 23: Oracle 11g new features for developers

This feature brings improved usability for the PL/SQL programmer and improved runtime performance and scalability.

Page 24: Oracle 11g new features for developers

SCOTT@sw11g> declare 2 n pls_integer; 3 begin 4 for i in 1 .. 50000 loop 5 select id_seq.nextval into n from dual; 6 end loop; 7 end; 8 /

PL/SQL procedure successfully completed.

Elapsed: 00:00:06.18

Page 25: Oracle 11g new features for developers

SCOTT@sw11g> declare 2 n pls_integer; 3 begin 4 for i in 1 .. 50000 loop 5 n := id_seq.nextval; 6 end loop; 7 end; 8 /

PL/SQL procedure successfully completed.

Elapsed: 00:00:06.68

Page 26: Oracle 11g new features for developers

alter session set sql_trace=true;

variable n numberbegin for i in 1 .. 100 loop :n := scott.id_seq.nextval; end loop;end;/

alter session set sql_trace=false;

Page 27: Oracle 11g new features for developers

Select ID_SEQ.NEXTVAL from dual

call count cpu elapsed disk query current rows------- ------ -------- ---------- ---------- ---------- ---------- ----------Parse 1 0.00 0.00 0 0 0 0Execute 100 0.01 0.03 0 0 0 0Fetch 100 0.00 0.05 0 0 5 100------- ------ -------- ---------- ---------- ---------- ---------- ----------total 201 0.01 0.09 0 0 5 100

Page 28: Oracle 11g new features for developers

Same Same. But Different

Page 29: Oracle 11g new features for developers

SCOTT@sw11g> insert into T test2 2 select rownum, rownum from dual connect by level < 50000;

49999 rows created.

Elapsed: 00:00:04.01

Page 30: Oracle 11g new features for developers

SCOTT@sw11g> drop trigger populate_id;

Trigger dropped.

SCOTT@sw11g> insert into T test2 2 select id_seq.nextval, rownum from dual connect by level < 50000;

49999 rows created.

Elapsed: 00:00:00.71

Page 31: Oracle 11g new features for developers

Triggers are still an overhead (in this case)

Seth Godin -->

Page 32: Oracle 11g new features for developers

As Connor McDonald likes to say:

What we really need is...

Page 33: Oracle 11g new features for developers

create table typical_table( id_col number default id_seq.nextval,...

Page 34: Oracle 11g new features for developers

But we do have trigger improvements

Page 35: Oracle 11g new features for developers

create or replacetrigger populate_idbefore insert on Tfor each rowdisablebegin :new.id := id_seq.nextval;end;/

Page 36: Oracle 11g new features for developers

Compound triggers

Page 37: Oracle 11g new features for developers

CREATE OR REPLACE TRIGGER compound_trigger FOR UPDATE OF salary ON employees COMPOUND TRIGGER

-- Declarative part (optional) -- Variables declared here have firing-statement duration. threshold CONSTANT SIMPLE_INTEGER := 200;

BEFORE STATEMENT IS BEGIN NULL; END BEFORE STATEMENT;

BEFORE EACH ROW IS BEGIN NULL; END BEFORE EACH ROW;

AFTER EACH ROW IS BEGIN NULL; END AFTER EACH ROW;

AFTER STATEMENT IS BEGIN NULL; END AFTER STATEMENT;

END compound_trigger;/ Trigger created.

Page 38: Oracle 11g new features for developers

To avoid the mutating-table error

eg: A business rule states that an employee's salary increase must not exceed 10% of the average salary for the employee's department.

Page 39: Oracle 11g new features for developers

To accumulate rows destined for a second table so that you can

periodically bulk-insert them

Page 40: Oracle 11g new features for developers

create table audit_emp (employee_id number(20) ,old_salary number(10) ,new_salary number(10) ,ts timestamp);

Page 41: Oracle 11g new features for developers

create or replace trigger old_wayafter update of salaryon emp_largefor each rowbegin insert into audit_emp values (:new.employee_id ,:old.salary ,:new.salary ,systimestamp);end old_way;/

Page 42: Oracle 11g new features for developers

SAGE@sw11g> update emp_large set salary = salary -1;

107892 rows updated.

Elapsed: 00:00:08.75

SAGE@sw11g> select count(*) from audit_emp;

COUNT(*)---------- 107892

1 row selected.

alter trigger old_way disable;

Page 43: Oracle 11g new features for developers

create or replace trigger new_wayfor update of salary on emp_largecompound trigger

threshhold constant simple_integer := 100;

type audit_t is table of audit_emp%rowtype index by simple_integer; t_audit audit_t; ln_index simple_integer := 0;

procedure flush_array is n constant SIMPLE_INTEGER := t_audit.count(); begin forall j in 1..n insert into audit_emp values t_audit(j); t_audit.delete(); ln_index := 0; end flush_array;

after each row is begin ln_index := ln_index + 1; t_audit(ln_index).employee_id := :new.employee_id; t_audit(ln_index).old_salary := :old.salary; t_audit(ln_index).new_salary := :new.salary; t_audit(ln_index).ts := systimestamp; if ln_index >= threshhold then flush_array; end if; end after each row;

after statement is begin flush_array; end after statement;end new_way;/

create or replace trigger new_wayfor update of salary on emp_largecompound trigger

threshhold constant simple_integer := 100;

type audit_t is table of audit_emp%rowtype index by simple_integer; t_audit audit_t; ln_index simple_integer := 0;

Page 44: Oracle 11g new features for developers

create or replace trigger new_wayfor update of salary on emp_largecompound trigger

threshhold constant simple_integer := 100;

type audit_t is table of audit_emp%rowtype index by simple_integer; t_audit audit_t; ln_index simple_integer := 0;

procedure flush_array is n constant SIMPLE_INTEGER := t_audit.count(); begin forall j in 1..n insert into audit_emp values t_audit(j); t_audit.delete(); ln_index := 0; end flush_array;

after each row is begin ln_index := ln_index + 1; t_audit(ln_index).employee_id := :new.employee_id; t_audit(ln_index).old_salary := :old.salary; t_audit(ln_index).new_salary := :new.salary; t_audit(ln_index).ts := systimestamp; if ln_index >= threshhold then flush_array(); end if; end after each row;

after statement is begin flush_array; end after statement;end new_way;/

after each row is begin ln_index := ln_index + 1; t_audit(ln_index).employee_id := :new.employee_id; t_audit(ln_index).old_salary := :old.salary; t_audit(ln_index).new_salary := :new.salary; t_audit(ln_index).ts := systimestamp; if ln_index >= threshhold then -- index >= 100 flush_array; end if; end after each row;

procedure flush_array is n constant SIMPLE_INTEGER := t_audit.count(); begin forall j in 1..n insert into audit_emp values t_audit(j); t_audit.delete(); ln_index := 0; end flush_array;

Page 45: Oracle 11g new features for developers

create or replace trigger new_wayfor update of salary on emp_largecompound trigger

threshhold constant simple_integer := 100;

type audit_t is table of audit_emp%rowtype index by simple_integer; t_audit audit_t; ln_index simple_integer := 0;

procedure flush_array is n constant SIMPLE_INTEGER := t_audit.count(); begin forall j in 1..n insert into audit_emp values t_audit(j); t_audit.delete(); ln_index := 0; end flush_array;

after each row is begin ln_index := ln_index + 1; t_audit(ln_index).employee_id := :new.employee_id; t_audit(ln_index).old_salary := :old.salary; t_audit(ln_index).new_salary := :new.salary; t_audit(ln_index).ts := systimestamp; if ln_index >= threshhold then flush_array; end if; end after each row;

after statement is begin flush_array; end after statement;end new_way;/

procedure flush_array is n constant SIMPLE_INTEGER := t_audit.count(); begin forall j in 1..n insert into audit_emp values t_audit(j); t_audit.delete(); ln_index := 0; end flush_array;

after statement is begin flush_array; end after statement;

Page 46: Oracle 11g new features for developers

SAGE@sw11g> update emp_large set salary = salary -1;

107892 rows updated.

Elapsed: 00:00:04.01

SAGE@sw11g> select count(*) from audit_emp;

COUNT(*)---------- 107892

1 row selected.

Page 47: Oracle 11g new features for developers

Triggers are still ok (in this case)

Page 48: Oracle 11g new features for developers

create or replace trigger package_triggerafter update of salaryon employeesfor each rowbegin dbms_output.put_line('package_trigger');end old_way;/

create or replace trigger custom_stuffafter update of salaryon employeesfor each rowfollows package_triggerbegin dbms_output.put_line('custom_stuff');end old_way;/

Page 49: Oracle 11g new features for developers

HR@sw11g> update employees set salary=1 where employee_id = 99;

package_triggercustom_stuff

1 row updated.

Page 50: Oracle 11g new features for developers

What about “PRECEDES”?

Page 51: Oracle 11g new features for developers

Named Parameters in SQL

Page 52: Oracle 11g new features for developers

create or replace function f(p1 in integer ,p2 in integer := 2 ,p3 in integer := null) return number isbegin return nvl(p1,0) +nvl(p2,0) +nvl(p3,0);end;/

Page 53: Oracle 11g new features for developers

SAGE@sw11g> select f(1,2,3) from dual;

F(1,2,3)---------- 6

1 row selected.

Page 54: Oracle 11g new features for developers

SAGE@sw11g> select f from dual;select f from dual *ERROR at line 1:ORA-06553: PLS-306: wrong number or types of arguments in call to 'F'

Page 55: Oracle 11g new features for developers

SAGE@sw11g> select f(1,null) from dual;

F(1,NULL)---------- 1

1 row selected.

Page 56: Oracle 11g new features for developers

SAGE@sw11g> select f(1,p3=>3) from dual;

F(1,P3=>3)---------- 6

1 row selected.

Page 57: Oracle 11g new features for developers

CONTINUE-WHEN

Page 58: Oracle 11g new features for developers

declare x number := 0;begin << my_loop >> loop -- after continue statement, control resumes here dbms_output.put_line ('Inside loop: x = ' || to_char(x)); x := x + 1; continue my_loop when x < 3;

dbms_output.put_line ('Inside loop, after CONTINUE: x = ' || to_char(x)); exit when x = 5; end loop my_loop; dbms_output.put_line ('After loop: x = ' || to_char(x));end;/

Inside loop: x = 0Inside loop: x = 1Inside loop: x = 2Inside loop, after CONTINUE: x = 3Inside loop: x = 3Inside loop, after CONTINUE: x = 4Inside loop: x = 4Inside loop, after CONTINUE: x = 5After loop: x = 5

PL/SQL procedure successfully completed.

Page 59: Oracle 11g new features for developers

Native PL/SQL Compilation

Page 60: Oracle 11g new features for developers

create or replacefunction factorial_interpreted(p_n number)return number isbegin if (p_n = 1) then return 1; else return factorial_interpreted(p_n-1)*p_n; end if;end;/

Page 61: Oracle 11g new features for developers

create or replacefunction factorial_native(p_n number)return number isbegin if (p_n = 1) then return 1; else return factorial_native(p_n-1)*p_n; end if;end;/

Page 62: Oracle 11g new features for developers

ALTER PROCEDURE factorial_native COMPILE PLSQL_CODE_TYPE=NATIVE REUSE SETTINGS;

Page 63: Oracle 11g new features for developers

declare l_n number;begin for i in 1..500000 loop l_n := factorial_interpreted(50); end loop;end;/Elapsed: 00:00:14.85

Page 64: Oracle 11g new features for developers

declare l_n number;begin for i in 1..500000 loop l_n := factorial_native(50); end loop;end;/Elapsed: 00:00:10.26

Page 65: Oracle 11g new features for developers

Read only tables

Page 66: Oracle 11g new features for developers

alter table logons read only;

Page 67: Oracle 11g new features for developers

HR@sw11g> update logons set user_id = upper(user_id); *ERROR at line 1:ORA-12081: update operation not allowed on table "HR"."LOGONS"

Page 68: Oracle 11g new features for developers

conn sage/sage

SAGE@sw11g> select privilege from user_tab_privs where table_name = 'LOGONS';

PRIVILEGE--------------------UPDATE

1 row selected.

Page 69: Oracle 11g new features for developers

SAGE@sw11g> update hr.logons set user_id = upper(user_id) *ERROR at line 1:ORA-12081: update operation not allowed on table "HR"."LOGONS"

Page 70: Oracle 11g new features for developers
Page 71: Oracle 11g new features for developers
Page 72: Oracle 11g new features for developers

VeryVersatileVirtualVerticals

Page 73: Oracle 11g new features for developers

Virtual Columns

• Formula/computed columns – on the database• Further constraints – on the database• New category for partitioning – on the database• Creative referential integrity – on the database

Without• Triggers - expensive• Views – sometimes forgotten• Re-design – too much hard work!

Demo…

Page 74: Oracle 11g new features for developers

And there’s more!

Query result cache PL/SQL Result Cache

Pivot / Unpivot

Invisible indexes

PL/SQL Inlining OptimisationSQL Plan Management

SQL Performance Analyser

DBA Stuff

Page 75: Oracle 11g new features for developers

SAGE Computing ServicesCustomised Oracle Training Workshops and

Consulting

Questions and Answers?

Presentations are available from our website:http://www.sagecomputing.com.au

[email protected]@sagecomputing.com.au