第二节,oracle sql编程以及高级

1.1基本的数据操纵语言(DML)

  insert into tableName values( 'aa','bb',1,null);

  update tableName set columnName = value where 1=1;

  delete tableName where [condition];

  select * from tableName;

2.1sql函数

  单行函数>>字符函数

常用的字符函数
函数 说明 输入 输出
INITCAP(CHAR) 首字母大写 initcap('hello’) Hello
LOWER(CHAR) 转为小写 lower('FUN') fun
UPPER(CHAR) 转为大写 upper('sun') SUN
LIRIM(CHAR,set) 左裁剪 lirim('123admin','123') admin
RIRIM(CHAR,set) 右裁剪 ririm('123admin','admin') 123
TRANSLATE(CHAR,form,to) 安字符翻译 translate('Jack','abcd','1234') J13k
REPLACE(CHAR,search_str,replace_str) 字符串替换 replace('a and a','a','b') b and b
INSTR(CHAR,substr) 查找字符串位置 instr('worldwide','d') 5
SUBSTR(CHAR,pos,len) 取字符串 substr('abcdef',3,2) cd
CONCAT(char1,char2) 连接字符串 concat('hello','world')

helloworld

单行函数>>数字函数

数字函数
函数 说明 输入 输出
ABS(N) 绝对值 abs(-1) 1
CEIL 向上取整 ceil(44.77) 45
SIN 正弦 sin(1.571) .9999999
SOS 余弦 cos(0) 1
SIGN 取符号 sign(-23) -1
FLOOR 正弦 floor(10.2) 10
POWER m的n次幂 power(4,2) 16
MOD 取余数 mod(10,3) 1
ROUND 四舍五入 round(10.256) 10.26
TRUNC 截断 round(10.256) 1.25

sqrt(n)   平方根    sqrt(4)   2

单行函数>>日期函数

日期函数
函数 功能 例子 结果
months_between 返回两个日期间的月份

months_between(

to_date('2014-1-10','yyyy-mm-dd'),

to_date('2014-3-21','yyyy-mm-dd'))

-2.3548387
add_months 月份的加减 add_moths(sysdate,1) 系统时间月份加减
next_day 返回指定日期后的星期对应的新日期 next_day(sysdate,'MONDAY ')  
last_day 返回指定日期所在月的最后一天 last_day(sysdate)  
trunc 对日期进行截断 trunc(sysdate,'YEAR')  

    select trunc(123.567,2) from dual;--123.56,将小数点右边指定位数后面的截去;
    select trunc(123.567,-2) from dual;--100,第二个参数可以为负数,表示将小数点左边指定位数后面的部分截去,即均以0记;
    select trunc(123.567) from dual;--123,默认截去小数点后面的部分;

单行函数>>其他函数

  to_char(1234.5,'$9999.9')>>$1234.5

  to_date('2018-01-01','yyyy-mm-dd')>>

  to_number('12')>>12

  nvl(exp1,exp2)  --如果exp1的值为null,返回exp2的值,否则返回exp1

  nvl2(exp1,exp2,exp3)  --如果exp1的值为null,则返回exp2的值,否则返回exp3的值

  decode(value,if1,then1,if2,then2,.....else)

聚合函数

  sum求和,avg(平均值),conut计算符合条件的列数,max符合条件的最大值,min符合条件的最小值

  注意:聚合函数与group by 一起使用时要求,所有未包含在聚合函数的列,都要跟在group by 后面

    select 类别,摘要,sum(数量) as 总和 from A group by 类别,摘要;

3.1查询基础及优化

  子查询:select  * from emp where deptno not in (select deptno from dept where danem in ('a','b'));

      select  * from emp where deptno not exists (select deptno from dept where danem in ('a','b'));

      注:exists的子查询只关心内层查询对否有返回值,并不需要具体值,因此效率要高。

3.2查询连接

  select   t1.col,t2.col form table t1, table2 t2 where t1.col(+)= t2.col;--(+)在左边表示右外连接,表示以右表为准,左表没有符合的数据以空值出现;在右边表示左外连接,同理。

  select  count(*) from emp CROSS JOIN dept;--交叉连接,产生笛卡尔积,即两表的行数相乘,列数相加,得出的大表;

3.3rownum分页

1 select * 
2     from (select e.* ,rownum rn 
3                     from (select * from emp order by sal desc) e  ) 
4     where rn>5 and rn<9;
原文地址:https://www.cnblogs.com/xuhewei/p/9247891.html