慎用create table as select,一定要注意默认值的问题---大一临时表方法
来源:互联网 发布:刷爱奇艺会员软件 编辑:程序博客网 时间:2024/06/01 07:53
摘要:
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;
========================================================
分类: Linux
大家都知道create table a as select * from b可以创建一个与b表结构一样的表,但是在实际应用中最好不要这么创建表。原因是这样只创建表的结构,而不会将原表的默认值一起创建。
说白了,表结构出来了,默认值没有。
另外,但是有一个我对一个大表执行create table a as select * from b时候报了一个temp表空间不足,不知道是什么原因,记录一下。下次发现在处理吧。
转载http://space.itpub.net/9252210/viewspace-660173
----------------------------------------
一、Symptoms
During CTAS (CREATE TABLE AS SELECT), be reported:
ORA-01652: unable to extend temp segment by 1024 in tablespace <DATA>
二、Cause
The tablespace where the object is being created doesnt have sufficient space to extend for the CTAS command to succeed.
三、Solution
Modify the datafile associated for the tablespace to AUTOEXTEND ON till the CTAS command gets executed successfully.
During the CTAS , it creates a data segment in the target tablespace and marks this segment as temporary in dictionary.
Once the table created successfully , the dictionary type is changed from TEMPRORAY to TABLE. In addition, if the SELECT performs a SORT operation,temporary space may be used as for the same.
四、TEST CASE
1. 创建表空间
SQL> CREATE TABLESPACE TEST DATAFILE 'D:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL10G\TEST.DBF' SIZE 10M AUTOEXTEND OFF;
Tablespace created
2. 为客户分配默认表空间
SQL> ALTER USER TESTER DEFAULT TABLESPACE TEST;
User altered.
3. 查询对象实际大小
SQL>SELECT BYTES/1024/1024,TABLESPACE_NAME FROM DBA_SEGMENTS WHERE SEGMENT_NAME='DUMMY';
11 system --Size of the DUMMY object is 11 M
4. CTAS创建新对象
SQL> CONN TESTER/TESTER
Connected.
SQL> CREATE TABLE DUMMY_123 AS SELECT * FROM SCOTT.DUMMY;
CREATE TABLE DUMMY_123 AS SELECT * FROM SCOTT.DUMMY
*
ERROR at line 1:
ORA-01652: unable to extend temp segment by 128 in tablespace TEST
The above error message reported above is because the Tablespace TEST is of 10 M size and AUTOEXTEND OFF. The object about to be created "DUMMY_123 " , requires 11 M size and as it doesn't have enough space to extend , it has failed with the ORA-1652 error message in "TEST" tablespace.
一、Temporary Segments Concept
Oracle often requires temporary work space for intermediate stages of database processing. There are different kinds of temporary segments in the database.
Some of them are created explicitly by the users. The others are created and accessed for the user by the system.
There are SQL operations containing a sorting step which require temporary segments. However, segments used for sorting are not the only segments having SEGMENT_TYPE=TEMPORARY. Temporary segments can also exist for permanent segments creation.
Temporary segments for sorting are created in the default temporary tablespace of the user. This tablespace may be of type TEMPORARY or PERMANENT.
(1) A TEMPORARY tablespace (Locally Managed Tablespace) is recommended for sort operations.
(2) Temporary segments for permanent segments creation are created in the tablespace specified in the create statement or in the user’s default tablespace.
a. Temporary Tables
only exists during a transaction or session.
DML statements on temporary tables do not generate redo logs for the data changes. However, undo logs for the data and redo logs for the undo logs are generated.
b. Temporary LOBs
The goal of temporary LOBs is to develop an interface to support the creation and deletion of lobs that act like local variables.
c. Temporary Segments as work area for sorting
When processing queries, Oracle often requires temporary workspace for intermediate stages of SQL statement execution.
The sort area is allocated in memory. If the sort operation needs additional memory (above the value specified by the SORT_AREA_SIZE parameter), the sorted rows are written to disk to free up the sort area so that it can be re-used for the remaining sort.
Oracle automatically allocates this disk space called a temporary segment.
The following statements may require the use of a temporary segment for sorting:
CREATE INDEX/SELECT ... ORDER BY/SELECT DISTINCT/SELECT ... GROUP BY/SELECT ... UNION/SELECT ... INTERSECT/SELECT ... MINUS/ANALYZE TABLE/Unindexed joins/Correlated subqueries
d. Temporary Segments for permanent segments creation
Besides sort operations, there are other SQL operations, which also require temporary segments:
CREATE PRIMARY/UNIQUE KEY CONSTRAINT
ALTER TABLE ... ENABLE PRIMARY/UNIQUE CONSTRAINT
CREATE TABLE STORAGE (MINEXTENTS>1)
CREATE TABLE AS SELECT
The CTAS creates a data segment in the target tablespace and marks this segment as temporary in dictionary.
On completion, the dictionary type is changed from temporary to table. In addition, if the SELECT performs a SORT operation, temporary space may be used as for a standard select.
CREATE PARTITION TABLE
ALTER TABLE ... SPLIT PARTITION
CREATE SNAPSHOT
CREATE INDEX
The CREATE INDEX statement, after sorting the index values, builds a temporary segment in the INDEX tablespace;
once the index is completely built, the segment type is changed to INDEX.
DROP TABLE
e. Temporary Tablespaces
二、Introduction to Direct-Path INSERT
1. Conventional insert operations:
Oracle reuses free space in the table, interleaving newly inserted data with existing data. During such operations, Oracle also maintains referential integrity constraints.
2. Direct-path INSERT operations:
Oracle appends the inserted data after existing data in the table. Data is written directly into datafiles, bypassing the buffer cache. Free space in the existing data is not reused, and referential integrity constraints are ignored. These procedures combined can enhance performance.
a. During direct-path INSERT, you can disable the logging of redo and undo entries
b.CREATE TABLE ... AS SELECT statement, does not have any indexes defined on it and not null constraint; you must define them later.
Note:If the database or tablespace is in FORCE LOGGING mode, then direct path INSERT always logs, regardless of the logging or nologging setting, such as STANDBY database.
========================================================
========================================================
今天鄙人有意翻出ORACLE 9I Performance Tuning 的官方教材,看到原文:
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.
但本人在自己的生产库的操作过程中,发现,只要是用as select CREATE的表,都是带有原表的CONSTRINTS....
有谁能解释一下,.
这难道是官方文件的手误???
Connected as serp01
SQL> select constraint_name from dba_constraints where table_name ='CRM_CUSTOMER';
CONSTRAINT_NAME
------------------------------
REFCRM_CUSTOMER2167
REFINV_WAREHOUSE482
REFINV_WAREHOUSE608
REFORG_EMPLOYEES0001
REFORG_EMPLOYEES1474
REFORG_ENTERPRISES1400
REFORG_ORGANIZATIONS000001
REFORG_ORGANIZATIONS2401
REFPUB_AREA_CODE379
REFPUB_CLASSIFY_CODE380
REFPUB_ENTERPRISE_TYPE_CODE381
REFPUB_INDUSTRY_CODE382
REFPUB_PAYCONDI_CODE0000001
REFTPUB_PAYMENT_CODE01
SYS_C0092306
SYS_C0092764
SYS_C0079060
SYS_C0079061
SYS_C0079062
SYS_C0079063
CONSTRAINT_NAME
------------------------------
SYS_C0079064
SYS_C0079065
SYS_C0079066
SYS_C0079067
SYS_C0079068
SYS_C0079069
SYS_C0079070
SYS_C0079071
SYS_C0079072
SYS_C0079073
SYS_C0079074
SYS_C0079075
SYS_C0079076
SYS_C0079077
SYS_C0079078
SYS_C0079079
SYS_C0079080
SYS_C0079081
SYS_C0079082
SYS_C0079083
SYS_C0079084
CONSTRAINT_NAME
------------------------------
SYS_C0079085
SYS_C0079086
SYS_C0079087
SYS_C0079088
SYS_C0079089
SYS_C0079090
SYS_C0079091
SYS_C0079092
SYS_C0079093
SYS_C0079094
SYS_C0079095
SYS_C0079096
SYS_C0079097
SYS_C0079098
SYS_C0079099
SYS_C0079100
SYS_C0079101
SYS_C0079102
SYS_C0079103
SYS_C0079104
SYS_C0079105
CONSTRAINT_NAME
------------------------------
SYS_C0079106
SYS_C0079107
SYS_C0079108
SYS_C0079109
PK19
67 rows selected
SQL> create table temp_h1 as select * from crm_customer where rownum<10;
create table temp_h1 as select * from crm_customer where rownum<10
ORA-00955: 名称已由现有对象使用
SQL> drop table temp_h1;
Table dropped
SQL> create table temp_h1 as select * from crm_customer where rownum<10;
Table created
SQL> select constraint_name from dba_constraints where table_name ='TEMP_H1';
CONSTRAINT_NAME
------------------------------
SYS_C0093440
SYS_C0093441
SYS_C0093442
SYS_C0093443
SYS_C0093444
SYS_C0093445
SYS_C0093446
SYS_C0093447
SYS_C0093448
SYS_C0093449
SYS_C0093450
SYS_C0093451
SYS_C0093452
SYS_C0093453
SYS_C0093454
SYS_C0093455
SYS_C0093456
SYS_C0093457
SYS_C0093458
SYS_C0093459
CONSTRAINT_NAME
------------------------------
SYS_C0093460
SYS_C0093461
SYS_C0093462
SYS_C0093463
SYS_C0093464
SYS_C0093465
SYS_C0093466
SYS_C0093467
SYS_C0093468
SYS_C0093469
SYS_C0093470
SYS_C0093471
SYS_C0093472
SYS_C0093473
SYS_C0093474
SYS_C0093475
SYS_C0093476
SYS_C0093477
SYS_C0093478
SYS_C0093479
SYS_C0093480
CONSTRAINT_NAME
------------------------------
SYS_C0093481
SYS_C0093482
SYS_C0093483
SYS_C0093484
SYS_C0093485
SYS_C0093486
SYS_C0093487
SYS_C0093488
SYS_C0093489
SYS_C0093490
SYS_C0093491
52 rows selected
SQL>
A答:也许你有好几个TEMP_H1, 在不同的SCHEMA里. 试试
select owner, table_name from dba_tables where table_name ='TEMP_H1';
就会一目了然
B答:这个是不可能的,用ctas从组表必然要对表加相应的约束等。。。。
- 慎用create table as select,一定要注意默认值的问题---大一临时表方法
- 慎用create table as select,一定要注意默认值的问题
- 慎用create table as select,一定要注意默认值的问题
- 慎用create table as select,一定要注意默认值的问题
- 慎用create table as select,一定要注意默认值的问题
- Create table as select
- create table as select
- 复制表的(create table table1 as select * from table2 )问题
- ORACLE CTAS(create table as select)使用注意点
- ORACLE CTAS(create table as select)使用注意点
- oracle create table xx as select ....CREATE TABLE XXX AS SELECT .... 拷贝表的时候 not null 约束 和check
- oracle create table as select
- Oracle Create Table as Select
- 浅谈create table as select
- create table as select 和 create table 再insert into select 的差异
- create table as 产生的问题
- create table tb as select 和create table tb like的区别
- Mysql create table tb as select 和create table tb like的区别
- 伙伴地址
- MyMFC(7-9)对话框 CProp3
- BZOJ 1069: [SCOI2007]最大土地面积
- android 4.2以后js 的问题
- HTML与XHTML区别比较
- 慎用create table as select,一定要注意默认值的问题---大一临时表方法
- PHP intval bcmul 方法变了
- Android开发之FastJson概述与简单使用
- 执行了getHibernateTemplate.save(user)后,控制台有hql语句输出,显示已经将数据存到数据库了,也没有抛出异常,但是去oracle数据库查的时候,压根就没有数据。。。。请问
- SolrCloud原理
- MyMFC(7-9)对话框 CPropSheet
- 11个实用的CSS学习工具
- 输出口和操作
- 有关linux系统登录出现启动会话失败