SQL學習 如何將乙個表的資料複製到另乙個新錶

2021-09-24 06:43:45 字數 866 閱讀 4505

1.兩個表的結構一樣

insert into 新錶

select * from 舊表;

2.兩個表的結構不一樣

insert into 新錶 (欄位1,欄位2,...) 

select 欄位1,欄位2,...

from 舊表;

1.複製表結構和資料到新錶

select * into 新錶

from 舊表

/* mysql不適用 */

2. 只複製表結構到新錶

create table 新錶 

select * from 舊表

where 1=2; /* 即:讓where條件不成立 */

create table 新錶

like 舊表;

/* creat table like 產生與源表相同的表結構,包括索引和主鍵,

資料需要用insert into 語句複製進去 */

2.只複製資料到新錶

cerete table 新錶

select * from 舊表;

/* 這種方法會將舊表中所有的內容都拷貝過來,用這種方法需要注意,

新錶中沒有了舊表中的primary key,extra,auto_increment等屬性 */

creat table 新錶 as select 欄位1,欄位2,...

from 舊表;

/* creat table as 只是複製原資料,其實就是把查詢的結果建乙個表 */

SQL如何將乙個表的資料插入另乙個表

sql實現將乙個表的資料插入到另外乙個表的 第一種情況的 1 如果2張表的字段一致,並且希望插入全部資料,可以用這種方法 insert into 目標表 select from 表 2 比如要將 articles 表插入到 newarticles 表中,則是 insert into newartic...

mysql如何將乙個表的資料複製到另乙個表

第一種 未建立表 建立表2,只拷貝表1的結構到表2,不拷貝資料 create table table2 like table1 建立表2,同時拷貝表1的資料和結構到表2 create table table2 select from table1 第二種 已建立表 表2,表1欄位完全匹配 已經建立了...

MySQL如何將乙個表的字段更新到另乙個表中

業務 將乙個表的字段更新到另乙個表中 今天遇到的乙個問題,迷茫了半天,在我記憶中第一種寫法是正確的,但是在mysql中,嘗試半天也不對,不知道其他資料是否支援 在網上看到有帖子也是這樣的寫法 第一種寫法 update t set t.spu b.spu from table1 t,table2 b ...