積跬步,聚小流------oracle快捷添加測試資料,------oracle

來源:互聯網
上載者:User

積跬步,聚小流------oracle快捷添加測試資料,------oracle

前一陣子在實施中發現問題,需要當時進行修改,而因為資料庫中資料是真實資料,不能進行修改,否則會出大紕漏吧,於是添加測試資料來方便修改,而單個添加效率太低下了,所以解決的辦法就是以真實的資料為模板增添新方便刪除的資料即可,就像將2014年的資料複製一份只將年份進行修改,刪除的時候講這個不存在的年份資料刪除即可。

相信大家很容易會想到這個方法,也很容易做出答案,舉個例子:


看這個表,因為主鍵中都是以當年年份開頭的,同時年度也是當年年份,這樣我們就可以進行添加修改:

假使說這個表格存在如下列:

btfid、production、code、retrieveid、location、tobaccostation、plantvillage、cooperation、tobaccotechnician、eastlong、eastlat、southlong、southlat、westlong、westlat、northlong、northlat、amsl、totalarea

那樣我們可以可以這樣寫:

insert into arc_basictobaccofieldselect '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

然後在刪除的時候只需要將年份為2016的全部刪除即可,但是這裡還存在一個問題,實際上表的屬性可能不僅僅只有這麼點列,事實上,就算只有這幾列,也遠遠大於我們需要修改的兩列資料了,我們是不是可以有更好的辦法來解決它呢?

答案是肯定的,這樣我們倒過來思考下,我們只需要修改兩列而已,那我們就把所有資料取出來,將這兩列資料修改之後進行插入不就可以了麼,我們來寫下看:

首先我們來建立一個暫存資料表:

create table arc_basictobaccofield1 as select * from arc_basictobaccofield where tobaccostation='37030405C' and productionyear=2015

然後我們將需要修改的列進行修改:

update arc_basictobaccofield1 set productionyear=2015update arc_basictobaccofield1 set btfid ='2015'||substr(btfid,5)

這時候將修改後的資料匯入我們需要匯入的表中:

insert into arc_basictobaccofield select * from arc_basictobaccofield1

最後這一步還是很關鍵的,千萬不能忘掉:檢查匯入資料,然後刪除暫存資料表

drop table arc_basictobaccofield1

這樣再來看,是不是不需要估計真實表究竟有多少列了呢,可見許多時候換個思考方式還是很有效率的




相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.