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.返回结果
触发器:
--编写一个触发器, 在对 my_emp 记录进行删除的时候, 在 my_emp_bak 表中备份对应的记录;create or replace trigger delete_emp_triggerbeforedelete on my_empfor each rowbegininsert into my_emp_bakvalues(:old.employee_id,:old.salary);end;
储存过程,储存函数;
要求: 定义一个函数: 获取给定部门的工资总和 和 该部门的员工总数(定义为 OUT 类型的参数).
要求: 部门号定义为参数, 工资总额定义为返回值.
create or replace function sum_sal(dept_id number, total_count out number)return numberiscursor sal_cursor is select salary from employees where department_id = dept_id;v_sum_sal number(8) := 0;begintotal_count := 0;for c in sal_cursor loopv_sum_sal := v_sum_sal + c.salary;total_count := total_count + 1;end loop;--dbms_output.put_line('sum salary: ' || v_sum_sal);return v_sum_sal;end;
用户自定义异常:
declaree_too_high_exception exception;v_sal employees.salary%type;beginselect salary into v_sal from employees where employee_id = 100;if v_sal > 10000 then raise e_too_high_exception;end if;exceptionwhen e_too_high_exception then dbms_output.put_line('工资太高了');end;
利用游标和记录类型遍历员工表的记录信息
方法①使用while循环:
declare--声明记录类型type emp_record is record(v_emp_id employees.employee_id%type,v_emp_sal employees.salary%type);--声明一个记录类型的变量v_emp_record emp_record;--声明游标cursor v_emp_cursor is select employee_id,salary from employees where department_id = 80;begin--打开游标open v_emp_cursor;--提取游标fetch v_emp_cursor into v_emp_record;while v_emp_cursor%found loopdbms_output.put_line(v_emp_record.v_emp_id||','||v_emp_record.v_emp_sal);fetch v_emp_cursor into v_emp_record;end loop;--关闭游标close v_emp_cursor;--exceptionend;
方法②使用for循环(简洁):
declarecursor v_emp_cursor is select employee_id,salary from employees where department_id = 80;beginfor i in v_emp_cursor loopdbms_output.put_line(i.employee_id||','||i.salary);end loop;end;
--输出2-100之间的素数;declarev_i number(3):=2;v_j number(3):=2;v_flag number(1):=1;beginwhile v_i <= 100 loopwhile v_j <= sqrt(v_i) loopif v_i mod v_j = 0 then v_flag := 0;end if;v_j := v_j +1;end loop;if v_flag = 1 then dbms_output.put_line(v_i);end if;v_j := 2 ;v_i := v_i +1;v_flag := 1;end loop;end;
高级子查询
PL/SQL基本语法
set serveroutput on/*输出helloworld*/declare --声明变量、类型。游标begin --执行体(相当于main()函数)dbms_output.put_line('helloworld');exception --异常处理end;
数据库权限管理
create user atuser identified by password --创建用户alter user atuser quota unlimited on users --创建表空间alter user scott identified by tiger --修改密码GRANT create session TO scott --授予权限create role my_role --创建角色grant create session,create table,create view to my_role --授予角色权限grant select,update on scott.employees to atuser --授予对象权限
Top-n分析
select rn,employee_id,last_name,salaryfrom( select rownum rn,employee_id,last_name,salaryfrom(select employee_id,last_name,salaryfrom employeesorder by salary desc))where rn<=50 and rn>40
创建序列(sequence)
create sequence empseqincrement by 10 --每次增长10个;start with 10 --从10增长;maxvalue 100 -- 提供的最大值;cycle --需要循环;nocache --不需要缓存登录;
- 选择雇用时间在1998-02-01到1998-05-01之间的员工姓名,job_id和雇用时间;

浙公网安备 33010602011771号