zoukankan      html  css  js  c++  java
  • oracle学习

    ---创建表
    create table persons(
           pid number(10),
           pname varchar2(20)
    );
    --授权
    connect --连接角色
    resource --开发者权限
    dba --超级管理员权限
    grant dba to persons;
    ---修改表结构 --添加一列 alter table persons add(gender number(1)); --修改列类型 alter table persons modify gender char(1); --修改列名称 alter table persons rename column gender to sex; --删除列 alter table persons drop column sex; --添加一条记录 insert into persons(pid,pname) values(1,'aaaa'); commit; --查询数据 select * from persons; --修改数据 update persons set pname='小马' where pid=1; commit; --三个删除 --删除表中所有的字段 delelte from persons; --删除表结构 drop table persons; --先删除表,在创建表。效果等同于删除表中全部记录 --在数据量大的情况下,尤其带有索引的情况下,该操作效率高 --索引可以提供查询效率,但是会影响增删改效率 truncate table persons; --序列:默认从1开始,依次递增,主要用来给主键值使用 --序列:默认从1开始,依次递增,主要用来给主键赋值使用。 --dual:虚表,只是为了不全语法,没有任何意义 create sequence s_persons; select s_persons.nextval from dual select s_persons.currval from dual insert into persons(pid,pname) values(s_persons.nextval,'aaaa'); commit; select *from persons --scott用户,密码tiger --解锁scott用户 alter user scott account unlock; alter user scott identified by tiger; --切换到scott用户下

    --单行函数:作用于一行,返回一个值
    --字符函数
    select upper(deptno),loc from dept;--结果转大写
    select lower(loc),loc from dept;--结果转小写
    select substr(loc,0,3),loc from dept--从第0位置开始,截取三位
    select initcap(loc),loc from dept;--将字符串变成第一个字母大写,其余都变成小写;
    select length(loc),loc from dept;--查询字符串长度
    select replace(loc,'A','q'),loc from dept;--字符串中的字符替换
    select concat('aaaa',loc),loc from dept;--将两个字符串连接起来

    --数学函数
    select round(26.28,1) from dept;---四舍五入,后面那个参数是保留几位小数
    select trunc(26.28,1) from dept;---直接截取,不再看是不是四舍五入了
    select mod(20,3) from dept;--求余数

    --日期函数,日期可以直接加减,单位是天
    ---查出emp表中所有的员工入职距离现在几天
    select sysdate-e.hiredate from emp e
    ---算出明天此刻
    select sysdate+1 from emp
    ---算出员工入职到现在几个月
    select months_between(sysdate,e.hiredate) from emp e
    ---算出员工入职到现在几年
    select months_between(sysdate,e.hiredate)/12 from emp e
    ---算出员工入职到现在几周
    select round((sysdate-e.hiredate)/7) from emp e;
    --日期转字符串 fm:日期前不加零 24:表示24小时时间制
    select to_char(sysdate,'fm yyyy-mm-dd hh24:mi:ss') from emp e;
    --字符串转日期
    select to_date('2019-10-21 11:24:24','fm yyyy-mm-dd hh:mi:ss')from emp e
    ------通用函数,mysql和oracl通用
    --算出emp表中所有员工的年薪
    --奖金里面有null值,如果null值和任意数字做算术运算,结果都是null
    --如果e.comm为null ,则用0,不是null直接使用他本身
    select e.sal*12+nvl(e.comm,0) from emp e;
    ---条件表达式
    select e.ename,
    case e.ename
    when 'SMITH' then '史密斯'
    ---when then
    else 'ok'
    end
    from emp e;
    ---判断emp表中的员工工资,如果高于3000显示高工资
    select e.ename,
    case
    when e.sal>3000 then '高收入'
    when e.sal>1500 then '中等收入'
    else '低等收入'
    end
    from emp e;

     ---oracle中专用条件表达式
     ---oracle中除了起别名,都用单引号
     select e.ename,
            decode(e.ename,
            'SMITH','史密斯',
            '无名')"中文名"
      from emp e;
    
    --多行函数(聚合函数):作用于多行,返回一个值
    select count(1) from emp;
    select sum(sal) from emp;--工资总和
    select max(sal) from emp;--最大工资
    select min(sal)from emp;--最低工资
    select avg(sal) from emp;--平均工资

    ---分组查询
    --查询每个部门的平均工资
    --分组查询中,出现在group by后面的原始列,才能出现在select后面
    --没有出现在group by 后面的列,想在select 后面,必须加上聚合函数

    --聚合函数可以吧多行记录变成一个值
    select e.deptno,avg(e.sal)
    from emp e group by e.deptno;

    ---所有条件都不能使用别名来判断,
    select e.deptno,avg(e.sal) as t
    from emp e group by e.deptno having avg(e.sal)>2000;

    --查询每个部门工资公寓800的员工的平均工资
    ----where 是过滤分组前的数据,having是过滤分组后的数据
    ----where 必须在group by之前,having是在group by之后
    select e.deptno,avg(e.sal) as t
    from emp e where e.sal>800 group by e.deptno;
    --查询出每个部门工资高于800的员工的平均工资,
    ---然后再查询出平均工资高于2000的部门
    select e.deptno,avg(e.sal) as t
    from emp e where e.sal>800 group by e.deptno
    having avg(e.sal)>2000;

  • 相关阅读:
    Oracle11g 干净卸载
    linux中的帮助命令
    Linux Samba文件共享服务,安装与案例配置
    linux文件系统相关命令(df/du/fsck/dumpe2fs)
    虚拟机VMware下CentOS6.6安装教程图文详解
    vnc连接远端linux服务器
    mysql57重新安装后无法再次启动mysql57服务“本地计算机上的MySQL服务启动后停止。某些服务在未由其他服务或程序使用时将自动。”--解决方法
    (二)分布式数据库tidb-事务
    Centos 6下使用cmake编译安装MariaDB
    MySQL数据库基本知识
  • 原文地址:https://www.cnblogs.com/gxlaqj/p/11710513.html
Copyright © 2011-2022 走看看