博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Oracle 触发器 插入,更新,删除,数据同步,两表同步
阅读量:7116 次
发布时间:2019-06-28

本文共 1307 字,大约阅读时间需要 4 分钟。

建表:

create table User_Info (

   ID                   INTEGER                         not null,
   UserName            VARCHAR(30)                     not null,
   PassWord            VARCHAR(20)                     not null,
   CreateDate          Date                            not null,
   Status              INTEGER                         not null,
   constraint PK_User_Info primary key (ID)
);

create table User_Info_temp (

   ID                   INTEGER                         not null,
   UserName            VARCHAR(30)                     not null,
   PassWord            VARCHAR(20)                     not null,
   CreateDate          Date                            not null,
   Status              INTEGER                         not null,
   constraint PK_User_Info_temp primary key (ID)
);

触发器写法:

create or replace trigger UserToTemp after insert or update or delete

on user_info for each row
declare
    integrity_error exception;
    errno            integer;
    errmsg           char(200);
    dummy            integer;
    found            boolean;
   
begin
if inserting then
    insert into User_info_temp(ID,UserName,PassWord,CreateDate,Status) values(:NEW.ID,:NEW.UserName,:NEW.PassWord,:new.CreateDate,:NEW.Status);
elsif updating then
    update User_info_temp set ID=:NEW.ID,UserName=:NEW.UserName,PassWord=:NEW.PassWord,Status=:NEW.Status where id=:OLD.id;
elsif deleting then
    delete from User_info_temp where id=:OLD.id;
end if;
exception
    when integrity_error then
       raise_application_error(errno, errmsg);
end;

测试数据:
insert into user_info(ID,UserName,PassWord,CreateDate,Status)values(1,'xier','222',to_date('2008-10-11','yyyy-mm-dd'),1)

update user_info u set u.status=3,u.username='xier' where u.id=1

delete from user_info u where u.id=1

转载地址:http://lofel.baihongyu.com/

你可能感兴趣的文章
酷狗音乐快速转换MP3格式的方法
查看>>
Web 开发学习笔记(4) --- 重定向与HSTS
查看>>
通过手机物理返回键实现弹出层的隐藏
查看>>
Spring Cloud Alibaba基础教程:Nacos配置的多文件加载与共享配置
查看>>
解决mac下webstorm编辑器识别less的问题
查看>>
原生JS 实现复杂对象深拷贝(对象值包含函数)
查看>>
优化体系结构 - 算法外置优化计算结构
查看>>
JS中的call、apply、bind方法详解
查看>>
启动时间知多少?8款音视频类应用测评报告分析
查看>>
码农张的Bug人生 - 见面之礼
查看>>
JS异步编程之Promise
查看>>
【算法专栏】-- 谈谈时间复杂度
查看>>
元数据治理框架Atlas研究——JanusGraph图数据库对象关系映射
查看>>
力扣(LeetCode)155
查看>>
sas 做 titanic 未完待续
查看>>
区块链是一个公共数据库,要放在一个块内
查看>>
jqGrid的rowNum属性默认值、-1情况的介绍
查看>>
一步步学会用docker部署应用(nodejs版)
查看>>
分享10个免费H5模版(主题)资源网站
查看>>
RabbitMQ预研
查看>>