Oracle
 sql >> Database >  >> RDS >> Oracle

Vincolo Oracle SQL Check tra 2 tabelle

Dovresti creare un trigger.

Prova questo

CREATE OR REPLACE TRIGGER   my_trg
              BEFORE INSERT OR UPDATE ON Relationships
                 FOR EACH ROW
  declare
     function i_Age(id int) return int is
     li_res int;
     begin
       select p.Age
        into li_res 
        from Persons p 
        where p.ID= id 
         and rownum=1;
        return li_res;
    exception when no_data_found then
    return NULL; --or Throw Exception depend on your logic if some datas not found
     end;

     BEGIN
      IF INSERTING OR UPDATING THEN
          IF :NEW.Relation == 'child' and i_Age(:NEW.Person_ID) < i_Age(:NEW.Relative_ID)  then 
           NULL; --Throw Exception or your logic
          END IF;
      END IF;
   END;