Sei sulla pagina 1di 13

Qualcomm

11g Database New Features

Author: Krishna Mendu/Neelomadhab padhy


Creation Date: February 09, 2011
Last Updated: February 28, 2011
Control Number:
Version: Draft

Approvals:
<Document Control Number>

Document Control

Change Record

Date Author Version Change Reference

Reviewers

Name Position

Distribution

Copy No. Name Location


1
Library Master Project Library
1 Project Manager
1
1
<Document Control Number>

Contents

Document Control...................................................................................................................ii

11g New Features.....................................................................................................................1


VIRTUAL COLUMNS......................................................................................................1
INVISIBLE INDEXES.......................................................................................................1
READ ONLY TABLES......................................................................................................2
CONTINUE CLAUSE......................................................................................................2
STRAIGHT SEQUENCES................................................................................................3
SPECIFYING ORDER OF TRIGGER FIRING...............................................................3
COMPUND TRIGGERS...................................................................................................3
PIVOT Technique..............................................................................................................5
Usage of Named and mixed notation with PL/SQL...................................................5
NEW PARTITIONING TECHNIQUES.........................................................................6
DDL WAIT OPTION........................................................................................................8
Reference..........................................................................................................................10
<Document Control Number>

11g New Features


This Document describes the 11g Database (Release 11.2.0.1.0) new features as applicable to Developers

VIRTUAL COLUMNS

 A virtual column is a column that is derived by evaluating an expression based on one or more of the actual
columns or by evaluating a SQL or PL/SQL function.

 Unlike normal columns, the virtual column data is not stored permanently on disk. The database computes
its value when you query it, by dynamically evaluating the expression on other columns.
LIMITATIONS:
 Can’t create virtual columns on IOT, external table, temporary table or a cluster.
 Its data type can’t be LOB, RAW or a user defined type.
 All columns in the column expression must belong to same table.
 You can’t update a virtual column by using it in the SET clause of an update statement.
Example:
CREATE TABLE emp (empno NUMBER(5), sal NUMBER(7,2), hrly_rate NUMBER(7,2) GENERATED ALWAYS AS
(sal/2080));
SQL> INSERT INTO emp(empno,sal) VALUES(10,10000);
1 row created.
SQL> SELECT * FROM EMP;
EMPNO SAL HRLY_RATE
---------- ---------- ----------
10 10000 4.81

INVISIBLE INDEXES

 Prior to Oracle 11g, the optimizer will be able to see all the indexes that are created.

 However in Oracle 11g, we can create invisible indexes that are not seen by optimizer, hence it won’t be taken
into account when the optimizer created the execution plan for the statement.
 You can use this as a temporary index, for testing the use of an index before making it ‘official ’.
 You can also use it as an alternative to dropping the indexes or making them unusable.
 You can use it to test the effects of dropping the index.

SQL> CREATE INDEX test_idx ON test_tbl(tname) TABLESPACE testdat INVISIBLE;

SQL> ALTER INDEX test_idx2 INVISIBLE;

SQL> ALTER INDEX test_idx2 VISIBLE;

 Also you can make all the invisible indexes visible to optimizer again by setting the
OPTIMIZER_USE_INVISIBLE_INDEXES parameter to TRUE either at session level or system level

SQL> ALTER SESSION SET OPTIMIZER_USE_INVISIBLE_INDEXES=TRUE;


<Document Control Number>

READ ONLY TABLES


 In Oracle 11g you can make a table read only, which means the database will not permit you to add, remove
or alter the data in any way.

 For example, if you have a configuration file that you want to keep safe from any changes by any users.

SQL > ALTER TABLE test_tbl READ ONLY;

 You won’t be permitted to perform the following operations on read-only tables.


 TRUNCATE table
 SELECT FOR UPDATE,
 any DML operation
 ALTER TABLE DROP/TRUNCATE/EXCHANGE PARTITION,
 FLASHBACK TABLE

 You can perform the following operations on a read-only table.

Syntax:

SELECT
CREATE|ALTER|DROP INDEX
ALTER TABLE ADD|MODIFY|DROP|ENABLE|DIASBLE,CONSTRAINT
RENAME TABLE AND ALTER TABLE RENAME TO
DROP TABLE

 You can return a table to the normal read-write status by specifying the READ WRITE clause in the ALTER
