create table with select union has no constraints

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 03:15:15

问题


I created a table using select with a union, as follows:

create table tableC as
select column1, column2 from tableA
union all
select column1, column2 from tableB

The resulting table (tableC) has inherited none of the constraints from tableA or tableB. Why weren't the constraints copied to the new table?


回答1:


Using select ... as ... to create a table never copies constraints. If you want the new table to inherit constraints from the original tables, you must create the new constraints manually.


As @Davek points out, not null constraints will get copied from a single table select ... as .... I imagine that's because they are both column attributes and constraints. However, once the column has more than one source, it is reasonable that Oracle would not try to apply that constraint.


In response to the follow-up question "would it be possible to give tableC the same constraints either from tableA or tableB, after a CTAs?":

Of course it's possible, but there's no single command to do it. You could write a procedure that used dynamic SQL to copy the constraints. However, unless you're looking to automate this behavior, it'll generally be easier to extract the DDL using an IDE and change the table name.



来源:https://stackoverflow.com/questions/26514166/create-table-with-select-union-has-no-constraints

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!