Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Approvals:
<Document Control Number>
Document Control
Change Record
Reviewers
Name Position
Distribution
Contents
Document Control...................................................................................................................ii
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.
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
For example, if you have a configuration file that you want to keep safe from any changes by any users.
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.
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;
/
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;
/
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:
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 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'))
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>
11g
TEST_FUNC(10,20)
----------------
30
1 row selected.
TEST_FUNC(10,P_VALUE_2=>20)
---------------------------
30
1 row selected.
TEST_FUNC(P_VALUE_1=>10,P_VALUE_2=>20)
--------------------------------------
30
1 row selected.
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:
The table sales is created as shown below. This is a child table of the customers table.
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:
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:
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:
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:
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:
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:
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>
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
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