Online table redefinition of a table
Lets create a table with some dependent objects for this setup.
DROP PROCEDURE get_description;
DROP VIEW redef_tab_v;
DROP SEQUENCE redef_tab_seq;
DROP TABLE redef_tab PURGE;
CREATE TABLE redef_tab (
id NUMBER,
description VARCHAR2(50),
CONSTRAINT redef_tab_pk PRIMARY KEY (id)
);
CREATE VIEW redef_tab_v AS
SELECT * FROM redef_tab;
CREATE SEQUENCE redef_tab_seq;
CREATE OR REPLACE PROCEDURE get_description (
p_id IN redef_tab.id%TYPE,
p_description OUT redef_tab.description%TYPE) AS
BEGIN
SELECT description
INTO p_description
FROM redef_tab
WHERE id = p_id;
END;
/
CREATE OR REPLACE TRIGGER redef_tab_bir
BEFORE INSERT ON redef_tab
FOR EACH ROW
WHEN (new.id IS NULL)
BEGIN
:new.id := redef_tab_seq.NEXTVAL;
END;
/
Lets confirm that objects are created
COLUMN object_name FORMAT A20
SELECT object_name, object_type, status FROM user_objects ORDER BY object_name;
OBJECT_NAME OBJECT_TYPE STATUS
-------------------- ------------------- -------
GET_DESCRIPTION PROCEDURE VALID
REDEF_TAB TABLE VALID
REDEF_TAB_BIR TRIGGER VALID
REDEF_TAB_PK INDEX VALID
REDEF_TAB_SEQ SEQUENCE VALID
REDEF_TAB_V VIEW VALID
6 rows selected.
Basic Online Table Redefinition
Now we perform an online table redefinition.
-- Check table can be redefined
EXEC DBMS_REDEFINITION.can_redef_table('ATOORPU', 'REDEF_TAB');
--- SCHEMA = ATOORPU, TABLE_NAME=REDEF_TAB
CREATE TABLE redef_tab2 AS
SELECT *
FROM redef_tab WHERE 1=2;
-- Start Redefinition
EXEC DBMS_REDEFINITION.start_redef_table('ATOORPU', 'REDEF_TAB', 'REDEF_TAB2');
-- Optionally synchronize new table with interim data before index creation
EXEC DBMS_REDEFINITION.sync_interim_table('ATOORPU', 'REDEF_TAB', 'REDEF_TAB2');
-- Add new PK.
ALTER TABLE redef_tab2 ADD (CONSTRAINT redef_tab2_pk PRIMARY KEY (id));
-- Complete redefinition
EXEC DBMS_REDEFINITION.finish_redef_table('ATOORPU', 'REDEF_TAB', 'REDEF_TAB2');
-- Remove original table which now has the name of the new table
DROP TABLE redef_tab2;
Finally, we verify the status of the schema objects.
COLUMN object_name FORMAT A20
SELECT object_name, object_type, status FROM user_objects ORDER BY object_name;
OBJECT_NAME OBJECT_TYPE STATUS
-------------------- ------------------- -------
GET_DESCRIPTION PROCEDURE VALID
REDEF_TAB TABLE VALID
REDEF_TAB2_PK INDEX VALID
REDEF_TAB_SEQ SEQUENCE VALID
REDEF_TAB_V VIEW VALID
Note : The GET_DESCRIPTION procedure and REDEF_TAB_V view are still valid, but the REDEF_TAB_BIR trigger is gone. The trigger was still associated with the original table, renamed to REDEF_TAB2, so when the original table was dropped, the trigger was dropped with it.
No comments:
Post a Comment