国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

MySQL中復制數據表中的數據到新表中的操作教程

2024-07-24 13:08:54
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了MySQL中復制數據表中的數據到新表中的操作教程,文中分為新表存在和新表不存在兩種情況來講,需要的朋友可以參考下
 

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

create table new_tableselect * from old_talbe;

這種方法會將old_table中所有的內容都拷貝過來,用這種方法需要注意,new_table中沒有了old_table中的primary key,Extra,auto_increment等屬性,需要自己手動加,具體參看后面的修改表即字段屬性.
只復制表結構到新表

# 第一種方法,和上面類似,只是數據記錄為空,即給一個false條件create table new_tableselect * from old_table where 1=2;# 第二種方法create table new_table like old_table;

2.新表存在
復制舊表數據到新表(假設兩個表結構一樣)

insert into new_tableselect * from old_table;

復制舊表數據到新表(假設兩個表結構不一樣)

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

復制全部數據

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)表已經存在復制

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) 

 



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 利辛县| 布拖县| 海宁市| 察隅县| 昌平区| 四子王旗| 五华县| 馆陶县| 扬州市| 溆浦县| 合阳县| 乳山市| 宝清县| 湖北省| 呼伦贝尔市| 儋州市| 郯城县| 元朗区| 青州市| 婺源县| 玉树县| 兴国县| 陵川县| 三门县| 祁东县| 府谷县| 库伦旗| 保德县| 延吉市| 新泰市| 土默特左旗| 漯河市| 安仁县| 砚山县| 成都市| 祁门县| 乐东| 镇江市| 孝昌县| 沧源| 沈丘县|