用shell腳本通過while循環(huán)批量生成mysql測試數(shù)據(jù)的方法。

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供天橋網(wǎng)站建設(shè)、天橋做網(wǎng)站、天橋網(wǎng)站設(shè)計(jì)、天橋網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、天橋企業(yè)網(wǎng)站模板建站服務(wù),十多年天橋做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
1、很多時(shí)候需要在mysql表中插入大量測試數(shù)據(jù),下面分享一個(gè)用shell腳本通過while循環(huán)批量生成mysql測試數(shù)據(jù)的方法,你只需要根據(jù)你自己的表結(jié)構(gòu)來生成sql語句即可。
復(fù)制代碼代碼如下:
#!/bin/bash
i=1;
MAX_INSERT_ROW_COUNT=$1;
while [ $i -le $MAX_INSERT_ROW_COUNT ]
do
mysql -uroot -proot afs -e "insert into afs_test (name,age,createTime) values ('HELLO$i',$i % 99,NOW());"
d=$(date +%M-%d\ %H\:%m\:%S)
echo "INSERT HELLO $i @@ $d"
i=$(($i+1))
sleep 0.05
done
exit 0
2、假定上面的shell腳本保存為create-data.sh,可以通過下面的命令來生成數(shù)據(jù):
復(fù)制代碼代碼如下:sh create-data.sh 10000。(參數(shù)10000是要生成的數(shù)據(jù)條數(shù)。)
mysql一千萬數(shù)據(jù)插入另外一個(gè)表的方法:
1、INSERTINTO目標(biāo)表SELECT*FROM來源表;2、例如,要將articles表插入到newArticles表中,則可以通過如下SQL語句實(shí)現(xiàn)3、INSERTINTOnewArticlesSELECT*FROMarticles。
這里以前面新建的 teacher 表為例, teacher 表的字段為 name、age、id_number,向教師表新增一條數(shù)據(jù)命令如下:
執(zhí)行下上面的這條命令,執(zhí)行結(jié)果如下圖:
其中 “INSERT INTO” 關(guān)鍵字表示這條命令是插入數(shù)據(jù)命令,“teacher” 是要插入數(shù)據(jù)的目標(biāo)數(shù)據(jù)表名稱,“name,age,id_number” 是表的字段名,“VALUES” 后面跟的是字段對應(yīng)的值,值的順序和前面字段的順序一致。
上面介紹了如何向數(shù)據(jù)表插入一條數(shù)據(jù),這里需要查看數(shù)據(jù)表已有的所有數(shù)據(jù):
執(zhí)行結(jié)果如下圖:
“SELECT” 關(guān)鍵字表示這條命令是查詢相關(guān)命令,"*" 則代表要查詢出表中所有的數(shù)據(jù)。“FROM teacher” 則表明要查詢的是哪一個(gè)數(shù)據(jù)表。
關(guān)于 SELECT 查詢語句還有很多中使用場景,比如我們要查詢出表中 age 字段的值大于 20 歲的數(shù)據(jù)。關(guān)于 SELECT 的其他使用我們會(huì)在后面的小節(jié)詳細(xì)講解。
新增多條數(shù)據(jù)命令:
執(zhí)行結(jié)果如下圖:
本小節(jié)介紹了如圖向數(shù)據(jù)表插入一條數(shù)據(jù)、查詢表所有數(shù)據(jù)、向表插入多條數(shù)據(jù)。前面介紹數(shù)據(jù)表的設(shè)計(jì)規(guī)范時(shí)介紹了數(shù)據(jù)表的第二設(shè)計(jì)范式要求所有數(shù)據(jù)表需要有業(yè)務(wù)主鍵。需要注意的是本節(jié)中 teacher 表的業(yè)務(wù)主鍵為自增 id,因此插入數(shù)據(jù)的時(shí)候不需要插入 id 字段的值。id 字段的默認(rèn)是從 1 開始自增的,也可以指定自增起始值,如下建表語句,id自增值是從100開始的:
其中 “AUTO_INCREMENT=100” 表示自增主鍵 id 的值默認(rèn)從 100 開始自增加的。
本節(jié)介紹數(shù)據(jù)的插入,復(fù)制數(shù)據(jù)到另一張表的Sql語法,主要語法有: insert into,insert into select,select into from 等用法,下面將一一為大家詳細(xì)說明:
以下面兩張表進(jìn)行sql腳本說明
insert into有兩種語法,分別如下:
語法1:INSERT INTO?table_name?VALUES (value1,value2,value3,...);? ?--這種形式無需指定要插入數(shù)據(jù)的列名,只需提供被插入的值即可:
語法2:INSERT INTO?table_name?(column1,column2,column3,...) VALUES (value1,value2,value3,...);? ? --這種形式需指定要插入數(shù)據(jù)的列名,插入的值需要和列名一一對應(yīng):
eg:insert into customer values('1006','14006','王欣欣','27','深圳市');? --向表customer插入一條數(shù)據(jù)
eg:insert into customer values('1007','14007','孟一凡','27','');? ? ? ? ? ? ?--向表customer插入一條數(shù)據(jù),最后一個(gè)值不填表示對應(yīng)的值為空,非必填項(xiàng)可以不用插入值
eg:insert into customer (cus_id,cus_no,cus_name,cus_age,cus_adds) values('1008','14008','孔凡','26','廣州市');? ? ? --向表customer插入一條數(shù)據(jù),插入的值與列名一一對應(yīng)
詳解:insert into select? ? --表示從一個(gè)表復(fù)制數(shù)據(jù),然后把數(shù)據(jù)插入到一個(gè)已存在的表中。目標(biāo)表中任何已存在的行都不會(huì)受影響。
語法1:INSERT INTO?table_name2?SELECT? * FROM?table_name1;? --表示將表table_name1中復(fù)制所有列的數(shù)據(jù)插入到已存在的表table_name2中。被插入數(shù)據(jù)的表為table_name2,切記不要記混了。
eg:insert into customer select * from asett ? --將表asett中所有列的數(shù)據(jù)插入到表customer中
語法2:INSERT INTO?table_name2?(column_name(s))?SELECT?column_name(s)?FROM? table_name1;? --指定需要復(fù)制的列,只復(fù)制制定的列插入到另一個(gè)已存在的表table_name2中:
eg:insert into customer (cus_id,cus_no) select ast_id,ast_no from asett ? --將表asett中列ast_id和ast_no的數(shù)據(jù)插入到表customer對應(yīng)的cus_id,cus_no列中
詳解:從一個(gè)表復(fù)制數(shù)據(jù),然后把數(shù)據(jù)插入到另一個(gè)新表中。
語法1:SELECT * INTO?newtable?[IN?externaldb] FROM?table1;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?--復(fù)制所有的列插入到新表中:
eg:select * into?customer from?asett? ?? --將asett表中數(shù)據(jù)插入到customer中,被插入的 表customer不存在
eg:select * into?customer from?asett where ast_id = '1008'? ? --只復(fù)制表asett中ast_id=1008的數(shù)據(jù)插入到customer中,被插入的 表customer不存在
語法2:SELECT?column_name(s)?INTO?newtable?[IN?externaldb] FROM?table1;? ?--只復(fù)制指定的列插入到新表中:
eg:select ast_id,ast_no into?customer?from?asett? --將asett表中列ast_id,ast_no數(shù)據(jù)插入到customer中,被插入的 表customer不存在
區(qū)別1:insert into customer select * from asett where ast_id='1009' --插入一行,要求表customer?必須存在
區(qū)別2:select * into customer? from asett? where ast_id='1009' --也是插入一行,要求表customer? 不存在
區(qū)別3:select into from?:將查詢出來的數(shù)據(jù)復(fù)制到一張新表中保存,表結(jié)構(gòu)與查詢結(jié)構(gòu)一致。
區(qū)別4:insert into select?:為已經(jīng)存在的表批量添加新數(shù)據(jù)。
網(wǎng)頁題目:mysql數(shù)據(jù)表怎么插入,mysql表里怎么添加數(shù)據(jù)
網(wǎng)頁地址:http://chinadenli.net/article33/dsshjss.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、標(biāo)簽優(yōu)化、網(wǎng)站設(shè)計(jì)公司、App開發(fā)、靜態(tài)網(wǎng)站、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)