数据库存储过程,触发器,游标,序列总结
来源:互联网 发布:线位图计算软件 编辑:程序博客网 时间:2024/05/17 15:17
存储过程(Stored Procedure)
在大型数据库系统中,一组为了完成特定功能的SQL 语句集,存储在数据库中,经过第一次编译后再次调用不需要再次编译,用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它。存储过程是数据库中的一个重要对象,任何一个设计良好的数据库应用程序都应该用到存储过程。
语法:
create [or replace] procedure name(parameter in type, parameter out type)
is/as
[变量名] type
begin
存储过程体
end;
/
1) in 代表输入,out 代表输出2) 存储过程中,is 和 as都一样
3) 声明变量时,若为number,不用指定大小,若为varchar2,则需要指定
在存储过程中使用游标:
游标
系统为用户开设的一个数据缓冲区,存放SQL语句的执行结果。每个游标区都有一个名字,用户可以用SQL语句逐一从游标中获取记录,并赋给主变量,交由主语言进一步处理。
在存储过程中声明游标:
cursor cursor_name is select column_name from table_name where condition;
循环使用:
open cursor_name;
loop
fetch cursor_name into variable1, variable2;
exit when cursor_name%notfound;
(operation on variables)
end loop;
close cursor_name;
示例:
<span style="font-size:14px;">create or replace procedure l_procedureis leave_num number; student_num varchar2(16); cursor c_studentnum is select distinct studentnum from leave_apply;begin open c_studentnum; loop fetch c_studentnum into student_num; exit when c_studentnum%notfound; select count(l.applyNum) into leave_Num from leave_apply l where l.studentnum = student_Num; insert into leave_apply_stat(studentnum,leavenum) values(student_Num, leave_Num); end loop; close c_studentnum; commit;end;</span>
--------------------------------------------------------------------------------------------------
触发器(trigger)
SQL server 提供给程序员和数据分析员来保证数据完整性的一种方法,它是与表事件相关的特殊的存储过程,它的执行不是由程序调用,也不是手工启动,而是由事件来触发。
create or replace trigger [trigger name]
before/after insert/update/delete on [table name]
for each row(行级触发器)
when(判断条件)
declare
[variable name] type;
begin
end;
/
1)before/ after 指代触发器是需要在操作完成前/后触发2)insert/update/delete 指代触发触发器的条件
3)for each row 代表这是一个行触发器,对每一行的操作都会触发该触发器。
(加 for each row 和不加的区别在于:加了之后,若update一次更新多行,则每一行的更改都会触发一次触发器;若不加,则无论修改多少行,对表的操作只有一次,只触发一次触发器)
4)对基表的操作引发的触发器无法在基表发生更改后访问该表。
序列的用法:
create sequence [sequence name] start with 1 increment by 1 maxvalue 100 minvalue 1 no circle cache 20;
1)start with代表序列起始值
2)increment代表序列自增值
3)maxvalue最大值, minvalue 最小值, no circle 是否允许循环, cache指代允许预先生成的存入内存中的序列值个数
示例:
<span style="font-size:14px;">create sequence messagenum_seq start with 0 increment by 1;create or replace trigger tri_updateafter update of state on leave_applyfor each rowdeclaremessagenum number;content varchar2(128); begin select messagenum_seq.nextval into messagenum from dual; content := '你好,' || :new.state; insert into message(messagenum, content, applynum, studentnum, time) values(messagenum, content, :new.applynum, :new.studentnum, sysdate());end;/</span>
- 数据库存储过程,触发器,游标,序列总结
- 数据库总结 建库 建表 触发器 存储过程 游标等等
- 数据库开发之存储过程、触发器、游标
- 数据库存储过程,触发器,游标,函数
- 数据库基础知识点二:PLSQL、游标、程序包、存储过程、触发器,序列,视图,索引,数据字典
- 游标,存储过程,触发器
- SQL-SERVER数据库中的游标、存储过程和触发器
- MYSQL数据库的索引、视图、触发器、游标和存储过程
- MYSQL数据库的索引、视图、触发器、游标和存储过程
- Oracle数据库语句 简单的存储过程+触发器+游标
- MYSQL数据库的索引、视图、触发器、游标和存储过程
- 5.存储过程 游标 触发器
- 游标、触发器、存储过程实例
- 包 存储过程 触发器 游标
- 游标,存储过程,触发器,事务
- Mysql -- 存储过程/触发器/游标
- 【MySQL】存储过程 游标 触发器
- MySQL存储过程+游标+触发器
- 【总结】如何利用云平台构建容错的APP
- 用pandas分析百万电影数据
- Hdu-5344 MZL's xor
- 页面表格的绘制
- 学习循环
- 数据库存储过程,触发器,游标,序列总结
- 端口理解
- 牛腩完结篇
- ACM 搜索 HDU 1728 搜索迷宫
- Python2学习笔记(3)
- 蓝牙4.0BLE cc2540 cc2541 ios OAD教程(空中更新固件)[原创,多图]
- Matrix67:漫话中文分词算法
- property 中的strong 与weak
- VIM开发环境配置