📄 张杨.txt
字号:
一,创建四个游标
1,从dept表打印出所有的部门名字
2,定义一个返回类型的游标,该游标返回dept表所有的数据 (用两种方式)
declare
type deptrecord is record
(deptno dept.deptno%type,
dname dept.dname%type,
dloc dept.loc%type
);
v_deptno dept.deptno%type;
v_dname dept.dname%type;
/* cursor a1 --声名a1游标
is
select dname from dept ;
cursor a2
return deptrecord
is
select deptno,dname,loc from dept ;*/
--v_dname dept.dname%type;
v_deptrecord deptrecord;
/*cursor a3(v_dept_no number)
is
select deptno,dname,loc from dept where deptno>=v_dept_no;*/
cursor a4
is
select deptno,dname from dept ;
begin
---------a1游标-----
/*open a1;
loop
fetch a1 into v_dname ;
if a1%found then
dbms_output.put_line('a1:'||v_dname);
else
dbms_output.put_line('a1:'||'over');
exit;
end if;
exit when a1%notfound;
end loop;
close a1;
end;
close a1;
---------a1游标-----
---------a2游标方法 1-------------
/*open a2;
loop
fetch a2 into v_deptrecord;
exit when a2%notfound;
dbms_output.put_line('a2:'||v_deptrecord.deptno|| ||v_deptrecord.dname|| ||v_deptrecord.dloc);
end loop;
end; close a2;*/
---------a2游标方法 1-------------
---------a2游标方法 2-------------
/* for dept_rec in a2
loop
dbms_output.put_line('a2:'||' '||dept_rec.deptno||' '||dept_rec.dname||' '||dept_rec.dloc);
end loop;
end;close a2;/
---------a2游标方法 2-------------
---------a3游标-----
/*open a3(30);
loop
fetch a3 into v_deptrecord;
if a3%found then
dbms_output.put_line('a3:'||' '||v_deptrecord.deptno||' '||v_deptrecord.dname||' '||v_deptrecord.dloc);
else
dbms_output.put_line('a3:ok');
exit;
end if;
end loop;
end; close a3;*/
---------a3游标-----
---------a4游标-----
/* open a4;
loop
fetch a4 into v_deptno,v_dname;
if a4%found then
dbms_output.put_line('a4:'||v_deptno||v_dname);
else
dbms_output.put_line('a4:ok');
exit;
end if;
end loop;
end;close a4;*/
二,建一个触发器,完成一个约束:emp表的薪水sal 不能大于20000
create or replace trigger saltrigger
before update of sal or insert
on emp
for each row
begin
case
when updating ('sal') then
if :new.sal>=20000 then
raise_application_error(-20002,'bu can da yu20000');
end if;
when inserting then
if :new.sal>=20000 then
raise_application_error(-20003,'bu can da yu20000');
end if;
end case;
end;
三,以上午写的student,teacher 表为例子,写一个触发器,当删除teacher表的一行数据库时,在student表删除相应的数据(即:删除老师所教的学生)
create or replace trigger tr_teacher
after delete
on teacher
for each row
begin
delete from student where lid=:old.tid;
end;
四,写一个触发器,来完成自增长列值的增加。如表temp(name varchar2(10),age int,id int),当执行insert into temp(name,age) values('andy',24)时 会自动的插入id 的值,id 是递增的列
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -