標籤:表結構 tools into 刪除 分類 選擇 包括 ima oracle資料庫
create table as select * from和insert into select from兩種表複製語句區別
[sql] view plain copy
- create table targer_table as select * from source_table
- insert into target_table(column1,column2) select column1,column2 from source_table
以上兩句都是將源表source_table的記錄插入到目標表target_table,但兩句又有區別。
第一句(create table as select * from)要求目標表target_table不存在,因為在插入時會自動建立。
第二句(insert into select from)要求目標表target_table存在,由於目標表已經存在,所以我們除了插入源表source_table的欄位外,還可以插入常量,如sql語句:
[sql] view plain copy
- insert into target_table(column1,column2) select column1,5 from source_table
例中的:5;
無論是create table as select * from還是insert into select from, from後面的都是源表(source_table);
1、Insert into Select from 語句
語句形式為:Insert into targer_table(field1,field2,...) select value1,value2,... from source_table
要求目標表 targer_table必須存在,由於目標表targer_table已經存在,所以我們除了插入源表source_table的欄位外,還可以插入常量。樣本如下:
--1.建立測試表:
[sql] view plain copy
- CREATE TABLE Table1
- (
- a varchar(10) PRIMARY KEY,
- b varchar(10),
- c varchar(10)
- );
[sql] view plain copy
- CREATE TABLE Table2
- (
- a varchar(10) PRIMARY KEY,
- c varchar(10),
- d int
- );
--2.建立測試資料:
[sql] view plain copy
- Insert into Table1 values (‘趙‘, ‘asds‘, ‘90‘);
- Insert into Table1 values (‘錢‘, ‘asds‘, ‘100‘);
- Insert into Table1 values (‘孫‘, ‘asds‘, ‘80‘);
- Insert into Table1 values (‘李‘, ‘asds‘, null);
查詢目標表:
select * from Table2
沒有記錄;
--3.INSERT INTO SELECT語句複製表資料:
[sql] view plain copy
- Insert into Table2(a, c, d) select a,c,5 from Table1
--4.顯示更新後的結果:
[sql] view plain copy
- select * from Table2;
[plain] view plain copy
- A C D
- 1 趙 90 5
- 2 錢 100 5
- 3 孫 80 5
- 4 李 5
注意:D欄位的值全部是常量5;
--5.刪除測試表:
[sql] view plain copy
- drop TABLE Table1
- drop TABLE Table2
2、create table as select * from語句
語句形式為:create table targer_table as select * from source_table;
要求目標表Table2不存在,因為在插入時會自動建立表Table2,並將Table1中指定欄位資料複製到Table2中。樣本如下:
--1.建立測試表:
[sql] view plain copy
- CREATE TABLE Table1
- (
- a varchar(10) PRIMARY KEY,
- b varchar(10),
- c varchar(10)
- );
--2.建立測試資料:
[sql] view plain copy
- Insert into Table1 values (‘趙‘, ‘asds‘, ‘90‘);
- Insert into Table1 values (‘錢‘, ‘asds‘, ‘100‘);
- Insert into Table1 values (‘孫‘, ‘asds‘, ‘80‘);
- Insert into Table1 values (‘李‘, ‘asds‘, null);
--3.create table as select * from語句建立表Table2並複製資料:
[sql] view plain copy
- create table TABLE2 as select * from TABLE1;
--4.顯示更新後的結果:
[sql] view plain copy
- select * from Table2
[plain] view plain copy
- A B C
- 1 趙 asds 90
- 2 錢 asds 100
- 3 孫 asds 80
- 4 李 asds
--5.刪除測試表:
[sql] view plain copy
- drop TABLE Table1
- drop TABLE Table2
附:
注意:
create table targer_table as select * from source_table是會複製表結構+表資料,
而create table targer_table as select * from source_table where 1=2;只會建立相同的表結構,不會複製表資料。
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 as select 2010-04-18 11:39:26
分類: Linux
大家都知道create table a as select * from b可以建立一個與b表結構一樣的表,但是在實際應用中最好不要這麼建立表。原因是這樣只建立表的結構,而不會將原表的預設值一起建立。
說白了,表結構出來了,預設值沒有。
另外,但是有一個我對一個大表執行create table a as select * from b時候報了一個temp資料表空間不足,不知道是什麼原因,記錄一下。下次發現在處理吧。
oracle資料庫【表複製】insert into select from跟create table as select * from 兩種表複製語句區別