MySQL中復制數(shù)據(jù)表中的數(shù)據(jù)到新表中的操作教程
MySQL是不支持SELECT … INTO語法的,使用INSERT INTO … SELECT替代相同用法,下面我們我們這里簡答分一下新表存在和不存在兩種情況,具體使用不同的語句。
1.新表不存在
復制表結構即數(shù)據(jù)到新表
create table new_table select * from old_talbe;
這種方法會將old_table中所有的內容都拷貝過來,用這種方法需要注意,new_table中沒有了old_table中的primary key,Extra,auto_increment等屬性,需要自己手動加,具體參看后面的修改表即字段屬性.
只復制表結構到新表
# 第一種方法,和上面類似,只是數(shù)據(jù)記錄為空,即給一個false條件 create table new_table select * from old_table where 1=2; # 第二種方法 create table new_table like old_table;
2.新表存在
復制舊表數(shù)據(jù)到新表(假設兩個表結構一樣)
insert into new_table select * from old_table;
復制舊表數(shù)據(jù)到新表(假設兩個表結構不一樣)
insert into new_table(field1,field2,.....) select field1,field2,field3 from old_table;
復制全部數(shù)據(jù)
select * into new_table from old_table;
只復制表結構到新表
select * into new_talble from old_table where 1=2;
3.實例
(1)表不存在復制
mysql>show tables; +-----------------+ |Tables_in_test1 | +-----------------+ |cpu_stat | |test1 | |test2 | |test3 | +-----------------+ 4rows in set (0.02 sec) mysql> create tabletest4 as select * from test1 where 1=0; //僅復制表結構 QueryOK, 0 rows affected (0.06 sec) Records:0 Duplicates: 0 Warnings: 0 mysql> create tabletest5 as select * from test1; //把表test1所有內容復制為test5 QueryOK, 7 rows affected (0.11 sec) Records:7 Duplicates: 0 Warnings: 0
(2)表已經(jīng)存在復制
mysql> create table test6(id int not null auto_increment primary key, name varchar(20)); Query OK, 0 rows affected (0.13 sec) mysql> insert into test6(name) select name from test1; //只復制name列 Query OK, 7 rows affected (0.06 sec) Records: 7 Duplicates: 0 Warnings: 0 mysql> select * from test6; +----+-------+ | id | name | +----+-------+ | 1 | wu | | 2 | terry | | 3 | tang | …… 7 rows in set (0.00 sec)
- MySQL中復制表結構及其數(shù)據(jù)的5種方式
- mysql 復制記錄實現(xiàn)代碼
- mysql大表復制的具體實現(xiàn)
- mysql復制表的幾種常用方式
- MySQL復制表常用的四種方式小結
- MySQL級聯(lián)復制下如何進行大表的字段擴容
- mysql復制表的幾種常用方式總結
- mysql?中的備份恢復,分區(qū)分表,主從復制,讀寫分離
- MySQL 復制表的方法
- MySQL復制表的三種方式(小結)
- Mysql復制表三種實現(xiàn)方法及grant解析
- Mysql將一個表中的某一列數(shù)據(jù)復制到另一個表中某一列里的方法
- MySQL不同表之前的字段復制
- Mysql數(shù)據(jù)表中的蠕蟲復制使用方法
- MySQL 復制表詳解及實例代碼
- MySQL查詢結果復制到新表的方法(更新、插入)
- mysql 復制表結構和數(shù)據(jù)實例代碼
- MySQL中表的復制以及大型數(shù)據(jù)表的備份教程
- mysql數(shù)據(jù)庫批量復制單條數(shù)據(jù)記錄
相關文章
關于for update和lock in share mode的區(qū)別及說明
這篇文章主要介紹了關于for update和lock in share mode的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08Mysql在Windows系統(tǒng)快速安裝部署方法(綠色免安裝版)
這篇文章主要介紹了Mysql在Windows系統(tǒng)快速安裝部署方法(綠色免安裝版),需要的朋友可以參考下2017-06-06windows7下啟動mysql服務出現(xiàn)服務名無效的原因及解決方法
這篇文章主要介紹了windows7下啟動mysql服務出現(xiàn)服務名無效的原因及解決方法,需要的朋友可以參考下2014-06-06MySQL將版本由5.7.37更新到5.7.39的實現(xiàn)方式
這篇文章主要介紹了MySQL將版本由5.7.37更新到5.7.39的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12