Caution create table as select, not copy constraints, primary keys, what stuff are not copied

 

1, do some data migration, many people will use the create table as select * from table where id = -1 mode in the coming year to establish exactly the same table, but to do so there is a lot of drawbacks, not the original table the default value is also migrated together.

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.

        Those are not null constraints, and other constraints, but come with a trigger is, strictly speaking, not null constraint is one, but it excluded the materials on it.

Caution create table as select, you must pay attention to the problem of defaults

Blog Category:   the Oracle

Do some data migration, many people will use the create table as select * from table where id = -1 mode in the coming year to establish exactly the same table, but to do so there is a lot of drawbacks, not the original list of default value also migrated together, you can see the following examples:

 

First, a new table

 

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

 

Second, the use of AS TABLE02 Table Create
SELECT * WHERE ID = table01 the From -1

 

Third, look at the structure of the two tables, you will find the second table of defaule value is not, as in Figure 2, can be clearly seen, add_date the default table 02 is not worth the sysdate

table01 table structure

 table02 table structure


 

So when you are doing database migration, use create table as select, be sure to pay attention to the problem of defaults

 

Last week, because of this problem, resulting in a large number of problems in a production environment, the first big day, hereby dedicate it.

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

 

Just two points Create table as select statement

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

First, pay attention to emp_copy table does not define any column names, as we get data from the emp table in Liezi sentence with a wildcard, so as Oracle emp table as emp_copy generated column in the table - the same name, the same data type definitions.

Second, any SQL * PLUS select statement can be issued can be placed create table as select statement, and Oracle are automatically selected from the emp table data in the table into emp_copy. But if the sentence Liezi select statement includes a list of specific column, then create table clause to be included in the list are listed or not listed, such as:

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; and can not write the write column can

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

 

 

We all know that create table a as select * from b to create a table structure as the b table, so it is best not to create a table in practical applications. The reason is that it is not only to create the table structure, but does not create a table with the original default values.

To put it plainly, the table structure came out, no default value.

In addition, but I have a big table to perform a create table a as select * from b time reported less than a temp table space, I do not know why, record it. Deal with it in the next discovery.

Guess you like

Origin www.cnblogs.com/fpcbk/p/12638271.html