简体   繁体   中英

Handling database integrity

I'm introducing database integrity using innodb constraints in the next version of my application. Everything goes well, but some of my tables have records with deleted references (dead records) and because of them I can't add constraints to the table.

I am trying:

ALTER TABLE `article` ADD FOREIGN KEY (`author_id`) REFERENCES `authors` (`id`) ON DELETE CASCADE;

And I get:

#1452 - Cannot add or update a child row: a foreign key constraint fails (`books`.<result 2 when explaining filename '#sql-442_dc'>, CONSTRAINT `#sql-442_dc_ibfk_1` FOREIGN KEY (`author_id`) REFERENCES `authors` (`id`) ON DELETE CASCADE)

Running this query, I found out that over 500 records have no references (authors were deleted, but their articles remained):

SELECT `articles`.`id`
FROM `articles` LEFT JOIN `authors` ON `articles`.`author_id` = `authors`.`id`
WHERE ISNULL(`authors`.`id`);

So, before I can add a constraint, I must deal with those. How do I delete all the records that I get using the query above?

I've tried:

DELETE FROM `articles` WHERE `id` IN (
  SELECT `articles`.`id`
  FROM `articles` LEFT JOIN `authors` ON `articles`.`author_id` = `authors`.`id`
  WHERE ISNULL(`authors`.`id`);
)

But mysql responds:

You can't specify target table 'articles' for update in FROM clause

Any help on this will be greatly appreciated.

I'm not too familiar with mySql's many quirks, but this should also work, and perhaps mySql won't choke on it:

delete from articles
 where not exists (
           select id from authors
            where authors.id = articles.author_id
       )

Um, of course we always have a backup of the table before we attempt set-based deletes :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM