使用子查询因子化(with..as)创建表


创建表有多少种方法?

CREATE TABLE AS SELECT * from table;

DMP

SQLLDR

...

这里介绍一种不常用的方法(使用with .. as建表):

22:10:22 SCOTT@cry> create table dept60_bonuses as with t
22:11:03   2  (select 103 as employee_id,0 as bonus_amt from dual
22:11:49   3  union all
22:11:52   4  select 104 as employee_id,100 as bonus_amt from dual
22:12:07   5  union all
22:12:09   6  select 105 as employee_id,0 as bonus_amt from dual
22:12:21   7  )
22:12:24   8  select * from t;

Table created.

Elapsed: 00:00:00.10
22:12:30 SCOTT@cry> select * from dept60_bonuses;

EMPLOYEE_ID  BONUS_AMT
----------- ----------
        103          0
        104        100
        105          0

这样表就建立了,让我们来观察一下表结构以及表的创建信息.

22:15:54 SCOTT@cry> set lines 80
22:15:59 SCOTT@cry> desc dept60_bonuses
 Name                                      Null?    Type
 ----------------------------------------- -------- -------------

 EMPLOYEE_ID                                        NUMBER
 BONUS_AMT                                          NUMBER

22:19:38 SYS@cry> set long 5000
22:21:15 SYS@cry> /

DBMS_METADATA.GET_DDL('TABLE','DEPT60_BONUSES','SCOTT')
--------------------------------------------------------------------------------


  CREATE TABLE "SCOTT"."DEPT60_BONUSES"
   (    "EMPLOYEE_ID" NUMBER,
        "BONUS_AMT" NUMBER
   ) SEGMENT CREATION IMMEDIATE
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DE

FAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "USERS"

让我们来对比一下正常的表

22:21:18 SYS@cry> select dbms_metadata.get_ddl('TABLE','EMP','SCOTT') from dual;


DBMS_METADATA.GET_DDL('TABLE','EMP','SCOTT')
--------------------------------------------------------------------------------


  CREATE TABLE "SCOTT"."EMP"
   (    "EMPNO" NUMBER(4,0),
        "ENAME" VARCHAR2(10),
        "JOB" VARCHAR2(9),
        "MGR" NUMBER(4,0),
        "HIREDATE" DATE,
        "SAL" NUMBER(7,2),
        "COMM" NUMBER(7,2),
        "DEPTNO" NUMBER(2,0),
         CONSTRAINT "PK_EMP" PRIMARY KEY ("EMPNO")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DE

FAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "USERS"  ENABLE,
         CONSTRAINT "FK_DEPTNO" FOREIGN KEY ("DEPTNO")
          REFERENCES "SCOTT"."DEPT" ("DEPTNO") ENABLE
   ) SEGMENT CREATION IMMEDIATE
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DE

FAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "USERS"

和正常表差不多,只是没有索引和约束而已。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值