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

首頁 > 數據庫 > MySQL > 正文

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

2024-07-24 12:45:05
字體:
來源:轉載
供稿:網友

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)

 

您可能感興趣的文章:

Python SqlAlchemy動態添加數據表字段實例解析java使用JDBC動態創建數據表及SQL預處理的方法如何將Oracle的一個大數據表快速遷移到 Sqlserver2008數據庫(圖文教程)Angualrjs和bootstrap相結合實現數據表格tablebootstrap table 數據表格行內修改的實現代碼MySQL中大數據表增加字段的實現思路mysql中數據庫與數據表編碼格式的查看、創建及修改Android實現仿excel數據表格效果MySQL清空數據表的方法實例與分析jQuery EasyUI框架中的Datagrid數據表格組件結構詳解Oracle的數據表中行轉列與列轉行的操作實例講解詳解數據庫中跨庫數據表的運算
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 中西区| 修武县| 高雄市| 饶平县| 马龙县| 荣昌县| 鄂尔多斯市| 雷波县| 启东市| 巩留县| 南城县| 大石桥市| 应用必备| 海淀区| 金塔县| 炉霍县| 司法| 会东县| 宜宾县| 集贤县| 汾阳市| 红河县| 平阳县| 东乡县| 三都| 遂宁市| 宁陕县| 松潘县| 宣武区| 乡城县| 碌曲县| 瑞丽市| 涪陵区| 达拉特旗| 余庆县| 恭城| 邓州市| 文水县| 孝昌县| 舒兰市| 濮阳县|