9i新特性之——在線表格重定義研究3
2024-07-21 02:39:59
供稿:網友
具體過程 1、我們創建一個單獨的測試用戶用來測試整個過程 SQL> create user mytest identified by mytest;User createdSQL> grant connect,resource to mytest;Grant sUCceeded SQL> connect mytest/mytest;Connected to Oracle9i EnterPRise Edition Release 9.2.0.4.0Connected as mytest 2、首先,我們創建一個原表,假定是我們需要在線重新定義的表,由于工作需要,我們需要把該表(不是分區表)重新定義為一個分區表,而且不能影響應用程序的運行。 SQL> create table test(a int,b int) tablespace users;Table created
模擬一個原表 declare i integer;beginfor i in 1..100 loopinsert into test values(i,100-i);end loop;commit;end; 插入100條模擬數據 create or replace trigger tr_test before insert or update or delete on test for each rowdeclare PRAGMA AUTONOMOUS_TRANSACTION; begin update audit_test set c=c+1; commit; end; 在上面創建一個觸發器,模擬原表可能會有的觸發器,這個觸發器的作用就是假如有任何DML操作,將在audit_test中增加1。
Audit_test現在的數據是:SQL> select c from audit_test; 100 檢查觸發器是否工作正常SQL> insert into test values(101,0);1 row insertedSQL> commit; SQL> select c from audit_test; 101可以看到,觸發器工作正常。 3、檢查該表是否能重定義 SQL> exec dbms_redefinition.can_redef_table('MYTEST', 'TEST'); begin dbms_redefinition.can_redef_table('MYTEST', 'TEST'); end; ORA-12089: cannot online redefine table "MYTEST"."TEST" with no primary key
ORA-06512: at "SYS.DBMS_REDEFINITION", line 8ORA-06512: at "SYS.DBMS_REDEFINITION", line 247ORA-06512: at line 1 可以看到,假如重新定義,需要主鍵,所以我們增加該表的主鍵 我們定義主鍵 SQL> alter table test add constraint pk_test_id primary key(a);Table altered SQL> exec dbms_redefinition.can_redef_table('MYTEST', 'TEST');PL/SQL procedure successfully completed 現在發現,我們可以定義該表了