Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Varios
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Grupo de Teaming del ClubDelphi

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 15-01-2015
JAI_ME JAI_ME is offline
Miembro
 
Registrado: ene 2006
Posts: 188
Poder: 19
JAI_ME Va por buen camino
Foreign key referenciando 2 tablas

Buenas tardes, tengo las siguientes tablas

Código:
create table table1 (
   idTable1 integer not null primary key,
   nombre varchar(20)
);

create table table2 (
   idTable2 integer not null primary key, 
   nombre varchar(20)
);

create table table3 (
   idTable3 integer not null primary key,
   idReferencia integer,
   nombre varchar(20),
   tipo integer
);

El campo idReferencia de la table3 debe tener los Id de las tablas 1 o 2 y el campo tipo indica si debe buscar en la tabla1 o tabla2 al momento de hacer una consulta.

En otras palabras quiero saber si idRefencia puedo crearle dos foreing key a las Tablas 1 y 2 y busque segun el valor del campo Tipo, o necesariamente tengo que crear 2 campos con sus repectivas referencias y dejar un de los dos null cuando se cree un registro en la tabla3.
__________________
JaiMelendez

Última edición por roman fecha: 15-01-2015 a las 19:37:14. Razón: Cambiar etiqueta quote por code para mayor legibilidad
Responder Con Cita
  #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
Poder: 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
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Problema Foreign key franfl Firebird e Interbase 2 27-05-2008 19:38:03
violation of Foreign Key JulioGO Firebird e Interbase 2 27-06-2006 00:33:24
foreign key clanmilano Conexión con bases de datos 4 03-11-2005 03:21:19
Foreign key fjardelphi Firebird e Interbase 2 20-12-2004 16:05:39
uso de FOREIGN KEY jzginez Firebird e Interbase 2 22-04-2004 23:20:25


La franja horaria es GMT +2. Ahora son las 21:06:46.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi