oracle中複製表和資料 && 多表插入語句

來源:互聯網
上載者:User

標籤:

建立測試表和測試資料

  create table test  (id number,name varchar(10));
  insert into  test values(1,‘liufang‘);
  insert into  test values(2,‘xiaozhang‘);
  insert into  test values(3,‘dawei‘);
  insert into  test values(4,‘laotan‘);
  insert into  test values(5,‘laotan‘);
  insert into  test values(6,‘laotan‘);
  insert into  test values(7,‘dawei‘);

1、複製表和表中的資料,我們可以通過下面的語句來實現

(註:複製表不包含預設值和約束等資訊,使用下面方法複製表和資料後需要重新見預設值及索引、約束資訊等)

create table test2 as select * from test;

也可以待條件去複製

create table test3 as select * from test where name=‘laotan‘

還可以先去定義表,然後再添加資料

create table test2 as select * from test where 2=1;

insert into test2 select * from test;

 

2、多表插入語句

先指定複製兩個測試表(指定列複製)

create table emp1 as select sequen,ename,sal from emp where 1=2;
create table emp2 as select sequen,ename,cid from emp where 1=2;


下面我們使用四種多表插入語句

a、 insert all 無條件插入
insert all
  into emp1(sequen,ename,sal) values (sequen,ename,sal)
    into emp2(sequen,ename,cid) values (sequen,ename,cid)
    select sequen,ename,sal,cid from emp ;



insert all 有條件插入:上面是沒有加條件的,同時向表中插入資料,而且兩個表的資料條數也一致,下面是加上不同的條件插入

insert all
    when sal>=2000 then
  into emp1(sequen,ename,sal) values (sequen,ename,sal)
    when cid in(1,2) then
    into emp2(sequen,ename,cid) values (sequen,ename,cid)
    select sequen,ename,sal,cid from emp ;

 

 

b、insert first插入(前面根據條件插入後是有相同的資料插入的,如果不想兩個表中資料有重複相同的可以使用insert first)
insert first
    when sal>=2000 then
  into emp1(sequen,ename,sal) values (sequen,ename,sal)
    when cid in(1,2) then
    into emp2(sequen,ename,cid) values (sequen,ename,cid)
    select sequen,ename,sal,cid from emp ;

insert first 語句中,當地一個表符合條件後,的二個表就不在插入對飲的行,表emp1中不會出現和表emp2相同的資料

這就是兩種插入方式的不同之處

 

oracle中複製表和資料 && 多表插入語句

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.