zoukankan      html  css  js  c++  java
  • Oracle笔记

    --学什么
    /*
    https://liushilive.github.io/
    数据定义语言(DDL):create(创建)、alter(修改)、drop(删除)
    数据操纵语言(DML):select(查询)、insert(插入)、update(更新)、delete(删除)
    数据查询语言(DQL):基本查询语句、order by (排序)、group by (分组)
    数据控制语言(DCL):grant(授权)、revoke(撤销)
    事务控制语言(TCL):commit(提交)、rollback(回滚)、savepoint(事务保存点)
    */
    --用户与权限
    --语法结构:创建用户
    create user 用户名 identified by 密码 [account lock|unlock];
    --ex:
    create user tom
    identified by tom
    account unlock;

    --语法结构:修改用户密码
    alter user 用户名 identified by 新密码;

    --语法结构:修改用户锁定状态
    alter user 用户名 account lock|unlock;

    --语法结构:删除用户
    drop user 用户名 cascade;

    /*权限与角色
    connect :连接数据的角色
    resource :资源角色
    dba :系统管理员角色
    */
    --语法结构:授予权限
    grant 角色|权限 to 用户|角色;
    --ex:
    grant connect, resource to tom;

    --语法结构:回收权限
    revoke 角色|权限 from 用户|角色;

    --练习
    -- 新建一个用户 tom,密码设置为 love
    create user tom
    identified by love;

    -- 给用户 tom 授权登录,访问资源的权限
    grant connect, resource to tom;

    -- 锁定 tom 用户的账户,使其不能登录
    alter user tom account lock;

    -- 给 tom 用户解锁
    alter user tom account unlock;

    -- 收回 tom 用户的登录、访问资源的权限
    revoke connect, resource from tom;

    -- 删除用户 tom
    drop user tom;

    ------------------------------------------
    --表与约束
    --语法结构:创建表
    create table 表名(
    列名1 类型 [null|not null] [constraint],
    列名2 类型
    );
    --ex:表名不能以数字开头,必须以英文字母开头
    create table test(
    name varchar2(6),
    sex varchar2(5),
    age number(3),
    address varchar2(120)
    );

    --语法结构:修改表名
    rename 原表名 to 新表名;
    --ex:
    rename test to test10;

    --语法结构:添加列
    alter table 表名 add 列名 类型 列约束;
    --ex:
    alter table test10 add fname varchar2(20) not null;

    --语法结构:添加列
    alter table 表名 add (列名 类型 列约束, 列名 类型 列约束);

    --编辑表的字段,修改一个列的数据类型(一般限于修改长度,修改为一个不同类型时有诸多限制):
    ALTER TABLE 表名 MODIFY(列名,数据类型);

    alter table skate_test modify (author number(10,0) )


    --修改列名(即修改字段名)
    alter table 表名 rename column 现列名 to 新列名;

    --语法结构:删除列
    alter table 表名 drop column 列名;
    --ex:
    alter table test10 drop column fname;

    --语法结构:删除表
    drop table 表名;
    --ex:
    drop table test10;

    --语法结构:创建约束
    alter table 表名 add constraint 约束名 约束内容;

    --ex1:创建表时增加约束
    create table infos(
    stuid varchar2(7) primary key, --主键约束
    stuname varchar2(20) not null, --非空约束
    sex varchar2(3) check(sex='男' or sex='女'), --检查约束
    seat number(10) unique, --唯一约束
    address varchar2(100) default '地址不详' -- 默认约束
    );
    --ex2:创建表后增加约束
    create table scores(
    id varchar2(7),
    term varchar2(2),
    score number(4)
    );

    --添加检查约束
    alter table scores
    add constraint ck_scores_score check(score>=0 and score<=100); --检查约束

    --添加主键
    alter table scores
    add constraint pk_scores primary key(id);

    --添加外键
    --外键的作用:保持数据一致性,完整性,主要目的是控制存储在外键表中的数据。 使两张表形成关联,外键只能引用外表中的列的值!
    alter table scores
    add constraint fk_scotes_id foreign key(id) references infos(stuid);--外键约束

    alter table scores
    modify score not null; -- 非空约束

    alter table scores
    modify score default 0; -- 默认约束

    --语法结构:删除约束
    alter table 表名 drop constraint 约束名;

    --练习
    -- 创建表 class_info
    /*
    字段名 类型 长度 可选值
    c_id 字符串 20 主键
    c_type 字符串 6 UI、测试、开发
    c_position 字符串 20 非空
    start_time 日期
    c_status 字符串 6 在读、毕业
    */
    create table class_info(
    c_id varchar2(20) primary key,
    c_type varchar2(6) check(c_type in ('UI','测试','开发')),
    c_position varchar2(20) not null,
    start_time date,
    c_status varchar2(6) check(c_status = '在读' or c_status = '毕业')
    );

    -- 把表 class_info 改成 class_info2
    rename class_info to class_info2

    --------------------------------------------------------------------------
    --上面的代码使用system用户
    --下面的代码使用scott用户
    --------------------------------------------------------------------------
    --语法结构:基本查询
    select *|列名|表达式
    from 表名;
    /*
    * 表示表中的所有列
    列名可以选取若干个列,用逗号分隔
    select 识别什么列
    from 识别哪个表
    */
    --ex:
    select *
    from dept;

    select dname, loc
    from dept;

    select d.dname, d.loc
    from dept d;

    select d.dname "部门名称", d.loc as 位置
    from dept d;

    --语法结构:基本查询
    select *|列名|表达式
    from 表名
    where 条件
    order by 列;
    /*
    * 表示表中的所有列
    列名可以选取若干个列,用逗号分隔
    select 识别什么列
    from 识别哪个表
    where 查询的条件
    order by 排序,asc(升序)、desc(降序)
    */
    --ex:
    select *
    from dept
    where deptno <=30
    order by loc desc, dname asc;

    /*操作符
    算术运算:+ - * /
    关系运算:= > < != <> >= <=
    逻辑运算:and or not
    拼接运算:
    || +
    */
    select e.ename, e.sal, e.sal+100, e.sal*100, e.sal/100
    from emp e;

    select e.ename, e.sal
    from emp e
    where e.sal = 3000;

    select *
    from emp e
    where e.sal > 2500 or e.job = 'MANAGER';

    select e.ename, e.sal, '我爱' || e.ename || '带上工资:'||e.sal||'来约会'
    from emp e
    where e.sal>2500
    order by e.sal desc;

    --消除重复行:distinct
    select distinct e.deptno
    from emp e;

    --null
    select *
    from emp e
    where comm is null;

    select *
    from emp e
    where comm is not null;

    select *
    from emp e
    where not comm is null;

    -- in
    select *
    from emp e
    where e.job in ('SALESMAN', 'CLERK', 'MANAGER');

    select *
    from emp e
    where e.job not in ('SALESMAN', 'CLERK', 'MANAGER');

    --between and
    select *
    from emp e
    where e.sal between 1000 and 2000;

    --模糊查询
    /* 通配符
    % 表示 0 个或 多个任意字符
    _ 表示1个任意字符
    */
    --语法结构:like
    like '字符串' [escape '字符']
    /*
    S% 以S开头的字符串
    %S 以S结尾的字符串
    _S% 第二个字符为S的字符串
    '/_%' escape '/' 以 _ 开头的字符串,escape 表示 / 后面的那个符号不当成特殊字符处理,就是查找普通的 _
    */
    --ex:
    select *
    from emp e
    where e.ename like 'S%';

    select *
    from emp e
    where e.ename like '%S';

    select *
    from emp e
    where e.ename like '_L%';

    select *
    from emp e
    where e.ename like '%S%';

    select *
    from emp e
    where e.ename like '/_%' escape '/';

    /*集合运算
    intersect :交集
    union:并集-不包含重复值
    union all:并集-包含重复值
    minus | except :补集
    minus:mysql、Oracle
    except:mysql、Microsoft sql server、db2
    */
    --ex:
    select deptno from dept
    intersect
    select deptno from emp;

    select deptno from dept
    union
    select deptno from emp;

    select deptno from dept
    union all
    select deptno from emp;

    select deptno from dept
    minus
    select deptno from emp;

    select deptno from emp
    minus
    select deptno from dept;

    --练习
    -- 显示薪水大于 2000,且工作类别是 MANAGER 的雇员信息
    select *
    from emp e
    where e.sal > 2000 and e.job = 'MANAGER';
    -- 显示年薪大于 30000,工作类别不是 MANAGER 的雇员信息
    select *
    from emp e
    where e.sal * 12 > 30000 and e.job != 'MANAGER';
    -- 显示薪水在 1500 到 3000 之间,工作类别以“M”开头的雇员信息
    select *
    from emp e
    where e.sal between 1500 and 3000
    and e.job like 'M%';
    -- 显示奖金为空并且部门号为 20 或 30 的雇员信息
    select *
    from emp e
    where e.comm is null and e.deptno in (20, 30);
    -- 显示奖金不为空或者部门号为 20 的雇员信息,要求按照薪水降序排列
    select *
    from emp e
    where e.comm is not null or e.deptno =20
    order by e.sal desc;
    -- 显示年薪大于 30000 工作类别不是 MANAGER,且部门号不是 10 和 40 的雇员信息,要求按照雇员姓名进行排序
    select *
    from emp e
    where e.sal * 12 > 30000 and e.job != 'MANAGER'
    and e.deptno not in (10, 40)
    order by e.ename;
    -- 选择在部门 30 中员工的所有信息
    select *
    from emp e
    where e.deptno = 30;
    -- 列出职位为(MANAGER)的员工的编号,姓名
    select e.ename, e.empno
    from emp e
    where e.job = 'MANAGER';
    -- 找出部门 10 中的经理 (MANAGER) 和部门 20 中的普通员工 (CLERK)
    select *
    from emp e
    where (e.deptno = 10 and e.job = 'MANAGER')
    or (e.deptno = 20 and e.job = 'CLERK');
    -- 找出部门 10 中既不是经理也不是普通员工,而且工资大于等于 2000 的员工
    select *
    from emp e
    where e.sal >= 2000 and e.job not in ('MANAGER', 'CLERK')
    and e.deptno = 10;
    -- 找出没有奖金或者奖金低于 500 的员工
    select *
    from emp e
    where e.comm is null or e.comm < 500;
    -- 显示雇员姓名,根据其服务年限,将最老的雇员排在最前面
    select e.ename
    from emp e
    order by e.hiredate asc;
    -- 找出有奖金的员工的不同工作
    select distinct e.job
    from emp e
    where e.comm is not null;
    -- 找出姓名中不带 R 这个字母的员工
    select *
    from emp e
    where e.ename not like '%R%';
    -- 显示所有员工,按工作降序排序,若相同,则按工资升序排序
    select *
    from emp e
    order by e.job desc, e.sal asc;
    -- 查找出不属于任何部门的员工
    select *
    from emp e
    where e.deptno is null;
    ---------------------------------------------------------------
    /*多表连接查询
    交叉连接
    内连接:等值连接、非等值连接、自连接
    外连接:左外连接、右外连接、满外连接
    */
    --交叉连接:笛卡尔积(集)
    select *
    from emp, dept;

    --内连接:
    select * from emp;
    select * from dept;
    select * from salgrade;
    --等值连接
    --ex:查询员工所在部门信息
    select *
    from emp e, dept d
    where e.deptno = d.deptno;

    select *
    from emp e
    join dept d
    on e.deptno = d.deptno;
    --非等值连接
    --ex:查询员工薪水等级
    select *
    from emp e, salgrade s
    where e.sal between s.losal and s.hisal;
    --自连接
    --ex:查询员工的领导姓名
    select e1.*,e2.empno 领导编号, e2.ename 领导姓名
    from emp e1, emp e2
    where e1.mgr = e2.empno;

    --语法结构:左外连接:从 A表查数据,左连接B表,A表全部显示
    select *
    from A
    left [outer] join B
    on 条件;
    --ex:
    select *
    from emp e
    left join dept d
    on e.deptno = d.deptno;
    --语法结构:右外连接:从 A表查数据,右连接B表,B表全部显示
    select *
    from A
    right [outer] join B
    on 条件;
    --ex:
    select *
    from emp e
    right join dept d
    on e.deptno = d.deptno;
    --语法结构:满外连接:从 A表查数据,满外连接B表,AB表全部显示
    select *
    from A
    full [outer] join B
    on 条件;
    --ex:
    select *
    from emp e
    full outer join dept d
    on e.deptno = d.deptno;

    --查询总结:
    select *
    from A
    join B --内连接
    on 表之间关联的条件
    right join C --右外连接
    on 表之间关联的条件
    left join D --左外连接
    on 表之间关联的条件
    full join E --满外连接
    on 表之间关联的条件
    where 结果筛选条件
    order by 排序列;

    ---------------------
    --子查询
    --ex:查询销售部下面的员工信息
    select e.*
    from emp e
    join dept d
    on e.deptno = d.deptno and d.dname = 'SALES';

    select *
    from emp e
    where e.deptno = (
    select d.deptno
    from dept d
    where d.dname = 'SALES'
    );
    /*any 子查询
    <any:小于最大的
    >any:大于最小的
    =any:等于任意一个
    */
    --ex:查询比销售员最高薪水低的员工信息
    select *
    from emp e
    where e.sal <any(
    select sal
    from emp
    where job = 'SALESMAN'
    )

    /*all 子查询
    >all:大于最高的
    <all:小于最低的
    */
    --ex:查询比所有销售员的薪水都高的员工信息
    select *
    from emp e
    where e.sal > all(
    select sal
    from emp
    where job = 'SALESMAN'
    );

    --练习
    -- 创建一查询,显示与 BLAKE 在同一部门工作的雇员的姓名和受雇日期、部门编号,但是 BLAKE 不包含在内。
    select e.ename, e.hiredate, e.deptno
    from emp e
    where e.ename != 'BLAKE'
    and e.deptno = (
    select e1.deptno
    from emp e1
    where e1.ename = 'BLAKE'
    );

    select e.ename, e.hiredate, e.deptno
    from emp e
    join emp e1
    on e.deptno = e1.deptno
    and e1.ename = 'BLAKE' and e.ename != 'BLAKE';

    -- 显示位置在 DALLAS 的部门内的雇员姓名、工作。
    select e.ename, e.job
    from emp e
    join dept d
    on e.deptno = d.deptno
    and d.loc = 'DALLAS';

    -- 显示被 KING 直接管理的雇员的姓名以及薪水。
    select e.ename, e.sal
    from emp e
    join emp e1
    on e.mgr = e1.empno
    and e1.ename = 'KING';

    -- 创建一查询,显示能获得与 SCOTT 一样薪水的其他雇员的姓名、受雇日期以及薪水。
    select e.ename, e.hiredate, e.sal
    from emp e
    join emp e1
    on e.sal = e1.sal and e1.ename = 'SCOTT'
    and e.ename != 'SCOTT';

    -- 查找出工资等级不为 4 级的员工的员工名字,部门名字,部门位置
    select e.ename, d.dname, d.loc
    from emp e
    left join dept d
    on e.deptno = d.deptno
    join salgrade s
    on e.sal between s.losal and s.hisal
    and s.grade != 4;

    -----------------------------------------------
    --语法结构:根据查询结果创建表
    create table 表名 as select语句;
    --ex:
    create table empinfo as
    select e.ename, e.job,e.sal
    from emp e
    where e.sal > 2500;

    select * from empinfo;
    --ex:复制表结构
    create table empinfo1 as
    select e.ename, e.job,e.sal
    from emp e
    where 1=2;

    select * from empinfo1;

    --语法结构:插入数据
    insert into 表名 [(列1, 列2)] values (值1, 值2);
    --ex:
    insert into empinfo1
    values ('李四','程序员', 2000);

    insert into empinfo1
    values ('李四1','程序员', null);

    insert into empinfo1 (ename, job)
    values ('张三', '测试');

    select * from empinfo1;

    --语法结构:查询结果插入
    insert into 表 select查询;
    --ex:
    insert into empinfo1
    select e.ename, e.job, e.sal
    from emp e
    where e.sal>3000;

    --语法结构:更新数据
    update 表名 set 列名1=值, 列名2=值 where 条件;
    --ex:
    update empinfo1
    set sal=50000
    where sal is null;

    update empinfo1
    set sal=null;

    --语法结构:删除数据
    delete from 表名 where 条件;
    --ex:
    delete from empinfo1
    where job='程序员';

    select * from empinfo1;

    --语法结构:删除所有数据:无法回滚,高度危险
    truncate table 表名;
    --ex:
    truncate table empinfo1;


    --练习
    -- 将 EMP 表中工资大于 2000 的员工数据,插入到 BONUS 表中
    select * from bonus;

    insert into bonus
    select e.ename, e.job, e.sal, e.comm
    from emp e
    where e.sal > 2000;

    --事务
    commit; --提交事务
    rollback; --回滚事务
    savepoint A; --事务保存点
    rollback to A;--回滚至保存点

    -------------------------------
    /*函数
    单行函数
    数字函数
    字符函数
    日期函数
    转换函数
    null函数
    聚合函数:聚组函数、单组分组语句
    其他函数
    */
    select abs(100),abs(-100),abs('100') from dual;
    select round(1000.123456, 4), round(100.123456, 2.56), round(123456.123456, -2) from dual;

    select trunc(100.23456, 4),
    trunc(100.23456, 2.56),
    trunc(155.23456, -2),
    trunc(155.23456)
    from dual;

    select mod(5, 2), mod(8 / 3, 5), mod('10', 5), mod(-10, 6), mod(1, 0)
    from dual;

    select ceil(10), ceil('10.2'), ceil(-10.2)
    from dual;

    select substr('ABCDE我FGHI', 5, 2), substr('ABCDE我FGHI', -5, 2)
    from dual;

    select length('ABCDE我FGHI')
    from dual;

    select concat('我的', '测试!'), '我的' || '测试!'
    from dual;

    select sysdate, to_char(sysdate, 'YYYY-MM-DD HH24:MI:SS')
    from dual;

    select to_char(add_months(to_date('2009-9-15', 'YYYY-MM-DD'), 1),
    'YYYY-MM-DD'),
    to_char(add_months(to_date('2009-9-30', 'YYYY-MM-DD'), 1),
    'YYYY-MM-DD'),
    to_char(add_months(to_date('2010-1-30', 'YYYY-MM-DD'), 1),
    'YYYY-MM-DD')
    from dual;

    select months_between(to_date('2010-7-1', 'YYYY-MM-DD'),
    to_date('2010-6-1', 'YYYY-MM-DD')) one,
    months_between(to_date('2010-5-31', 'YYYY-MM-DD'),
    to_date('2010-4-30', 'YYYY-MM-DD')) two,
    months_between(to_date('2010-5-31', 'YYYY-MM-DD'),
    to_date('2010-9-30', 'YYYY-MM-DD')) three
    from dual;

    select last_day(sysdate)
    from dual;

    select to_char(16.89), to_char(16.89, '99.9')
    from dual;

    select to_char(sysdate, 'YYYY-MM-DD'),
    to_char(sysdate, 'HH24:MI:SS'),
    to_char(sysdate, 'D'),
    to_char(sysdate, 'WW'),
    to_char(sysdate)
    from dual;

    select to_char(to_date('2010-7-1', 'YYYY-MM-DD'), 'MONTH')
    from dual;

    select to_number('2456.304', '9999.999')
    from dual;

    select ename, nvl(comm, 0)
    from emp;

    select ename, comm, nvl2(comm, '有奖金', '没有奖金')
    from emp;

    --练习
    -- 找出每个月倒数第三天受雇的员工(如:2009-5-29)
    select *
    from emp e
    where e.hiredate = last_day(e.hiredate)-2;
    -- 找出 25 年前雇的员工
    select *
    from emp e
    where e.hiredate <= add_months(sysdate, -25*12);
    -- 所有员工名字前加上 Dear , 并且名字首字母大写
    select e.ename, initcap(e.ename), concat('Dear', initcap(e.ename))
    from emp e;
    -- 找出姓名为 5 个字符的员工
    select *
    from emp e
    where length(e.ename) = 5;
    -- 显示所有员工的姓名的第一个字
    select e.ename, substr(e.ename, 1, 1)
    from emp e;
    -- 找到 2 月份受雇的员工
    select *
    from emp e
    where to_char(e.hiredate, 'mm') = 2;
    -- 对薪水是 2000 元以下的员工进行查询,如果没有发奖金,每人奖金 100 元。
    select e.*, nvl(e.comm, 100)
    from emp e;
    -- 对 EMP 表中薪水为 2000 元以下的员工进行查询,如果没有奖金,则显示奖金为 200 元,如果有奖金,则在原来的奖金基础上加 100 元。
    select e.*, e.comm, nvl2(e.comm, e.comm + 100, 200)
    from emp e
    where e.sal < 2000;

    --------------------------------------
    --聚合函数:会忽略列中的 null 值
    --avg
    select avg(comm), avg(all comm), avg(distinct comm)
    from emp;

    --ex:group by having
    select deptno, job, avg(sal)
    from emp
    where deptno = 20
    group by deptno, job
    having avg(sal) > 2000;

    --count
    select count(*), count(comm), count(distinct sal)
    from emp
    where sal < 3000;

    --max
    select max(e.sal),max(distinct e.sal)
    from emp e;

    --min
    select min(e.sal),min(distinct e.sal)
    from emp e;

    --sum
    select sum(sal), sum(distinct sal)
    from emp;

    --练习
    -- 列出至少有五个员工的所有部门信息。
    select *
    from dept d
    where d.deptno in (
    select e.deptno
    from emp e
    group by e.deptno
    having count(e.deptno) >= 5
    );

    -- 查询出 KING 所在部门的工作年限最大的员工名字
    select e.ename
    from emp e
    where e.hiredate = (
    select min(e1.hiredate)
    from emp e1
    where deptno in (
    select deptno
    from emp
    where ename = 'KING'
    )
    ) and e.deptno = (
    select deptno
    from emp
    where ename = 'KING'
    );

    -- 查询出 KING 所在部门的部门号、部门名称、部门人数
    select e2.deptno, e2.empno, count(e1.ename), d.dname,d.deptno
    from emp e1, emp e2, dept d
    where e2.ename = 'KING' and e1.deptno = e2.deptno and e1.deptno = d.deptno
    group by e2.deptno, e2.empno, d.dname,d.deptno;

    select d.deptno, d.dname, t.人数
    from dept d, (
    select count(*) 人数, deptno
    from emp
    where deptno in (
    select deptno
    from emp
    where ename = 'KING'
    )
    group by deptno
    ) t
    where d.deptno = t.deptno;

    select e.deptno, d.dname, count(e.ename)
    from emp e
    join emp e1
    on e.deptno = e1.deptno and e1.ename = 'KING'
    join dept d
    on e1.deptno = d.deptno
    group by e.deptno, d.dname;

    -- 算出部门 30 中得到最多奖金的员工姓名
    select e.ename
    from emp e
    where comm = (
    select max(comm)
    from emp
    where deptno = 30
    ) and deptno = 30;

    -- 统计各部门下平均工资大于 500 的部门
    select deptno, avg(sal)
    from emp
    group by deptno
    having avg(sal) > 500;

    --其他函数
    --ex:查询当前会话登录名
    select user from dual;

    --ex:查询当前会话信息
    select userenv('ISDBA') from dual; -- 是否为dba登录
    select userenv('Language') from dual; -- 返回当前会话对应的语言信息
    select userenv('SESSIONID') from dual; --查询sessionid

    --ex:decode
    select sal,
    decode(sign(sal - 3000), 1, '高薪', -1, '低薪', 0, '刚好')
    from emp;

    --查询小结
    select [distinct] *
    from A
    join B --内连接
    on 表之间关联的条件
    right join C --右外连接
    on 表之间关联的条件
    left join D --左外连接
    on 表之间关联的条件
    full join E --满外连接
    on 表之间关联的条件
    where 结果筛选条件
    group by 分组列
    having 分组后结果筛选
    order by 排序列 [asc|desc];

    -------------------------------------------------
    --伪列
    --rowid:物理地址
    --rownum:行号
    select e.*, rowid, rownum
    from emp e;
    --ex:查询员工表中前 5 名员工信息
    select *
    from emp
    where rownum <= 5;

    --ex:查询薪水最高的前5名员工信息
    select *
    from (
    select e.*
    from emp e
    order by sal desc
    )
    where rownum <= 5;

    --ex:查询emp表中第5条到第10条之间的数据 -- 分页查询
    select *
    from (
    select e.*, rownum R
    from emp e
    )
    where R between 5 and 10;

    --练习
    -- 按部门统计员工数,查出员工数最多的部门的第二名到第三名(列出部门名字,部门位置)
    select *
    from dept d, (
    select t.*, rownum R
    from (
    select count(*) 人数, deptno
    from emp
    group by deptno
    order by 人数 desc
    ) t
    ) t1
    where R between 2 and 3
    and d.deptno = t1.deptno;

    -- 查找出部门 10 和部门 20 中,工资最高第 3 名到第 5 名的员工的员工名字,部门名字,部门位置
    select ename, dname, loc
    from dept d,(
    select ename, deptno, rownum R
    from (
    select *
    from emp
    where deptno in (10, 20)
    order by sal desc
    )
    ) t
    where d.deptno = t.deptno
    and t.R between 3 and 5;
    --------------------------------
    /*
    视图:预定义的查询,可作为表一样操作,一张虚拟的表
    索引:提高查询的效率,类似书本的目录
    */
    --语法结构:创建视图
    create [or replace] [[no] force] view 视图名
    as
    select查询语句
    [with read only];
    --ex:
    create or replace view empdept
    as
    select e.*, d.dname, d.loc
    from emp e
    join dept d
    on e.deptno = d.deptno
    with read only;

    select *
    from empdept
    where ename='KING';

    ------------------------------
    --语法结构:创建索引
    create [unique] index 索引名称 on 表名(列);
    --ex:
    create index a1 on emp(sal);

    ----
    create table testtable as
    select rownum id,
    sysdate + rownum/24/3600 in_datetime,
    trunc(dbms_random.value(1, 100)) random_id,
    --取随机整数 1 -- 100
    dbms_random.string('x', 20) random_string
    -- 长度为20 的随机的大写字母与数字组成的字符串
    from dual
    connect by level <= 100000;
    -- 递归查询

    select count(*) from testtable;

    --2.4 3.26 14
    insert into testtable
    select rownum id,
    sysdate + rownum/24/3600 in_datetime,
    trunc(dbms_random.value(1, 100)) random_id,
    dbms_random.string('x', 20) random_string
    from dual
    connect by level <= 1000000;

    create index iii on testtable(id);
    create index iii11 on testtable(random_id,random_string);

    drop index iii;
    drop index iii11;

    select *
    from testtable t
    where t.id in (1,10000,100000,20000,50000,30000,50700,45678,12356,25,250,369,741,852,963,45621);

    ----------------------------
    --存储过程
    create or replace procedure sp_update_sal(name in varchar)
    --通知Oracle创建一个叫做 sp_update_sal 的存储过程,如果存在则覆盖
    is -- 将跟随一个PL/SQL 程序体
    begin
    update emp set sal= sal + 1
    where ename = name;

    commit;
    end;


    --需要在命令窗口执行
    exec sp_update_sal('KING');

    -------------------------------------------------------------------------
    select e.ename, e.sal, (
    case -- 选择结构的语句
    when e.sal > 3500 then '高工资'
    when e.sal > 2000 then '中等工资'
    else '低等工资'
    end
    ) 工资等级
    from emp e;

  • 相关阅读:
    阅读任务
    自我介绍
    学习总结
    第十二周课程总结
    第十一周课程总结
    第十周课程总结
    第九周课程总结&实验报告(七)
    第四周课程总结&实验报告
    第3周Java编程总结
    学习总结
  • 原文地址:https://www.cnblogs.com/szl6/p/9508280.html
Copyright © 2011-2022 走看看