MySQL復(fù)制表的三種方式(小結(jié))
復(fù)制表結(jié)構(gòu)及其數(shù)據(jù)
下面這個語句會拷貝數(shù)據(jù)到新表中。
注意:這個語句其實(shí)只是把select語句的結(jié)果建一個表,所以新表不會有主鍵,索引。
create table table_name_new as (select * from table_name_old);
只復(fù)制表結(jié)構(gòu)
create table table_name_new as select * from table_name_old where 1=2;
或者
create table table_name_new like table_name_old;
注意:前一種方式是不會復(fù)制主鍵類型,索引的,而后一種方式是把舊表的所有字段類型都復(fù)制到新表。
只復(fù)制表數(shù)據(jù)
如果兩個表結(jié)構(gòu)一樣
insert into table_name_new select * from table_name_old;
如果兩個表結(jié)構(gòu)不一樣
insert into table_name_new(column1,column2...) select column1,column2... from table_name_old;
注意:很多文章說可以通過如下語句進(jìn)行數(shù)據(jù)復(fù)制,table_name_new表可以不存在,會在執(zhí)行的過程中自動創(chuàng)建。其實(shí)該SELECT ... INTO形式是使查詢結(jié)果存儲在變量或?qū)⑵鋵懭胛募磘able_name_new是一個變量或者文件。
select column1,column2,.... into table_name_new from table_name_old;
到此這篇關(guān)于MySQL復(fù)制表的三種方式(小結(jié))的文章就介紹到這了,更多相關(guān)MySQL 復(fù)制表內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. MySQL/MariaDB 如何實(shí)現(xiàn)數(shù)據(jù)透視表的示例代碼2. 如何修改MySQL字符集3. mysql5.7 設(shè)置遠(yuǎn)程訪問的實(shí)現(xiàn)4. SQLSERVER調(diào)用C#的代碼實(shí)現(xiàn)5. MSSQL跨服務(wù)器連接的幾種方法6. Access中批量替換數(shù)據(jù)庫內(nèi)容的兩種方法7. 如何在SQL Server 2005中為安裝程序增加計(jì)數(shù)器注冊表項(xiàng)值8. DB2的高可用性和災(zāi)難恢復(fù)概述9. sql server 災(zāi)難恢復(fù)10. 講解MaxDB數(shù)據(jù)庫和MySQL數(shù)據(jù)庫的主要差別
