mysql 互相备份,从mysqldump备份执行相互依赖的视图

当使用mysqldump创建数据库备份时,视图按字母顺序排列可能会导致依赖关系问题,如先创建的视图依赖于后创建的视图。文章提到可以通过设置FOREIGN_KEY_CHECKS=0来解决这个问题,但这种方法可能导致其他问题,如某些占位符表无法创建。作者建议进行视图的拓扑排序以更优雅地解决依赖问题,虽然这可能需要额外的工作。此外,文章指出在恢复数据库时,每个视图和表都存储在单独的.sql文件中,便于独立版本控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mysqldump creates a dump with the tables (and views) listed alphabetically. When there are foreign key relationships between the tables this is not very convenient, however, the problem is easily resolved by running:

SET FOREIGN_KEY_CHECKS=0;

I have a situation whereby the view, vwapple, is dependent on the view, vworange, say. With the mysqldump, vwapple is listed and executed before vworange, which is problematic as we'll get a "view vworange does not exist" error message.

How is this situation solved? Do views have a similar solutions as tables have? Or is it better to make each view independent just to get round this problem?

NOTE

I'm restoring the database using a python script. Each view and table is in its separate .sql file as I want to source control each database object independently.

解决方案

Recent versions of mysqldump, when dumping whole databases, solve things this way: for every view in the database, they first create an empty table with the same structure, then they create the views, dropping each placeholder table before creating a view. It seems that replacing a table with a view won't harm views that depend on it.

I know about this because this approach has another flaw, which bit me: rows in views may contain more data than rows in tables, so some of the placeholder tables cannot be created, causing some dumps to be unrestorable without manual intervention. For this reason, a topological sorting of views would be preferable, although it might require some work.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值