当前位置:网站首页>Can Oracle's CTAs bring constraints and other attributes to the new table?
Can Oracle's CTAs bring constraints and other attributes to the new table?
2022-06-27 21:15:00 【Ink Sky Wheel】
I saw a question today , These mentioned functional attributes are familiar , But just ask , It's a little vague , Practice is the only criterion for testing truth , Only through experiments , To prove our guess ,

Translate the question , What we want to verify is , The primary key on a table 、 Unique constraint 、NOT NULL constraint 、 Foreign key constraints , When in Oracle Pass through CTAS Will a new table be copied when it is created ?
Create test table ,
CREATE TABLE t1(id NUMBER PRIMARY KEY, c1 varchar2(1) NOT NULL, c2 NUMBER, c3 NUMBER UNIQUE);CREATE TABLE t2(id NUMBER PRIMARY KEY, c1 varchar2(1) NOT NULL);ALTER TABLE t1 ADD CONSTRAINT fk_t1_t2 FOREIGN KEY (c2) REFERENCES t2(id);
id It's the primary key column ,c1 There is NOT NULL constraint ,c2 There are foreign key constraints ,c3 There is a unique constraint , There is a DBeaver The display problem of the database client , Table constraints 、 In the foreign key view , Are not displayed c2 This foreign key constraint ,

But through the view user_constraints, You can find the created foreign key constraint ,
SQL> exec print_table('select constraint_name, constraint_type from user_constraints where table_name=''T1''');CONSTRAINT_NAME : FK_T1_T2CONSTRAINT_TYPE : R-----------------CONSTRAINT_NAME : SYS_C007441CONSTRAINT_TYPE : C-----------------CONSTRAINT_NAME : SYS_C007442CONSTRAINT_TYPE : P-----------------CONSTRAINT_NAME : SYS_C007443CONSTRAINT_TYPE : U-----------------PL/SQL procedure successfully completed.
CTAS Create table ,
CREATE TABLE t1_1 AS SELECT * FROM t1;
You can see , The new table only has this non empty constraint , Primary key 、 Unique constraint 、 Foreign key constraints , I didn't bring it with me ,
SQL> exec print_table('select constraint_name, constraint_type, search_condition from user_constraints where table_name=''T1_1''');CONSTRAINT_NAME : SYS_C007445CONSTRAINT_TYPE : CSEARCH_CONDITION : "C1" IS NOT NULL-----------------PL/SQL procedure successfully completed.
therefore , The answer is obvious .
however , Why is this phenomenon ? We from Oracle The official manual , Can see some clues ,《SQL Language Reference》 When it comes to the CTAS The ability to replicate constraints , In addition to the constraint types involved in the above experiments , Other properties that cannot be copied , Are explained here ,
https://docs.oracle.com/cd/E11882_01/server.112/e41084/statements_7002.htm#sthref5328
Oracle Database derives data types and lengths from the subquery. Oracle Database follows the following rules for integrity constraints and other column and table attributes:
> Oracle Database automatically defines on columns in the new table any NOT NULL constraints that have a state of NOT DEFERRABLE and VALIDATE, and 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.
Explicit NOT NULL Constraints are automatically brought to the new table .
> 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.
implicit NOT NULL Constraints will not be brought to the new table , For example, a primary key .
> 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.
in addition , Primary key , only , Foreign keys ,check constraint , Partition , The default values of indexes and columns will not be brought to the new table .
> 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.
On the new table, you can choose whether to partition like the old table , Or different partition forms , Or create a non partitioned table . stay AS subquery Specify before the sentence .
> A column that is encrypted using Transparent Data Encryption in the selected table will not be encrypted in the new table unless you define the column in the new table as encrypted at create time.
TDE The column of will not be brought to the new table .
therefore , Common attributes like constraints , There will be these hidden phenomena , The technical field of database is really abstruse , And this may be the charm of Technology , Problems encountered 、 Explore the problem 、 solve the problem , Always enjoy it .
Recently updated articles :
《" Red Alert " Game open source code brings us a shock 》
《 China's four major space launch sites 》
《 Some problems encountered recently 》
《MySQL A case in which setting the administrator password cannot take effect 》
Article classification and indexing :
边栏推荐
- 分享|智慧环保-生态文明信息化解决方案(附PDF)
- Shell command used in actual work - sed
- 本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献
- 润迈德医疗开启招股:未有基石投资者参与,亏损金额翻倍增长
- 爱数课实验 | 第九期-利用机器学习方法进行健康智能诊断
- Best practice: optimizing Postgres query performance (Part 2)
- CSDN skill tree experience and product analysis (1)
- Model reasoning acceleration based on tensorrt
- 行业案例|从零售之王看银行数字化转型的运营之道
- Wechat applet based service management system for college party members' Home System applet graduation design, Party members, activists, learning, punch in, forum
猜你喜欢

众昂矿业:新能源或成萤石最大应用领域

展现强劲产品综合实力 ,2022 款林肯飞行家Aviator西南首秀

一场分销裂变活动,不止是发发朋友圈这么简单

送你12个常用函数公式,用过的都说好

Installation and configuration of grayog new generation log collection early warning system

What is a low code development platform? Why is it so hot now?

难怪大家丢掉了postman而选择 Apifox

数仓的字符截取三胞胎:substrb、substr、substring

CSDN 技能树使用体验与产品分析(1)

Mongodb introduction and typical application scenarios
随机推荐
爱数课实验 | 第七期-基于随机森林的金融危机分析
谈谈我写作生涯的画图技巧
白嫖红队goby&POC,叫你如何白嫖?
通过CE修改器修改大型网络游戏
一套系统,减轻人流集中地10倍的通行压力
Unity3D Button根据文本内容自适应大小
How to do a good job of gateway high availability protection in the big promotion scenario
After kotlin wechat payment callback, the interface is stuck and uipagefragmentactivity windowleft is thrown
麒麟V10安装字体
Csdn Skills Tree use Experience and Product Analysis (1)
教程|fNIRS数据处理工具包Homer2下载与安装
“好声音“连唱10年,星空华文如何唱响港交所?
低代码开发平台是什么?为什么现在那么火?
What is a stack?
JPA踩坑系列之save方法
展现强劲产品综合实力 ,2022 款林肯飞行家Aviator西南首秀
Navicat premium connection problem --- host 'XXXXXXXX' is not allowed to connect to this MySQL server
Ble Bluetooth module nrf518/nrf281/nrf528/nrf284 chip scheme comparison
Cloud native Security Guide: learn kubernetes attack and defense from scratch
开启生态新姿势 | 使用 WrodPress 远程附件存储到 COS