在oracle中更新后创建触发器

时间:2017-10-31 07:57:02

标签: oracle

我有两个表,一个是 MailAddress &具有相同架构的 BackupMailAddress 。我想创建一个触发器,在DB中完成以下活动。

1)一旦邮件状态更新为"发送"在 MailAddress 表中。应在 BackupMailAddress 表中插入相同的行。

2)成功插入 BakcupMailAddress 表中的行后,应从 MailAddress 表中删除相同的行。

1 个答案:

答案 0 :(得分:0)

Trigger应该是:

CREATE OR REPLACE TRIGGER insrt_mail_status
   BEFORE UPDATE OF MAIL_STATUS
   ON MAILADDRESS
   FOR EACH ROW
   WHEN (new.MAIL_STATUS = 'Send')
BEGIN
   INSERT INTO BackupMailAddress (MAIL_STATUS, col1, col3)
        VALUES (:new.MAIL_STATUS, :new.col1, :new.col3);
END;

参见演示:

表:

CREATE TABLE MAILADDRESS
(
   MAIL_STATUS   VARCHAR2 (10),
   col1          VARCHAR2 (10),
   col3          VARCHAR2 (10)
);

CREATE TABLE BackupMailAddress
AS
   SELECT * FROM MAILADDRESS;


SQL> SELECT * FROM MAILADDRESS;

MAIL_STATU COL1       COL3
---------- ---------- ----------
Send       A          B

SQL> SELECT * FROM BackupMailAddress;

no rows selected

SQL> UPDATE MAILADDRESS
   SET MAIL_STATUS = 'Send';    

1 row updated.

SQL> commit;

Commit complete.

SQL> SELECT * FROM BackupMailAddress;

MAIL_STATU COL1       COL3
---------- ---------- ----------
Send       A          B