注意create table as select 不会带默认值及相关

本文探讨了在Oracle中使用CREATE TABLE AS SELECT (CTAS)时,默认值和约束的应用问题。CTAS创建的新表不会继承原表的默认值,但会自动携带原表显式定义的NOT NULL约束。此外,文章还介绍了如何手动设置新表的默认值。

        看到这篇文章Beware of default values when using CTAS,关于create table as select (CTAS)值得注意的地方:使用这条sql创建的表不会带默认值。

        如上面的文章中所说,可以在创建表之后,修改默认值:

alter table t2 modify (id number default (0));

      或者在创建的语句里给列加上默认值

create table t2(id default 1) as select * from t1;

       然后查询了一下文档(11gr1),关于 AS subquery 里面叙述到

  • Oracle Database automatically defines on columns in the new table any  NOT NULL  constraints that were explicitly created on the corresponding columns of the selected table if the subquery selects the column rather than an expression containing the column. If any rows violate the constraint, then the database does not create the table and returns an error.

显示的NOT NULL约束自动会带到新表。

  • NOT NULL  constraints that were implicitly created by Oracle Database on columns of the selected table (for example, for primary keys) are not carried over to the new table.

隐式的NOT NULL约束不会带到新表,如主键。

  • In addition, primary keys, unique keys, foreign keys, check constraints, partitioning criteria, indexes, and column default values are not carried over to the new table.

另外,主键,唯一,外键,check约束,分区,索引以及列的默认值不会带到新表。

  • If the selected table is partitioned, then you can choose whether the new table will be partitioned the same way, partitioned differently, or not partitioned. Partitioning is not carried over to the new table. Specify any desired partitioning as part of the  CREATE TABLE  statement before the  AS subquery  clause.

在新表上可以选择是否像像旧表那样分区,或者不同的分区形式,或者创建非分区表。在AS subquery句之前指定。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23650854/viewspace-697781/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/23650854/viewspace-697781/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值