TABLE statement as shown below.

SQL> ALTER TABLE test_tbl READ WRITE;

CONTINUE CLAUSE

 With all its power, until now PL/SQL was missing one important piece of grammar: how to instruct it to do
nothing, go to the end of the loop, and loop again.

 In Oracle Database 11g PL/SQL has a new construct called CONTINUE, which is used in a loop. The
statement moves the logic to the end of the loop and then to the beginning of the loop. Here is a small
example that shows how the control moves to the end of the loop when the counter is not a multiple of 10.
Example:

BEGIN
FOR ctr in 1..100 LOOP
CONTINUE WHEN MOD(ctr,10) != 0;
DBMS_OUTPUT.PUT_LINE ('ctr='||ctr);
END LOOP;
END;
/

Here is the output:


ctr=10
ctr=20
<Document Control Number>

ctr=30
…...

STRAIGHT SEQUENCES

 When you had to use a sequence in a PL/SQL program earlier, you had to use a construct like

SELECT <Seq>. NEXTVAL INTO <VariableName> FROM DUAL prior to this release.

DECLARE
trans_id NUMBER (10);
BEGIN
SELECT myseq.NEXTVAL
INTO trans_id
FROM DUAL;
END;

 Not anymore. You can directly assign the next value of a sequence to a variable:

DECLARE
trans_id NUMBER (10);
BEGIN
trans_id := myseq.NEXTVAL;
END;
/

SPECIFYING ORDER OF TRIGGER FIRING

Prior to Oracle11g, when you defined more than one trigger on the same action (e.g., "before insert"), there was no
guarantee of the order in which the triggers would fire. Now simply include a FOLLOWS clause:
CREATE OR REPLACE TRIGGER
after_insert_validate
BEFORE INSERT
ON my_table
FOR EACH ROW
FOLLOWS after_insert_adjust
BEGIN
...
END;

COMPUND TRIGGERS

 A compound trigger allows code for one or more timing points for a specific object to be combined into a
single trigger.
<Document Control Number>

 The individual timing points can share a single global declaration section, whose state is maintained for the
lifetime of the statement. Once a statement ends, due to successful completion or an error, the trigger state is
cleaned up. In previous releases this type of functionality was only possible by defining multiple triggers
whose code and global variables were defined in a separate package but the compound trigger allows for a
much tidier solution.

 The triggering actions are defined in the same way as any other DML trigger, with the addition of the
COMPOUND TRIGGER clause. The main body of the trigger is made up of an optional global declaration
section and one or more timing point sections, each of which may contain a local declaration section whose
state is not maintained.

Syntax:

CREATE OR REPLACE TRIGGER <trigger-name>


FOR <trigger-action> ON <table-name>
COMPOUND TRIGGER
-- Global declaration.
g_global_variable VARCHAR2(10);
BEFORE STATEMENT IS
BEGIN
NULL; -- Do something here.
END BEFORE STATEMENT;
BEFORE EACH ROW IS
BEGIN
NULL; -- Do something here.
END BEFORE EACH ROW;
AFTER EACH ROW IS
BEGIN
NULL; -- Do something here.
END AFTER EACH ROW;
AFTER STATEMENT IS
BEGIN
NULL; -- Do something here.
END AFTER STATEMENT;
END <trigger-name>;
/

In Oracle Database 11g you can, using compound triggers. A compound trigger is actually four different triggers defined as
one. For instance, an UPDATE compound trigger has a before statement, before row, after statement, and after row all
rolled into one compound trigger. This a single piece of code, so you can pass variables just like any other monolithic
PL/SQL code.
CREATE OR REPLACE TRIGGER compound_trigger_t
FOR UPDATE OF salary ON employees
COMPOUND TRIGGER
ctr PLS_INTEGER := 0;
BEFORE STATEMENT IS
BEGIN
dbms_output.put_line('In before statement');
END BEFORE STATEMENT;
BEFORE EACH ROW IS
BEGIN
dbms_output.put_line('In before eachrow');
END BEFORE EACH ROW;

AFTER EACH ROW IS


BEGIN
dbms_output.put_line('In after eachrow');
END AFTER EACH ROW;
<Document Control Number>

AFTER STATEMENT IS
BEGIN
dbms_output.put_line('In after statement');
END AFTER STATEMENT;

END compound_trigger_t;
/

