📄 田粟.txt
字号:
一,创建四个游标
1,从dept表打印出所有的部门名字
declare
cursor bname
is
select dname from dept;
deptname dept.dname%type;
begin
open bname;
loop
fetch bname into deptname;
if bname%found then
dbms_output.put_line('department is'||' '||deptname);
else
dbms_output.put_line('shu ju is over');
exit;
end if;
end loop;
close bname;
end;
********************************************************************************************
declare
cursor bname
is
select dname from dept;
deptname dept.dname%type;
begin
open bname;
loop
fetch bname into deptname;
dbms_output.put_line('department is'||' '||deptname);
exit when bname%notfound;
end loop;
close bname;
end;
2,定义一个返回类型的游标,该游标返回dept表所有的数据 (用两种方式)
declare
cursor dept_note-----声明游标
return myrecord
is
select * from dept;
type myrecord is record( --------声明类型
tempname emp.enanme%type,
tempjob emp.job%type,
tempsal emp.sal%type
);
temprecord myrecord;
begin
open emp_fetch;-----打开游标
loop -----提取游标
fetch emp_fetch into tempname;
dbms_out.put_line(tempname);
exit when emp_fetch%notfound;
end loop;
end;
二,建一个触发器,完成一个约束:emp表的薪水sal 不能大于20000
create or replace trigger emp_sal
before insert
or update
on emp
for each row
when (new.sal=20000)
begin
if :old.sal>:new.sal then
raise_application_error(-20001,'sal必须小于20000');
end if;
end;
三,以上午写的student,teacher 表为例子,写一个触发器,当删除teacher表的一行数据库时,在student表删除相应的数据(即:删除老师所教的学生)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -