但是注意到 CTAS 不会复制每列的默认值
原因很简单。CTAS 仅用于复制数据而不是元数据(表结构除外)。
所以,你会失去:
基本上,与表相关的所有内容。
您必须提取元数据并更改表并显式应用它。
alter table new_table_name modify (column default 1);
或者,您可以:
- 使用dbms_metadata.get_ddl获取表DDL
- 用新名称重命名表
- 将旧表中的数据插入新表。
当然,后者会慢得多。
更新
有一种方法可以使用CTAS提供默认值而不是空约束:
设置
SQL> create table t1 (id number default 1 not null, text varchar2(10));
Table created.
SQL> insert into t1 (text) values ('a');
1 row created.
CTAS
SQL> create table t2 (id default 1 not null, text )
2 as select * from t1;
Table created.
SQL> select *from t2;
ID TEXT
---------- ----------
1 a
使用默认值以及旧表中的数据创建新表。