触发器的简单使用
1.触发器介绍
数据库触发器是一个与表相关联的、存储的PL/SQL程序。每当一个特定的数据操作语句(Insert,update,delete)在指定的表上发出时,Oracle自动地执行触发器中定义的语句序列。
2.触发器的类型
(1)语句级触发器
在指定的操作语句操作之前或之后执行一次,不管这条语句影响了多少行 。
(2)行级触发器(FOR EACH ROW)
触发语句作用的每一条记录都被触发。在行级触发器中使用old和new伪记录变量, 识别值的状态。
3.触发器的举例
(1)限制非工作时间向数据库中插入数据
create or replace trigger insertEmp
before insert on emp
declare
begin
if(to_char(sysdate,'day') in ('星期四','星期五')) then
raise_application_error(-20001,'不能插入数据');
end if;
end;
测试:
SQL> insert into emp(empno,deptno) values(14,10);
insert into emp(empno,deptno) values(14,10)
ORA-20001: 不能插入数据
ORA-06512: 在 "TEST1.INSERTEMP", line 5
ORA-04088: 触发器 'TEST1.INSERTEMP' 执行过程中出错
(2)更改的数据不能小于原原有的值
create or replace trigger updateEmp
before update on emp
for each row
declare
-- local variables here
begin
if :new.sal< :old.sal then
raise_application_error(-20002,'更改的数据不能小于原原有的值');
end if;
end updateEmp;
测试:
SQL> update emp set sal=200 where empno=7369;
update emp set sal=200 where empno=7369
ORA-20002: 更改的数据不能小于原原有的值
ORA-06512: 在 "TEST1.UPDATEEMP", line 5
ORA-04088: 触发器 'TEST1.UPDATEEMP' 执行过程中出错
(3) 指定部门插入的员工数不能大于5
create or replace trigger limite
before insert on emp
for each row
declare
-- local variables here
cursor cl is select count(*) from emp group by deptno;
emp_count emp.empno%type;
begin
open cl;
fetch cl into emp_count;
if (emp_count>5) then
raise_application_error(-20002,'员工个数不能多余5个');
end if;
close cl;
end limite;
或
create or replace trigger limite
before insert on emp
for each row
declare
num number;
begin
select count(*) into num from emp where deptno=:new.deptno;
if(num>=5) then
raise_application_error(-20002,'员工个数不能多余5个');
end if;
end limite;
结果和以上的格式一样
相关文章
- SQL Server中变量的声明和使用方法
- oncontextmenu简单使用方法,以及在js函数中失效的问题
- springMVC中两种validation的简单使用
- C++中函数模板与类模板的简单使用
- Mysql中触发器的使用
- SQL Server 触发器中 Update的方法 判断一列是否更新
- oracle12C数据库JSON的应用 --PL/SQL使用json简单入门篇
- C#委托(delegate)的常用方式- 委托的定义 // 委托的核心是跟委托的函数结构一样 public delegate string SayHello(string c); public delegate string SayHello(string c);:定义了一个公共委托类型 SayHello,该委托接受一个 string 类型的参数 c,并返回一个 string 类型的值。 Main 方法 static void Main(string args) { // 本质上其实就是把方法当作委托的参数 SayHello sayC = new SayHello(SayChinese); Console.WriteLine(sayC("欢迎大家")); SayHello sayE = new SayHello(SayEgnlish); Console.WriteLine(sayE("Welcome to")); // 简单的写法:必须类型一样 SayHello s1 = SayChinese; SayHello s2 = SayEgnlish; Console.WriteLine(s1("好好好")); Console.WriteLine(s2("Gooood")); // 最推荐 SayHello ss1 = con => con; Console.WriteLine(ss1("niiiice")); // 匿名委托:一次性委托 SayHello ss3 = delegate(string s) { return s; }; Console.WriteLine(ss3("说中国话")); } 常规实例化委托 SayHello sayC = new SayHello(SayChinese);:创建了一个 SayHello 委托的实例 sayC,并将 SayChinese 方法作为参数传递给委托的构造函数。 Console.WriteLine(sayC("欢迎大家"));:通过委托实例调用 SayChinese 方法,并输出结果。 同理,SayHello sayE = new SayHello(SayEgnlish); 和 Console.WriteLine(sayE("Welcome to")); 是对 SayEgnlish 方法的委托调用。 简化的委托赋值方式 SayHello s1 = SayChinese; 和 SayHello s2 = SayEgnlish;:当委托类型和方法签名一致时,可以直接将方法赋值给委托变量,无需使用 new 关键字。 Console.WriteLine(s1("好好好")); 和 Console.WriteLine(s2("Gooood"));:通过委托实例调用相应的方法。 使用 Lambda 表达式实例化委托 SayHello ss1 = con => con;:使用 Lambda 表达式创建委托实例 ss1,con => con 表示接受一个参数 con 并返回该参数本身。 Console.WriteLine(ss1("niiiice"));:通过委托实例调用 Lambda 表达式。 匿名委托 SayHello ss3 = delegate(string s) { return s; };:使用匿名委托创建委托实例 ss3,delegate(string s) { return s; } 是一个匿名方法,直接在委托实例化时定义了方法体。 Console.WriteLine(ss3("说中国话"));:通过委托实例调用匿名方法。 委托引用的方法定义 public static string SayChinese(string content) { return content; } public static string SayEgnlish(string content) { return content; } public static string SayChinese(string content) 和 public static string SayEgnlish(string content):定义了两个静态方法,分别接受一个 string 类型的参数 content,并返回该参数本身。这两个方法的签名与 SayHello 委托一致,可以被 SayHello 委托引用。 常规的委托实例化、简化的赋值方式、Lambda 表达式和匿名委托。委托在 C# 中是一种强大的机制,它允许将方法作为参数传递,实现了代码的灵活性和可扩展性。
- mybatis动态sql中的trim标签的使用(转)
- python中sklearn的朴素贝叶斯方法(sklearn.naive_bayes.GaussianNB)的简单使用