본문 바로가기

빅데이터과정/WORKSHOP 2

#29_140723_WSHOP2_FLASHBACK QUERY

728x90
# FLASHBACK QUERY






  • flashback drop
테이블을 drop 하게 되면 10g 버전부터는 휴지통에 해당 테이블이 들어가게 된다휴지통속에서 복원하는 명령어 flashback drop이다.

EMP 테이블을 drop 하는데 휴지통 속에 들어가도록 dop 하시오
SQL> drop table emp;
SQL> drop table emp purge;
purge 옵션은 휴지통을 거치지않고 삭제하는 기능

휴지통속에 있는 object를 확인하시오
SQL> show recyclebin

휴지통속에 있는 emp테이블 복원
SQL> flashback table emp to before drop;
SQL> select * from emp;

인덱스의 이름을 변경

SQL> alter index "BIN$/tJ1XjaMxL3gQAB/AQAv5Q==$0" rename to emp_sal;
SQL> select index_name from user_indexes;
INDEX_NAME
------------------------------
EMP_SAL




문제3. emp테이블에 sal에 인덱스를 생성하시오

SQL> create index emp_sal on emp(sal);
SQL> select index_name from user_indexes
    where table_name='EMP';
INDEX_NAME
------------------------------
EMP_SAL



문제4. 지금 휴지통속에서 emp 테이블을 복원했는데 그렇다면 emp 테이블에 걸려있던 인덱스는 어떻게 됐을까?

SQL> select index_name from user_indexes;
INDEX_NAME
------------------------------
BIN$/tJ1XjaMxL3gQAB/AQAv5Q==$0
복원되면 인덱스도 복원된다. 위의 인덱스는 emp_sal 이다



문제5. 사원테이블에 ename과 hiredate와 deptno에 각각 인덱스를 걸고 emp테이블을 drop하시오

SQL> create index emp_ename on emp(ename);
SQL> create index emp_hiredate on emp(hiredate);
SQL> create index emp_deptno on emp(deptno);
SQL> drop table emp;

  
SQL> select original_name, object_name, type from user_recyclebin;

ORIGINAL_NAME                    OBJECT_NAME                    TYPE
-------------------------------- ------------------------------ -------------------------
BIN$/iQHHIFOodPgQAB/AQB0/Q==$4   BIN$/jDbC9WXuSzgQAB/AQB28w==$5 INDEX
EMP                              BIN$/jDbC9WYuSzgQAB/AQB28w==$0 TABLE
DEPT                             BIN$/jDbC9WbuSzgQAB/AQB28w==$0 TABLE
EMP800                           BIN$/kdZYnclDiXgQAB/AQAXQA==$0 TABLE
PK_DEPT                          BIN$/jDbC9WauSzgQAB/AQB28w==$0 INDEX
EMP700                           BIN$/kwbB0IP1WzgQAB/AQAf/A==$0 TABLE
EMP800                           BIN$/likn3c/fcPgQAB/AQAkBQ==$0 TABLE
EMP800                           BIN$/likn3dBfcPgQAB/AQAkBQ==$0 TABLE
EMP700                           BIN$/r+QE+aokzHgQAB/AQAjEQ==$0 TABLE
EMP900                           BIN$/r+QE+apkzHgQAB/AQAjEQ==$0 TABLE
EMP_SAL                          BIN$/tJ1XjaPxL3gQAB/AQAv5Q==$0 INDEX
EMP_ENAME                        BIN$/tJ1XjaQxL3gQAB/AQAv5Q==$0 INDEX
EMP_HIREDATE                     BIN$/tJ1XjaRxL3gQAB/AQAv5Q==$0 INDEX
EMP_DEPTNO                       BIN$/tJ1XjaSxL3gQAB/AQAv5Q==$0 INDEX
EMP                              BIN$/tJ1XjaTxL3gQAB/AQAv5Q==$0 TABLE



문제6. 휴지통속에 있는 이름을 원래 인덱스 이름으로 변경하는 스크립트를 머리를 써서 생성하시오

SQL>  SELECT 'alter index "' || object_name || '" rename to ' || original_name || ';'
 FROM user_recyclebin
 WHERE TYPE = 'INDEX';
