Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Oracle Database 11g.pdf
Скачиваний:
78
Добавлен:
10.06.2015
Размер:
12.69 Mб
Скачать

Lab 9.1: Using the Recycle Bin 

737

SQL> select * from dupe_table; ID THE_DATE

---------- ---------

1 11-OCT-08

If you want to run this exercise again after the first successful run, you will need to perform these steps:

1.Shut down the auxiliary instance (now it’s a new database!).

2.Remove the spfile assigned to the auxiliary instance.

3.Mount the auxiliary instance with the Startup nomount command.

Lab 9.1: Using the Recycle Bin

This lab was created using Windows XP. However, it should also work using Unix (and in fact was tested using Linux). This lab shows you how to set up the Recycle Bin and use it to restore a dropped table. The overall steps are as follows: create a table, insert data into the table, enable the Recycle Bin, drop the table, restore the table from the Recycle Bin, and query the table to verify the contents.

1.Create the table.

SQL> create table recycle_test (x number, y varchar2(10)); Table created.

SQL>

2.Next, we’ll add some rows and commit the transaction.

SQL>

insert into recycle_test values (1, ‘row 1’); insert into recycle_test values (2, ‘row 2’); insert into recycle_test values (3, ‘row 3’); insert into recycle_test values (4, ‘row 4’); insert into recycle_test values (5, ‘row 5’); Commit;

SQL>

3.Then, enable the Recycle Bin for the session.

SQL> ALTER SESSION SET recyclebin = ON; Session altered.

738  Appendix A  n  Lab Exercises

4.Now drop the table.

SQL> drop table recycle_test; Table dropped.

5.Restore the table from the Recycle Bin.

SQL> flashback table recycle_test to before drop; Flashback complete.

6.Finally, query the table and verify the contents.

SQL> select * from recycle_test;

X

Y

----------

----------

1

row 1

2

row 2

3

row 3

4

row 4

5

row 5

Lab 9.2: Performing a More Complex

Flashback Query Analysis

This lab shows you how to perform a more complex analysis using the Flashback Query feature. We’ll create and populate a table, then step through adding rows and querying the table at various points in time to demonstrate the feature.

1.Create the table.

SQL> create table flashback_query_test (x number,

y varchar2(10), z date

); Table created.

2.Now insert five rows of data and commit.

SQL>

insert into flashback_query_test values (1, ‘row 1’, sysdate);

Lab 9.2: Performing a More Complex Flashback Query Analysis 

739

insert into flashback_query_test values (2, ‘row 2’, sysdate); insert into flashback_query_test values (3, ‘row 3’, sysdate); insert into flashback_query_test values (4, ‘row 4’, sysdate); insert into flashback_query_test values (5, ‘row 5’, sysdate); commit;

Commit complete.

3.Now query the table to view the results.

SQL> alter session set nls_date_format = ‘dd-mon-yy hh24:mi:ss’; Session altered.

SQL> select * from flashback_query_test;

X

Y

Z

 

----------

----------

--------------------

1

row 1

21-nov-2008 13:48:51

2

row 2

21-nov-2008 13:48:51

3

row 3

21-nov-2008

13:48:51

4

row 4

21-nov-2008

13:48:51

5

row 5

21-nov-2008

13:48:51

SQL>

 

 

 

4.Query the table, specifying a timestamp after the table was created but prior to inserting the first five rows:

SQL> select * from flashback_query_test as of timestamp(to_timestamp(

‘21-nov-2008 13:48:50’,’DD-MON-YYYY HH24:MI:SS’)); no rows selected

SQL>

5.Now insert five more rows, wait five minutes, then insert five more rows, then commit. The results should look similar to this:

insert into flashback_query_test values (6, ‘row 6’, sysdate); insert into flashback_query_test values (7, ‘row 7’, sysdate); insert into flashback_query_test values (8, ‘row 8’, sysdate); insert into flashback_query_test values (9, ‘row 9’, sysdate); insert into flashback_query_test values (10, ‘row 10’, sysdate); commit;

740  Appendix A  n  Lab Exercises

Wait here five minutes.

insert into flashback_query_test values (11, ‘row 11’, sysdate); insert into flashback_query_test values (12, ‘row 12’, sysdate); insert into flashback_query_test values (13, ‘row 13’, sysdate); insert into flashback_query_test values (14, ‘row 14’, sysdate); insert into flashback_query_test values (15, ‘row 15’, sysdate); commit;

SQL> select * from flashback_query_test;

X

Y

Z

----------

----------

--------------------

1

row 1

21-nov-2008 13:48:51

2

row 2

21-nov-2008 13:48:51

3

row 3

21-nov-2008 13:48:51

4

row 4

21-nov-2008 13:48:51

5

row 5

21-nov-2008 13:48:51

6

row 6

21-nov-2008 13:50:00

7

row 7

21-nov-2008 13:50:00

8

row 8

21-nov-2008 13:50:00

9

row 9

21-nov-2008 13:50:00

10

row 10

21-nov-2008 13:50:00

11

row 11

21-nov-2008 13:55:00

12

row 12

21-nov-2008 13:55:00

13

row 13

21-nov-2008 13:55:00

14

row 14

21-nov-2008 13:55:00

15

row 15

21-nov-2008 13:55:00

15 rows selected.

SQL>

