设为首页 收藏本站
查看: 1073|回复: 0

[经验分享] ORACLE CTAS(create table as select)使用注意点

[复制链接]
YunVN网友  发表于 2016-8-14 06:34:32 |阅读模式
ORACLE CTAS(create table as select)使用注意点
看到这篇文章Beware of default values when using CTAS,关于create table as select
(CTAS)值得注意的地方:使用这条sql创建的表不会带默认值。
操作以下实验证明之:

scott@TICKET>  create table p
2     ( id number primary key ,
3        username varchar(25) ,
4        passwd varchar(24),
5        email varchar(30),
6      birth date,
7  tel number ,
8  sex char(1));
表已创建。

scott@TICKET> alter table p add  age number  ;
表已更改。
创建一个唯一约束
scott@TICKET> alter table p modify tel unique;
表已更改。
创建一个检查约束
scott@TICKET> alter table p  add constraint p_ck_age check(age>0 and age<150);
表已更改。
创建一个默认约束
scott@TICKET> alter table p modify sex default '0';
表已更改。
scott@TICKET> desc user_constraints;
名称                                      是否为空? 类型
----------------------------------------- -------- ----------------------------
OWNER                                     NOT NULL VARCHAR2(30)
CONSTRAINT_NAME                           NOT NULL VARCHAR2(30)
CONSTRAINT_TYPE                                    VARCHAR2(1)
TABLE_NAME                                NOT NULL VARCHAR2(30)
SEARCH_CONDITION                                   LONG
R_OWNER                                            VARCHAR2(30)
R_CONSTRAINT_NAME                                  VARCHAR2(30)
DELETE_RULE                                        VARCHAR2(9)
STATUS                                             VARCHAR2(8)
DEFERRABLE                                         VARCHAR2(14)
DEFERRED                                           VARCHAR2(9)
VALIDATED                                          VARCHAR2(13)
GENERATED                                          VARCHAR2(14)
BAD                                                VARCHAR2(3)
RELY                                               VARCHAR2(4)
LAST_CHANGE                                        DATE
INDEX_OWNER                                        VARCHAR2(30)
INDEX_NAME                                         VARCHAR2(30)
INVALID                                            VARCHAR2(7)
VIEW_RELATED                                       VARCHAR2(14)
查看原表中的各种约束信息.
scott@TICKET> select constraint_name,constraint_type,status from user_constraints where table_name='P';
CONSTRAINT_NAME                C STATUS
------------------------------ - --------
SYS_C0015996                   P ENABLED
SYS_C0015997                   U ENABLED
P_CK_AGE                       C ENABLED
通过查询创建相关的表信息.
scott@TICKET> create table  persion as select * from p;
表已创建。
scott@TICKET> select * from persion;
未选定行
scott@TICKET> insert into persion select * from p;
已创建0行。
检查新表中的约束信息
scott@TICKET> select constraint_name,constraint_type,status from user_constraints where table_name='PERSION';
未选定行
再次证明了使用CTAS存在一些问题.

Oracle查询了一下文档(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句之前指定。



   

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-257300-1-1.html 上篇帖子: 获取Oracle 表信息 整成数据字典 下篇帖子: Oracle 创建表空间并分配给用户
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表