oracle triggers 实现两个结构相同的表的数据级联更新操作
首先创建两个结构相同的表
-- Create table
create table TABLE_TEMP
(
userid NUMBER not null,
username NVARCHAR2(50),
userno NVARCHAR2(60),
cardid NVARCHAR2(18),
createdate DATE,
redate DATE,
month VARCHAR2(6),
amount NUMBER
)
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 16
next 8
minextents 1
maxextents unlimited
);
第二个表结构
-- Create table
create table TEST_TABLE_TEMP
(
userid NUMBER not null,
username NVARCHAR2(50),
userno NVARCHAR2(60),
cardid NVARCHAR2(18),
createdate DATE,
redate DATE,
month VARCHAR2(6),
amount NUMBER
)
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 16
next 8
minextents 1
maxextents unlimited
);
其中这两个表的ID都是不能自动增长的属性 没有建立sequences 序列以及自动增长的触发器
建立触发器:
create or replace trigger triggers_table_tempToTemp
before insert or update or delete
on table_temp for each row
declare
integrity_error exception;
errno integer;
errmsg char(200);
dummy integer;
found boolean; begin if inserting then
insert into test_table_temp(userid,UserName,userno,cardid,createdate,redate,month,amount)
values(:NEW.userid,:NEW.UserName,:NEW.userno,:new.cardid,:NEW.createdate,:NEW.redate,:NEW.month,:NEW.amount);
elsif updating then
update test_table_temp set userid=:NEW.userid,
UserName=:NEW.UserName,userno=:NEW.userno,
cardid=:NEW.cardid ,createdate=:NEW.createdate,
redate=:NEW.redate,
month=:NEW.month,
amount=:NEW.amount
where USERID=:OLD.USERID;
elsif deleting then
delete from test_table_temp where userid=:OLD.userid;
end if;
exception
when integrity_error then
raise_application_error(errno, errmsg);
end;
--执行报错,错误信息:ORA-04084 无法更改此触发器类型的NEW值
--把触发器的after改成before 触发
--实现数据增删改 同时实现两个数据表同步信息
测试数据
insert into TABLE_temp(userid,USERNAME,USERNO,CARDID,CREATEDATE,REDATE,MONTH,AMOUNT)
values('1','小晴','20140408','201404087976',to_date('2014-04-08','yyyy-mm-dd'),to_date('2014-04-08','yyyy-mm-dd'),'201404','100000') update TABLE_temp set USERNAME='小清新' where userid=1
delete from TABLE_temp where userid=1
数据库中把一个表中的数据复制到另一个表中 如果不用其他方法直接用SQL语句实现:
1、如果是整个表复制如下:
insert into table1 select * from table2
2、如果是有选择性的复制数据如下:
insert into table1(column1,column2,column3...) select column1,column2,colunm3... from table2
3、一个数据库中的表中的数据复制到另一个数据库中的一个表,使用方法如下:
insert into 数据库A.dbo.table1(col1,col2,col3...) select col1,col2,col3... from 数据库B.dbo.table2
oracle triggers 实现两个结构相同的表的数据级联更新操作的更多相关文章
- oracle 批量更新之将一个表的数据批量更新至另一个表
oracle 批量更新之将一个表的数据批量更新至另一个表 CreationTime--2018年7月3日17点38分 Author:Marydon Oracle 将一个表的指定字段的值更新至另一个 ...
- SQLSEVER 不同服务器下两个结构相似的表实现数据同步(触发器)
1.建立链接服务器 在ServerA 中创建指向ServerB的链接服务器,并做好账号映射.addlinkedserver存储过程创建一个链接服务器,参数详情参见官方文档. 第1个参数LNK_Serv ...
- SQL 两张结构一样的表合并查询 .
select * from table1 union all select * from table2 union all 是所有的都显示出来: select * from table1 union ...
- SQL 将两个结构相同的表合并到成一个表
select * into 新表名 from (select * from T1 union all select * from T2) 这个语句可以实现将合并的数据追加到一个新表中. 不合并重复数据 ...
- Oracle数据库中将一个数据库中一张表的数据导入到另外一张表
INSERT INTO DBTHNEW.L_MEMBER_ROLE_REL SELECT *FROM DBTH.L_MEMBER_ROLE_REL
- JDBC获得DB2表结构并且将表中数据脱敏后转移的程序示例
完整项目地址:https://github.com/zifeiy/totomi 代码示例: import java.io.File; import java.io.FileInputStream; i ...
- mybatis+Oracle 批量插入数据,有数据做更新操作
<!-- 批量添加 --> <insert id="batchAdd" parameterType="java.util.List"& ...
- Oracle 给表添加主键和使ID自增、触发器、创建结构一样的表
1.关于主键:在建表时指定primary key字句即可:create table test( id number(6) primary key, name varchar2(30));如果是对于已经 ...
- oracle、mysql、sybase和sqlserver复制表结构和数据
Sql Server(sybase): 1.复制表结构: 新建表student2,并且结构同表syn_xj_student一致.Sql语句如下: 2.复制表数据,并排除俩表中相同的数据: insert ...
随机推荐
- $ionicModal
Ionic中[弹出式窗口]有两种(如下图所示),$ionicModal和$ionicPopup; $ionicModal是完整的页面: $ionicPopup是(Dialog)对话框样式的,直接用Ja ...
- nexus 批量导入本地库
1.复制D:\maven\repository(本地仓库)到D:\sonatype-work\nexus\storage\central(nexus库路径) 2.Central --> upda ...
- bzoj1296【SCOI2009】粉刷匠
1296: [SCOI2009]粉刷匠 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 1479 Solved: 837 [id=1296" ...
- js setAttribute removeAttribute
<input type="button" value="生效" style="font-size:111px"/> <sc ...
- Mac新建文件夹、txt文件、其他格式文件
Mac新建txt,正好有人问我,我就把我自己的方法记录一下: 先cd到你指定的文件路径下: 新建文件夹: mkdir test 新建txt touch test.txt 新建无后缀格式文件 touch ...
- java中普通代码块,构造代码块,静态代码块的区别及代码示例
本文转自:http://www.cnblogs.com/sophine/p/3531282.html 执行顺序:(优先级从高到低)静态代码块>main方法>构造代码块>构造方法. 其 ...
- HDUOJ---1195Open the Lock
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- Lucene使用与优化(转)
原文链接:http://blog.csdn.net/hongfu_/article/details/1933346 本文所使用的Lucene版本较低,年代久远,许多API可能已经变了. 1 lucen ...
- C# 发布REST接口地址API服务
原文地址:https://blog.csdn.net/chinacsharper/article/details/21256569 今天碰巧,用到了淘宝的在线IP地址查询的Rest API,它提供接口 ...
- .NET压缩图片保存
需求: 需要将用户后买的图片批量下载打包压缩,并且分不同的文件夹(因:购买了多个用户的图片情况) 文章中用到了一个第三方的类库,Nuget下载 SharpZipLib 目前用的 1.1的版本 效果: ...