关闭

Oracle触发器使用

199人阅读 评论(0) 收藏 举报
分类:

触发器(trigger)

        应用场景:1.复杂的安全性检查;2.数据确认;3.实现审计功能;4.完成数据的备份和同步。
        类型:语句级触发器(针对是表)和行级触发器(针对是行, :old和:new表示一行的记录,其中:old表示操作之前的值,:new表示操作之后的值)。

基本结构

create or replace trigger 触发器名称
after【or before】 insert 【or delete or update[列名]】
on 表名称
for each row when(条件)
declare
begin
    dbms_output.put_line('触发器已经执行!'); // PL/SQL语句块
end;

案例一(非工作时间禁止插入数据,针对应用场景1、2、3原理都一样):
create or replace 
trigger SECURITYEMP 
BEFORE INSERT ON BAR3D 
BEGIN
 if to_char(sysdate,'day') in('星期六','星期日') or
    to_number(to_char(sysdate,'hh24')) not between 9 and 18 then
 raise_application_error(-20001, '禁止在非工作时间插入数据');
 end if;
END;

案例二(将子表汇总数据添加到主表中,针对应用场景4,数据备份与同步原理一样)
create or replace 
trigger "SCOTT"."CAPACITY_SUM" 
AFTER INSERT OR UPDATE ON "SCOTT"."MACHINE_UNIT_INFO"
for each row
declare 
PRAGMA AUTONOMOUS_TRANSACTION;
capa NUMBER(10,2) := 10000;
BEGIN
SELECT CAPACITY into capa  FROM VIEW_SECURITY_ORGANIZATION where CODE=:new.FACTORYCODE;
UPDATE SECURITY_ORGANIZATION  SET CAPACITY=capa  WHERE CODE=:new.FACTORYCODE;
COMMIT;
END;

附表结构:
表SECURITY_ORGANIZATION
/*
Navicat Oracle Data Transfer
Oracle Client Version : 10.2.0.5.0

Source Server         : 192.168.1.201ORCL
Source Server Version : 100200
Source Host           : 192.168.1.201:1521
Source Schema         : SCOTT

Target Server Type    : ORACLE
Target Server Version : 100200
File Encoding         : 65001

Date: 2015-11-19 09:40:25
*/


-- ----------------------------
-- Table structure for SECURITY_ORGANIZATION
-- ----------------------------
DROP TABLE "SCOTT"."SECURITY_ORGANIZATION";
CREATE TABLE "SCOTT"."SECURITY_ORGANIZATION" (
"ORG_ID" NUMBER NOT NULL ,
"NAME" VARCHAR2(64 BYTE) NOT NULL ,
"DESCRIPTION" VARCHAR2(255 BYTE) NULL ,
"PARENT_ID" NUMBER NULL ,
"CODE" VARCHAR2(20 BYTE) NULL ,
"CAPACITY" NUMBER(10) NULL ,
"LONGITUDE" NUMBER(20,10) NULL ,
"LATITUDE" NUMBER(20,10) NULL ,
"ADDRESS" VARCHAR2(150 BYTE) NULL ,
"HOTAREA" NUMBER(10,2) NULL ,
"HOTWAYS" VARCHAR2(100 BYTE) NULL ,
"MINRUNWAYS" NUMBER(10,2) NULL ,
"DJJDATA" NUMBER(10,2) NULL ,
"JCDATA" NUMBER(10,2) NULL ,
"ISAPPLYHOT" CHAR(1 BYTE) NULL 
)
LOGGING
NOCOMPRESS
NOCACHE

;
COMMENT ON TABLE "SCOTT"."SECURITY_ORGANIZATION" IS '组织结构';
COMMENT ON COLUMN "SCOTT"."SECURITY_ORGANIZATION"."NAME" IS '组织名称';
COMMENT ON COLUMN "SCOTT"."SECURITY_ORGANIZATION"."DESCRIPTION" IS '组织名称描述';
COMMENT ON COLUMN "SCOTT"."SECURITY_ORGANIZATION"."CODE" IS '组织编码';
COMMENT ON COLUMN "SCOTT"."SECURITY_ORGANIZATION"."CAPACITY" IS '总装机容量(MW)';
COMMENT ON COLUMN "SCOTT"."SECURITY_ORGANIZATION"."LONGITUDE" IS '经度';
COMMENT ON COLUMN "SCOTT"."SECURITY_ORGANIZATION"."LATITUDE" IS '纬度';
COMMENT ON COLUMN "SCOTT"."SECURITY_ORGANIZATION"."ADDRESS" IS '地点';
COMMENT ON COLUMN "SCOTT"."SECURITY_ORGANIZATION"."HOTAREA" IS '供热面积';
COMMENT ON COLUMN "SCOTT"."SECURITY_ORGANIZATION"."HOTWAYS" IS '供热方式';
COMMENT ON COLUMN "SCOTT"."SECURITY_ORGANIZATION"."MINRUNWAYS" IS '最小运行方式';
COMMENT ON COLUMN "SCOTT"."SECURITY_ORGANIZATION"."DJJDATA" IS '电监局数据';
COMMENT ON COLUMN "SCOTT"."SECURITY_ORGANIZATION"."JCDATA" IS '机测数据';
COMMENT ON COLUMN "SCOTT"."SECURITY_ORGANIZATION"."ISAPPLYHOT" IS 'Y供热?N不供热';

-- ----------------------------
-- Indexes structure for table SECURITY_ORGANIZATION
-- ----------------------------