6.Now that you have different discrete insert times, you can run queries that show the state of the table at various points in time. Run an AS OF query that will show only rows 1 through 10, based on the timestamp that was inserted.

SQL> select * from flashback_query_test as of timestamp(to_timestamp(

‘21-nov-2008 13:51:00’,’DD-MON-YYYY HH24:MI:SS’));

X Y Z

---------- ---------- --------------------

1 row 1 21-nov-2008 13:48:51

Lab 9.2: Performing a More Complex Flashback Query Analysis 

741

2 row 2 21-nov-2008 13:48:51

3 row 3 21-nov-2008 13:48:51

4 row 4 21-nov-2008 13:48:51

5 row 5 21-nov-2008 13:48:51

6 row 6 21-nov-2008 13:50:00

7 row 7 21-nov-2008 13:50:00

8 row 8 21-nov-2008 13:50:00

9 row 9 21-nov-2008 13:50:00

10 row 10 21-nov-2008 13:50:00

10 rows selected.

SQL>

7.Now insert five more rows, but don’t commit. Run a query showing the new rows in the table, and then an AS OF query with a timestamp following the insert that shows that the data is not committed and not available for Flashback Query.

SQL>

insert into flashback_query_test values (16, ‘row 16’, sysdate); insert into flashback_query_test values (17, ‘row 17’, sysdate); insert into flashback_query_test values (18, ‘row 18’, sysdate); insert into flashback_query_test values (19, ‘row 19’, sysdate); insert into flashback_query_test values (20, ‘row 20’, sysdate);

SQL> select * from flashback_query_test;

X

Y

Z

 

----------

----------

--------------------

1

row 1

21-nov-2008 13:48:51

2

row 2

21-nov-2008 13:48:51

3

row 3

21-nov-2008 13:48:51

4

row 4

21-nov-2008 13:48:51

5

row 5

21-nov-2008 13:48:51

6

row 6

21-nov-2008 13:50:00

7

row 7

21-nov-2008 13:50:00

8

row 8

21-nov-2008 13:50:00

9

row 9

21-nov-2008 13:50:00

10

row 10

21-nov-2008

13:50:00

11

row 11

21-nov-2008

13:55:00

12

row 12

21-nov-2008

13:55:00

742  Appendix A  n  Lab Exercises

13 row 13 21-nov-2008 13:55:00

14 row 14 21-nov-2008 13:55:00

15 row 15 21-nov-2008 13:55:00

16 row 16 21-nov-2008 14:00:00

17 row 17 21-nov-2008 14:00:00

18 row 18 21-nov-2008 14:00:00

19 row 19 21-nov-2008 14:00:00

20 row 20 21-nov-2008 14:00:00

20 rows selected.

SQL>

SQL> select * from flashback_query_test as of timestamp(to_timestamp(

‘21-nov-2008 14:05:00’,’DD-MON-YYYY HH24:MI:SS’));

X

Y

Z

----------

----------

--------------------

1

row 1

21-nov-2008 13:48:51

2

row 2

21-nov-2008 13:48:51

3

row 3

21-nov-2008 13:48:51

4

row 4

21-nov-2008 13:48:51

5

row 5

21-nov-2008 13:48:51

6

row 6

21-nov-2008 13:50:00

7

row 7

21-nov-2008 13:50:00

8

row 8

21-nov-2008 13:50:00

9

row 9

21-nov-2008 13:50:00

10

row 10

21-nov-2008 13:50:00

11

row 11

21-nov-2008 13:55:00

12

row 12

21-nov-2008 13:55:00

13

row 13

21-nov-2008 13:55:00

14

row 14

21-nov-2008 13:55:00

15

row 15

21-nov-2008 13:55:00

15 rows selected.

SQL>

Lab 9.2: Performing a More Complex Flashback Query Analysis 

743

8.Commit and then requery, noting that the AS OF timestamp must be after the commit for you to see the committed data using Flashback Query.

SQL> commit;

SQL> select sysdate from dual; SYSDATE

--------------------

21-nov-2008 14:21:41

SQL> select * from flashback_query_test as of timestamp(to_timestamp(

‘21-nov-2008 14:21:41’,’DD-MON-YYYY HH24:MI:SS’));

X

Y

Z

----------

----------

--------------------

1

row 1

21-nov-2008 13:48:51

2

row 2

21-nov-2008 13:48:51

3

row 3

21-nov-2008 13:48:51

4

row 4

21-nov-2008 13:48:51

5

row 5

21-nov-2008 13:48:51

6

row 6

21-nov-2008 13:50:00

7

row 7

21-nov-2008 13:50:00

8

row 8

21-nov-2008 13:50:00

9

row 9

21-nov-2008 13:50:00

10

row 10

21-nov-2008 13:50:00

11

row 11

21-nov-2008 13:55:00

12

row 12

21-nov-2008 13:55:00

13

row 13

21-nov-2008 13:55:00

14

row 14

21-nov-2008 13:55:00

15

row 15

21-nov-2008 13:55:00

16

row 16

21-nov-2008 14:00:00

17

row 17

21-nov-2008 14:00:00

18

row 18

21-nov-2008 14:00:00

19

row 19

21-nov-2008 14:00:00

20

row 20

21-nov-2008 14:00:00

20 rows selected. SQL>

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]