Sunteți pe pagina 1din 4

In-Database Archiving

Introduction and Configuring In-Database Archiving


Rather than deleting data, some applications have a concept of "mark for delete", so the data
remains present in the table, but is not visible to the application. This is usually achieved by
doing the following.

Add an extra column to the relevant tables that holds a flag to indicate the data is
deleted.
Add an extra predicate to every statement that checks the deleted status, like "WHERE
deleted = 'N'", to exclude the deleted rows from the SQL. The predicate can be hard
coded into the SQL, or applied dynamically using a security policy, like in Virtual Private
Database (VPD).

In-Database Archiving is a feature added to Oracle Database 12c to allow this type of "mark for
delete" functionality out-of-the-box, with fewer changes to the existing application code.
Note. You can actually set ORA_ARCHIVE_STATE column to any string value other than '0' to
archive the data, but the DBMS_ILM package uses the following constants.

ARCHIVE_STATE_ACTIVE='0'
ARCHIVE_STATE_ARCHIVED='1'

The hidden rows can be made visible to a session by setting ROW ARCHIVAL VISIBILITY to the
value ALL. Setting it back to ACTIVE makes the rows invisible again.
SQL> create user hr identified by oracle default tablespace users;
User created.
SQL> grant connect,resource to hr;
Grant succeeded.
SQL> alter user hr quota unlimited on users;
User altered.
SQL> connect hr/oracle@orcl
Connected.
SQL> create table emp_arch (
2 empno number(7),

In-Database Archiving

3 fullname varchaR2(100),
4 job varchar2(9),
5 mgr number(7)) row archival;
Table created.
SQL> insert into emp_arch values (&1,'&2','&3',4);
Enter value for 1: 100
Enter value for 2: JEAN
Enter value for 3: MGR
old 1: insert into emp_arch values (&1,'&2','&3',4)
new 1: insert into emp_arch values (100,'JEAN','MGR',4)
1 row created.
SQL> insert into emp_arch (EMPNO,FULLNAME,JOB,MGR) values (102,'TOM','ADMIN',100);
1 row created.
SQL> insert into emp_arch (EMPNO,FULLNAME,JOB,MGR) values (103,'JIM','WRITER',100);
1 row created.
SQL> insert into emp_arch (EMPNO,FULLNAME,JOB,MGR) values (101,'ADAM','CLERK',100);
1 row created.
SQL> commit;
Commit complete.
SQL> col fullname format a10
SQL> col ora_archive_state format a30
SQL> select ora_archive_state,fullname from emp_arch;
ORA_ARCHIVE_STATE
FULLNAME
------------------------------ ---------0
JEAN
0
TOM
0
JIM
0
ADAM
SQL> create table emp as select * from emp_arch;
Table created.
SQL> select ora_archive_state,fullname from emp;

In-Database Archiving

select ora_archive_state,fullname from emp


*
ERROR at line 1:
ORA-00904: "ORA_ARCHIVE_STATE": invalid identifier
SQL> update emp_arch set ora_archive_state=DBMS_ILM.ARCHIVESTATENAME(1) WHERE
empno IN (101,102);
2 rows updated.
SQL> commit;
Commit complete.
SQL> select ora_archive_state,fullname from emp_arch;
ORA_ARCHIVE_STATE
FULLNAME
------------------------------ -----------------------0
JEAN
0
JIM
SQL> alter session set row archival visibility = all;
Session altered.
SQL> select ora_archive_state,fullname from emp_arch;
ORA_ARCHIVE_STATE
FULLNAME
-------------------------------------------- ---------0
JEAN
1
TOM
0
JIM
1
ADAM
SQL> alter table emp row archival;
Table altered.
SQL> insert into emp select empno+100, fullname || '_SENIOR' , job, mgr from emp_arch;
4 rows created.
SQL> select ora_archive_state,fullname from emp_arch;
ORA_ARCHIVE_STATE
FULLNAME
-------------------------------------------- ---------0
JEAN

In-Database Archiving

1
0
1

TOM
JIM
ADAM

SQL> commit;
Commit complete.
SQL> select ora_archive_state,fullname from emp_arch;
ORA_ARCHIVE_STATE
FULLNAME
------------------------------ ----------------------0
JEAN
1
TOM
0
JIM
1
ADAM
SQL> col fullname format a30
SQL> select ora_archive_state,fullname from emp;
ORA_ARCHIVE_STATE
FULLNAME
------------------------------------------------ ---------0
JEAN
0
TOM
0
JIM
0
ADAM
0
JEAN_SENIOR
0
TOM_SENIOR
0
JIM_SENIOR
0
ADAM_SENIOR
8 rows selected.
SQL> alter table emp_arch no row archival;
Table altered.
SQL> select ora_archive_state,fullname from emp_arch;
select ora_archive_state,fullname from emp_arch
*
ERROR at line 1:
ORA-00904: "ORA_ARCHIVE_STATE": invalid identifier

In-Database Archiving

S-ar putea să vă placă și