用户名:  密码:   
您当前的位置:首页 > SQL数据库 >  Oracle > 

第14讲 只读事务设置,字符函数lower,upper,length,substr,replace,数学函数round,trunc,mod,floor,ceil函数的说明和用法

上次修改时间: 2015-10-15 17:18:11      来源:网络总结      作者:钢铁长陈
上一页      路径:DSD\Learn\SQL数据库相关\Oracle数据库学习\韩顺平.玩转oracle教程笔记1_30讲\      下一页

只读事务设置,字符函数lower,upper,length,substr,replace,数学函数round,trunc,mod,floor,ceil函数的说明和用法

一、只读事务作用与演示
用以只取某时间点以前的数据,只读事务不会取得最新数据的变化
演示:
先用system登录:set transaction read only; //设置只读事务
用soctt登录修改数据:insert into emp values(8888,'ok','MANAGER',7782,to_date('1988-12-12','yyyy-mm-dd'),34.34,23.23,10);
commit;
set linesize 120
select * from emp;
再在system下查询发现看不到scott新增的
注:设置只读事务后只允许执行查询的操作,不允许dml操作,比较查询某时间点以前的销售统计

二、SQL字符函数及连接符号||双竖的使用
1. lower(char):将字符串转为小写。 例:将所有员工姓名按小写显示:select lower(ename),sal  from emp;
2. upper(char):转化为大写。例:将所有员工姓名大写显示,select upper(ename),sal  from emp; //只对字母有效,文字无效
3. length(char):返回字符串长度。例:显示正好有5个字符的员工姓名,select *  from emp where length(ename)=5;
4. substr(char,m,n):切取字符串某部分。例:显示所有姓名前3个字符:select substr(ename,1,3) from emp; //从第1个字符总共截取3个字符。
  综合使用例子:
  以首字母大写方式显示所有员工的姓名
  select upper(substr(ename,1,1)) from emp; //先把姓名首字母大写查询出来
  select lower(substr(ename,2,length(ename)-1)) from emp; //第2位开始取,取长度减1
  select upper(substr(ename,1,1))||lower(substr(ename,2,length(ename)-1)) from emp; //再用||连接起来
  以首字母小写方式显示所有员工的姓名
  select lower(substr(ename,1,1)||upper(substr(ename,2,length(ename)-1) from emp; //再用||连接起来
5.replace()字符替换函数
例:显示所有员工的姓名,用所有A替换为a:select replace(ename,'A','a') from emp; 把A替换为a

三、SQL语句的数学函数
包括cos,cosh,exp,In,log.sin,sinh,sqrt,等很多,下面讲一些常用的
1. round(n,[m]) 切断数据但保留m位小数,最后一位执行四舍五入,无m只保留整数
  select round(sal/30,1),sal from emp where ename='shunping'; //1是保留一位小数
2. truncn,[m]) 从小数点后m位直接切断数据,最后一位不执行四舍五入,无m只保留整数
  select trunc(sal/30,1),comm from emp where ename='FORD'; //切取到小数点后第几位,没有1直接切到整数位
  用负数1,会把个位也切数
3. mod(m,n) 取模,取余数
  select mod(10,2) from dual; //dual是亚元表,虚拟的,用来做测试
4. floor(n) 用以返回整数,当n有小数时返回小于或等于n的最大整数
  select floor(comm),comm from emp where ename='shunping';  //向下取整,向下离最近的整数
5. ceil(n) 用以返回整数,当n有小数时返回大于或等于n的整数
  select ceil(comm),comm from emp where ename='shunping';//向上取整,向上离最近的整数
  例:显示在一个月为30天的情况所有员工的日薪,忽略余数
  方法一:select  trunc(sal/30),ename from emp;
  方法二:select floor(sal/30), ename from emp;