積跬步,聚小流------oracle快捷添加測試數(shù)據(jù)
來源:程序員人生 發(fā)布時(shí)間:2015-02-09 08:39:10 閱讀次數(shù):3003次
前1陣子在實(shí)行中發(fā)現(xiàn)問題,需要當(dāng)時(shí)進(jìn)行修改,而由于數(shù)據(jù)庫中數(shù)據(jù)是真實(shí)數(shù)據(jù),不能進(jìn)行修改,否則會(huì)出大紕漏吧,因而添加測試數(shù)據(jù)來方便修改,而單個(gè)添加效力太低下了,所以解決的辦法就是以真實(shí)的數(shù)據(jù)為模板增加新方便刪除的數(shù)據(jù)便可,就像將2014年的數(shù)據(jù)復(fù)制1份只將年份進(jìn)行修改,刪除的時(shí)候講這個(gè)不存在的年份數(shù)據(jù)刪除便可。
相信大家很容易會(huì)想到這個(gè)方法,也很容易做出答案,舉個(gè)例子:

看這個(gè)表,由于主鍵中都是以當(dāng)年年份開頭的,同時(shí)年度也是當(dāng)年年份,這樣我們就能夠進(jìn)行添加修改:
假使說這個(gè)表格存在以下列:
btfid、production、code、retrieveid、location、tobaccostation、plantvillage、cooperation、tobaccotechnician、eastlong、eastlat、southlong、southlat、westlong、westlat、northlong、northlat、amsl、totalarea
那樣我們可以可以這樣寫:
insert into
arc_basictobaccofield
select '2016' || substr(btfid, 5),
'2016',
code,
retrieveid,
location,
tobaccostation,
plantvillage,
cooperation,
tobaccotechnician,
eastlong,
eastlat,
southlong,
southlat,
westlong,
westlat,
northlong,
northlat,
amsl,
totalarea
from arc_basictobaccofield
where tobaccostation = '37030405C'
and productionyear = 2015
然后在刪除的時(shí)候只需要將年份為2016的全部刪除便可,但是這里還存在1個(gè)問題,實(shí)際上表的屬性可能不單單只有這么點(diǎn)列,事實(shí)上,就算只有這幾列,也遠(yuǎn)遠(yuǎn)大于我們需要修改的兩列數(shù)據(jù)了,我們是否是可以有更好的辦法來解決它呢?
答案是肯定的,這樣我們倒過來思考下,我們只需要修改兩列而已,那我們就把所有數(shù)據(jù)取出來,將這兩列數(shù)據(jù)修改以后進(jìn)行插入不就能夠了么,我們來寫下看:
首先我們來創(chuàng)建1個(gè)臨時(shí)表:
create table arc_basictobaccofield1 as
select * from arc_basictobaccofield where tobaccostation='37030405C' and productionyear=2015
然后我們將需要修改的列進(jìn)行修改:
update arc_basictobaccofield1 set productionyear=2015
update arc_basictobaccofield1 set btfid ='2015'||substr(btfid,5)
這時(shí)候候?qū)⑿薷暮蟮臄?shù)據(jù)導(dǎo)入我們需要導(dǎo)入的表中:
insert into arc_basictobaccofield select * from arc_basictobaccofield1
最后這1步還是很關(guān)鍵的,千萬不能忘掉:檢查導(dǎo)入數(shù)據(jù),然后刪除臨時(shí)表
drop table arc_basictobaccofield1
這樣再來看,是否是不需要估計(jì)真實(shí)表究竟有多少列了呢,可見許多時(shí)候換個(gè)思考方式還是很有效力的

生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)