欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MySQL中復(fù)制數(shù)據(jù)表中的數(shù)據(jù)到新表中的操作教程

 更新時(shí)間:2016年03月13日 12:01:59   作者:kumu_Linux  
這篇文章主要介紹了MySQL中復(fù)制數(shù)據(jù)表中的數(shù)據(jù)到新表中的操作教程,文中分為新表存在和新表不存在兩種情況來講,需要的朋友可以參考下

MySQL是不支持SELECT … INTO語法的,使用INSERT INTO … SELECT替代相同用法,下面我們我們這里簡答分一下新表存在和不存在兩種情況,具體使用不同的語句。
1.新表不存在
復(fù)制表結(jié)構(gòu)即數(shù)據(jù)到新表

create table new_table
select * from old_talbe;

這種方法會(huì)將old_table中所有的內(nèi)容都拷貝過來,用這種方法需要注意,new_table中沒有了old_table中的primary key,Extra,auto_increment等屬性,需要自己手動(dòng)加,具體參看后面的修改表即字段屬性.
只復(fù)制表結(jié)構(gòu)到新表

# 第一種方法,和上面類似,只是數(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)一樣)

insert into new_table
select * from old_table;

復(fù)制舊表數(shù)據(jù)到新表(假設(shè)兩個(gè)表結(jié)構(gòu)不一樣)

insert into new_table(field1,field2,.....)
select field1,field2,field3 from old_table;

復(fù)制全部數(shù)據(jù)

select * into new_table from old_table;

只復(fù)制表結(jié)構(gòu)到新表

select * into new_talble from old_table where 1=2;

3.實(shí)例

(1)表不存在復(fù)制

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> 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) 

 

相關(guān)文章

最新評論