Mysql, limpia los cabos sueltos

Is there some nice automated way/program for cleaning up my database? I have a couple of tables with relations that in some cases just point at records that doesn't exist.

preguntado el 02 de febrero de 12 a las 10:02

use a proper relational schema and cascade on foreign key attributes -

1 Respuestas

Sounds like you are missing some Restricciones de clave externa.
Dependiendo de los ON DELETE option, orphaned records will be deleted together with the referenced records, referencing columns set to NULL, or deleting will be rejected.

You will have to delete those existing entries manually using a query like this, before creating your constraints:

DELETE FROM table_a
WHERE ref_b IS NOT NULL
  AND NOT EXISTS ( SELECT 1 FROM table_b WHERE table_b.id = table_a.ref_b )

Respondido 02 Feb 12, 15:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.