Ver Mensaje Individual
  #2  
Antiguo 16-01-2015
Avatar de duilioisola
[duilioisola] duilioisola is offline
Miembro Premium
 
Registrado: ago 2007
Ubicación: Barcelona, España
Posts: 1.734
Reputación: 20
duilioisola Es un diamante en brutoduilioisola Es un diamante en brutoduilioisola Es un diamante en bruto
No mencionas la base de datos que deseas utilizar. Supondré por tanto que utilizas Firebird o MySQL. En ese caso no puedes controlar la restricción que mencionas mediante FKs. Deberías hacerlo mediant triggers.
Código SQL [-]
CREATE TRIGGER TABLE3_BI0
as
begin
   if (tipo = 1) then
   begin
      if (not exists(select id_Table1 from table1 where id_Table1 = new.idReferencia)) then
         exception ERR_NO_EXISTE_REF;
   end

   if (tipo = 2) then
   begin
      if (not exists(select id_Table2 from table2 where id_Table2 = new.idReferencia)) then
         exception ERR_NO_EXISTE_REF;
   end
end

CREATE TRIGGER TABLE3_BU0
as
begin
   if ((new.idReferencia <> old.idReferencia) or (new.tipo <> old.tipo)) then
   begin 
      if (tipo = 1) then
      begin
         if (not exists(select id_Table1 from table1 where id_Table1 = new.idReferencia)) then
            exception ERR_NO_EXISTE_REF;
      end

      if (tipo = 2) then
      begin
         if (not exists(select id_Table2 from table2 where id_Table2 = new.idReferencia)) then
            exception ERR_NO_EXISTE_REF;
      end
   end
end
Responder Con Cita