'ALTERINDEX"'||OBJECT_NAME||'"RENAMETO'||ORIGINAL_NAME||';'
-----------------------------------------------------------------------
alter index "BIN$/jDbC9WXuSzgQAB/AQB28w==$5" rename to BIN$/iQHHIFOodPgQAB/AQB0/Q==$4;
alter index "BIN$/jDbC9WauSzgQAB/AQB28w==$0" rename to PK_DEPT;
alter index "BIN$/tJ1XjaPxL3gQAB/AQAv5Q==$0" rename to EMP_SAL;
alter index "BIN$/tJ1XjaQxL3gQAB/AQAv5Q==$0" rename to EMP_ENAME;
alter index "BIN$/tJ1XjaRxL3gQAB/AQAv5Q==$0" rename to EMP_HIREDATE;
alter index "BIN$/tJ1XjaSxL3gQAB/AQAv5Q==$0" rename to EMP_DEPTNO;
SQL> flashback table emp to before drop;
SQL> alter index "BIN$/tJ1XjaPxL3gQAB/AQAv5Q==$0" rename to EMP_SAL;
alter index "BIN$/tJ1XjaQxL3gQAB/AQAv5Q==$0" rename to EMP_ENAME;
alter index "BIN$/tJ1XjaRxL3gQAB/AQAv5Q==$0" rename to EMP_HIREDATE;
alter index "BIN$/tJ1XjaSxL3gQAB/AQAv5Q==$0" rename to EMP_DEPTNO;
Index altered.
SQL> select index_name from user_indexes;
INDEX_NAME
------------------------------
EMP_SAL
EMP_ENAME
EMP_HIREDATE
EMP_DEPTNO








  • flashback query

