sql中select into和insert select的用法小結(jié)
在工作中,我們經(jīng)常需要備份表,今天來(lái)聊一下備份表用到的sql語(yǔ)句
一、復(fù)制表結(jié)構(gòu)及數(shù)據(jù)
復(fù)制表結(jié)構(gòu)及數(shù)據(jù)有三種方法,不同類型數(shù)據(jù)庫(kù)所支持的語(yǔ)法有所不同,下面具體說(shuō)
1.create table as select ...
這條語(yǔ)句適用于大多數(shù)關(guān)系型數(shù)據(jù),也適用于數(shù)據(jù)倉(cāng)庫(kù)hive。下面我們用MySQL環(huán)境去執(zhí)行(非MySQL有備注)。目標(biāo):將t_user的表結(jié)構(gòu)及數(shù)據(jù)復(fù)制到t_user_2中。
先show create table t_user看下t_user的表結(jié)構(gòu)
再查下t_user的數(shù)據(jù)
將t_user的表結(jié)構(gòu)及數(shù)據(jù)復(fù)制到t_user_2,執(zhí)行語(yǔ)句:
create table t_user_2 as select * from t_user;
執(zhí)行完畢后查看表t_user_2
我們可以看到已經(jīng)成功把t_user的結(jié)構(gòu)及數(shù)據(jù)都復(fù)制到了t_user_2。
2.select into
SQL Server、Oracle、Greenplum支持select into語(yǔ)法,但MySQL不支持。目標(biāo):創(chuàng)建表t_user_3的同時(shí),將t_user的全部數(shù)據(jù)插入到t_user_3,實(shí)現(xiàn):
select * into t_user_3 from t_user;
注:此條語(yǔ)句是在Greenplum的環(huán)境下執(zhí)行,MySQL不支持select into語(yǔ)法
3.create table like;insert select...;
這條語(yǔ)句可以分成為兩部分:
(1) create table like;
復(fù)制表結(jié)構(gòu),實(shí)現(xiàn):
create table t_user_4 like t_user;
將t_user的表結(jié)構(gòu)復(fù)制到t_user_4。
(2) insert select;
插入數(shù)據(jù),實(shí)現(xiàn):
insert t_user_4 select * from t_user;
將t_user表的全部數(shù)據(jù)插入到t_user_4。
合并執(zhí)行:
create table t_user_4 like t_user;insert t_user_4 select * from t_user;
效果:
二、僅復(fù)制表結(jié)構(gòu)
下面來(lái)說(shuō)兩種復(fù)制表結(jié)構(gòu)的方法。
1.create table like
目標(biāo):把t_user表結(jié)構(gòu)復(fù)制到t_user_5,實(shí)現(xiàn):
create table t_user_5 like t_user;
效果:
2.create table as
create table as也能單獨(dú)復(fù)制表結(jié)構(gòu),例如將t_user的表結(jié)構(gòu)復(fù)制到t_user_6
create table t_user_6 as select * from t_user where 1=2;
效果:
三、僅復(fù)制數(shù)據(jù)
無(wú)需復(fù)制表結(jié)構(gòu),僅復(fù)制表數(shù)據(jù)可以使用insert table select語(yǔ)句,在上面我們創(chuàng)建了表t_user_6,現(xiàn)在想把表t_user的數(shù)據(jù)全量寫入t_user_6,執(zhí)行sql:
insert t_user_6 select * from t_user;
效果如下,可以看到把t_user的數(shù)據(jù)都插入到了t_user_6
到此這篇關(guān)于sql中select into和insert select的用法小結(jié)的文章就介紹到這了,更多相關(guān)sql中select into和insert select內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SELECT INTO 和 INSERT INTO SELECT 兩種表復(fù)制語(yǔ)句詳解(SQL數(shù)據(jù)庫(kù)和Oracle數(shù)據(jù)庫(kù)的區(qū)別)
- 解析MySQL中INSERT INTO SELECT的使用
- insert select與select into 的用法使用說(shuō)明
- SQL Server之SELECT INTO 和 INSERT INTO SELECT案例詳解
- select?into?from和insert?into?select的使用舉例詳解
- select?into?from和insert?into?select的區(qū)別舉例詳解
相關(guān)文章
MySQL表數(shù)據(jù)文件損壞導(dǎo)致數(shù)據(jù)庫(kù)無(wú)法啟動(dòng)的原因與解決方案
在日常的數(shù)據(jù)庫(kù)管理中,遇到MySQL表數(shù)據(jù)文件損壞的情況并不罕見(jiàn),這種情況下,MySQL數(shù)據(jù)庫(kù)可能會(huì)無(wú)法正常啟動(dòng),給業(yè)務(wù)運(yùn)行帶來(lái)嚴(yán)重影響,本文將探討如何診斷和解決MySQL表數(shù)據(jù)文件損壞導(dǎo)致的數(shù)據(jù)庫(kù)無(wú)法啟動(dòng)問(wèn)題,需要的朋友可以參考下2025-03-03MySQL replace函數(shù)替換字符串語(yǔ)句的用法
MySQL replace函數(shù)我們經(jīng)常用到,下面就為您詳細(xì)介紹MySQL replace函數(shù)的用法,希望對(duì)您學(xué)習(xí)MySQL replace函數(shù)方面能有所啟迪。2010-12-12percona-toolkit對(duì)MySQL的復(fù)制和監(jiān)控類操作教程
這篇文章主要介紹了使用percona-toolkit對(duì)MySQL進(jìn)行復(fù)制和監(jiān)控類操作的教程,percona-toolkit是一款強(qiáng)大的MySQL輔助軟件,需要的朋友可以參考下2015-11-11mysql 使用inet_aton和inet_ntoa處理ip地址數(shù)據(jù)的實(shí)例
下面小編就為大家?guī)?lái)一篇mysql 使用inet_aton和inet_ntoa處理ip地址數(shù)據(jù)的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04