帮我保存我的数据库

发布于 2024-10-19 06:39:22 字数 441 浏览 2 评论 0原文

我最近将数据库迁移到另一台服务器。

在旧服务器上,我大量使用了 InnoDB 的 ON DELETE CASCADE 规则。

在新服务器上,MySql 没有 InnoDB 引擎设置。

在迁移时,新服务器默认为 MyISAM - 直到现在我才注意到这一点。 (头撞墙!)

所以,情况是这样的:因为数据库已经在使用中,所以我的一些使用 ON DELETE CASCADE 规则的表现在不同步了。 (仍然在头撞墙。)

这意味着我不能只是将表更改为 InnoDB 并重新应用级联规则 - 一些键现在引用不再存在的行。

我的问题是:

有没有办法配置 INSERT 语句,以便如果插入的任何行违反级联规则,它只是默默地忽略该行并移至下一行? (起初我以为 INSERT IGNORE 可以完成此任务,但我已经尝试过,但似乎不起作用。)

预先感谢您的帮助

I recently migrated my database to a different server.

On the old server I made heavy use of InnoDB's ON DELETE CASCADE rules.

On the new server, MySql did not have the InnoDB engine setup.

At the time of migration, the new server defauted to MyISAM - and I did not notice this until now. (Knocking head against wall!)

So, here is the situation: Because the database has been in use, some of my tables that used the ON DELETE CASCADE rule are now out of sync. (Still knocking head against wall.)

This means I can't just change the tables over to InnoDB and reapply the cascade rules - some of the keys now reference rows that no longer exist.

My question is this:

Is there a way to configure an INSERT statement so that if any of the rows that are being inserted violate a cascade rule, it just silently ignores that row and moves onto the next? (At first I thought INSERT IGNORE would accomplish this, but I have tried it and it does not seem to work.)

Thanks in advance for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最单纯的乌龟 2024-10-26 06:39:22

第 1 步

进行备份

第 2 步

SET foreign_key_checks = 0;

Migrate your data

SET foreign_key_checks = 1;

第 3 步

DELETE FROM table WHERE id NOT IN (SELECT foreign_key_id FROM table2);

删除语句与您的 ON DELETE CASCADE 规则相匹配。
请记住,删除的顺序很重要,因此如果需要的话,请在此过程中计划并进行额外的备份。

再次完成备份后,编辑备份 SQL 语句,使 SETforeign_key_checks = x出现。
如果启用外键检查的备份有效,那么您就可以恢复正常工作了。

我确信您可以找出更有效的删除查询,无需 IN,只需我的 2 美分

STEP 1

Make a backup

STEP 2

SET foreign_key_checks = 0;

Migrate your data

SET foreign_key_checks = 1;

STEP 3

DELETE FROM table WHERE id NOT IN (SELECT foreign_key_id FROM table2);

Where the delete statements match your ON DELETE CASCADE rules.
Remember the order in which you do your deletes is important, so plan and make extra backups along the way if need be.

Once you are done backup one more time and edit the backup SQL statement so that the lines SET foreign_key_checks = x are not present.
If your backup with foreign key checks enabled works, you're back in business.

I'm sure you can figure out more efficient delete queries without IN, just my 2 cents

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文