--__________________________________Function(函数)_______________________________

/*dual 表  是Oracle 中一个虚拟的表,有一行一列,所有者是sys,可以被数据库中所有的用户使用
  用户不得插入数据, 是使用此表选择系统变量或计算一个表达式的值*/

--_________________________________转换函数(5)__________________

--1.to_char(d|n [.fmt]): 用于将日期和数字按指定的格式转化为varchar2()类型的数据.  d:日期  n:数字 fmt:日期或数字的格式
    select to_char(sysdate,'yyyy-mm-dd')  格式日期 from dual;
    select to_char(sysdate,'yyyy年mm月dd天') 格式日期 from dual;

    select to_char(12.34567,'9999.99') 格式化数字  from dual;  12.35
    select to_char(12.34567.'L9999.99') 格式化数字 from dual;   ¥12.35

--2.to_date(char [,fmt]):将字符串转化为日期数据类型
    select to_date('2011-4-8','yyyy-mm-dd') - to_date('2011-4-10','yyyy-mm-dd')  日期差距 from dual;  --->2

--3.to_number(char);将包含数字的字符串转化为Number类型,从而实现运算
    select to_numner('123')+2 求和 from dual;--->125
    select '123'+2 求和 from dual; --->125    {Oracle 可以对数字字符串自动进行隐式转换}

--4.NVL: 将null值 替换为指定的值
    nvl(expr1,expr2): ===> expr1!=null ? expr1:expr2;

    NVL2(expr1,expr2,expr3):===> expr1!=null ? expr2:expr3;  --->记得是NVL2

--5.Decode:相当于 if; 将输入值与函数中的参数列表进行比较,根据输入值返回一个对应值。
      --语法: decode(input_value,value,result [,value,result,value,result] [,default_result]);
      update employees set salary=salary+ decode(deparment_id,20,100,30,80,40,120,20);  --->20 表示默认值 (找不到匹配的后)
      --表示 department_id==20 ? 100:(department_id==30? 80 : (department_id==40 ? 120 :20));



--____________________________基本函数(6)_______________________
--1 获取系统的时间
     select sysdate 当前系统的时间 from dual;

--2  add_months(d,n):在制定日期上添加月数后的值   d:表示 要操作的日期  n:表示月数
    select add_months(sysdate,1) 新的日期 Form dual;  --返回 当前日期上月数+1后的日期

--3 months_between(d1,d2):返回两个日期之间的月份数 
    select sysdate  from dual;  ---返回  8-4月-11
    select months_between('6-7月-11',sysdate) 相差的月份 from dual;  --- 返回 2.62636789   
    select floor(months_between('6-7月-11',sysdate)) 相差的月份 from dual; --返回  2 

--4 last_Day(d):返回制定日期月份后的最有一天
    select sysdate 当前日期,last_day(sysdate) 当月最后一天 form dual;

--5 next_day(d,day):返回指定下星期几的日期  d:制定的日期  day:一周内的第几天
    select sysdate 当前日期,next_day(sysdate,'星期五') 下个星期五是 from dual  --返回系统日期本周星期五的日期

--6 extract(fmt,from d):获取日期中指定特定部分
    --1  获取当前时间中的 年 月 日 
    select sysdate, extract(year from sysdate) 年,extract(month from sysdate) 月,extract(day from sysdate)日 from dual;--> 12-4月-11 --- 2010 --- 4 ----12   
    --2  在oracle 中日期是一个特殊的数字, 以天为单位;所以 可以进行 日期 + || - 数字=日期   日期-日期=数字
    select sysdate 当前日期,sysdate+1 日期加数字,sysdate-1 日期减数字,sysdate-to_date('2011-7-6','yyyy-mm-dd') 日期减日期, floor(sysdate-to_date('2011-7-6','yyyy-mm-dd')) 日期差距取整 from dual;
           --返回 8-4月-2011     9-4月-2011         7-4月-2011      2.62636789           2


--______________________________数学函数(7)_________________________________

--1.Abs:取绝对值
     select abs(-1) form dual; --->1

--2.Ceil: 向上取整
     select ceil (1.33) from dual;--->2

--3.Floor:向下取整
     select floor(1.33) from dual;--->1

--4.Power:求幂
     select power(2,3) from dual;--->8;

--5.Mod:取余
     select mod(10,3) from dual;--->1

--6.Round:四舍五入
     select round(12.126,2) from dual;--->12.13

--7.Trunc:截断
     select trunc(12.126,2) from dual;--->12.12

