I'm having a problem, I explain:
I have a table called Tipo_Base that contains Id, nombre_tipo_base
I have a table called Tipo_Base_Lista that contains Id, id_tipo_base, and lista_id
I have a table called Modelo_unidad that contains Id, nombre_modelo and id_tipo_base
I have a table called Modelo_Lista that contains Id, id_modelo, id_lista
Each id_lista of the table modelo_lista MUST be present in the table tipo_base_lista, then, when I delete a id_lista from the table tipo_base_lista, it must also be deleted from the table modelo_lista.
Try the following:
DELETE Tbl_modelo_lista
FROM
Tbl_modelo_lista
INNER JOIN Tbl_modelo_unidad as MU ON MU.id_modelo = Tbl_modelo_lista.id_modelo
INNER JOIN Tbl_tipo_base_lista as TBL ON TBL.id_tipo_base = MU.id_tipo_base
WHERE
TBL.id_lista <> Tbl_modelo_lista.id_lista
I think the logic you want is more like this:
DELETE ml
FROM Tbl_modelo_lista ml INNER JOIN
Tbl_modelo_unidad mu
ON mu.id_modelo = ml.id_modelo LEFT JOIN
Tbl_tipo_base_lista tbl
ON tbl.id_tipo_base = mu.id_tipo_base AND
tbl.id_lista = ml.id_lista
WHERE tbl.id_lista IS NULL;
Normally, the way to implement this logic is with a cascading delete constraint. In your case, I'm not sure this would work. Cascading triggers are useful when you need to propagate changes from the reference table outwards. They don't keep track of incoming references and delete a record when there are no references.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments