MySQL數(shù)據(jù)庫高級數(shù)據(jù)操作之新增數(shù)據(jù)
多數(shù)據(jù)插入
只要寫一次insert,可以插入多條數(shù)據(jù)
基本語法:
insert into 表名 [(字段列表)] values (值列表), (值列表)...; create table my_student( id int primary key auto_increment, name varchar(10) ); insert into my_student (name) values ('張三'), ('李四'), ('王五'); mysql> select * from my_student; +----+--------+ | id | name | +----+--------+ | 1 | 張三 | | 2 | 李四 | | 3 | 王五 | +----+--------+
主鍵沖突
insert into my_student (id, name) values (1, '張飛'); ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
1、主鍵沖突更新
如果插入過程中主鍵沖突,那么采用更新方式
insert into 表名 [(字段列表)] on duplicate key update 字段=新值; insert into my_student (id, name) values (1, '張飛') on duplicate key update name = '張飛'; mysql> select * from my_student; +----+--------+ | id | name | +----+--------+ | 1 | 張飛 | | 2 | 李四 | | 3 | 王五 | +----+--------+
2、主鍵沖突替換
replace into 表名 [(字段列表)] values (值列表); replace into my_student (id, name) values (1, '劉備'); mysql> select * from my_student; +----+--------+ | id | name | +----+--------+ | 1 | 劉備 | | 2 | 李四 | | 3 | 王五 | +----+--------+
蠕蟲復(fù)制
一分為二,成倍增加
從已有的數(shù)據(jù)中獲取數(shù)據(jù),并且插入到數(shù)據(jù)表中
insert into 表名 [(字段列表)] select */字段列表 from 表名; insert into my_student (name) select name from my_student; mysql> select * from my_student; +----+--------+ | id | name | +----+--------+ | 1 | 劉備 | | 2 | 李四 | | 3 | 王五 | | 4 | 劉備 | | 5 | 李四 | | 6 | 王五 | +----+--------+
注意:
- 蠕蟲復(fù)制通常是重復(fù)數(shù)據(jù),可以短期內(nèi)復(fù)制大量的數(shù)據(jù),從而測試表的壓力
- 需要注意主鍵沖突
到此這篇關(guān)于MySQL數(shù)據(jù)庫高級數(shù)據(jù)操作之新增數(shù)據(jù)的文章就介紹到這了,更多相關(guān)MySQL新增數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
當(dāng)mysqlbinlog版本與mysql不一致時(shí)可能導(dǎo)致出哪些問題
這篇文章主要介紹了當(dāng)mysql服務(wù)器為mysql5.6時(shí),mysqlbinlog版本不對可能導(dǎo)致出哪些問題,下面通過模擬2種場景分析此類問題,需要的朋友可以參考下2015-07-07mysql8.0.30安裝配置最詳細(xì)教程(windows?64位)
這篇文章主要給大家介紹了關(guān)于windows?64位下mysql8.0.30安裝配置的相關(guān)資料,主要以圖片的形式展示安裝教程x,簡單易懂,小白專屬,需要的朋友可以參考下2022-09-09CentOS7.5 安裝 Mysql8.0.19的教程圖文詳解
這篇文章主要介紹了CentOS7.5 安裝 Mysql8.0.19的教程,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01MySQL數(shù)據(jù)庫MyISAM存儲引擎轉(zhuǎn)為Innodb的方法
mysql數(shù)據(jù)庫存儲引擎為MyISAM的時(shí)候,在大訪問量的情況下數(shù)據(jù)表有可能會出現(xiàn)被鎖的情況,這就會導(dǎo)致用戶連接網(wǎng)站時(shí)超時(shí)而返回502,此時(shí)就需要MySQL數(shù)據(jù)庫MyISAM存儲引擎轉(zhuǎn)為Innodb,這篇文章主要介紹了MySQL數(shù)據(jù)庫MyISAM存儲引擎轉(zhuǎn)為Innodb的方法,需要的朋友可以參考下2014-06-06MySql獲取某個(gè)字段存在于哪個(gè)表的sql語句
本文為大家詳細(xì)介紹下通過MySql查詢某個(gè)字段所在表是哪一個(gè),具體的sql語句如下,感興趣的朋友可以參考下,希望對大家有所幫助2013-07-07