PIVOT Technique

PIVOT is a SQL operation, introduced in Oracle 11g, that lets you write cross tabulation (also called transposed,
crosstab and matrix) queries that rotate rows into columns while aggregating data in the rotation process.
SELECT *
FROM(
SELECT order_number, TO_CHAR(ordered_date,'MON') ord_month
FROM oe_order_headers_all
WHERE ordered_date BETWEEN TO_DATE('01-JAN-2010','DD-MON-YYYY') AND TO_DATE('31-DEC-2010','DD-MON-
YYYY')
)
PIVOT ( COUNT(order_number) FOR ord_month IN
('JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC'))

Usage of Named and mixed notation with PL/SQL

Prior to 11g, to use a sub-program (e.g. function) in a SELECT statement you have to provide the real parameters in
positional notation. Let’s look at an example using both 10g and 11g to understand it better.
-- Build a test function with multiple parameters.
CREATE OR REPLACE FUNCTION test_func(p_value_1 IN NUMBER DEFAULT 0,
p_value_2 IN NUMBER DEFAULT 0)
RETURN NUMBER AS
BEGIN
RETURN p_value_1 + p_value_2;
END test_func;
/

Function created.

10g
SQL> -- Positional Notation.
SQL> SELECT test_func(10, 20) FROM dual;

TEST_FUNC(10,20)
----------------
30
<Document Control Number>

SQL> SELECT test_func(10, p_value_2 => 20) FROM dual;


SELECT test_func(10, p_value_2 => 20) FROM dual
*
ERROR at line 1:
ORA-00907: missing right parenthesis

SQL> -- Named Notation.


SQL> SELECT test_func(p_value_1 => 10, p_value_2 => 20) FROM dual;
SELECT test_func(p_value_1 => 10, p_value_2 => 20) FROM dual
*
ERROR at line 1:
ORA-00907: missing right parenthesis

11g

SQL> -- Positional Notation.


SQL> SELECT test_func(10, 20) FROM dual;

TEST_FUNC(10,20)
----------------
30

1 row selected.

SQL> -- Mixed Notation.


SQL> SELECT test_func(10, p_value_2 => 20) FROM dual;

TEST_FUNC(10,P_VALUE_2=>20)
---------------------------
30

1 row selected.

SQL> -- Named Notation.


SQL> SELECT test_func(p_value_1 => 10, p_value_2 => 20) FROM dual;

TEST_FUNC(P_VALUE_1=>10,P_VALUE_2=>20)
--------------------------------------
30

1 row selected.

NEW PARTITIONING TECHNIQUES

Extended Composite partitioning:


Composite partitioning was first introduced in Oracle 8i where you could sub partition a RANGE partitioned table
via HASH sub partitioning.
In Oracle9i, composite partitioning was expanded to include RANGE-LIST.
In Oracle 11g, you can create the following types of composite partitions
 Range-range
<Document Control Number>

 Range-hash
 Range-list
 List-range
 List-hash
 List-list
Reference Partitioning
Here is a typical problem in designing partitioning schemes: not all the tables have the same columns on which you
need to partition. Suppose you are creating a sales system with two simple tables, sales and customers:

CREATE TABLE customers


(
cust_id NUMBER PRIMARY KEY,
cust_name VARCHAR2(200),
rating VARCHAR2(1) NOT NULL
)
PARTITION BY LIST (rating)
(
PARTITION pA VALUES ('A'),
PARTITION pB VALUES ('B')
);

The table sales is created as shown below. This is a child table of the customers table.

CREATE TABLE sales


(
sales_id NUMBER PRIMARY KEY,
cust_id NUMBER NOT NULL,
sales_amt NUMBER,
CONSTRAINT fk_sales_01
FOREIGN KEY (cust_id)
REFERENCES customers
);

Ideally, you would want to partition the table sales in the same manner as table customers: list partitioned on the
column rating. But there is a serious problem: table sales does not have a column called rating! So how do you
partition it on a non-existent column?

In Oracle Database 11g you can, using a new feature called Reference Partitioning. Here is an example to show how you can
apply it to the sales table:

CREATE TABLE sales


(
sales_id NUMBER PRIMARY KEY,
cust_id NUMBER NOT NULL,
sales_amt NUMBER,
CONSTRAINT fk_sales_01
FOREIGN KEY (cust_id)
REFERENCES customers
)
PARTITION BY REFERENCE (fk_sales_01);

This creates partitions identical to those in the parent table, customers. Note that there is no column called rating, yet
the table has been partitioned on that column. The clause partition by reference (fk_sales_01) has the name of the
foreign key in the partition definition. This instructs Oracle Database 11g to confirm the partitioning is done per the
scheme used in the parent table—in this case, customers. Note the NOT NULL constraint for column cust_id. This is
required for reference partitioning.

Interval Partitioning
<Document Control Number>

Range partitioning allows you to create partitions based on ranges of the values of the partition key column. Here is
an example of the range partitioned table:

CREATE TABLE sales6


(
sales_id NUMBER,
sales_dt DATE
) PARTITION BY RANGE (sales_dt)
(
PARTITION p0701 VALUES LESS THAN (TO_DATE('2007-02-01','yyyy-mm-dd')),
PARTITION p0702 VALUES LESS THAN (TO_DATE('2007-03-01','yyyy-mm-dd'))
);

Here you have defined partitions for January 2007 and February 2007 only, so what happens if a record is inserted
into the table that has the sales_dt in March 2007? The insert will fail with the following error:

ORA-14400: inserted partition key does not map to any partition

Obviously you need to add a partition for March 2007 before you can insert a record. But this is often easier said than
done. Often you can’t afford to create a lot of partitions beforehand and too few of them may result in this error.
Wouldn’t it be better if Oracle somehow automatically sensed the need for new partitions and then created them?
Oracle Database 11g does, with a feature called Interval Partitioning. Here, you don’t define partitions and their
boundaries but merely an interval that defines each partition’s boundaries. Here is the same example in interval
partitioning:

CREATE TABLE sales6


( sales_id NUMBER,
sales_dt DATE)
PARTITION BY RANGE (sales_dt)
INTERVAL (NUMTOYMINTERVAL(1,'MONTH'))
(
PARTITION p0701 VALUES LESS THAN (TO_DATE('2007-02-01','yyyy-mm-dd'))
);

Note the clause: interval followed by the interval. Here you have instructed Oracle to create intervals of one month
each. You have also created the initial partition named p0701, for the January 2007 data. Now, suppose you insert a
record with June 2007 data:

SQL> INSERT INTO sales6 VALUES (1,’01-jun-07’);


1 row created.

Oracle does not return an error; rather; it successfully executes the statement. So where does the record go to?
The partition p0701 can’t have the record and we haven’t defined a partition for June 2007. But at this time if you
check the partitions of the table:

SQL> SELECT partition_name, high_value


FROM user_tab_partitions WHERE table_name = 'SALES6';

PARTITION_NAME HIGH_VALUE
--------------- ----------------------------------------------------------------
P0701 TO_DATE(‘ 2007-02-01 00:00:00’, ‘SYYYY-MM-DD HH24:MI:SS’, ‘NLS_C
ALENDAR=GREGORIA
SYS_P41 TO_DATE(‘ 2007-07-01 00:00:00’, ‘SYYYY-MM-DD HH24:MI:SS’, ‘NLS_C
ALENDAR=GREGORIA
<Document Control Number>

DDL WAIT OPTION


 Oracle automatically places DML locks on tables when some of their rows are being modified by any
transaction. Lock type for these will be ‘TX’ in V$LOCK table).
 In addition to the DML locks, such a transaction will also hold a table level DDL lock, preventing other
transactions from altering or dropping the table until the ongoing DML transaction is completed. Lock type
for this will be ‘TM’ in V$LOCK table).
 Prior to the introduction of Oracle 11g, DDL lock request won’t wait for a DML lock. It will automatically fail
throwing the following error.

ALTER TABLE sales ADD (tax_code VARCHAR2 (10))


*

ERROR at line 1:

ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired.

 In oracle 11g,we can use ddl_lock_timeout parameter to specify the duration for which a DDL statement will
wait for DML lock

SQL> ALTER SESSION SET ddl_lock_timeout=100;

 Here it will not error out. Instead, it will wait for 100 seconds. In those 100 seconds, it continually re-tries the
DDL operation until it’s successful or the time expires, whichever comes first.
<Document Control Number>

Reference

1. Oracle Technology Network (OTN)


2. Oracle Metalink

Potrebbero piacerti anche