慎用create table as select,不会copy约束,主键什么东东都不会复制

 

1、再做一些数据迁移时候,很多人会使用create table  as select * from table where id=-1的方式来年建立一摸一样的表,但是这样做有个很大的弊端,不能将原表中的default value也一同迁移过来。

2、  Using the CREATE TABLE ... AS SELECT ... command: This command will copy acrooss to the new table all the data,but the constraints triggers ,and so on will not be transferred to the new table.

        那些都是not null约束,其他的约束和trigger是带不过来了,严格说来not null也是约束的一种,只不过教材上把它排除在外了吧。

慎用create table as select,一定要注意默认值的问题

博客分类:   Oracle

再做一些数据迁移时候,很多人会使用create table  as select * from table where id=-1的方式来年建立一摸一样的表,但是这样做有个很大的弊端,不能将原表中的default value也一同迁移过来,可以看下面的例子:

第一,新建一个表

-- Create table
create table table01
(
  id        number(16),
  add_date  date default sysdate,
  status    number(1),
  entp_code varchar2(200)
)

第二,使用create table table02 as
select * From table01 where id=-1

第三、看看两个表的结构,会发现第二张表的defaule value没有了,如下2图,可以很明显看出来,表02的add_date的默认值得sysdate没有了

table01的表结构

 table02的表结构


所以各位在做数据库迁移时候,使用create table as select时候,一定要注意默认值的问题

上周,因为此问题,导致生产环境下产生了大量的问题,头大了一天,特此奉献出来。

====================================================================

Create table as select 语句的两点说明

SQL > create table emp_copy as select * from emp where deptno=10;

第一,注意emp_copy表中没有定义任何列名,因为我们在列子句中用通配符从emp表取得数据,让Oracle像emp表中一样生成emp_copy表中的列——相同名称,相同数据类型定义。

第二,SQL*PLUS中可以发出的任何select语句可以放在create table as select 语句中,然后Oracle会自动获得从emp表选择的数据,在进emp_copy表中。但是 如果select语句的列子句中包括特定列清单,则create table子句列出表中要包括的列或者不列,例如:

SQL > create table emp_copy_2 (empno,sal) as select empno, sal from emp where deptno=10,或者

create table emp_copy_1  as select empno, sal from emp where deptno=10;都可以  不写和写列都可以

========================================================

 

大家都知道create table a as select * from b可以创建一个与b表结构一样的表,但是在实际应用中最好不要这么创建表。原因是这样只创建表的结构,而不会将原表的默认值一起创建。

说白了,表结构出来了,默认值没有。

另外,但是有一个我对一个大表执行create table a as select * from b时候报了一个temp表空间不足,不知道是什么原因,记录一下。下次发现在处理吧。

猜你喜欢

转载自www.cnblogs.com/fpcbk/p/12638271.html