3. Edition Based Redefinition (EBR)?
• New Feature on 11gR2
• Upgrade an application while it is in use
• Edition object types
– SYNONYM
– VIEW
– All PL/SQL object types:
(FUNCTION,LIBRARY,PACKAGE and PACKAGE
BODY,PROCEDURE,TRIGGER,TYPE and TYPE BODY)
http://surachartopun.com
4. Three New Object Types
• Edition
– Replacing Edition object types (PL/SQL, synonyms,
and views)
• Editioning View
– Table is not an edition type. If you change the
structure of one or more tables.
• Crossedtion trigger
– Other users must be able to change data in the
tables while you are changing their structure
– If the pre- and post-upgrade applications will be in
ordinary use at the same time
http://surachartopun.com
6. What is an Edition?
• A nonschema object type, uniquely identified,
therefore, by just its name.
• Be able to check in DBA_OBJECTS.
select * from dba_objects where object_type=
’EDITION’;
• Every database from 11.2 onwards - Least one
edition.
• The default edition name is ORA$BASE
http://surachartopun.com
7. • A new edition must be created as the child of an
existing one.
create edition version2 as child of ora$base;
• An edition may have no more than one child.
ORA$BASE <- VERSION2 <- VERSION3 <- VERSION4
create edition version2 as child of ora$base;
create edition version3 as child of version2;
create edition version4 as child of version3;
http://surachartopun.com
9. • A new database property, DEFAULT_EDITION.
alter database default edition = Some_Edition
SQL> select PROPERTY_NAME, PROPERTY_VALUE
from database_properties
where PROPERTY_NAME='DEFAULT_EDITION';
PROPERTY_NAME PROPERTY_VALUE
------------------------------ --------------------
DEFAULT_EDITION ORA$BASE
http://surachartopun.com
10. • A new alter session command allows the edition that
a session is using to be changed.
alter session set edition = Some_Edition
SQL> alter session set edition=version3;
Session altered.
SQL> SELECT SYS_CONTEXT
('userenv','current_edition_name') ce FROM DUAL;
CE
----------
VERSION3
http://surachartopun.com
11. Edition Privileges
• System Privileges
– CREATE ANY EDITION
– ALTER ANY EDITION
– DROP ANY EDITION
• Object Privileges - USE (not granted by default)
GRANT USE ON <edition_name> TO <user_name>;
• Roles
system privileges are granted to the DBA role (only).
• Enable Editioning
ALTER USER <user_name> ENABLE EDITIONS;
http://surachartopun.com
14. SQL> SELECT * FROM DBA_EDITIONS;
EDITION_NAME PARENT_EDITION_NAME USA
------------------------------ ------------------------------ ---
ORA$BASE YES
SQL> CREATE EDITION VERSION2 AS CHILD OF ORA$BASE;
Edition created.
SQL> SELECT * FROM DBA_EDITIONS;
EDITION_NAME PARENT_EDITION_NAME USA
------------------------------ ------------------------------ ---
ORA$BASE YES
VERSION2 ORA$BASE YES
SQL> ALTER USER DEMO ENABLE EDITIONS;
SQL> GRANT USE ON EDITION VERSION2 TO DEMO;
http://surachartopun.com
15. SQL> connect demo/demo
Connected.
SQL> SELECT SYS_CONTEXT ('userenv','current_edition_name') ce FROM DUAL;
CE
--------------------------------------------------------------------------------
ORA$BASE
SQL> create or replace procedure my_procedure
as
Begin
dbms_output.put_line ( 'I am version 1.0' );
end;
/
http://surachartopun.com
16. SQL> ALTER SESSION SET EDITION=VERSION2;
Session altered.
SQL> create or replace procedure my_procedure
as
Begin
dbms_output.put_line ( 'I am version 2.0' );
end;
/
SQL> SELECT SYS_CONTEXT ('userenv','current_edition_name') ce FROM
DUAL;
CE
--------------------------------------------------------------------------------
ORA$BASE
http://surachartopun.com
17. SQL> exec my_procedure;
I am version 1.0
SQL> ALTER SESSION SET EDITION=VERSION2;
Session altered.
SQL> SELECT SYS_CONTEXT ('userenv','current_edition_name') ce FROM
DUAL;
CE
--------------------------------------------------------------------------------
VERSION2
SQL> exec my_procedure;
I am version 2.0
http://surachartopun.com
18. What is an Editioning View?
• Editioning view selects a subset of the columns from
a single base table.
• Use the SQL statement CREATE VIEW with the OR
REPLACE clause and the keyword EDITIONING.
CREATE OR REPLACE EDITIONING VIEW …
• Partition-Extended Editioning View Names
http://surachartopun.com
19. Editioning View Related Data
Dictionary Views
• DBA_EDITIONING_VIEW_COLS
• DBA_EDITIONING_VIEW_COLS_AE
• DBA_EDITIONING_VIEWS
• DBA_EDITIONING_VIEWS_AE
• DBA_ERRORS_AE (editioning_name)
• DBA_OBJECTS_AE (editioning_name)
• DBA_VIEWS (editioning_view)
• AE = All Editions
http://surachartopun.com
20. SQL> CREATE EDITIONING VIEW ed01_tab AS SELECT name, tel_num FROM
ed01_tab_tmp;
SQL> select * from ED01_TAB partition (par_1);
NAME TEL_NUM
------------------------------ ----------
TEST1 1234567
TEST5 1234567
SQL> CREATE TRIGGER ed01_tab_tri
BEFORE INSERT ON ed01_tab FOR EACH ROW
BEGIN
null;
END;
/
Trigger created.
http://surachartopun.com
21. What is a Crossedition Trigger?
• A new, and special type of trigger specific to
editioning
• Can only be created on a table (not on an editioning
view)
• Propagates transactions between editions
• Two types
– FORWARD
– REVERSE
http://surachartopun.com
22. • Crossedition triggers are always created in the child
• Crossedition triggers are temporary—drop them
after you have made the restructured tables
available to all users.
http://surachartopun.com
23. Crossedition Trigger Types
• Forward Crossedition Triggers
– Only fired by code running in the parent edition
– Transforms from the old representation to the
new
• Reverse Crossedition Triggers
– Only fired by code running in the child edition
– Transforms from the new representation to the
old
http://surachartopun.com
24. Crossedition Trigger Related Data
Dictionary Views
• DBA_TRIGGERS
• DBA_TRIGGER_ORDERING
• DBA_ERRORS_AE (editioning_name)
• DBA_OBJECTS_AE (editioning_name)
• AE = All Editions
http://surachartopun.com
25. CREATE TABLE PERSONS (ID NUMBER(10) NOT NULL, FIRST_NAME VARCHAR2(100)
NOT NULL, LAST_NAME VARCHAR2(100) NOT NULL,EMAIL VARCHAR2(100) NOT
NULL)
/
RENAME persons TO persons_tab;
CREATE EDITIONING VIEW persons AS
SELECT id, first_name AS firstname, last_name AS lastname, email FROM persons_tab;
create sequence persons_seq;
http://surachartopun.com
26. CREATE OR REPLACE TRIGGER persons_bi_trg
BEFORE INSERT ON persons FOR EACH ROW
BEGIN
:new.id := persons_seq.nextval;
END;
/
insert into persons values (null,'Somchai','A.','somchai@test.com');
commit;
ALTER TABLE persons_tab ADD (email_recipient VARCHAR2(100),email_domain
VARCHAR2(100));
SQL> select * from persons;
ID FIRSTNAME LASTNAME EMAIL
---------- --------------- --------------- --------------------
1 Somchai A. somchai@test.com
http://surachartopun.com
27. alter session set edition=VERSION2;
CREATE TRIGGER persons_fc_trg
BEFORE INSERT OR UPDATE ON persons_tab FOR EACH ROW
FORWARD CROSSEDITION
DISABLE
BEGIN
:new.email_recipient :=
regexp_substr(:new.email,'(.*)@',1,1,NULL,1);
:new.email_domain :=
regexp_substr(:new.email,'@(.*)',1,1,NULL,1);
END;
/
http://surachartopun.com
28. column CE for a10
SELECT SYS_CONTEXT ('userenv','current_edition_name') ce FROM DUAL;
CREATE OR REPLACE EDITIONING VIEW persons AS
SELECT id, first_name AS firstname, last_name AS
lastname,email_recipient,email_domain FROM persons_tab;
SQL> select * from persons;
ID FIRSTNAME LASTNAME EMAIL_RECIPIENT EMAIL_DOMAIN
---------- --------------- --------------- -------------------- --------------------
1 Somchai A.
http://surachartopun.com
29. ALTER TRIGGER persons_fc_trg ENABLE;
DECLARE
c INTEGER;
r INTEGER;
BEGIN
c := dbms_sql.open_cursor;
dbms_sql.parse(
c => c,
statement => 'UPDATE persons SET email_domain = email_domain',language_flag =>
dbms_sql.native,
apply_crossedition_trigger => 'persons_fc_trg');
r := dbms_sql.execute(c);
dbms_sql.close_cursor(c);
COMMIT;
END;
/
http://surachartopun.com
30. SQL> select * from persons;
ID FIRSTNAME LASTNAME EMAIL_RECIPIENT EMAIL_DOMAIN
---------- --------------- --------------- -------------------- --------------------
1 Somchai A. somchai test.com
---TEST FORWARD ---
alter session set edition=ORA$BASE;
update persons set email='surachart@gmail.com' where id=1;
commit;
SQL> select * from persons;
ID FIRSTNAME LASTNAME EMAIL
---------- --------------- --------------- -------------------------
1 Somchai A. surachart@gmail.com
http://surachartopun.com
31. alter session set edition=version2;
SQL> select * from persons;
ID FIRSTNAME LASTNAME EMAIL_RECIPIENT EMAIL_DOMAIN
---------- --------------- --------------- -------------------- --------------------
1 Somchai A. surachart gmail.com
http://surachartopun.com
32. ---TEST REVERSE---
alter session set edition=version2;
CREATE TRIGGER persons_rc_trg
BEFORE INSERT OR UPDATE ON persons_tab FOR EACH ROW
REVERSE CROSSEDITION
DISABLE
BEGIN
:new.email := :new.email_recipient || '@' || :new.email_domain;
END;
/
alter trigger persons_rc_trg enable;
insert into persons values (null,'Surachart','O.','surachart',‘test.com');
commit;
http://surachartopun.com
33. SQL> select * from persons;
ID FIRSTNAME LASTNAME EMAIL_RECIPIENT EMAIL_DOMAIN
---------- --------------- --------------- -------------------- --------------------
1 Somchai A. surachart gmail.com
2 Surachart O. surachart test.com
alter session set edition=ORA$BASE;
SQL> select * from persons;
ID FIRSTNAME LASTNAME EMAIL
---------- --------------- --------------- ------------------------------
1 Somchai A. surachart@gmail.com
2 Surachart O. surachart@test.com
http://surachartopun.com