insert table 和create table as 區別,insertcreate

來源:互聯網
上載者:User

insert table 和create table as 區別,insertcreate

作者:iamlaosong

首先,最大的區別是二者屬於不同類型的語句,前者是DML語句(資料操作語言,SQL中處理資料等操作統稱為資料操縱語言),完成後需要提交才會生效,後者是DDL語句(資料定義語言 (Data Definition Language),用於定義和管理 SQL 資料庫中的所有對象的語言 ),執行完直接生效,不提供復原,效率比較高。其次,功能不同,前者只是插入資料,必須先建表;後者則建表和插入資料一塊完成。

insert  into  table1(columns1,columns2) select  columns1,columns2  from  table2;
從table2中查詢的結果插入到table1中,前提是table1和table2已經存在;
 
 create.  as..select一般有以下三種方式:
1. create table table1 as select  * from table2  where 1=2;
建立一個表結構與table2一模一樣的表,只複製結構不複製資料;
2.create  table table1  as  select *   from table2  ;
  建立一個表結構與table2一模一樣的表,複製結構同時也複製資料;
3.create  table table1(columns_a,columns_b) as select  columns1,columns2  from table2; 
建立一個表結構與table2一模一樣的表,複製結構同時也複製資料,但是指定新表的列名,這種格式也用於複製table2部分欄位;

後面兩種格式,如果後面跟上合適的查詢條件,可以只複製合格資料到新的表中,例如:

create  table table1  as  select *   from table2  where columns1>=1;

可能的情況下,建議用第二種方式,這種方式不僅能一次完成建表和插入資料,而且效率高,特別是當複製的資料量比較大時,可以避免大量資料存在復原空間中等待提交。


相關文章

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.