数据库基础

1.DSN
dsn(本地服务名,数据库链接串中的Data Source部分)即oracle客户端目录下tnsnames.ora中定义的每个节“=”号前边的部分,如
如果是oracle92客户端,此文件一般在C:\oracle92\network\ADMIN\下;
如果是oracle11g客户端,此文件一般在D:\app\Administrator\product\11.2.0\client_1\network\ADMIN下

常用内置函数

to_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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值