天天看點

oracle 一張表中的資料插入到另一張表_表結構不同,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