資料庫基礎

2021-09-23 22:25:20 字數 3983 閱讀 7905

1.dsnto_date:日期轉化函式,如to_date(『2012-08-09』, 『yyyy-mm-dd』)、 to_date(『20120809』, 『yyyymmdd』)、 to_date(『2012-08-09 15:35:28』, 『yyyy-mm-dd hh24:mi:ss』)、 to_date(『2012-08-09 10:35:28』, 『yyyy-mm-dd hh12:mi:ss』) 注意是mi不是mm

date:日期轉化函式,必須使用帶-的日期格式,如date 『2012-08-09『或者』2012-8-9『(date』:rq』這種方式目前資料訪問層不認識)

add_months:年月加減函式,如add_months(date 『2012-8-09』,-1)

last_day:返回某日期所在月份的最後一天,如last_day(date 『2014-7-8』)

trunc:日期取整,去除時分秒 ,如trunc(to_date(『2012-08-09 10:35:28』, 『yyyy-mm-dd hh12:mi:ss』))返回不帶時分秒的日期,即等於to_date(『2012-08-09,『yyyy-mm-dd』)

round:四捨五入,如round(3.2893,2)得到3.29

nvl:空判斷函式,如nvl(a.jh,b.jh)解釋為如果a.jh為null,則返回b.jh,否則返回a.jh

decode:值判斷函式,如decode(spbz,』1』,』已審批』,』未審批』)

case when

replace:字串替換,如replace(『abcd』,』cd』,』1』)得到ab1

lower:字元小寫,如lower(『abcd』)返回abcd

upper:字元大寫,如upper(『abcd』)返回abcd

length:返回字串長度,如length(『abcd』)返回4,漢字當作1位元組長度,如length(『我』)返回1

lengthb:返回字串位元組長度,如lengthb(『abcd』)返回4,漢字當作2位元組,如lengthb(『我』)返回2

常用業務函式:

1.f_div(number n1,number n2) 除法函式,當被除數n2為0自動返回null,如f_div(3.5,0)返回null

2.f_hs(number v_yl,nunmber v_sl) 求油井的含水,v_yl(液量),v_sl(水量)

3.f_nl(number vyl,number vscts) 求能力,vyl(液量、油量、注水量等), vscts(生產、注水天數)

4.f_sp(number vyl,char vny) 求水平,vyl(液量、油量、注水量等),vny(年月)

5. get_dwscgs(varchar2 dwdm) 返回單位**對應的單位名稱

6. get_gtuser_name(varchar2 login) 返回某使用者的姓名

7. f_rqtime(varchar2 time1,varchar2 time2) 返回某日期段、年月段和年度段之間的所有元素,返回型別是table,只有rq

例如:如select rq from table(f_rqtime(『20130809』, 『20130815』))

得到

常用於和資料表進行左關聯,取出固定的時間,不管資料表有沒有資料

8、資料字典自動生成過程

p_auto_xtb:會自動處理表xtb_dict_table和xtb_dict_field

常用sql操作:

1.從別的資料表獲取資料插入原資料表

insert into t(t1,t2,t3) select t1,t2,t3 from a

單條記錄具體值的插入大家都知道,就不說了

2.從別的資料表獲取資料更新原資料表

update 表1 set 列1=(select 列2 from 表2 where 表2.列3=表1的列3),如:以用gtuit表資料更新dab60的單位名稱為例

update dab60 t set dwscgs=(select dwscgs from gtunit where dwdm=t.dwdm)

3.在子查詢中關聯主表獲取最大對應記錄

盡量在子句中和主表進行條件關聯,以獲取油井日資料ys_dba01中jh=『e3a98』的最大生產日期的日產液量為例

推薦寫法:

select rcyl1 from ys_dba01 t where t.rq=(select max(rq) from ys_dba01 where jh=t.jh) and t.jh= 『e3a98』 ;

不推薦寫法:

selct a.rcyl1 from (select jh,rq ,rcyl1 from ys_dba01 where jh= 『e3a98』)a ,(select jh,max(rq) rq from ys_dba01 where jh= 『e3a98』 group by jh) b where a.jh=b.jh and a.rq=b.rq

3.修改表資料

盡量在pl/sql中少用for update語句,會經常導致鎖表,使用右鍵表->編輯資料的方式修改資料

1.in(in具體值效率不受影響,這裡是多表關聯)

原語句:select jh from ys_dba01 where jh in(select jh from ys_daa01)

優化1:表外部全關聯方式

select jh from ys_dba01 a, ys_daa01 b

where a.jh=b.jh

優化2:使用exists

select jh from ys_dba01 a where exists(select 『x』 from ys_daa01 where jh=a.jh)

2.not in(指多表關聯時,not in會執行全表檢索)

原語句:select jh from ys_dba01 where jh not in(select jh from ys_daa01)

優化1:表外部左連線關聯方式

select a.jh from ys_dba01 a, ys_daa01 b

where a.jh=b.jh(+) and b.jh is null

優化2:使用not exists

select jh from ys_dba01 a where not exists(select 『x』 from ys_daa01 where jh=a.jh)

3.避免在索引列上進行型別轉化

低效:select jh from ys_dba01 where to_char(rq,』yyyy-mm-dd』)=『2014-08-09』

因為rq是ys_dba01的主鍵之一,使用to_char導致型別變成了字串,索引的功能就沒有了

高效:select jh from ys_dba01 where rq=date 『2014-08-09』

sql> insert all

2  into test1 values(3, 『cc』)

3  into test2 values(1, 2, 3)

4  select * from dual;

資料庫 資料庫基礎

什麼是sql 結構化查詢語言 structtured query language sql的作用 啟動mysql.exe,連線伺服器後,就可以使用sql來操作伺服器了。類似php中操作mysql的語句就是sql語句 sql標準 由國際標準化組織 iso 制定的,對dbms 資料庫管理系統 的統一操作...

資料庫基礎 資料庫設計

一 資料庫生命週期 1.週期 需求分析,概念結構設計,邏輯設計,物理設計,系統實施,系統執行和維護。2.響應的文件 二 正規化 一般資料庫要求規範化到第三正規化,視具體需求重新調整。第一正規化 1nf 強調列的原子性,即列不能分為多列。第二正規化 2nf 關係屬於第一正規化,且每乙個非主屬性完全函式...

資料庫基礎

一.資料庫事物特徵 1.atomic 原子性 不可分割 2.consistency 一致性 參照完整 3.isolation 隔離性 防止併發 4.durability 永續性 永久儲存資料 二.事物和併發 1.併發問題 第一類丟失,髒讀,虛讀,不可重複讀,第二類丟失更新 不可重複讀的特例 2.鎖的...