zoukankan      html  css  js  c++  java
  • PL/SQL重点难点

    几种常用数据库的JDBC URL:
    对于 Oracle 数据库连接,采用如下形式:
    jdbc:oracle:thin:@localhost:1521:sid
    对于 SQLServer数据库连接,采用如下形式:
    jdbc:microsoft:sqlserver//localhost:1433; DatabaseName=sid
    对于 MYSQL 数据库连接,采用如下形式:  
    jdbc:mysql://localhost:3306/sid
    数据库事务:完全执行或完全不执行的SQL语句。
    JDBC
    JDBC作用:①提供操作数据库的统一接口
             ②屏蔽了硬件的差异化。
    JDBC的任务:1.同一个数据库建立连接
                2.像数据库发送SQL语句
                3.处理数据库返回的请求 
    JDBC四大步:
    1.加载驱动Class.forName("...");
    2.建立连接Connection conn = DriverManager.getConnection(url,user,password);
    3.SQL语句Statement sqlStatement = conn.createStatement();
    4.返回结果

    触发器:
    1. --编写一个触发器, 在对 my_emp 记录进行删除的时候, my_emp_bak 表中备份对应的记录;
    2. create or replace trigger delete_emp_trigger
    3. before
    4. delete on my_emp
    5. for each row
    6. begin
    7. insert into my_emp_bak
    8. values(:old.employee_id,:old.salary);
    9. end;

    储存过程,储存函数;
    要求: 定义一个函数: 获取给定部门的工资总和 和 该部门的员工总数(定义为 OUT 类型的参数).
    要求: 部门号定义为参数, 工资总额定义为返回值.
    1. create or replace function sum_sal(dept_id number, total_count out number)
    2. return number
    3. is
    4. cursor sal_cursor is select salary from employees where department_id = dept_id;
    5. v_sum_sal number(8) := 0;
    6. begin
    7. total_count := 0;
    8. for c in sal_cursor loop
    9. v_sum_sal := v_sum_sal + c.salary;
    10. total_count := total_count + 1;
    11. end loop;
    12. --dbms_output.put_line('sum salary: ' || v_sum_sal);
    13. return v_sum_sal;
    14. end;

    用户自定义异常:
    1. declare
    2. e_too_high_exception exception;
    3. v_sal employees.salary%type;
    4. begin
    5. select salary into v_sal from employees where employee_id = 100;
    6. if v_sal > 10000 then raise e_too_high_exception;
    7. end if;
    8. exception
    9. when e_too_high_exception then dbms_output.put_line('工资太高了');
    10. end;

    利用游标和记录类型遍历员工表的记录信息
    方法①使用while循环:
    1. declare
    2. --声明记录类型
    3. type emp_record is record(
    4. v_emp_id employees.employee_id%type,
    5. v_emp_sal employees.salary%type
    6. );
    7. --声明一个记录类型的变量
    8. v_emp_record emp_record;
    9. --声明游标
    10. cursor v_emp_cursor is select employee_id,salary from employees where department_id = 80;
    11. begin
    12. --打开游标
    13. open v_emp_cursor;
    14. --提取游标
    15. fetch v_emp_cursor into v_emp_record;
    16. while v_emp_cursor%found loop
    17. dbms_output.put_line(v_emp_record.v_emp_id||','||v_emp_record.v_emp_sal);
    18. fetch v_emp_cursor into v_emp_record;
    19. end loop;
    20. --关闭游标
    21. close v_emp_cursor;
    22. --exception
    23. end;

    方法②使用for循环(简洁):
    1. declare
    2. cursor v_emp_cursor is select employee_id,salary from employees where department_id = 80;
    3. begin
    4. for i in v_emp_cursor loop
    5. dbms_output.put_line(i.employee_id||','||i.salary);
    6. end loop;
    7. end;

    1. --输出2-100之间的素数;
    2. declare
    3. v_i number(3):=2;
    4. v_j number(3):=2;
    5. v_flag number(1):=1;
    6. begin
    7. while v_i <= 100 loop
    8. while v_j <= sqrt(v_i) loop
    9. if v_i mod v_j = 0 then v_flag := 0;
    10. end if;
    11. v_j := v_j +1;
    12. end loop;
    13. if v_flag = 1 then dbms_output.put_line(v_i);
    14. end if;
    15. v_j := 2 ;
    16. v_i := v_i +1;
    17. v_flag := 1;
    18. end loop;
    19. end;

    高级子查询

    PL/SQL基本语法
    1. set serveroutput on
    2. /*输出helloworld*/
    3. declare --声明变量、类型。游标
    4. begin --执行体(相当于main()函数)
    5. dbms_output.put_line('helloworld');
    6. exception --异常处理
    7. end;

    数据库权限管理
    1. create user atuser identified by password --创建用户
    2. alter user atuser quota unlimited on users --创建表空间
    3. alter user scott identified by tiger --修改密码
    4. GRANT create session TO scott --授予权限
    5. create role my_role --创建角色
    6. grant create session,create table,create view to my_role --授予角色权限
    7. grant select,update on scott.employees to atuser --授予对象权限

    Top-n分析
    1. select rn,employee_id,last_name,salary
    2. from( select rownum rn,employee_id,last_name,salary
    3. from(
    4. select employee_id,last_name,salary
    5. from employees
    6. order by salary desc)
    7. )
    8. where rn<=50 and rn>40

    创建序列(sequence)
    1. create sequence empseq
    2. increment by 10 --每次增长10个;
    3. start with 10 --从10增长;
    4. maxvalue 100 -- 提供的最大值;
    5. cycle --需要循环;
    6. nocache --不需要缓存登录;

    1. 选择雇用时间在1998-02-011998-05-01之间的员工姓名,job_id和雇用时间;




  • 相关阅读:
    Golang 实现简单的 Web 服务器
    Aliyun linux repo文件
    云服务器查看登录ip和本机出口ip
    10个高效Linux技巧及Vim命令对比
    使用mkfs.ext4格式化大容量磁盘
    LINUX SHELL 多个命令一起执行的几种方法
    GPT分区
    3种方法更改Linux系统的主机名(hostname)
    Nginx代理访问RDS
    Centos7安装Docker
  • 原文地址:https://www.cnblogs.com/Jxiaobai/p/6617643.html
Copyright © 2011-2022 走看看