Oracle - 使用表交换分区

时间:2017-07-06 11:32:31

标签: oracle database-administration

我尝试在Oracle 11g.2.0.3上使用:

创建一个table2
CREATE table2 
LOGGING TABLESPACE TS_table1_2014 PCTFREE 10 INITRANS 1 STORAGE ( INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS UNLIMITED BUFFER_POOL DEFAULT ) NOCOMPRESS
as (select * from table1 where date_text <= '2015-12-31');

当我尝试将此table2与分区table3交换时,我收到了以下错误:

alter table table3 exchange partition partition_name WITH TABLE table2;

    Error report -
    SQL Error: ORA-14097: column type or size mismatch in ALTER TABLE EXCHANGE PARTITION
    14097. 00000 -  "column type or size mismatch in ALTER TABLE EXCHANGE PARTITION"
    *Cause:    The corresponding columns in the tables specified in the
               ALTER TABLE EXCHANGE PARTITION are of different type or size
    *Action:   Ensure that the two tables have the same number of columns
               with the same type and size.

我在下面的查询中有测试差异:

Select a.COLUMN_NAME 
  , a.DATA_TYPE, b.DATA_TYPE 
  , a.data_length, b.data_length 
  , a.data_precision, b.data_precision 
  , a.data_scale, b.data_scale 
  , a.nullable, b.nullable 
from ALL_TAB_COLUMNS a 
full outer join ALL_TAB_COLUMNS b on a.column_name=b.column_name 
 and b.owner=user and b.table_name='&table2' 
 where  a.owner=user and a.table_name='&table1'   
 and ( 
    nvl(a.data_type,'#')!=nvl(b.data_type,'#') 
    or nvl(a.data_length,-1)!=nvl(b.data_length,-1) 
    or nvl(a.data_precision,-100)!=nvl(b.data_precision,-100) 
    or nvl(a.data_scale,-100)!=nvl(b.data_scale,-100) 
    or nvl(a.nullable,'#')!=nvl(b.nullable,'#') 
   ) 
;

产生的一些差异是列大小。此语法“create as select”未保留创建新表的顺序和大小。

如何在table1中创建table2 作为选择,并且强制保持与主table1源相同的大小列?

谢谢!

2 个答案:

答案 0 :(得分:1)

我发现你的DDL没有任何差异。我建议使用相同的DDL来创建table2,然后执行:

insert into table2 select * from table1;

答案 1 :(得分:1)

您需要使用dbms_metadata包或查询数量的数据字典视图(如all_tab_columns等)来获取有关现有表的元数据,以便您可以为swap-table构建正确的sql(用于交换分区操作)。除了NOT NULL检查之外,CTAS不会传输DEFAULT值作为示例和约束。 最佳实践是使用分区表同时创建/重新创建/修改此表。