SQL> create table table1
2 (id number,
3 data varchar2(20),
4 insertion_time date default sysdate
5 );
Table created.
SQL> create table table2
2 (id number,
3 data varchar2(20)
4 );
Table created.
触发器:
SQL> create or replace trigger trg_ai_tab1
2 after insert on table1
3 for each row
4 begin
5 insert into table2 (id, data)
6 values (:new.id, :new.data);
7 end;
8 /
Trigger created.
测试:
SQL> insert into table1 (id, data)
2 select 1, 'Little' from dual union all
3 select 2, 'Foot' from dual;
2 rows created.
SQL> select * from table1;
ID DATA INSERTION_TIME
---------- -------------------- -------------------
1 Little 24.09.2023 08:32:42
2 Foot 24.09.2023 08:32:42
SQL> select * from table2;
ID DATA
---------- --------------------
1 Little
2 Foot
SQL>
SQL> connect mike/lion@pdb1
Connected.
SQL> create table table2
2 (id number,
3 data varchar2(20)
4 );
Table created.
回到scott:创建数据库链接、table1和触发器:
SQL> create database link dbl_mike_t2
2 connect to mike
3 identified by lion
4 using 'my_laptop:1521/xepdb1';
Database link created.
SQL> create table table1
2 (id number,
3 data varchar2(20),
4 insertion_time date
5 );
Table created.
SQL> create or replace trigger trg_ai_tab1
2 before insert on table1
3 for each row
4 begin
5 :new.insertion_time := sysdate;
6 insert into table2@dbl_mike_t2 (id, data)
7 values (:new.id, :new.data);
8 end;
9 /
Trigger created.
SQL>
让我们试试看:
SQL> insert into table1 (id, data)
2 select 1, 'Little' from dual;
1 row created.
SQL> select * From table1;
ID DATA INSERTION_TIME
---------- -------------------- -------------------
1 Little 24.09.2023 08:55:02
SQL> select * from table2@dbl_mike_t2;
ID DATA
---------- --------------------
1 Little
SQL>
1条答案
按热度按时间elcex8rz1#
使用
insertion_time
的默认值和复制数据的触发器。示例表:
触发器:
测试:
**[编辑]**基于您的评论。
好吧,当您在TABLE 1中插入row时触发器就会触发,那么
INSERTION_TIME
也是一样的。可能有小数秒的差异,但是-这有关系吗?如果是,那么删除默认值并将其放入触发器(但这次更改其类型)。用户
mike
现在将包含通过数据库链接访问的table2
:回到
scott
:创建数据库链接、table1
和触发器:让我们试试看: