当前位置:网站首页>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 :
边栏推荐
- How to reduce the weight transfer of unnecessary pages that users pay attention to?
- Original translation | comparison of machine learning model service tools: kserve, Seldon core and bentoml
- 一场分销裂变活动,不止是发发朋友圈这么简单
- At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions
- Animal breeding production virtual simulation teaching system | Sinovel interactive
- Question brushing notes - tree (easy) - updating
- “好声音“连唱10年,星空华文如何唱响港交所?
- 低代码开发平台是什么?为什么现在那么火?
- Best practice: optimizing Postgres query performance (Part 2)
- Practice of combining rook CEPH and rainbow, a cloud native storage solution
猜你喜欢

Massive data attended the Lanzhou opengauss meetup (ECOLOGICAL NATIONAL trip) activity, enabling users to upgrade their applications with enterprise level databases

分享|智慧环保-生态文明信息化解决方案(附PDF)

Best practice: optimizing Postgres query performance (Part 2)

Oracle的CTAS能不能将约束等属性带到新表?

数据平台调度升级改造 | 从Azkaban 平滑过度到Apache DolphinScheduler 的操作实践

Oracle architecture summary

Animal breeding production virtual simulation teaching system | Sinovel interactive

At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions

NVIDIA three piece environment configuration

元宇宙虚拟数字人离我们更近了|华锐互动
随机推荐
Runmaide medical opened the offering: without the participation of cornerstone investors, the amount of loss doubled
Graduation design of police report convenience service platform based on wechat applet
爱数课实验 | 第六期-金融反欺诈案例研究
[STL programming] [common competition] [Part 2]
Ble Bluetooth module nrf518/nrf281/nrf528/nrf284 chip scheme comparison
Animal breeding production virtual simulation teaching system | Sinovel interactive
1030 Travel Plan
Shuttle hides the return button of the AppBar
eval函数,全局、本地变量
【STL编程】【竞赛常用】【part 2】
非常全面的DolphinScheduler(海豚调度)安装使用文档
Csdn Skills Tree use Experience and Product Analysis (1)
Abap-sm30 check before deletion
After kotlin wechat payment callback, the interface is stuck and uipagefragmentactivity windowleft is thrown
如何降低用户关注的非必要页面的权重传递?
送你12个常用函数公式,用过的都说好
Shell command used in actual work - sed
华为伙伴暨开发者大会2022开源时刻全纪录
SQL Server for循环用法
2021全球独角兽榜发布:中国301家独角兽企业全名单来了!