Luo Hao

Oracle实践数据库笔记-4

Rehoni / 2018-03-13


PL/SQL QL(select)

循环 (sql中用斜杠/执行命令)

  1. 绝对循环
  2. while循环
  3. for循环
begin
    for i in 1..10 loop
        insert into t1 values(i);
    end loop;
    commit;
end;(功能结束就加分号,块结束)

块结构

  1. 匿名块:4个部分
  1. 命名块:
create or replace procedure p1
    as
        begin
            for i in 1..10 loop
                insert into t1 values(i);
            end loop;
            commit;
        end;
    select a.id,b.id from t1 a,t1 b
    where a.id+5=b.id;
    或者where a.id+(select count(*) from t1)/2=b.id;
    where a.id+round((select count(*) from t1)/2)=b.id; //round 四舍五入
    不显示12的话,用外部语句
    where a.id+round((select count(*) from t1)/2)=b.id(+); //报错
    where a.id=b.id(+)-round((select count(*) from t1)/2);
    and a.id<=round((select count(*) from t1)/2);

子查询

  1. 嵌套子查询
  2. 关联子查询
        查询lastname
        条件老百姓,不要领导
        select last_name from employees
        where employee_id not in (select nvl(manager_id,0)  from employees)
        select last_name from employees e
        where not exists
            (select employee_id from employees
            where e.employee_id = manager_id)
        select empno,ename from emp where ename=upper(&xm);
        define gh=7839; //undefine 取消定义
        select empno,ename from emp where ename=&gh;

        select empno,ename,&c3 from emp order by &c3;
        select empno,ename,&&c3 from emp order by &c3;

DML(insert,update,delete.merge)

grant ...限制
select department_id from departments order by 1;

合并 读写一致性

回滚段的作用: