两个数据库之间的外键

时间:2016-04-01 05:00:29

标签: sql oracle plsql triggers distributed-database

我通过前面的回答创建了一个伪外键来引用Netbeans 8.1中两个数据库之间的表。这是我提出的代码,

DELIMITER //

CREATE OR REPLACE TRIGGER conf_track_FK
AFTER INSERT OR UPDATE on S26994437.track@FIT5148B
FOR EACH ROW
BEGIN
    IF EXISTS(select * from inserted I where not exists (select * from
    S1234567.conference@FIT5148A A where I.conf_id=A.conf_id))
    RAISE_APPLICATION_ERROR(-20001,'Violation of pseudo-foreign key.');
    ROLLBACK;
    END IF;
END;

/

但是,我遇到以下错误:

PLS-00103: Encountered the symbol ";" when expecting one of the following:
   ) with and or group having intersect minus start union where
   connect

PLS-00103: Encountered the symbol "ROLLBACK" when expecting one of the following:
   := . ( % ;
The symbol ":=" was substituted for "ROLLBACK" to continue.

PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:

   end not pragma final instantiable order overriding static
   member constructor map

2 个答案:

答案 0 :(得分:0)

创建触发器dbo.MyTableTrigger ON dbo.MyTable,插入后,更新 如 开始

如果不存在(从OtherDB.dbo.TableName中选择PK,其中PK in(从插入中选择FK)BEGIN        - 在此处理参考错误    END

END

答案 1 :(得分:0)

尝试下面举例说明的代码段。希望能帮助到你。并且任何像COMMIT / ROLLBACK这样的TRANSACTIONS都不能放在INSIDE Trigger中,除非它是一个自治事务,但由于父事务对于触发器事务是隐藏的,所以这也不是一个好主意。因此,即使父事务处理失败,也会完成Automnomous事务。

CREATE OR REPLACE TRIGGER conf_track_FK AFTER
  INSERT OR
  UPDATE ON S26994437.track@FIT5148B --Remove DB link as it cant be used in Trigger
  FOR EACH ROW 
  DECLARE 
  lv_cnt PLS_INTEGER;
  BEGIN
    SELECT COUNT(1)
    INTO lv_cnt
    FROM inserted I
    WHERE NOT EXISTS
      (SELECT 1 FROM S1234567.conference@FIT5148A A WHERE I.conf_id=A.conf_id
      );
    IF lv_cnt > 0 THEN
      RAISE_APPLICATION_ERROR(-20001,'Violation of pseudo-foreign key.');
    END IF;
  END;
  /