MySQL中復制數(shù)據(jù)表中的數(shù)據(jù)到新表中的操作教程
MySQL是不支持SELECT … INTO語法的,使用INSERT INTO … SELECT替代相同用法,下面我們我們這里簡答分一下新表存在和不存在兩種情況,具體使用不同的語句。
1.新表不存在
復制表結(jié)構(gòu)即數(shù)據(jù)到新表
create table new_table select * from old_talbe;
這種方法會將old_table中所有的內(nèi)容都拷貝過來,用這種方法需要注意,new_table中沒有了old_table中的primary key,Extra,auto_increment等屬性,需要自己手動加,具體參看后面的修改表即字段屬性.
只復制表結(jié)構(gòu)到新表
# 第一種方法,和上面類似,只是數(shù)據(jù)記錄為空,即給一個false條件 create table new_table select * from old_table where 1=2; # 第二種方法 create table new_table like old_table;
2.新表存在
復制舊表數(shù)據(jù)到新表(假設兩個表結(jié)構(gòu)一樣)
insert into new_table select * from old_table;
復制舊表數(shù)據(jù)到新表(假設兩個表結(jié)構(gòu)不一樣)
insert into new_table(field1,field2,.....) select field1,field2,field3 from old_table;
復制全部數(shù)據(jù)
select * into new_table from old_table;
只復制表結(jié)構(gòu)到新表
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; //僅復制表結(jié)構(gòu) QueryOK, 0 rows affected (0.06 sec) Records:0 Duplicates: 0 Warnings: 0 mysql> create tabletest5 as select * from test1; //把表test1所有內(nèi)容復制為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中復制表結(jié)構(gòu)及其數(shù)據(jù)的5種方式
- mysql 復制記錄實現(xiàn)代碼
- mysql大表復制的具體實現(xiàn)
- mysql復制表的幾種常用方式
- MySQL復制表常用的四種方式小結(jié)
- MySQL級聯(lián)復制下如何進行大表的字段擴容
- mysql復制表的幾種常用方式總結(jié)
- mysql?中的備份恢復,分區(qū)分表,主從復制,讀寫分離
- MySQL 復制表的方法
- MySQL復制表的三種方式(小結(jié))
- Mysql復制表三種實現(xiàn)方法及grant解析
- Mysql將一個表中的某一列數(shù)據(jù)復制到另一個表中某一列里的方法
- MySQL不同表之前的字段復制
- Mysql數(shù)據(jù)表中的蠕蟲復制使用方法
- MySQL 復制表詳解及實例代碼
- MySQL查詢結(jié)果復制到新表的方法(更新、插入)
- mysql 復制表結(jié)構(gòu)和數(shù)據(jù)實例代碼
- MySQL中表的復制以及大型數(shù)據(jù)表的備份教程
- mysql數(shù)據(jù)庫批量復制單條數(shù)據(jù)記錄
相關文章
window下mysql 8.0.15 winx64安裝配置方法圖文教程
這篇文章主要為大家詳細介紹了window下mysql 8.0.15 winx64安裝配置方法圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-03-03
mysqldump數(shù)據(jù)庫備份參數(shù)詳解
這篇文章主要介紹了mysqldump數(shù)據(jù)庫備份參數(shù)詳解,需要的朋友可以參考下2014-05-05
mysql分組后合并顯示一個字段的多條數(shù)據(jù)方式
這篇文章主要介紹了mysql分組后合并顯示一個字段的多條數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01

