在Oracle中的垂直分段表中使用REFERENCES

时间:2016-06-14 10:07:36

标签: sql database oracle oracle11g

首先在两个不同的数据库中创建以下表格

Fragment1:
Employee(EID number(4)PRIMARY KEY, Name varchar2(20),Address varchar2(50));

Fragment2:
Employee(EID number(4),Dep_Id number(4),Mgr_Id REFERENCES Employee(EID),Salary number(6));

然后 创建一个触发器以将数据插入到适当的表中。

我无法在Mgr_Id中使用[片段1]的REFERENCE Employee(EID)

请帮助

1 个答案:

答案 0 :(得分:0)

您无法通过dblink实现FK约束。你可以做的是写一个模仿FK约束功能的触发器。像这样:

首先,您需要创建一个dblink(例如,remote)到server2

CREATE OR REPLACE TRIGGER Example
  AFTER INSERT ON fagment1
    FOR EACH ROW
 DECLARE
    cuenta number;
 BEGIN

  select count(*) 
    into cuenta 
    from fagment2@Remote f2 
   where f2.EID = :new.EID 
     and rownum=1;

  IF cuenta = 0 then
    raise_application_error(-20001,'EID: '||:new.EID||'  not found');
  END IF;

EXCEPTION
  WHEN OTHERS THEN --dblink down?
            raise_application_error(-20001,'EID: '||:new.EID||'  not found');

END;
相关问题