Hi folks, the goal here is to share the procedures for moving tables in Oracle Database from version 9i up to version 12cR2.

In this article I’m using version 12cR2 (12.2.0.1) so I can also demonstrate the features of this version. So, apart from the Multitenant architecture part, the procedures also work for non-CDB databases.
Let’s first set up our environment before doing the move…
Environment Setup
I’m connected to the Root container:
SQL> show con_name
CON_NAME
------------------------------
CDB$ROOT
I check the existing Pluggable Databases:
SQL> select name, open_mode from v$pdbs;
NAME OPEN_MODE
-------------- ----------
PDB$SEED READ ONLY
PDB1 MOUNTED
Elapsed: 00:00:00.02
I create a new PDB called SOEPDB:
SQL> create pluggable database soepdb admin user pdbadmin identified by oracle default tablespace soedata datafile size 100M autoextend on parallel 2 storage (maxsize 5g);
Pluggable database created.
Elapsed: 00:00:08.18
I switch my session to the newly created PDB:
SQL> alter session set container=soepdb;
Session altered.
Elapsed: 00:00:00.01
I open the PDB in question:
SQL> alter pluggable database soepdb open;
Pluggable database altered.
Elapsed: 00:00:09.74
I create the user FRANKY already with the DBA role:
SQL> grant dba to franky identified by oracle;
Grant succeeded.
Elapsed: 00:00:00.09
I create two tablespaces to run the tests:
SQL> create tablespace users;
Tablespace created.
Elapsed: 00:00:00.78
SQL> create tablespace users2;
Tablespace created.
Elapsed: 00:00:00.71
I connect to the PDB:
SQL> conn franky/oracle@localhost:1521/soepdb.localdomain
Connected.
Session altered.
Elapsed: 00:00:00.00
Online Table Move Using the DBMS_REDEFINITION Package
The goal here is to move the table T_MOVE1 to the USERS2 tablespace without causing downtime for the application, using the DBMS_REDEFINITION package. This procedure works from Oracle Database 10gR1, although the DBMS_REDEFINITION package has existed since Oracle Database 9i.
I create the table in question in the USERS tablespace from the ALL_OBJECTS view:
SQL> create table t_move1 tablespace users as select * from all_objects;
Table created.
Elapsed: 00:00:05.55
I create an index for this table, also in the USERS tablespace:
SQL> create index i_move1 on t_move1 (object_id) tablespace users;
Index created.
Elapsed: 00:00:00.16
I check the segments created and the tablespace where they are stored:
SQL> select segment_name, segment_type, tablespace_name from user_segments;
SEGMENT_NAME SEGMENT_TYPE TABLESPACE_NAME
------------------------------ ------------------ ------------------------------
I_MOVE1 INDEX USERS
T_MOVE1 TABLE USERS
Elapsed: 00:00:00.20
I check whether the table T_MOVE1 of the owner FRANKY can be redefined:
SQL> exec dbms_redefinition.can_redef_table('FRANKY','T_MOVE1');
BEGIN dbms_redefinition.can_redef_table('FRANKY','T_MOVE1'); END;
*
ERROR at line 1:
ORA-12089: cannot online redefine table "FRANKY"."T_MOVE1" with no primary key
ORA-06512: at "SYS.DBMS_REDEFINITION", line 242
ORA-06512: at "SYS.DBMS_REDEFINITION", line 5439
ORA-06512: at line 1
Elapsed: 00:00:00.04
I got an error in the check because this table has no primary key. We can only use this method for tables that have a PK, or use the dbms_redefinition option with rowid. Oracle needs a way to uniquely identify each row, and that can be done through a PK or through the rowid.
I create the PK constraint on the table:
SQL> alter table t_move1 add constraint t_move1_pk primary key (object_id);
Table altered.
Elapsed: 00:00:00.14
Again I check whether the table can be redefined:
SQL> exec dbms_redefinition.can_redef_table('FRANKY','T_MOVE1');
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.06
I create the table T_MOVE2 in the USERS2 tablespace, based on T_MOVE1 but without its data:
SQL> create table t_move2 tablespace users2 as select * from t_move1 where 1=0;
Table created.
Elapsed: 00:00:00.22
I start the redefinition process, indicating the owner, the source table and the destination table (which must be empty):
SQL> exec dbms_redefinition.start_redef_table('FRANKY','T_MOVE1','T_MOVE2');
PL/SQL procedure successfully completed.
Elapsed: 00:00:03.05
I query the data of both tables to see whether they have the same number of records:
SQL> select count(*) from t_move1;
COUNT(*)
----------
68108
Elapsed: 00:00:00.10
SQL> select count(*) from t_move2;
COUNT(*)
----------
68108
Elapsed: 00:00:00.05
Having confirmed that both are equal, I’ll use the anonymous block below to copy the dependents of the source table to the destination table:
SQL> SET SERVEROUTPUT ON
SQL> DECLARE
num_errors PLS_INTEGER;
BEGIN
dbms_redefinition.copy_table_dependents(
'FRANKY',
'T_MOVE1',
'T_MOVE2',
copy_indexes => dbms_redefinition.cons_orig_params,
num_errors => num_errors
);
dbms_output.put_line(num_errors);
END;
/
DECLARE
*
ERROR at line 1:
ORA-01442: column to be modified to NOT NULL is already NOT NULL
ORA-06512: at "SYS.DBMS_REDEFINITION", line 2074
ORA-06512: at "SYS.DBMS_REDEFINITION", line 2074
ORA-06512: at "SYS.DBMS_REDEFINITION", line 1608
ORA-06512: at "SYS.DBMS_REDEFINITION", line 1487
ORA-06512: at "SYS.DBMS_REDEFINITION", line 408
ORA-06512: at "SYS.DBMS_REDEFINITION", line 1480
ORA-06512: at "SYS.DBMS_REDEFINITION", line 2056
ORA-06512: at "SYS.DBMS_REDEFINITION", line 3115
ORA-06512: at "SYS.DBMS_REDEFINITION", line 5578
ORA-06512: at line 4
Elapsed: 00:00:09.52
An error was returned saying that the NOT NULL columns of the source are already NOT NULL in the destination.
We can ignore these errors by setting the ignore_errors parameter to TRUE in the anonymous block below:
SQL> DECLARE
num_errors PLS_INTEGER;
BEGIN
dbms_redefinition.copy_table_dependents(
'FRANKY',
'T_MOVE1',
'T_MOVE2',
copy_indexes => dbms_redefinition.cons_orig_params,
num_errors => num_errors,
ignore_errors => true
);
dbms_output.put_line(num_errors);
END;
/
PL/SQL procedure successfully completed.
Elapsed: 00:00:12.55
Another option is to use this anonymous block (by oracle-base.com) setting the copy_constraints parameter to FALSE:
-- Copy table dependents
SET SERVEROUTPUT ON
DECLARE
l_num_errors PLS_INTEGER;
BEGIN
DBMS_REDEFINITION.copy_table_dependents(
uname => 'FRANKY',
orig_table => 'T_MOVE1',
int_table => 'T_MOVE2',
copy_indexes => DBMS_REDEFINITION.cons_orig_params, -- Non-Default
copy_triggers => TRUE, -- Default
copy_constraints => FALSE, -- Non Default
copy_privileges => TRUE, -- Default
ignore_errors => FALSE, -- Default
num_errors => l_num_errors);
DBMS_OUTPUT.put_line('l_num_errors=' || l_num_errors);
END;
/
It is important to watch the other objects that depend on a table during the procedure, because in some versions of Oracle Database PL/SQL objects can become invalid, especially triggers, since they are directly related to the tables.
After copying the dependents I check the existing segments. Notice that we have the table T_MOVE1 and its index I_MOVE1 in the USERS tablespace, and the table T_MOVE2 in the USERS2 tablespace and its index TMP$$_I_MOVE10 in the USERS tablespace:
SQL> select segment_name, segment_type, tablespace_name from user_segments;
SEGMENT_NAME SEGMENT_TYPE TABLESPACE_NAME
------------------------------ ------------------ ------------------------------
I_MOVE1 INDEX USERS
TMP$$_I_MOVE10 INDEX USERS
T_MOVE1 TABLE USERS
T_MOVE2 TABLE USERS2
I’m going to insert a new record into the table T_MOVE1:
SQL> insert into t_move1 (OWNER,OBJECT_NAME,OBJECT_ID,CREATED,LAST_DDL_TIME,NAMESPACE) values ('FRANKY','ASDF',1,SYSDATE,SYSDATE,999);
1 row created.
Elapsed: 00:00:00.50
SQL> commit;
Commit complete.
Elapsed: 00:00:00.06
And now I compare the source table with the destination one:
SQL> select count(*) from t_move1;
COUNT(*)
----------
68109
Elapsed: 00:00:00.01
SQL> select count(*) from t_move2;
COUNT(*)
----------
68108
Elapsed: 00:00:00.01
Notice that the number of records is different, so we conclude that our application could keep working on the source table during this move process.
To make the tables equal we need to run the synchronization. For that we use the SYNC_INTERIM_TABLE procedure, again passing the owner, the source table and the destination table:
SQL> exec dbms_redefinition.sync_interim_table('FRANKY','T_MOVE1','T_MOVE2');
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.30
Now I query the table T_MOVE2 to see whether it was synchronized with T_MOVE1:
SQL> select count(*) from t_move2;
COUNT(*)
----------
68109
Elapsed: 00:00:00.01
Again I query the segments created after this synchronization process:
SQL> select segment_name, segment_type, tablespace_name from user_segments;
SEGMENT_NAME SEGMENT_TYPE TABLESPACE_NAME
------------------------------ ------------------ ------------------------------
I_MLOG$_T_MOVE1 INDEX SOEDATA
MLOG$_T_MOVE1 TABLE SOEDATA
T_MOVE2 TABLE USERS2
I_MOVE1 INDEX USERS
TMP$$_I_MOVE10 INDEX USERS
T_MOVE1 TABLE USERS
6 rows selected.
Elapsed: 00:00:00.05
Note that two more segments were created: the table MLOG$_T_MOVE1 and the index I_MLOG$_T_MOVE1. What we actually have here is a Materialized View Log, and the table T_MOVE2 is like our Materialized View. We conclude that this process is done through a Snapshot method.
I’m going to insert one more record to see whether the synchronization happens automatically:
SQL> insert into t_move1 (OWNER,OBJECT_NAME,OBJECT_ID,CREATED,LAST_DDL_TIME,NAMESPACE) values ('C##FRANKY','ASDF',9999998,SYSDATE,SYSDATE,999);
1 row created.
Elapsed: 00:00:00.07
SQL> commit;
Commit complete.
Elapsed: 00:00:00.03
When querying the tables we can see that the synchronization did not happen:
SQL> select count(*) from t_move1;
COUNT(*)
----------
68110
Elapsed: 00:00:00.01
SQL> select count(*) from t_move2;
COUNT(*)
----------
68109
Elapsed: 00:00:00.01
It has to be done manually:
SQL> exec dbms_redefinition.sync_interim_table('FRANKY','T_MOVE1','T_MOVE2');
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.29
Now the tables are synchronized:
SQL> select count(*) from t_move2;
COUNT(*)
----------
68110
Elapsed: 00:00:00.01
I query the segments again to confirm that no more were created:
SQL> select segment_name, segment_type, tablespace_name from user_segments;
SEGMENT_NAME SEGMENT_TYPE TABLESPACE_NAME
------------------------------ ------------------ ------------------------------
I_MLOG$_T_MOVE1 INDEX SOEDATA
I_MOVE1 INDEX USERS
MLOG$_T_MOVE1 TABLE SOEDATA
TMP$$_I_MOVE10 INDEX USERS
T_MOVE1 TABLE USERS
T_MOVE2 TABLE USERS2
6 rows selected.
Elapsed: 00:00:00.09
Now, to finish the process, I run the FINISH_REDEF_TABLE procedure. This procedure will do the final synchronization and remove the Materialized View Log:
SQL> exec dbms_redefinition.finish_redef_table('FRANKY','T_MOVE1','T_MOVE2');
PL/SQL procedure successfully completed.
Elapsed: 00:00:02.04
I query the segments again to confirm that the MLOG was removed and that the table T_MOVE1 is now in the USERS2 tablespace and T_MOVE2 is in the users tablespace:
SQL> select segment_name, segment_type, tablespace_name from user_segments;
SEGMENT_NAME SEGMENT_TYPE TABLESPACE_NAME
------------------------------ ------------------ ------------------------------
I_MOVE1 INDEX USERS
TMP$$_I_MOVE10 INDEX USERS
T_MOVE1 TABLE USERS2
T_MOVE2 TABLE USERS
Elapsed: 00:00:00.11
What actually happened was a rename of the tables involved. The redefinition process does this transparently.
Now I drop the table T_MOVE2:
SQL> drop table T_MOVE2;
Table dropped.
Elapsed: 00:00:00.37
I query the index of the table T_MOVE1 to confirm that its status is valid after the redefinition:
SQL> select index_name, table_name, status from user_indexes where table_name='T_MOVE1';
INDEX_NAME TABLE_NAME STATUS
-------------------- -------------------- --------
I_MOVE1 T_MOVE1 VALID
Elapsed: 00:00:00.01
Again I query the segments:
SQL> select segment_name, segment_type, tablespace_name from user_segments;
SEGMENT_NAME SEGMENT_TYPE TABLESPACE_NAME
------------------------------ ------------------ ------------------------------
BIN$UbJLSmoxG+ngUxMPqMBjFg==$0 INDEX USERS
BIN$UbJLSmoyG+ngUxMPqMBjFg==$0 TABLE USERS
I_MOVE1 INDEX USERS
T_MOVE1 TABLE USERS2
Elapsed: 00:00:00.11
Since I have the recyclebin enabled, the segments keep the BIN$… prefix, so the recyclebin needs to be purged:
SQL> purge recyclebin;
Recyclebin purged.
Elapsed: 00:00:00.39
Phew! The move of the table T_MOVE1 to the USERS2 tablespace was completed successfully, online.
Moving Tables Using MOVE (Offline)
The goal here is to move the table T_MOVE1 to the USERS tablespace using the MOVE clause of the ALTER TABLE command. This method causes downtime for the application and also invalidates the table’s indexes. This procedure works since Oracle Database 9i.
I’m going to query the segments and check in which tablespace they are stored:
SQL> select segment_name, segment_type, tablespace_name from user_segments;
SEGMENT_NAME SEGMENT_TYPE TABLESPACE_NAME
------------------------------ ------------------ ------------------------------
I_MOVE1 INDEX USERS
T_MOVE1 TABLE USERS2
Elapsed: 00:00:00.05
I confirm that the index is valid:
SQL> select index_name, table_name, status from user_indexes where table_name='T_MOVE1';
INDEX_NAME TABLE_NAME STATUS
-------------------- -------------------- --------
I_MOVE1 T_MOVE1 VALID
Elapsed: 00:00:00.01
I move the table T_MOVE1 to the USERS tablespace:
SQL> alter table t_move1 move tablespace users;
Table altered.
Elapsed: 00:00:00.24
I check that the index is now in an unusable state:
SQL> select index_name, table_name, status from user_indexes where table_name='T_MOVE1';
INDEX_NAME TABLE_NAME STATUS
-------------------- -------------------- --------
I_MOVE1 T_MOVE1 UNUSABLE
Elapsed: 00:00:00.00
I check the segments and see that the index object exists, but its segment doesn’t:
SQL> select segment_name, segment_type, tablespace_name from user_segments;
SEGMENT_NAME SEGMENT_TYPE TABLESPACE_NAME
------------------------------ ------------------ ------------------------------
T_MOVE1 TABLE USERS
Elapsed: 00:00:00.01
I rebuild the index, also in the USERS tablespace:
SQL> alter index i_move1 rebuild tablespace users;
Index altered.
Elapsed: 00:00:00.26
Now the index is valid:
SQL> select index_name, table_name, status from user_indexes where table_name='T_MOVE1';
INDEX_NAME TABLE_NAME STATUS
-------------------- -------------------- --------
I_MOVE1 T_MOVE1 VALID
Elapsed: 00:00:00.00
The index segment was created after its rebuild:
SQL> select segment_name, segment_type, tablespace_name from user_segments;
SEGMENT_NAME SEGMENT_TYPE TABLESPACE_NAME
------------------------------ ------------------ ------------------------------
I_MOVE1 INDEX USERS
T_MOVE1 TABLE USERS
Elapsed: 00:00:00.01
Moving Tables Using MOVE ONLINE (Online)
The goal here is to move the table T_MOVE1 to the USERS2 tablespace using the MOVE ONLINE clause of the ALTER TABLE command. This method causes no downtime for the application and does not invalidate the table’s indexes. This procedure works from Oracle Database 12cR2 on.
I’m going to query the segments and check in which tablespace they are stored:
SQL> select segment_name, segment_type, tablespace_name from user_segments;
SEGMENT_NAME SEGMENT_TYPE TABLESPACE_NAME
------------------------------ ------------------ ------------------------------
I_MOVE1 INDEX USERS
T_MOVE1 TABLE USERS
Elapsed: 00:00:00.01
I move the table T_MOVE1 to the USERS2 tablespace using MOVE ONLINE:
SQL> alter table t_move1 move online tablespace users2;
Table altered.
Elapsed: 00:00:00.94
I check the state of the table’s index and see that it remains valid:
SQL> select index_name, table_name, status from user_indexes where table_name='T_MOVE1';
INDEX_NAME TABLE_NAME STATUS
-------------------- -------------------- --------
I_MOVE1 T_MOVE1 VALID
Elapsed: 00:00:00.00
Here, as a bonus of the process, I rebuild the index online in the USERS2 tablespace:
SQL> alter index i_move1 rebuild online tablespace users2;
Index altered.
Elapsed: 00:00:00.20
I check the segments again:
SQL> select segment_name, segment_type, tablespace_name from user_segments;
SEGMENT_NAME SEGMENT_TYPE TABLESPACE_NAME
------------------------------ ------------------ ------------------------------
I_MOVE1 INDEX USERS2
T_MOVE1 TABLE USERS2
Table and index moved to the USERS2 tablespace without causing downtime for the application, using the new MOVE ONLINE feature of Oracle Database 12cR2.
Moving tables in Oracle has other methods not covered in this article, but you can also move them using Data Pump.
I hope this article is useful to a lot of people. If you liked it, subscribe to the blog as a way of contributing, and you’ll also find out every time a new article comes out.
Big hug and see you soon,
Franky

No dbms_redefinition foi citado que é necessário criar uma constraint para realizar a definição, porém, existe um parametro para redefinir tabelas que não possuem chaves.
Ex.:
exec DBMS_REDEFINITION.can_REDEF_TABLE(‘OWNER’,’TABLE’,2);