国产探花免费观看_亚洲丰满少妇自慰呻吟_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) 

 



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 老河口市| 靖宇县| 雷州市| 罗田县| 杭州市| 兴安县| 长治县| 东光县| 三穗县| 饶阳县| 旬阳县| 宜昌市| 台东市| 林周县| 鹤山市| 德昌县| 汝城县| 彭泽县| 浠水县| 兰州市| 泸溪县| 伊春市| 石河子市| 股票| 肥东县| 依安县| 手游| 扬中市| 广州市| 许昌市| 梧州市| 潜江市| 赤壁市| 潜山县| 淳安县| 邢台市| 五莲县| 盐源县| 宁蒗| 余姚市| 颍上县|