---
title: "How To Partition Existing Table Using DBMS_Redefinition [ID 472449.1]来自Metalink"
author: "libin123"
date: "2010-05-17T23:27:26Z"
tags: []
categories: ["数据库"]
slug: "how-to-partition-existing-table-using-dbms-redefinition-id-472449-1-metalink"
draft: false
lastmod: "2010-05-17T23:28:14Z"
id: 668364
url: "https://dbua.iteye.com/blog/668364"
---

**Applies to:**

Oracle Server - Standard Edition - Version: 9.2.0.4  
Information in this document applies to any platform.

[]()**Goal**

The purpose of this document is to provide step by step instructions on how to convert unpartitioned table to partitioned one using dbms\_redefinition package.

[]()**Solution**

1\) Create unpartitioned table with the name unpar\_table

SQL&gt; CREATE TABLE unpar\_table (  
id NUMBER(10),  
create\_date DATE,  
name VARCHAR2(100)  
);

2\) Apply some constraints to the table:

SQL&gt; ALTER TABLE unpar\_table ADD (  
CONSTRAINT unpar\_table\_pk PRIMARY KEY (id)  
);

SQL&gt; CREATE INDEX create\_date\_ind ON unpar\_table(create\_date);

3\) Gather statistics on the table:

SQL&gt; EXEC DBMS\_STATS.gather\_table\_stats(USER, 'unpar\_table', cascade =&gt; TRUE);

4\) Create a Partitioned Interim Table:

SQL&gt; CREATE TABLE par\_table (  
id NUMBER(10),  
create\_date DATE,  
name VARCHAR2(100)  
)  
PARTITION BY RANGE (create\_date)  
(PARTITION unpar\_table\_2005 VALUES LESS THAN (TO\_DATE('01/01/2005', 'DD/MM/YYYY')),  
PARTITION unpar\_table\_2006 VALUES LESS THAN (TO\_DATE('01/01/2006', 'DD/MM/YYYY')),  
PARTITION unpar\_table\_2007 VALUES LESS THAN (MAXVALUE));

5\) Start the Redefinition Process:

a) Check the redefinition is possible using the following command:

SQL&gt; EXEC Dbms\_Redefinition.can\_redef\_table(USER, 'unpar\_table');

b)If no errors are reported, start the redefintion using the following command:

SQL&gt; BEGIN  
DBMS\_REDEFINITION.start\_redef\_table(  
uname =&gt; USER,   
orig\_table =&gt; 'unpar\_table',  
int\_table =&gt; 'par\_table');  
END;  
/

Note: This operation can take quite some time to complete.

c) Optionally synchronize new table with interim name before index creation:

SQL&gt; BEGIN  
dbms\_redefinition.sync\_interim\_table(  
uname =&gt; USER,   
orig\_table =&gt; 'unpar\_table',  
int\_table =&gt; 'par\_table');  
END;  
/

d) Create Constraints and Indexes:

SQL&gt; ALTER TABLE par\_table ADD (  
CONSTRAINT unpar\_table\_pk2 PRIMARY KEY (id)  
);

SQL&gt; CREATE INDEX create\_date\_ind2 ON par\_table(create\_date);

e) Gather statistics on the new table:

SQL&gt; EXEC DBMS\_STATS.gather\_table\_stats(USER, 'par\_table', cascade =&gt; TRUE);

f) Complete the Redefintion Process:

SQL&gt; BEGIN  
dbms\_redefinition.finish\_redef\_table(  
uname =&gt; USER,  
orig\_table =&gt; 'unpar\_table',  
int\_table =&gt; 'par\_table');  
END;  
/

At this point the interim table has become the "real" table and their names have been switched in the name dictionary.

g) Remove original table which now has the name of the interim table:

SQL&gt; DROP TABLE par\_table; 

h)Rename all the constraints and indexes to match the original names.

ALTER TABLE unpar\_table RENAME CONSTRAINT unpar\_table\_pk2 TO unpar\_table\_pk;  
ALTER INDEX create\_date\_ind2 RENAME TO create\_date\_ind;

i) Check whether partitioning is successful or not:

SQL&gt; SELECT partitioned  
FROM user\_tables  
WHERE table\_name = 'unpar\_table';

PAR  
\---  
YES

1 row selected.

SQL&gt; SELECT partition\_name  
FROM user\_tab\_partitions  
WHERE table\_name = 'unpar\_table';

PARTITION\_NAME  
\------------------------------  
unpar\_table\_2005  
unpar\_table\_2006  
unpar\_table\_2007

3 rows selected.

Please not that the 9i redefinition procedures has some restrictions:

\* There must be enough space to hold two copies of the table.  
\* Primary key columns cannot be modified.  
\* Tables must have primary keys.  
\* Redefinition must be done within the same schema.  
\* New columns added cannot be made NOT NULL until after the redefinition operation.  
\* Tables cannot contain LONGs, BFILEs or User Defined Types.  
\* Clustered tables cannot be redefined.  
\* Tables in the SYS or SYSTEM schema cannot be redefined.  
\* Tables with materialized view logs or materialized views defined on them cannot be redefined.  
\* Horizontal sub setting of data cannot be performed during the redefinition.