mysql如何把一個表直接拷貝到一個新的表


一:在新表已經建立好的情況下

1,拷貝所有的字段

insert into new_table select * from old_table

2,拷貝部分字段表

insert into new_table(id,name,sex) select id,name,sex from old_table

3,拷貝部分的行

insert into new_table select  *  from old_table where id="1"

4,拷貝部分的行和字段

insert into new_table(id,name,sex) select id,name,sex form old_table where id='1'

二:在新表還沒有建的情況下

方案一:

create table new_table (select * from old_table)

這種方案建的話,只是拷貝的查詢的結果,新表不會有主鍵和索引

方案二:

create table new_table LIKE old_table

該方案只能拷貝表結構到新表中,不會拷貝數據

方案三:

如果要真正的復制一個數據到新表,我們可以直接執行下面的語句

create table new_table LIKE old_table;

insert into new_table select * from old_table;

三:我們也可以操作其它的數據庫中的表

create table new_table LIKE ortherdatabase.old_table;

insert into new_table select * from ortherdatabase.old_table;

ortherdatabase.old_table中的ortherdatabase是指定的數據庫名

四:我們也可以在新建表時改名字

create table new_table (select id,name as username from old_table)

 


免責聲明!

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



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