动手实验 Oracle 在线重定义普通表为分区表

#表的在线重定义
#普通的堆表定义为分区表
#思路-----现有emp1表,需要定义为分区表.需要一个临时分区表emp1_temp.把emp1_temp定义为分区表之后,将emp1 和 emp1_temp进行互换。
#完成emp1定义为分区表

#构造emp1
SYS@PROD>create table scott.emp1 as select * from scott.emp;

Table created.

#建立约束信息,验证定义过程约束是否迁移
SYS@PROD>alter table scott.emp1 add constraint pk_emp1 primary key(empno);

Table altered.

#表的联机重定义要求表是自包含的,所谓自包含就是该表的对象不依赖与其他表空间
#比如表的索引是在该表所在的表空间 a,而不是在表空间b(个人理解)
SYS@PROD>BEGIN
  2    DBMS_REDEFINITION.CAN_REDEF_TABLE('scott','emp1');
  3  END;
  4  /

PL/SQL procedure successfully completed.

SYS@PROD>



#建立临时分区表emp1_temp
SYS@PROD>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.



#启动表的联机重定义
SYS@PROD>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



SYS@PROD>select count(*) from scott.emp1_temp;

  COUNT(*)
----------
        14

#查看临时分区表数据        
SYS@PROD> 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
      

SYS@PROD>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
      
      
      
#将索引 约束信息迁移到临时分区表
SYS@PROD>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 互换
SYS@PROD> EXECUTE dbms_redefinition.finish_redef_table('scott','emp1','emp1_temp');

PL/SQL procedure successfully completed.



#查看结果
SCOTT@PROD>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



SCOTT@PROD>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.


SCOTT@PROD>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

猜你喜欢

转载自blog.csdn.net/lv941002/article/details/82913655