Oracle
 sql >> Base de données >  >> RDS >> Oracle

Contrainte Oracle SQL Check entre 2 tables

Vous devez créer un déclencheur.

Essayez ceci

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;