如何将Oracle数据库的普通表转换成分区表

如题所述

普通表txn转换成分区表
一 创建普通表txn
SQL> create table txn as select level as id from dual connect by level<=29;
SQL> desc txn

Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER
二 创建表空间

SQL> create tablespace t1 datafile '/home/oracle/t1.dbf' size 5M;
SQL> create tablespace t2 datafile '/home/oracle/t2.dbf' size 5M;

SQL> create tablespace t3 datafile '/home/oracle/t3.dbf' size 5M;

三 创建分区表,命名为txn_1

SQL> create table txn_1(id number) partition by range(id)
2 (
3 partition part1 values less than(10) tablespace t1,
4 partition part2 values less than(20) tablespace t2,
5 partition part3 values less than(30) tablespace t3
6 );
四 导出普通表数据

[oracle@ogg1 ~]$ exp chen/chen file=txn.dmp tables=txn
五 更改表名

SQL> rename txn to txn_old;
SQL> rename txn_1 to txn;
六 将数据导入到分区表中

[oracle@ogg1 ~]$ imp chen/chen file=txn.dmp fromuser=chen touser=chen ignore=y
七 查看分区表

SQL> col table_name for a10
SQL> col partition_name for a10;
SQL> select table_name,partition_name from user_tab_partitions;

TABLE_NAME PARTITION_
---------- ----------
TXN PART1
TXN PART2
TXN PART3

SQL> select * from txn partition(part2);

ID
----------
10
11
12
13
14
15
16
17
18
19

10 rows selected.
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-11-12
以 create table tablename as ...的格式创建一个分区表,然后把原表重命名,再把分区表重命名为目标表,这样应该可以了,注意主键索引都需要手工添加进去。本回答被提问者和网友采纳