How can I import a partition from one table into another in Oracle?

烈酒焚心 提交于 2021-02-08 06:09:42

问题


I would like to know if the following steps are possible and how fast this is:

  1. Create a partition named part1 in Table A
  2. Drop partition part1 in Table B
  3. Import the Table A partition part1 into Table B

Can you provide me with an example if it is possible indeed? Or any resources I can look at?

Note that the tables would have the exact same structure.


回答1:


You can do something similar with the ALTER TABLE ... EXCHANGE PARTITION command. This would exchange a single partition with a table that has the same structure.

A little example:

/* Partitionned Table Creation */
SQL> CREATE TABLE table_a (
  2     ID NUMBER PRIMARY KEY,
  3     DATA VARCHAR2(200)
  4  )
  5  PARTITION BY RANGE (ID) (
  6     PARTITION part100 VALUES LESS THAN (100),
  7     PARTITION part200 VALUES LESS THAN (200)
  8  );

Table created

/* Swap table creation */
SQL> CREATE TABLE swap_table (
  2     ID NUMBER PRIMARY KEY,
  3     DATA VARCHAR2(200)
  4  );

Table created

SQL> INSERT INTO swap_table SELECT ROWNUM, 'a' FROM dual CONNECT BY LEVEL <= 99;

99 rows inserted

SQL> select count(*) from table_a partition (part100);

  COUNT(*)
----------
         0

This will exchange the partition part100 with the transition table swap_table:

SQL> ALTER TABLE table_a EXCHANGE PARTITION part100 WITH TABLE swap_table;

Table altered

SQL> select count(*) from table_a partition (part100);

  COUNT(*)
----------
        99


来源:https://stackoverflow.com/questions/2501160/how-can-i-import-a-partition-from-one-table-into-another-in-oracle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!