《Mysql實(shí)例MySQL中復(fù)制數(shù)據(jù)表中的數(shù)據(jù)到新表中的操作教程》要點(diǎn):
本文介紹了Mysql實(shí)例MySQL中復(fù)制數(shù)據(jù)表中的數(shù)據(jù)到新表中的操作教程,希望對您有用。如果有疑問,可以聯(lián)系我們。
MySQL是不支持SELECT … INTO語法的,使用INSERT INTO … SELECT替代相同用法,下面我們我們這里簡答分一下新表存在和不存在兩種情況,具體使用不同的語句.
1.新表不存在
復(fù)制表結(jié)構(gòu)即數(shù)據(jù)到新表
MYSQL教程
create table new_table select * from old_talbe;
這種方法會將old_table中所有的內(nèi)容都拷貝過來,用這種方法需要注意,new_table中沒有了old_table中的primary key,Extra,auto_increment等屬性,需要自己手動(dòng)加,具體參看后面的修改表即字段屬性.
只復(fù)制表結(jié)構(gòu)到新表MYSQL教程
# 第一種方法,和上面類似,只是數(shù)據(jù)記錄為空,即給一個(gè)false條件 create table new_table select * from old_table where 1=2; # 第二種方法 create table new_table like old_table;
2.新表存在
復(fù)制舊表數(shù)據(jù)到新表(假設(shè)兩個(gè)表結(jié)構(gòu)一樣)
MYSQL教程
insert into new_table select * from old_table;
復(fù)制舊表數(shù)據(jù)到新表(假設(shè)兩個(gè)表結(jié)構(gòu)不一樣)
MYSQL教程
insert into new_table(field1,field2,.....) select field1,field2,field3 from old_table;
復(fù)制全部數(shù)據(jù)
MYSQL教程
select * into new_table from old_table;
只復(fù)制表結(jié)構(gòu)到新表
MYSQL教程
select * into new_talble from old_table where 1=2;
3.實(shí)例MYSQL教程
(1)表不存在復(fù)制
MYSQL教程
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; //僅復(fù)制表結(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)容復(fù)制為test5 QueryOK, 7 rows affected (0.11 sec) Records:7 Duplicates: 0 Warnings: 0
?
(2)表已經(jīng)存在復(fù)制MYSQL教程
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; //只復(fù)制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教程
《Mysql實(shí)例MySQL中復(fù)制數(shù)據(jù)表中的數(shù)據(jù)到新表中的操作教程》是否對您有啟發(fā),歡迎查看更多與《Mysql實(shí)例MySQL中復(fù)制數(shù)據(jù)表中的數(shù)據(jù)到新表中的操作教程》相關(guān)教程,學(xué)精學(xué)透。維易PHP學(xué)院為您提供精彩教程。
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/11220.html