mysql中insert語句的5種用法簡單示例
前言
insert語句是標(biāo)準(zhǔn)sql中的語法,是插入數(shù)據(jù)的意思。在實際應(yīng)用中,它也演變了很多種用法來實現(xiàn)特殊的功能,下面介紹在mysql數(shù)據(jù)庫中insert語句的五種用法。
一、values參數(shù)后單行插入
語法:
insert into tableName (colunm1,colunm2,...) value(value1,value2,...);
如果插入多條數(shù)據(jù),需要寫多條sql。
insert into a(id,name,type) values (1,'A1','T1'); insert into a(id,name,type) values (2,'A2','T2');
二、values參數(shù)后多行插入
語法:
insert into tableName(colunm1,colunm2,..) values(value1,value2...),(value1,value2...);
多條數(shù)據(jù)1條sql即可,相較于方法1效率更高。
insert into a(id,name,type) values (1,'A1','T1'),(2,'A2','T2');
三、搭配select插入數(shù)據(jù)
語法:
insert into tableName(colunm1,colunm2,..) select colunm1,colunm2,..;
多條數(shù)據(jù)使用union all關(guān)聯(lián)即可。
insert into a(id,name,type) select 1,'A1','T1' union all select 2,'A2','T2';
四、復(fù)制舊表的信息到新表
語法:
insert into tableName(colunm1,colunm2,..) select colunm1,colunm2,.. from tableName1;
假設(shè)兩個表的表結(jié)構(gòu)一樣則語句如下,否則請指定字段名稱。
insert into a select * from b where id=1;
五、搭配set插入數(shù)據(jù)
語法:
insert into tableName set colunm1=value1,colunm2=value2....;
使用set是拓展寫法,可以精準(zhǔn)的對列賦值,防止賦值時由于順序混亂導(dǎo)致的數(shù)據(jù)錯誤,同時這種寫法插入數(shù)據(jù)的速度更快,但不適合批量循環(huán)插入。
insert into a set id=1,name='A1',type='T1';
總結(jié)
到此這篇關(guān)于mysql中insert語句的5種用法的文章就介紹到這了,更多相關(guān)mysql insert語句用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Grafana+Prometheus監(jiān)控mysql服務(wù)性能
這篇文章主要介紹了使用Grafana+Prometheus監(jiān)控mysql服務(wù)性能的相關(guān)知識,本文給大家介紹的非常詳細(xì),對大家的工作或?qū)W習(xí)具有一定的參考借鑒價值,需要的朋友可以參考下方法2020-03-03
mysql5.7與mysql8.0身份認(rèn)證插件的區(qū)別及說明
MySQL5.7和MySQL8.0在身份認(rèn)證插件、密碼管理和安全性方面有顯著區(qū)別,MySQL8.0引入了更安全的caching_sha2_password插件,默認(rèn)使用SHA-256哈希算法,并提供更靈活的密碼過期策略,遷移時需要考慮客戶端庫的兼容性問題,可能需要升級或更改用戶的認(rèn)證插件2025-11-11
mysql查詢優(yōu)化之100萬條數(shù)據(jù)的一張表優(yōu)化方案
這篇文章主要介紹了mysql查詢優(yōu)化之100萬條數(shù)據(jù)的一張表優(yōu)化方案,需要的朋友可以參考下2021-05-05

