触发器和java的关系_触发器-1 - java ee spring - 博客园
------------------------------------环境代码
create table student
(stuid varchar2(10) not null,
stuname varchar2(10) not null,
sex char(2)
);
create table subject
(subjectid int,
subjectname varchar2(10)
);
create table score
(
stuid int,
subjectid int,
score int
);
insert into student values (1001, 'wind', '男');
insert into student values (1002, 'snow', '女');
insert into student values (1003, 'apple', '男');
insert into subject values (1, 'oracle');
insert into subject values (2, 'java');
insert into score values (1001, 1, 90);
insert into score values (1002, 2, 88);
create table apple
(sid int,
sno int);
---------------主体部分
一、触发器
1.触发器具有三个部分
(1).触发事件
(2).可选的触发器约束条件
(3).触发器动作
2.可以新建对应如下语句的触发器
(1).DML语句(insert、delete、update)
(2)DDL语句(create、alter、drop)
(3).数据库操作(servererror、logon、logoff、startup、shutdown)
3.可以创建触发器的对象:1.数据库表 2.数据库视图 3.用户模式 4.数据库实例
4.触发器类型
(1).DML触发器(包括行级触发器、语句级触发器)
(2).系统触发器
(3).替代触发器
(4).模式触发器
5.执行DML语句的顺序
(1).执行before语句级触发器(如果存在)
(2).对于受语句影响的每一行执行DML语句
(3).执行after语句触发器(如果存在)
6.两个特殊的值
:new 新产生的值
:old 原是的值
7.触发器中的谓语
(1). inserting
(2).updating
(3). deleting
---------------------------------------------------------------
****************************************************************
一、语句触发器
****************************************************************
--------------------------------------------------------------
一、语句触发器
create or replace trigger schemaname.triggername
before | after |delete |update of 列名
on 表名
[for each row]
when 条件
------------------------------------------------------
第一部分:before触发器
------------------------------------------------------
--案例01:新建一个测试的行前触发器
create or replace trigger tr01
before insert on student
begin
dbms_output.put_line('这个是行前触发器!');
end;
--案例02:禁止工作人员在非工作日修改表信息
create table k01
(sid int,
sno int);
create or replace trigger tr101
before insert or update or delete on k01
begin
if to_char(sysdate, 'DY','nls_date_language=AMERICAN')
in ('SAT','SUN') then
raise_application_error(-20001,'不能休息日修改该表数据');
end if;
end;
--案例03:使用三个条件谓语
create or replace trigger tr102
before insert or update or delete on k01
begin
if to_char(sysdate, 'DY','nls_date_language=AMERICAN')
in ('SAT','SUN') then
case
when inserting then
begin
raise_application_error(-20001,'对不起,不能完成插入操作!');
end;
when updating then
begin
raise_application_error(-20002,'对不起,不能在休息日更新该表数据!');
end;
when deleting then
begin
raise_application_error(-20003,'对不起,不能在休息日删除该表数据!');
end;
end case;
end if;
end;
------------------------------------------------------
第二部分:after触发器
------------------------------------------------------
--案例01:新建一个测试的行后触发器
create or replace trigger tr02
after update on student /*没有for each row说明是个表级别触发器*/
begin
dbms_output.put_line('这个是表级update触发器!');
/*如果你一次更新的语句是几条只触发一次!*/
end;
--案例02:
/*新建一个触发器完成审计工作,审计在k01表上的insert、update、delete的操作次数、最早执行时间、最近执行时间*/
第一步:新建用于记录审计信息的审计表
create table audit_table
(
name varchar2(10),
login_user varchar2(20),
ins int,
upd int,
del int,
starttime date,
endtime date
);
第二步:新建触发器
方法1:用于后台监控
create or replace trigger tr_audit_k01_01
after insert or update or delete on k01
declare
cnt int;
begin
select count(*) into cnt from audit_table where name='K01';
if cnt=0 then
insert into audit_table values ('K01',ora_login_user,0,0,0,sysdate, null);
end if;
case
when inserting then
update audit_table set ins=ins+1, endtime=sysdate where name='K01';
when updating then
update audit_table set upd=upd+1, endtime=sysdate where name='K01';
when deleting then
update audit_table set del=del+1, endtime=sysdate where name='K01';
end case;
end;
方法2:前后台监控
create or replace trigger tr_audit_k01_02
after insert or update or delete on k01
declare
cnt int;
begin
select count(*) into cnt from audit_table where name='K01';
if cnt=0 then
insert into audit_table values ('K01',ora_login_user,0,0,0,sysdate, null);
end if;
case
when inserting then
begin
update audit_table set ins=ins+1, endtime=sysdate where name='K01';
dbms_output.put_line('你完成了数据插入操作!!');
end;
when updating then
begin
update audit_table set upd=upd+1, endtime=sysdate where name='K01';
dbms_output.put_line('你完成了数据更新操作!');
end;
when deleting then
begin
update audit_table set del=del+1, endtime=sysdate where name='K01';
dbms_output.put_line('你在进行删除操作,请慎重!!');
end;
end case;
end;
触发器和java的关系_触发器-1 - java ee spring - 博客园相关推荐
- java 级联删除_级联删除 - Asher鑫与 - 博客园
相信学过数据库基础的童鞋们都知道,当有两个表A和B,表A的外键正是表B的主键时,我们会发现,当我们用数据库语言对B表数据进行删除操作的时候,我们会发现根本就删除不了,所以呢,在实际代码中,想要通过点击 ...
- java锚点_定位与锚点 - strongerPian - 博客园
文档流: 普通流:上至下,左至右 浮动流:元素添加了float:: 定位流:添加了定位属性 定位 position:; static 默认值 absolute 绝对定位(脱离文档流,不占位:默认参考浏 ...
- java 雷达反射面积_毫米波雷达 - Magnum Programm Life - 博客园
微波是通信和雷达使用的主要频段, 300Mhz ~ 300GHz. 毫米波是微波的一个子频段. 可见光,红外,激光, 由于频率不同和微波的特性有很大差异. 不同频段的电磁波在 "反射, 吸 ...
- java毕业设计项目_第167期ssm多用户博客个人网站_计算机毕业设计
java毕业设计项目_第167期ssm多用户博客个人网站_计算机毕业设计 [源码请到资源专栏下载] 今天分享的项目是<ssm多用户博客个人网站> 该项目分为2个角色,管理员和用户. 用户可 ...
- webbrowser中localhost和发布的地址_发布一款android版博客园官方app
1.前言 博客园创立于2004年1月,是一个面向开发者的知识分享社区.自创建以来,博客园一直致力并专注于为开发者打造一个纯净的技术交流社区,推动并帮助开发者通过互联网分享知识,从而让更多开发者从中受益 ...
- Java 内存 关系_发生在Java内存模型中的关系之前
(1) What does it really mean by saying "ordered before"? Because even if action_a happens- ...
- java费波拉切_面试题 - 不再犯错 - 博客园
js的基本类型有哪些?引用类型有哪些?null和undefined的区别. 如何判断一个变量是Array类型?如何判断一个变量是Number类型?(都不止一种) Object是引用类型嘛?引用类型和基 ...
- java编写某计算器控制台程序_计算器 - 进阶的憨狗 - 博客园
源起 最近在看程杰著作的<大话设计模式>,全书以小菜和大鸟对话的形势,由浅入深的讲解程序的设计思想,影射出一个个设计模式.我之前虽然也使用过一些设计模式,但没有系统的学习.整理.总结,现从 ...
- mysql是大端小端_大端和小端 - HackerVirus - 博客园
在计算机中是以字节为单位,每个地址对应一个字节,一个字节8bit.在C中,除了8bit的char以外,还有16bit的short,32位的int,64位long,当然具体要由编译器决定,可以通过siz ...
最新文章
- 分布式事务篇——第二章:分布式事务解决之2PC剖析
- JavaScript 为什么要有 Symbol 类型?
- 利用 bugly 分析应用崩溃
- RenderManager - cssHook - select_all icon render logic
- Unity的几个特殊文件夹
- 分布式搜索elasticsearch
- python下载-python下载及安装
- 三十款国外IES灯光经典素材整理i
- 【转】opencv中widthStep不一定等于width*nChannels的原因
- 关于电脑前置耳机插孔没声音的问题
- 用ps制作LOGO(个人向)
- 求助:Python识别PDF段落和翻译的问题
- linux ap 模式,无线AP是什么,客户端模式(apclient)是什么意思?
- Android Studio 插件-Android Styler 的使用 (转)
- WPF中的文字修饰——上划线,中划线,基线与下划线
- 从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t。
- 10.Python面向对象编程
- mysql 分区合并_mysql中的合并表和分区表详解(经常使用的概念)
- 数据结构与算法33-PIPI捡垃圾
- python无法启动0xc0000022_无法正常启动0xc0000022?0xc0000022一键修复教程
热门文章
- HDU 5816 Hearthstone
- BZOJ2888 : 资源运输
- 按钮点击WIN8 磁贴效果
- UIWebView实现离线浏览
- 很多人都不知道,其实博客园给我们博客开了二级域名
- 《BREW进阶与精通——3G移动增值业务的运营、定制与开发》连载之76——BREW中的安全性网络编程...
- 谈吉日嘎拉的《白话反射技术》及其他(吵架篇)
- LCS2005客户端配置详解:LCS2005系列之二
- 注意服务器系统日期对防病毒软件的影响
- nodejs+html转换pdf,Nodejs中使用phantom将html转为pdf或图片格式的方法.pdf