動手實驗 Oracle 線上重定義普通表為分割槽表
阿新 • • 發佈:2018-12-12
#表的線上重定義
#普通的堆表定義為分割槽表
#思路-----現有emp1表,需要定義為分割槽表.需要一個臨時分割槽表emp1_temp.把emp1_temp定義為分割槽表之後,將emp1 和 emp1_temp進行互換。
#完成emp1定義為分割槽表
#構造emp1
[email protected]>create table scott.emp1 as select * from scott.emp;
Table created.
#建立約束資訊,驗證定義過程約束是否遷移
[email protected]>alter table scott.emp1 add constraint pk_emp1 primary key(empno);
Table altered.
#表的聯機重定義要求表是自包含的,所謂自包含就是該表的物件不依賴與其他表空間
#比如表的索引是在該表所在的表空間 a,而不是在表空間b(個人理解)
[email protected]>BEGIN
2 DBMS_REDEFINITION.CAN_REDEF_TABLE('scott','emp1');
3 END;
4 /
PL/SQL procedure successfully completed.
[email protected]>
#建立臨時分割槽表emp1_temp
[email protected]>CREATE TABLE scott.emp1_temp
2 (empno number(4) not null,
3 ename varchar2(10),
4 job varchar2(9),
5 mgr number(4),
6 hiredate date,
7 sal number(7,2),
8 deptno number(2))
9 PARTITION BY RANGE(sal)
10 (PARTITION sal_low VALUES LESS THAN(2500),
11 PARTITION sal_high VALUES LESS THAN (maxvalue));
Table created.
#啟動表的聯機重定義
[email protected]>BEGIN
2 dbms_redefinition.start_redef_table('scott','emp1','emp1_temp',
3 'empno empno,
4 ename ename,
5 job job,
6 mgr mgr,
7 hiredate hiredate,
8 sal sal,
9 deptno deptno');
10 END;
11 /
PL/SQL procedure successfully completed
[email protected]>select count(*) from scott.emp1_temp;
COUNT(*)
----------
14
#檢視臨時分割槽表資料
[email protected]> select * from scott.emp1_temp partition(sal_low);
EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO
---------- ---------- --------- ---------- ------------ ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7934 MILLER CLERK 7782 23-JAN-82 1300 10
[email protected]>select * from scott.emp1_temp partition(sal_high);
EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO
---------- ---------- --------- ---------- ------------ ---------- ----------
7566 JONES MANAGER 7839 02-APR-81 2975 20
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7902 FORD ANALYST 7566 03-DEC-81 3000 20
#將索引 約束資訊遷移到臨時分割槽表
[email protected]>DECLARE
2 log_error int;
3 BEGIN
4 DBMS_REDEFINITION.COPY_TABLE_DEPENDENTS(
5 uname =>'SCOTT',
6 orig_table =>'EMP1',
7 int_table =>'EMP1_TEMP',
8 num_errors =>log_error
9 );
10 END;
11 /
PL/SQL procedure successfully completed
#emp1 和 emp1_temp 互換
[email protected]> EXECUTE dbms_redefinition.finish_redef_table('scott','emp1','emp1_temp');
PL/SQL procedure successfully completed.
#檢視結果
[email protected]>select table_name,partition_name,high_value from user_tab_partitions where table_name='EMP1';
TABLE_NAME PARTITION_NAME HIGH_VALUE
------------------------------ ------------------------------ ----------
EMP1 SAL_HIGH MAXVALUE
EMP1 SAL_LOW 2500
[email protected]>select * from emp1 partition(sal_low);
EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO
---------- ---------- --------- ---------- ------------ ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7934 MILLER CLERK 7782 23-JAN-82 1300 10
9 rows selected.
[email protected]>select * from emp1 partition(sal_high);
EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO
---------- ---------- --------- ---------- ------------ ---------- ----------
7566 JONES MANAGER 7839 02-APR-81 2975 20
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7902 FORD ANALYST 7566 03-DEC-81 3000 20