📄 朱志明6-12.txt
字号:
1, 把咱们练习(6月8号)里面写到的过程和函数放到包里进行管理,然后调用测试
create or replace package masterpkg
is
pragma serially_reusable;
function sum_function(n out int)
return int;
function input_function(v_no int)
return int;
procedure updataemp( v_no int,v_sal int);
procedure updatasal(v_no int);
end masterpkg;
**********************************************************************************
create or replace package body masterpkg
is
pragma serially_reusable;
function sum_function(n out int)
return int;
declare
v_n int := &请输入一个大于0的整数;
v_m int := 1;
v_sum int :=0;
v_small exception;
begin
if v_n<=0 then
raise v_small;
end if;
for v_m in 1..n loop
v_sum :=v_sum + v_n;
end loop;
return v_sum;
dbms_output.put_line(v_sum);
exception
when v_small then
dbms_output.put_line('输入错误,请输入整数');
end sum_function;
function input_function(v_no int)
return int;
as
v_sal emp.sal%type;
begin
select sal into v_sal from emp where empno = v_no;
return v_sal;
end input_function;
procedure updataemp( v_no int,v_sal int)
is
begin
update emp set sal=v_sal where empno=v_no;
exception
when case_not_found then
dbms_output.put_line('没有你想要的数据');
end updataemp;
procedure updatasal(v_no int)
is
emp_sal emp.sal%type;
type empno_table is table of emp.empno%type
index by binary_integer;
e_emp empno_table;
type rowtable is table of emp%rowtype
index by binary_integer;
datas rowtable;
begin
for i in 1..5 loop
select empno into e_emp(i) from emp group by empno having count(empno)=1;
end loop;
for j in 1..5 loop
select * into datas(j) from emp where empno=e_emp(j);
emp_sal := input_function(e_emp(j));
if emp_sal<2500 then
updataemp(e_emp(j),3600);
end if;
end loop;
end updatasal;
end masterpkg;
**********************************************************************************
2,把昨天最后一道题写出来
create or replace view tempview as
select name,age,id
from mytemp
*********先创建一张mytemp的视图,再对这个视图进行插入操作********
create or replace trigger addtri
instead of insert
on tempview
for each row
declare
shu int;
num int;
begin
select count(id) into shu from mytemp where id = :new.id;
select max(id) into num from mytemp;
if shu =0 then
insert into mytemp values(:new.name,:new.age,num+1);
end if;
end;
***********************************************************************
3,把刚才的例子敲一遍
create or replace package my_pk
is
pragma serially_reusable;
v_deptrec dept%rowtype;
v_sqlcode number;
v_sqlerrm varchar2(2048);
function add_dept(v_deptno number,
v_deptname varchar2,
v_deptloc varchar2)
return number;
function remove_dept(v_deptno number)
return number;
procedure query_dept(v_deptno number);
procedure read_dept;
end my_pk;
***************************************************************************
create or replace package body my_pk
is
pragma serially_reusable;
--私有变量声明
v_flag number;
--私有游标声明
cursor mycursor
is
select deptno,dname from dept;
--私有函数定义和声明
function check_dept(v_deptno number)
return number
is
begin
select count(*) into v_flag from dept
where deptno = v_deptno;
if v_flag > 0 then
v_flag := 1;
end if;
return v_flag;
end check_dept;
--公有函数定义
function add_dept(v_deptno number,
v_deptname varchar2,
v_deptloc varchar2)
return number
is
begin
if check_dept(v_deptno ) = 0 then
insert into dept values(v_deptno ,v_deptname ,v_deptloc );
return 1;
else
return 0;
end if;
exception
when others then
v_sqlcode := sqlcode;
v_sqlerrm := sqlerrm;
return -1;
end add_dept;
--公有函数定义
function remove_dept(v_deptno number)
return number
is
begin
if check_dept(v_deptno) = -1 then
delete from dept where deptno = v_deptno;
return 1;
else
return 0;
end if;
exception
when others then
v_sqlcode :=sqlcode;
v_sqlerrm :=sqlerrm;
return -1;
end remove_dept;
--公有过程的定义
procedure query_dept(v_deptno number)
is
begin
if check_dept(v_deptno) = 1 then
select * into v_deptrec from dept
where deptno = v_deptno;
end if;
exception
when others then
v_sqlcode :=sqlcode;
v_sqlerrm :=sqlerrm;
end query_dept;
--公有过程的定义
procedure read_dept
is
v_deptno number;
v_dname varchar2(14);
begin
for c_mycursor in mycursor loop
v_deptno := c_mycursor.deptno;
v_dname := c_mycursor.dname;
dbms_output.put_line(v_deptno||' '||v_dname);
end loop;
end read_dept;
begin
v_sqlcode := null;
v_sqlerrm :='初始化消息文本';
end my_pk;
*****************************************************************************************
4,自己写一个应用的例子,这个例子里面用到游标,过程(把写的游标放到过程里),触发器(把过程 放到触发器里),题目不限,自己想象
/*实现功能------将员工号为30的员工的补贴加800 ,但员工的编号不能为空
create or replace trigger testcase
after update
on emp
for each row
declare
v_deptno number;
procedure test1(v_no int)
is
cursor tes
is
select * from emp for update;
v_emp emp%rowtype;
begin
open tes;
loop
fetch tes into v_emp;
if v_emp.deptno>=30 then
update emp set comm=800
where current of tes;
else
exit;
end if;
end loop;
close tes;
end test1;
begin
case
when updating then
dbms_output.put_line('部门的部门编号不为空');
test1(v_deptno);
end case;
end testcase;
***************************************************************************************
5,每人对6,7章学的内容做一个总结(包含讲了那些内容,自己掌握了多少等等)
通过这段时间对第六章SQL语句的学习,发现自己在语句的查询上还是有所欠缺。而第六章
主要是讲了数据四大块。一,查询数据,二,维护数据,三,事物的控制,四,SQL函数 。在数
据的查询中主要是讲了数据的基本查询。如:对表的查询,查询表中的数据,显示表的基本结构,
这些都是基础容易掌握。在讲到查询时还讲到了使用表达式、使用WHERE子句、使用ORDER BY子句
这些还能够掌握。在查询当中有时会涉及分组查询,在分组里面还会有组处理函数(MAX,MIN,AVG,
SUM,count...)等等主要函数,在使用GROUP BY子句的时候还要分成单列分组、多列分组的情况。
关键是HAVING子句的用法。在什么情况下用,在用的时候要注意它的用法,这些通过联系已经得到了
加深,在分组的时候还不知道为什么要以那个属性值分组,这是我还没弄清楚的。在查询的时候还讲
了连接查询他饱含有(相等连接、自我连接、不等连接和笛卡尔连接)在合并查询中有(UNION,UNION ALL
,INTERSECT,MINUS),还有子查询(单行子查询,多行子查询,相关子查询,标量子查询,多列子查询
)概念很多,但还是比较简能够掌握.
在数据的维护方面主要讲了数据的插入,数据的更改,数据的删除.这个比较容易,很容易上手.
在事物的控制方面就是前面的数据的查询和和数据的维护方面的知识了加了点事物的提交,为事物设置
保留点以及事物的全部回退和部分回退知识代码简单容易理解.SQL函数部分主要讲了单行数学函数
单行字符函数,单行日期时间函数,单行转换函数和其他函数这是需要记着的
第七章就是重中之重了主要讲了PL/SQL的常用符号及字符集,和他的数据类型和sql 有很大
的区别添加了(%type,%rowtype,table,record)这四种变量类型,还在联系当中不是很熟练阿.当
然重要的还是PL/SQL的流程控制 他分为条件控制,循环控制,顺序控制.条件控制就是(if...then
...end if和if...then...else...end if,if...then...elsif...end if和case结构了)循环控制主要
是(loop...end loop,while...loop...end loop,for...loop...end loop).顺序控制主要讲了
(goto语句,null语句).这些还有待联系 还有PL/SQl的异常处理难点就是预定义异常错误,非预定义
异常错误和自定义
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -