Remember once Oracle Rename Table of practical application

Due

We want to remove some duplication of data on a table PRODUCT in a production environment. Before we start, we first made a backup of the table, as follows:

    CREATE TABLE APP.PRODUCT_201910 TABLESPACE "TABLESPACE_1" nologging AS SELECT * FROM APP.PRODUCT WHERE 1=2;
    INSERT /*+ APPEND PARALLEL(8) */ INTO APP.PRODUCT_201910 nologging SELECT * FROM APP.PRODUCT;
    COMMIT;

Note that, create it using CATS (CREATE TABLE AS) method of the table, the table does not inherit from its parent constraints, identity columns, column default value or primary key.

After finished, we started to PRODUCT operate, but because of DB Performance is not completed within the original estimate of time. In order not to affect the follow-up to other scripts, decided to abandon this operation will be restored back PRODUCT.

development of

First of all, we let the DBA to locate a running query, manually kill off.

Secondly, we replace with backup table back to the original table.

    RENAME APP.PRODUCT TO APP.PRODUCT_BROKEN;
    RENAME TABLE APP.PRODUCT_201910 TO APP.PRODUCT;

However, it does not end here, there are many original tables need to modify the properties back. This process requires special attention because it is easy to miss some steps.

设置 Sequence, Default Value

It is relatively common and perform a few steps too fast.

Add sequence

    DROP SEQUENCE APP.PRODUCT_ID_SEQ
    ALTER TABLE APP.PRODUCT MODIFY PRODUCT_ID NUMBER(10,0) DEFAULT APP.PRODUCT_ID_SEQ.NEXTVAL

Set default value

    ALTER TABLE APP.PRODUCT MODIFY PRODUCT_KEY VARCHAR2(300 CHAR) DEFAULT '-1';
    ALTER TABLE APP.PRODUCT MODIFY PRODUCT_TYPE_ID NUMBER(5,0) DEFAULT -1;
    ALTER TABLE APP.PRODUCT MODIFY DATE TIMESTAMP (6) DEFAULT CURRENT_TIMESTAMP;

Set Constrain

  1. Plus the primary key.
  2. Check if there is a foreign key link to the table.
    -- 1
    DROP CONSTRAINT APP.PRODUCT_PK
    ALTER TABLE APP.PRODUCT ADD CONSTRAINT APP.PRODUCT_PK PRIMARY KEY ("PRODUCT_PK");
    
    -- 2
    SELECT * FROM DBA_CONSTRAINTS A, DBA_CONSTRAINTS B 
    WHERE A.CONSTRAINT_TYPE = 'R'
    AND A.R_CONSTRAINT_NAME = B.CONSTRAINT_NAME
    AND A.R_OWNER = B.OWNER
    AND B.TABLE_NAME = 'PRODUCT'
    AND B.OWNER = 'APP';

Set Index

Add up the index may be a little slow.

    DROP INDEX APP.IDX_PRODUCT_KEY_DATE

    CREATE INDEX APP.IDX_PRODUCT_KEY_DATE ON APP.PRODUCT ("PUBLIC_KEY", "END_DATE") 
    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 "TABLESPACE_1";

It should be noted, index the primary key do not need to create additional.

Oracle enforces a UNIQUE key or PRIMARY KEY integrity constraint on a table by creating a unique index on the unique key or primary key. This index is automatically created by Oracle when the constraint is enabled.

Set Dependency

Dependent objects reconstruction: reconstruction here is only directly dependent objects.

    -- check sql
    SELECT * FROM DBA_DEPENDENCIES A WHERE A.REFERENCED_NAME = 'PRODUCT' AND A.REFERENCED_OWNER = 'APP';
    -- modify sql
    SELECT 'alter '||DECODE(TYPE,'PACKAGE BODY','PACKAGE',TYPE)||' '||OWNER||'.'||NAME||' compile;' 
    FROM DBA_DEPENDENCIES A 
    WHERE A.REFERENCED_NAME = 'PRODUCT' 
    AND A.REFERENCED_OWNER = 'APP';

Set Privilege

Permissions reconstruction

    -- check sql
    SELECT * FROM DBA_TAB_PRIVS WHERE TABLE_NAME = UPPER('PRODUCT') AND OWNER = UPPER('APP');
    -- modify sql
    SELECT 'grant ' || PRIVILEGE || ' on ' || OWNER || '.' || TABLE_NAME || ' to ' || GRANTEE || ';'
    FROM DBA_TAB_PRIVS
    WHERE TABLE_NAME = UPPER('PRODUCT')
    AND OWNER = UPPER('APP');

to sum up

In addition to the above-mentioned

  • Sequence, Default Value
  • Constrain
  • Index
  • Dependency
  • Privilege

, There are materialized views and materialized view logs, you can see specific reference links.

So, with the rename method is rather cumbersome. If the amount of data is not a lot of cases, it is recommended to insert the data directly back to the original table, convenient and simple less error-prone.

Reference links

Guess you like

Origin www.cnblogs.com/maxstack/p/11714559.html