问题
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