ORACLE 常用的知识

tech2025-11-01  6

用了2年多的MySQL 然后用ORACLE感觉区别不大, 主要是 数据类型、分页、函数 有些不一样,增删改 需要手动commit

创建表,增删改-表字段 与MySQL 一样

数据类型

常用函数(持续更新)

数值

select round(56.16, -2) from dual;---四舍五入,后面的参数表示保留的位数 select trunc(56.16, -1) from dual;---直接截取,不在看后面位数的数字是否大于5. select mod(10, 3) from dual;---求余数

字符

select upper('yes') from dual;--YES select lower('YES') from dual;--yes

格式1:substr(string string, int a, int b);   格式2:substr(string string, int a) ; ps: a 截取字符串的开始位置(注:当a等于0或1时,都是从第一位开始截取) substr('HelloWorld',0,3); //返回结果:Hel,截取从“H”开始3个字符

日期

sysdate; -- 当前时间 select sysdate+1 from dual;----算出明天此刻 select months_between(sysdate,e.hiredate) from emp e;----查询出emp表中所有员工入职距离现在几月。

转换

select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') from dual;---日期转字符串 select to_date('2018-06-07 16:39:50','yyyy-mm-dd hh24:mi:ss') from dual;---字符串转日期

其他

NVL(exp1,exp2)函数的作用机制 : 如果 exp1 为空 , 函数 返回 exp2 的值 ; 如果 exp1 不为空, 函数 返回 exp1 的值 ; 如果两个参数都为NULL ,则返回NULL

NVL2(exp1,exp2,exp3) 如果 exp1 不为空 ,函数 返回 exp2 的值; 如果 exp1 为空 ,函数 返回 exp3 的值;

ORACLE分页

因为没有mysql 的 limit 所以 使用rownum(行号-虚拟) 当select操作的时候,每查询出一行记录,就会在该行上加上一个行号,行号从1开始,依次递增,不能跳着走。ps: 排序操作会影响rownum的顺序 先有rownum再有排序

查询第二页数据(每页显示5行)

select * from( select rownum rn, tt.* from( select * from emp order by sal desc ) tt where rownum<11 ) where rn>5

存储过程

//TODO  理解不是很深

ps :参数 数据类型 不能有长度

建立存储过程语法:

create [or replace] PROCEDURE 过程名(参数名 in/out 数据类型) AS begin  PLSQL 子程序体; End;

执行存储过程语法:

//1、call 调用 call procedureName(); //2、begin ... end 调用 begin procedureName(); end;

 

创建表 实现主键自增(序列 + 触发器)

1、新建表 Student 2、创建序列

CREATE SEQUENCE student_sequence      INCREMENT BY 1 -- 每次增加1个      START WITH 1 --从1开始计数      NOMAXVALUE -- 不设置最大值      NOCYCLE --直累加,不循环      NOCACHE --不建立缓冲

3、创建触发器

CREATE OR REPLACE TRIGGER STUDENT_TRIGGER   BEFORE INSERT ON STUDENT   FOR EACH ROW       -- WHEN (new.ID is null) 设置主键存在时,不触发触发器 BEGIN   SELECT STUDENT_SEQUENCE.NEXTVAL INTO :NEW.ID FROM DUAL; END;

 

函数

没有数据插入 有数据则更新

<insert id="merge" parameterType="cn.evun.srm.sc.model.RfqConfig"> merge into GSC_RFQ_CONFIG t using dual on(t.configType = #{configType}) when matched then update <set> <if test="param1 != null"> param1 = #{param1}, </if> <if test="param2 != null"> param2 = #{param2}, </if> <if test="modifyId != null"> modifyId = #{modifyId}, </if> <if test="modifyTime != null"> modifyTime = #{modifyTime}, </if> </set> when not matched then INSERT ( billItem,configType,param1,param2,createId,createTime) VALUES ( GSC_RFQ_CONFIG_SEQ.nextval,#{configType},#{param1,jdbcType=VARCHAR},#{param2,jdbcType=VARCHAR},#{createId},#{createTime}) </insert>

 

 

 

最新回复(0)