Oracle 把一個表中的數據插入到另外一個表中


1.在Oracle中可以用下面兩種:

01: 
     create table newtable   as select * from oldtable;//用於復制前未創建新表newtable不存在的情況 
02: 
      insert into newtable   select * from oldtable;//已經創建了新表newtable 的情況

注意:第一種方式只是復制了表結構,但是主鍵什么的並沒有復制進去,所以用的時候要小心在意。

2.如果想簡單快速的復制表結構,而不需要oldtable里面的數據,可以用下面的語句:

    create table newtable   as  select * from oldtable where 1=2;(把數據過濾掉)

3.如過newtable 和oldtable的表結構不同,可以使用下面的方式:

 create table newtable  as select  s.c1,s.c2  from oldtable s;

 

4.如果想重新命名newtable的列名:

在oracle中:

 create table  newtable(id,name1) as select  s.c1,s.c2  from oldtable s;

或者

 create table  newtable as select  s.c1 ,s.c2  from oldtable s;

在mysql中恐怕只能用第二種方式了。

5.如果是只需要把一部分的oldtable中的數據添加到newtable中。可以這樣:

 create table newtable   as (select * from oldtable where ...);//加where過濾條件

6.最常見的情況是id列新表中要用,並且和舊表中的不同,使用下面的語句就可以了(我們可以重新建一個sequence)

create table yang(id,name) as select hibernate_sequence.nextval,t.ename from emp t;

7.要注意,導出表的時候不能用select...into語句

 

來源:https://www.linuxidc.com/Linux/2012-01/52491.htm


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM