本篇文章给大家分享的是有关Oracle中怎么利用联机重定义给表增加新列与分区,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
1.用要执行联机重定义操作的用户登录数据库
SQL> conn pm/pm@jypdb Connected.
2.验证表emp_redef是否可以执行联机重定义。在这种情况下,可以使用主键或伪主键来来进行验证。
SQL> exec dbms_redefinition.can_redef_table(uname=>'HR',tname=>'EMP_REDEF',options_flag=>dbms_redefinition.cons_use_pk); PL/SQL procedure successfully completed.
3.创建一个中间表hr.int_emp_redef
SQL> create table hr.int_emp_redef ( employee_id NUMBER(6) not null, first_name VARCHAR2(20), last_name VARCHAR2(25) not null, job_id VARCHAR2(10) not null, department_id NUMBER(4) not null, mgr NUMBER(5), hiredate DATE DEFAULT(sysdate), sal NUMBER(7,2), bonus NUMBER(7,2) DEFAULT(0) ) partition by range(employee_id) ( partition emp200 values less than(200) tablespace users, partition emp400 values less than(400) tablespace users ); Table created
4.开始重定义操作
SQL> begin dbms_redefinition.start_redef_table( uname => 'hr', orig_table => 'emp_redef', int_table => 'int_emp_redef', col_mapping => 'employee_id employee_id, first_name first_name,last_name last_name, job_id job_id, department_id+10 department_id,0 bonus', options_flag => DBMS_REDEFINITION.CONS_USE_PK); end; / PL/SQL procedure successfully completed.
5.复制依赖对象(自动对表hr.int_emp_redef创建任何触发器,索引,物化视图日志,授权与约束)
SQL> declare num_errors pls_integer; begin dbms_redefinition.copy_table_dependents( uname => 'hr', orig_table => 'emp_redef', int_table => 'int_emp_redef', copy_indexes => DBMS_REDEFINITION.CONS_ORIG_PARAMS, copy_triggers => TRUE, copy_constraints => TRUE, copy_privileges => TRUE, ignore_errors => TRUE, num_errors => num_errors); end; / PL/SQL procedure successfully completed.
注意,在调用这个过程时ignore_errors参数需要设置为TRUE。原因是中间表创建了主键约束,并且当执行copye_table_dependents过程来试图从原始表复制主键约束与索引时会发生错误。可以忽略这些错误,但必须执行下一步操作中的查询来查看是否还存在其它错误。
6.查询dba_redefinition_errors视图来查看错误信息
SQL> set long 8000 SQL> set pages 8000 SQL> column object_name heading 'object name' format a20 SQL> column base_table_name heading 'base table name' format a10 SQL> column ddl_txt heading 'ddl that caused error' format a40 SQL> select object_name, base_table_name, ddl_txt from dba_redefinition_errors; object name base table ddl that caused error -------------------- ---------- ---------------------------------------- SYS_C0023200 EMP_REDEF ALTER TABLE "HR"."INT_EMP_REDEF" MODIFY ("LAST_NAME" CONSTRAINT "TMP$$_SYS_C0023 2000" NOT NULL ENABLE NOVALIDATE) SYS_C0023201 EMP_REDEF ALTER TABLE "HR"."INT_EMP_REDEF" MODIFY ("JOB_ID" CONSTRAINT "TMP$$_SYS_C0023201 0" NOT NULL ENABLE NOVALIDATE) 2 rows selected.
上面的错误信息是说中间表的last_name与job_id列为not null,而原因表为null,这种错误可以忽略。
7.同步中间表hr.int_emp_redef
SQL> begin dbms_redefinition.sync_interim_table( uname => 'hr', orig_table => 'emp_redef', int_table => 'int_emp_redef'); end; / PL/SQL procedure successfully completed.
8.完成重定义操作
SQL> begin dbms_redefinition.finish_redef_table( uname => 'hr', orig_table => 'emp_redef', int_table => 'int_emp_redef'); end; / PL/SQL procedure successfully completed.
表hr.emp_redef只会以排他模式被锁定很短的时间来结束重定义操作。在操作完成后,表hr.emp_redef将使用hr.int_emp_redef表的所有属性来重定义。
SQL> desc hr.emp_redef Name Type Nullable Default Comments ------------- ------------ -------- --------- -------- EMPLOYEE_ID NUMBER(6) FIRST_NAME VARCHAR2(20) Y LAST_NAME VARCHAR2(25) JOB_ID VARCHAR2(10) DEPARTMENT_ID NUMBER(4) MGR NUMBER(5) Y HIREDATE DATE Y (sysdate) SAL NUMBER(7,2) Y BONUS NUMBER(7,2) Y (0) SQL> select dbms_metadata.get_ddl(object_type =>'TABLE',name =>'EMP_REDEF',schema => 'HR') from dual; DBMS_METADATA.GET_DDL(OBJECT_TYPE=>'TABLE',NAME=>'EMP_REDEF',SCHEMA=>'HR') -------------------------------------------------------------------------------- CREATE TABLE "HR"."EMP_REDEF" ( "EMPLOYEE_ID" NUMBER(6,0) NOT NULL ENABLE, "FIRST_NAME" VARCHAR2(20), "LAST_NAME" VARCHAR2(25) NOT NULL ENABLE, "JOB_ID" VARCHAR2(10) NOT NULL ENABLE, "DEPARTMENT_ID" NUMBER(4,0) NOT NULL ENABLE, "MGR" NUMBER(5,0), "HIREDATE" DATE DEFAULT (sysdate), "SAL" NUMBER(7,2), "BONUS" NUMBER(7,2) DEFAULT (0), CONSTRAINT "EMP_REDEF_EMP_ID_PK" PRIMARY KEY ("EMPLOYEE_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT) TABLESPACE "USERS" ENABLE ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 STORAGE( BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT) TABLESPACE "USERS" PARTITION BY RANGE ("EMPLOYEE_ID") (PARTITION "EMP200" VALUES LESS THAN (200) SEGMENT CREATION IMMEDIATE PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING STORAGE(INITIAL 8388608 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT) TABLESPACE "USERS" , PARTITION "EMP400" VALUES LESS THAN (400) SEGMENT CREATION IMMEDIATE PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING STORAGE(INITIAL 8388608 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT) TABLESPACE "USERS" )
row selected.
可以看到表hr.emp_redef已经成功能联机重定义
9.等任何查询中间表的语句执行完成后将其删除,而且中间表在重定义后其结构就变成了原始表的表结构
SQL> desc hr.int_emp_redef Name Type Nullable Default Comments ------------- ------------ -------- ------- -------- EMPLOYEE_ID NUMBER(6) Y FIRST_NAME VARCHAR2(20) Y LAST_NAME VARCHAR2(25) JOB_ID VARCHAR2(10) DEPARTMENT_ID NUMBER(4) Y SQL> drop table hr.int_emp_redef purge; Table dropped
以上就是Oracle中怎么利用联机重定义给表增加新列与分区,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。