三角函数 ....


--_______________________字符函数(9)_____________________

--1.Lower(c):小写转换
     select lower('SccE') from dual;  ---> scce

--2.Upper(c):大写转换
     select upper('scce') from dual; --->  SCCE

--3.Ltrim(c1[,c2]): 删除c1 左侧所包含的c2 任何字符,遇到不属于c2中的字符时 返回剩下的字符(c2 省略 默认表示空格)left trim; 
     select ltrim('  adminscce') from dual;--> adminscce  如果 c2 不写 表示 去除空格('  ')
     select ltrim('adminscce','ad') from dual;--->minscce

--4.Rtrim(c1[,c2]):删除c1 右侧所包含的c2 任何字符,遇到不属于c2中的字符时 返回剩下的字符(c2 省略 默认表示空格) right trim;
     select rtrim('adminscce  ') from dual;--> adminscce  如果 c2 不写 表示 去除空格('  ')
     select rtrim('adminscce','scce') from dual;--->admin   

--5.Replace(c1,c2[,c3]):将c1中岀现c2都替换成c3;c3默认为空,如果c3为null,所有出现c2的地方都被删除,如果c2为null,则返回c1,如果c1为null,返回null
     select replace('a*b*','*','/') from dual;--->a/b/
     select replace('a*b*','*') from dual; --->ab
     select replace('a*b*') from dual; ---> a*b*
     select replace() from dual; ---->null     

--6.Instr(c1,c2): 在c1中搜索c2首次出现的位置, 找不到 返回 "0"
     select instr('abcdbc','bc') from dual;--->2 

--7.Substr(c,m[,n]):返回c的子串,m是子串的开始位置,n是子串的长度,省略n则截取从m开始的所有子字符串
     select substr('scce',2,2) from dual; -->cc

--8.Concat(c1,c2):连接字符串
     select concat('scc','e') from dual;   --->scce

--9.Length(c):返回字符串的长度
     select length('scce') form dual; --->4;

----------------------------------同义词 ---------------------------------------
  --1.方案同义词 :要权限  create  synonym  或者 create any synonym 
     grant create syonoym to scott; 
     grant create any synonym to scott;  
     --获得权限
     create [or replace] synonym  syn_dept for dept;  --为dept 表创建同义词 syn_dept


  --2.公用同义词  :要权限  create pulic syonoym 
    grant create public synonym to scott;
    create [or replace] public synonym  pubsyn_dept for dept;  --  pubsyn_dept  为名字

  --3.方案同义词的使用
       --1.scott使用自己的
        select * from syn_dept;

       --2.hr 使用 scott用户 创建的同义词
       conn scott/tiger@scce;
       grant select on dept to hr;  -- 要scott给hr 赋权限

       conn hr/tiger@scce;     --自己登录后 拿着 scott的钥匙(方案) 开门后 查看资源
       select * from scott.syn_dept;


       --3 hr 为scoot用户中的资源创建 同义词  但是 必须要scott 给hr 权限 否则 创建了 使用 不了

       conn hr/tiger@scce;
       create or replace synonym syn_scott_dept for scott.dept;  --为scoot用户的dept资源创建 同义词 

       --  select * from syn_scott_dept; -->此时写会报错 没有权限反问    要下面 的操作

       conn scott/tiger@scce;
       grant select on dept to hr;   --scott 给  hr 权限

       conn hr/tiger@scce;
       select  * from syn_scott_dept;  --此时才有用

   --4.公用同义词的使用  重要是要有权限 就 有了
       conn hr/tiger@scce;
       select * from pubsyn_dept;

       conn scott/tiger@scce;
       revoke select on dept from hr;  --移除了 hr 的select 权限

       conn hr/tiger@scce;
       select * from pubsyn_dept;  --报错没有 权限

   --5.删除方案同义词
       --1.scott 可以删除 自己的任何同义词
       drop synonym syn_dept;
       --2.scoot 要删除 别人中的方案同义词 必须要有 drop any synonym 权限
       conn sys/admin@scce;
       grant drop any synonym to scott;

       conn scott/tiger@scce;
       drop synonym syn_scott_dept;  --syn_scott_dept 是 hr 创建的同义词

   --6.删除公用同义词   用户要有 drop public synonym 权限
       conn sys/admin@scce;
       grant drop public synonym to scott;

       conn scott/tiger@scce;
       drop public synonym pubsyn_dept;   --- 删除 名为 pubsyn_dept 公用同义词