how a table can be reorganized on-line using the DBMS_REDEFINITION




---------------------------------------------------------------------
-- Connect as SYSDBA to grant privs to ...
---------------------------------------------------------------------
connect / as sysdba

grant execute on dbms_redefinition to ;
grant dba to ;

---------------------------------------------------------------------
-- Create new empty interim table...
---------------------------------------------------------------------
connect /tiger
CREATE TABLE NEW_TABLE AS SELECT * FROM WHERE 1=2;

---------------------------------------------------------------------
-- Test if table can be redefined...
---------------------------------------------------------------------
EXEC DBMS_REDEFINITION.CAN_REDEF_TABLE('', '', 2);

---------------------------------------------------------------------
-- Start table redefinition...
---------------------------------------------------------------------
EXEC DBMS_REDEFINITION.START_REDEF_TABLE('', '', 'NEW_TABLE', NULL, 2);

--------------------------------------------------------------------
-- Add ALL constraints, indexes, triggers, grants, etc...
---------------------------------------------------------------------
ALTER TABLE emp ADD PRIMARY KEY (empno);

---------------------------------------------------------------------
-- Finish the redefinition process (this will swap the two tables)...
---------------------------------------------------------------------
EXEC DBMS_REDEFINITION.FINISH_REDEF_TABLE('', '', 'NEW_TABLE');

---------------------------------------------------------------------
-- Drop the interim working table...
---------------------------------------------------------------------
DROP TABLE NEW_TABLE;

Post a Comment

And that's all there is to it!

If anyone has any other questions or requests for future How To posts, you can either ask them in the comments or email me. Please don't feel shy at all!

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

Previous Post Next Post