-- ----------------------------
-- Checks structure for table SECURITY_ORGANIZATION
-- ----------------------------
ALTER TABLE "SCOTT"."SECURITY_ORGANIZATION" ADD CHECK ("ORG_ID" IS NOT NULL);
ALTER TABLE "SCOTT"."SECURITY_ORGANIZATION" ADD CHECK ("NAME" IS NOT NULL);

-- ----------------------------
-- Primary Key structure for table SECURITY_ORGANIZATION
-- ----------------------------
ALTER TABLE "SCOTT"."SECURITY_ORGANIZATION" ADD PRIMARY KEY ("ORG_ID");

<span style="font-family: 微软雅黑; font-size: 14px; line-height: 21px;">表</span>MACHINE_UNIT_INFO
/*
Navicat Oracle Data Transfer
Oracle Client Version : 10.2.0.5.0

Source Server         : 192.168.1.201ORCL
Source Server Version : 100200
Source Host           : 192.168.1.201:1521
Source Schema         : SCOTT

Target Server Type    : ORACLE
Target Server Version : 100200
File Encoding         : 65001

Date: 2015-11-19 09:40:42
*/


-- ----------------------------
-- Table structure for MACHINE_UNIT_INFO
-- ----------------------------
DROP TABLE "SCOTT"."MACHINE_UNIT_INFO";
CREATE TABLE "SCOTT"."MACHINE_UNIT_INFO" (
"ID" NUMBER NOT NULL ,
"FACTORYCODE" VARCHAR2(50 BYTE) NULL ,
"UNITCODE" VARCHAR2(50 BYTE) NULL ,
"UNITNAME" VARCHAR2(100 BYTE) NULL ,
"DESCRIPTION" VARCHAR2(255 BYTE) NULL ,
"CAPACITY" NUMBER(10,2) NULL ,
"TLGY" VARCHAR2(100 BYTE) NULL ,
"TXGY" VARCHAR2(100 BYTE) NULL ,
"CCGY" VARCHAR2(100 BYTE) NULL 
)
LOGGING
NOCOMPRESS
NOCACHE

;
COMMENT ON COLUMN "SCOTT"."MACHINE_UNIT_INFO"."FACTORYCODE" IS '风场编码';
COMMENT ON COLUMN "SCOTT"."MACHINE_UNIT_INFO"."UNITCODE" IS '机组编码';
COMMENT ON COLUMN "SCOTT"."MACHINE_UNIT_INFO"."UNITNAME" IS '机组名称';
COMMENT ON COLUMN "SCOTT"."MACHINE_UNIT_INFO"."DESCRIPTION" IS '机组描述';
COMMENT ON COLUMN "SCOTT"."MACHINE_UNIT_INFO"."CAPACITY" IS '装机容量(MW)';
COMMENT ON COLUMN "SCOTT"."MACHINE_UNIT_INFO"."TLGY" IS '脱硫工艺';
COMMENT ON COLUMN "SCOTT"."MACHINE_UNIT_INFO"."TXGY" IS '脱销工艺';
COMMENT ON COLUMN "SCOTT"."MACHINE_UNIT_INFO"."CCGY" IS '除尘工艺';

-- ----------------------------
-- Indexes structure for table MACHINE_UNIT_INFO
-- ----------------------------

-- ----------------------------
-- Triggers structure for table MACHINE_UNIT_INFO
-- ----------------------------
CREATE OR REPLACE TRIGGER "SCOTT"."CAPACITY_SUM" AFTER INSERT OR UPDATE ON "SCOTT"."MACHINE_UNIT_INFO" REFERENCING OLD AS "OLD" NEW AS "NEW" FOR EACH ROW
declare 
  PRAGMA AUTONOMOUS_TRANSACTION;
  capa NUMBER(10,2) := 0;
BEGIN
  SELECT CAPACITY into capa  FROM VIEW_SECURITY_ORGANIZATION where CODE=:new.FACTORYCODE;
  UPDATE SECURITY_ORGANIZATION  SET CAPACITY=capa  WHERE CODE=:new.FACTORYCODE;
  COMMIT;
END;
-- ----------------------------
-- Checks structure for table MACHINE_UNIT_INFO
-- ----------------------------
ALTER TABLE "SCOTT"."MACHINE_UNIT_INFO" ADD CHECK ("ID" IS NOT NULL);

-- ----------------------------
-- Primary Key structure for table MACHINE_UNIT_INFO
-- ----------------------------
ALTER TABLE "SCOTT"."MACHINE_UNIT_INFO" ADD PRIMARY KEY ("ID");
视图VIEW_SECURITY_ORGANIZATION
SELECT
	A .ORG_ID,
	A ."NAME",
	A ."DESCRIPTION",
	A .PARENT_ID,
	A .CODE,
	A .LONGITUDE,
	A .LATITUDE,
	A .ADDRESS,
	A .HOTAREA,
	A .HOTWAYS,
	A .MINRUNWAYS,
	A .DJJDATA,
	A .JCDATA,
	A .ISAPPLYHOT,
	b.CAPACITY
FROM
	SECURITY_ORGANIZATION A --装机总容量计算
LEFT JOIN (
	SELECT
		SUM (CAPACITY) CAPACITY,
		FACTORYCODE
	FROM
		MACHINE_UNIT_INFO
	GROUP BY
		FACTORYCODE
) b ON A .CODE = b.FACTORYCODE --WHERE CODE NOT LIKE '%.%.%' OR CODE IS NULL










0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:9094次
    • 积分:202
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:1篇
    • 译文:0篇
    • 评论:2条
    echarts2javatag