亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

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

標簽:
MySQL

MySQL是不支持SELECT … INTO语法的,使用INSERT INTO … SELECT替代相同用法,下面我们我们这里简答分一下新表存在和不存在两种情况,具体使用不同的语句。
1.新表不存在
复制表结构即数据到新表

?

12create table new_tableselect * from old_talbe;

这种方法会将old_table中所有的内容都拷贝过来,用这种方法需要注意,new_table中没有了old_table中的primary key,Extra,auto_increment等属性,需要自己手动加,具体参看后面的修改表即字段属性.
只复制表结构到新表

?

123456# 第一种方法,和上面类似,只是数据记录为空,即给一个false条件create table new_tableselect * from old_table where 1=2; # 第二种方法create table new_table like old_table;

2.新表存在
复制旧表数据到新表(假设两个表结构一样)

?

12insert into new_tableselect * from old_table;

复制旧表数据到新表(假设两个表结构不一样)

?

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

复制全部数据

?

1select * into new_table from old_table;

只复制表结构到新表

?

1select * 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; //只复制nameQuery 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)

 

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
移動開發工程師
手記
粉絲
20
獲贊與收藏
132

關注作者,訂閱最新文章

閱讀免費教程

  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消