zoukankan      html  css  js  c++  java
  • Oracle学习第四天--分页+函数+集合+视图

    ---1.分页---
    --分页查询台账表T_ACCOUNT,每页10条记录
    --查看第一页
    select rownum,ac.* from t_account ac where rownum<=10;
    select rownum,ac.* from t_account ac where rownum between 1 and 10;
    --查看第二页
    --rownum:对最终结果集的编号:错误的分页
    select rownum,ac.* from t_account ac where rownum between 11 and 20;
    --陷入了递归或者无限循环,最终查不出数据:错误的分页
    select rownum,ac.* from t_account ac where rownum between 2 and 20;
    --正确的分页
    select rownum,a.* from
    (select rownum mynum,ac.* from t_account ac) a
    where a.mynum between 11 and 20;


    --------2. 字符函数------------------------
    --concat:字符串连接,mysql也是这个函数
    --显示emp员工的姓名
    --方式一:oracle中连接字符串的符号 ||
    select first_name||last_name from emp;
    --方式二:concat:连接字符串
    select concat(first_name,last_name) from emp;

    --initcap:首写字母变大写,其余字母变小写
    select initcap('hELLOWoRlD') from dual;--Helloworld
    --instr:获取字符首次出现的位置,oracle从1开始计数
    select instr('helloworld','l') from dual;--3
    --length:长度
    select length('helloworld') from dual;--10
    --replace:替换
    select replace('helloworld','l','@') from dual;--he@@owor@d
    --substr:截取字符串
    select substr('helloworld',2) from dual;--elloworld
    --trim:去除首尾空格
    select length(' hello ') from dual;--9
    select length(trim(' hello ')) from dual;--5
    --lower:全部转小写
    select lower('hELLOWoRlD') from dual;--helloworld
    --upper:全部转大写
    select upper('hELLOWoRlD') from dual;--HELLOWORLD


    ----------3. 数值函数---------------------
    --abs:求绝对值
    select abs(-90) from dual;
    select abs(90) from dual;
    --ceil:进一法
    select ceil(90.1) from dual;
    select ceil(90.9) from dual;
    --floor:去尾法
    select floor(90.1) from dual;
    select floor(90.9) from dual;
    --mod:取模,求余数
    select mod(8,3) from dual;
    --power:幂次方, 2^3 4^9
    select power(2,10) from dual;
    --sqrt:开平方
    select sqrt(9) from dual;
    --round:四舍五入
    select round(90.1) from dual;
    select round(90.5) from dual;


    ---------4. 日期函数-----------------
    --1 查看系统当前时间
    select sysdate from dual;
    --2 to_date:构造出一个时间
    select to_date('20181203','yyyymmdd') from dual;
    select to_date('2018-12-03','yyyy-mm-dd') from dual;
    select to_date('2018/12/03','yyyy/mm/dd') from dual;
    select to_date('20181203 11:23:36','yyyymmdd hh:mi:ss') from dual;
    select to_date('20181203 15:23:36','yyyymmdd hh24:mi:ss') from dual;


    --add_months:月份相加
    --15个月以后的今天是几月几号?
    select add_months(sysdate,15) from dual;
    --15个月以前的今天是几月几号?
    select add_months(sysdate,-15) from dual;
    --15天前的今天是几月几号?
    select sysdate-15 from dual;
    --15天后的今天是几月几号?
    select sysdate+15 from dual;

    --last_day:求当前给定日期所在月份的最后一天
    --2018年的2月份最后一天的几号?
    select last_day(to_date('20180210','yyyymmdd')) from dual;
    --2000年的2月份最后一天是几号?
    select last_day(to_date('20000210','yyyymmdd')) from dual;


    --months_between:日期相减,求月差
    select months_between(sysdate,to_date('20000210','yyyymmdd')) from dual;

    --求30年之前入职的员工信息
    --方式一
    select * from emp where add_months(hire_date,12*30) <sysdate;
    --方式二
    select * from emp where months_between(sysdate,hire_Date)/12>30;

    -----------5. 转换函数-----------
    --to_char:将日期类型转字符串显示
    select to_char(sysdate,'yyyymmdd') from dual;
    select to_char(sysdate,'yyyy-mm-dd') from dual;
    select to_char(sysdate,'yyyy/mm/dd') from dual;
    select to_char(sysdate,'yyyy') from dual;
    select to_char(sysdate,'mm') from dual;
    select to_char(sysdate,'dd') from dual;
    select to_char(sysdate,'hh24') from dual;
    select to_char(sysdate,'mi') from dual;
    select to_char(sysdate,'ss') from dual;

    --to_date(): 字符串转日期

    --to_number:将字符串转数值类型
    select to_number('10') from dual;

    select to_number('10a') from dual;--报错,无法转换


    -----------6. 其他函数--------------
    --nvl(列,值):当列=null的时候,用值代替
    select nvl(manager_id,-100) from emp;
    --nvl2(列,列为空的时候的值,列不为空的时候的值)
    select nvl2(manager_id,manager_id*10,-100) from emp;
    --decode(列,key,value,key,value,key,value)
    select decode(ownertypeid,1,'民用电',2,'事业单位用电',3,'商用电') from t_owners;


    -----------7. 分析函数---------------
    --rank:数值相同排名相同,排名跳跃
    select rank() over(order by usenum desc),usenum from t_account;
    --dense_rank:数值相同排名相同,排名连续
    select dense_rank() over(order by usenum desc),usenum from t_account;
    --row_number:不管数值是否相同,排名连续
    select row_number() over(order by usenum desc),usenum from t_account;

    ------------8. 集合操作-------------------------
    --并集:
    --union all:重复的也显示
    select * from t_owners where id <4
    union all
    select * from t_owners where id <7

    --union:虑重
    select * from t_owners where id <4
    union
    select * from t_owners where id <7

    --交集 intersect
    select * from t_owners where id <4
    intersect
    select * from t_owners where id <7

    --差集
    select * from t_owners where id <7
    minus
    select * from t_owners where id <4


    select usenum from t_account order by usenum;

    ----------8. 视图------------
    --创建视图
    --创建视图 :业主类型为1的业主信息
    create or replace view v_owners
    as
    select * from t_owners where ownertypeid = 1;


    --查询视图
    select * from v_owners;

    --
    create or replace view v_owners2
    as
    select id,name,addressid from t_owners where ownertypeid = 1;


    --创建视图:查询在海淀区(t_area)的小区名字中含有花园的业主记录(t_owner)(t_address)
    create or replace view v_area_address_owner
    as
    select o.* from t_owners o where o.addressid in
    (select id from t_address where name like '%花园%' and areaid in
    (select id from t_area where name = '海淀'))


    select * from v_area_address_owner;


    -----------9. 序列----------
    --为student的stuno创建序列
    create sequence sq_stuno
    start with 1
    increment by 2
    minvalue 1
    maxvalue 999999999

    --使用序列
    select sq_stuno.nextval from dual;

    --插入数据的时候,使用序列作为主键
    insert into student values(sq_stuno.nextval,'lucy','lucy@126.com',21,'男',sysdate,'kong',2300,'江苏');

    ------------------11. 创建索引--------------
    --我们经常要根据业主名称搜索业主信息,所以我们基于业主表的name字段来建立索引。
    create index index_owner_name on t_owners(name);

    select * from student;

    select * from t_owners;

  • 相关阅读:
    Palindrome Partitioning
    Minimum Path Sum
    Maximum Depth of Binary Tree
    Minimum Depth of Binary Tree
    Unique Binary Search Trees II
    Unique Binary Search Trees
    Merge Intervals
    Merge Sorted Array
    Unique Paths II
    C++ Primer Plus 笔记第九章
  • 原文地址:https://www.cnblogs.com/javaWHL/p/10308911.html
Copyright © 2011-2022 走看看