oracle資料庫【表複製】insert into select from跟create table as select * from 兩種表複製語句區別

來源:互聯網
上載者:User

標籤:表結構   tools   into   刪除   分類   選擇   包括   ima   oracle資料庫   

create table  as select * from和insert into select from兩種表複製語句區別

 

[sql] view plain copy 
  1. create table targer_table as select * from source_table  
  2. 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 
  1. 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 
  1. CREATE TABLE Table1  
  2. (  
  3.   a varchar(10) PRIMARY KEY,  
  4.   b varchar(10),  
  5.   c varchar(10)  
  6. );  

 

[sql] view plain copy 
  1. CREATE TABLE Table2  
  2. (  
  3.   a varchar(10) PRIMARY KEY,  
  4.   c varchar(10),  
  5.   d int  
  6. );  


--2.建立測試資料:

 

 

[sql] view plain copy 
  1. Insert into Table1 values (‘趙‘, ‘asds‘, ‘90‘);  
  2. Insert into Table1 values (‘錢‘, ‘asds‘, ‘100‘);  
  3. Insert into Table1 values (‘孫‘, ‘asds‘, ‘80‘);  
  4. Insert into Table1 values (‘李‘, ‘asds‘, null);  


查詢目標表:

 

select * from Table2

沒有記錄;

--3.INSERT INTO SELECT語句複製表資料:

 

[sql] view plain copy 
  1. Insert into Table2(a, c, d) select a,c,5 from Table1  


--4.顯示更新後的結果:

 

 

[sql] view plain copy 
  1. select * from Table2;  
[plain] view plain copy 
  1.     A   C   D  
  2. 1   趙   90  5  
  3. 2   錢   100 5  
  4. 3   孫   80  5  
  5. 4   李       5  

注意:D欄位的值全部是常量5;

 

--5.刪除測試表:

 

[sql] view plain copy 
  1. drop TABLE Table1  
  2. 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 
  1. CREATE TABLE Table1  
  2. (  
  3.   a varchar(10) PRIMARY KEY,  
  4.   b varchar(10),  
  5.   c varchar(10)  
  6. );  

 

 --2.建立測試資料:

[sql] view plain copy 
  1. Insert into Table1 values (‘趙‘, ‘asds‘, ‘90‘);  
  2. Insert into Table1 values (‘錢‘, ‘asds‘, ‘100‘);  
  3. Insert into Table1 values (‘孫‘, ‘asds‘, ‘80‘);  
  4. Insert into Table1 values (‘李‘, ‘asds‘, null);  

 

  --3.create table  as select * from語句建立表Table2並複製資料:

 

[sql] view plain copy 
  1. create table TABLE2 as select * from TABLE1;  

 

 

--4.顯示更新後的結果:

 

[sql] view plain copy 
  1. select * from Table2  
[plain] view plain copy 
  1.     A   B   C  
  2. 1   趙   asds    90  
  3. 2   錢   asds    100  
  4. 3   孫   asds    80  
  5. 4   李   asds      

 

--5.刪除測試表:

[sql] view plain copy 
  1. drop TABLE Table1  
  2. 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 兩種表複製語句區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.