특정 테이블의 과거의 data의 상태를 확인하고 싶을 때 사용
-  SQL> select * from emp
               as of timestamp to_timestamp('23-JUL-14 10.14.42','DD-MON-RR                                                         
               HH24:MI:SS');


SQL> select systimestamp from dual;
SYSTIMESTAMP
---------------------------------------------------------------------------
23-JUL-14 10.14.42.034812 AM +09:00
SQL> select current_scn from v$database;
grant
CURRENT_SCN
-----------
    1311894
SQL> delete from emp;
SQL> commit;
SQL> select * from emp
as of timestamp to_timestamp('23-JUL-14 10.14.42','DD-MON-RR HH24:MI:SS');
     EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
      7839 KING       PRESIDENT            1981/11/17:00:00:00       5000                    10
      7698 BLAKE      MANAGER         7839 1981/05/01:00:00:00       2850                    30
      7782 CLARK      MANAGER         7839 1981/05/09:00:00:00       2450                    10
      7566 JONES      MANAGER         7839 1981/04/01:00:00:00       2975                    20
      7654 MARTIN     SALESMAN        7698 1981/09/10:00:00:00       1250       1400         30
      7499 ALLEN      SALESMAN        7698 1981/02/11:00:00:00       1600        300         30
      7844 TURNER     SALESMAN        7698 1981/08/21:00:00:00       1500          0         30
      7900 JAMES      CLERK           7698 1981/12/11:00:00:00        950                    30
      7521 WARD       SALESMAN        7698 1981/02/23:00:00:00       1250        500         30
      7902 FORD       ANALYST         7566 1981/12/11:00:00:00       3000                    20
      7369 SMITH      CLERK           7902 1980/12/09:00:00:00        800                    20
      7788 SCOTT      ANALYST         7566 1982/12/22:00:00:00       3000                    20
      7876 ADAMS      CLERK           7788 1983/01/15:00:00:00       1100                    20
      7934 MILLER     CLERK           7782 1982/01/11:00:00:00       1300                    10



문제7. 위의 쿼리를 다시 수정하는데 시간이 아니라 1분전 또는 5분전의 emp 테이블의 상태를 보는 것으로 쿼리를 수정하시오

select * from emp
as of timestamp (systimestamp - interval '5' minute);



문제8. 위의 쿼리를 다시 수정해서 5분전에 EMP 테이블의 이름이 KING인 사원의 이름과 월급을 출력하시오

select * from emp
as of timestamp (systimestamp - interval '10' minute)
WHERE ename='KING';






  • flashback version query

특정 테이블이 과거에 어떻게 변경 되어왔는지 이력정보를 보는 기능

사전작업
SCN 권한 주기
SQL> connect / as sysdba
SQ> grant select on v_$database to scott;
SQL> connect scott/tiger
SQL> select current_scn from v$database;

문제9. KING의 월급을 8000으로 변경하고 commit하고 KING의 커미션을 5000으로 변경하고commit하고 KING의 부서번호를 40번으로 변경하고 commit하고 KING의 데이터를 지우고commit 하시오

SQL> select current_scn from v$database;
CURRENT_SCN
-----------
    1312414
SQL> update emp
    set sal=8000
    where ename='KING';
SQL> commit;
SQL> update emp
    set comm=5000
    where ename='KING';
SQL> commit;
SQL> update emp
    set deptno=40
    where ename='KING';
SQL> commit;
SQL> delete from emp
    where ename='KING';
SQL> commit;
SQL> select versions_startscn, versions_endscn, versions_operation,
    ename, sal, comm, deptno
    from emp
    versions between scn minvalue and maxvalue
    where ename='KING';
VERSIONS_STARTSCN VERSIONS_ENDSCN V ENAME             SAL       COMM     DEPTNO
----------------- --------------- - ---------- ---------- ---------- ----------
          1312484                 D KING             8000       5000         40
          1312468         1312484 U KING             8000       5000         40
          1312457         1312468 U KING             8000       5000         10
          1312447         1312457 U KING             8000                    10
          1312194         1312447 I KING             5000                    10


문제10. KING의 데이터를 업데이트하기 전 상태로 되돌리시오

SQL> flashback table emp to scn 1312414;
SQL> select * from emp where ename='KING';
     EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
      7839 KING       PRESIDENT            1981/11/17:00:00:00       5000

DBA롤을 부여했는데 권한 문제가 발생하는 이유와 해결방법
내가 부여받은 롤이 활성화가 안돼있기 때문이다.
SQL> select * from session_roles;
 # 현재 접속한 내 세션에서 활성화 된 룰을 출력





  • Flashback Transaction Query

특정 테이블에 대해서 그 동안 수행했던 DML 반대로 수행하는 DML문을 보는 쿼리

1.     SQL> select supplemental_log_data_min from v$database;
transaction query를 보기위한 로그 데이터가 저장될 수 있도록 설정되어 있는지 확인

2.     SQL> alter database add supplemental log data;
transaction query를 보기위한 로그 데이터가 저장될 수 있도록 설정

3.     SQL> select supplemental_log_data_min from v$database;
transaction query를 보기위한 로그 데이터가 저장될 수 있도록 설정되어 있는지 확인

4.     SQL> select current_scn from v$database



문제11. KING의 월급을 8000으로 변경하고 commit하고 KING의 커미션을 5000으로 변경하고 commit하고 KING의 부서번호를 40번으로 변경하고 commit하고 KING의 데이터를 지우고 commit 하시오

SQL> alter database add supplemental log data;
SQL> select current_scn from v$database;
CURRENT_SCN
-----------
    1316039
SQL> update emp
    set sal=8000
    where ename='KING';
SQL> commit;
SQL> update emp
    set comm=5000
    where ename='KING';
SQL> commit;
SQL> update emp
    set deptno=40
    where ename='KING';
SQL> commit;
SQL> delete from emp
    where ename='KING';
SQL> commit;
SQL> select versions_startscn, versions_endscn, versions_operation,
    ename, sal, comm, deptno
    from emp
    versions between scn minvalue and maxvalue
    where ename='KING';
VERSIONS_STARTSCN VERSIONS_ENDSCN V ENAME             SAL       COMM     DEPTNO
----------------- --------------- - ---------- ---------- ---------- ----------
          1316052                 D SCOTT            8000       5000         40
          1316049         1316052 U SCOTT            8000       5000         40
          1316046         1316049 U SCOTT            8000       5000         20
          1316043         1316046 U SCOTT            8000                    20
          1316029         1316043 I SCOTT            3000                    20
SQL> select undo_sql from flashback_transaction_query
where table_name='EMP'
and commit_scn between 1314173 and 1313877
order by start_timestamp desc;
commit_scn between current_scn and VERSIONS_STARTSCN
versions_startscn의 delete 된 부분의 번호를 넣어줘야 한다

update "SCOTT"."EMP" set "SAL" = '3000' where ROWID = 'AAASQNAAGAAAAEcAAW';
insert into "SCOTT"."EMP"("EMPNO","ENAME","JOB","MGR","HIREDATE","SAL","COMM","DEPTNO") values ('7788','SCOTT','ANALYST','7566',TO_DATE('82-12-22', 'RR-MM-DD'),'8000','5000','40');
update "SCOTT"."EMP" set "DEPTNO" = '20' where ROWID = 'AAASQNAAGAAAAEcAAW';
update "SCOTT"."EMP" set "COMM" = NULL where ROWID = 'AAASQNAAGAAAAEcAAW';
insert 하는순간 rowid의 값이 변경되서 안됨