简体   繁体   English

MySQL关系和约束,我应该使用它们吗?

[英]MySQL relationships & constraints, should I use them?

I've built a large database in MySQL and created all the relationships between the tables with SQLYog. 我在MySQL中建立了一个大型数据库,并使用SQLYog创建了表之间的所有关系。 That's all well and good but in my PHP site that I'm building I often have trouble when it comes to deleting rows from the database, I often run into referential integrity constraints. 很好,但是在我正在构建的PHP站点中,从数据库删除行时经常遇到麻烦,我经常遇到参照完整性约束。 So then I try deleting data or setting the field to NULL from the linking tables first. 因此,我首先尝试从链接表中删除数据或将字段设置为NULL。 I can usually get this to work with some trial and error but there seems to be no method or proper process in which I should do this. 通常,我可以通过一些反复试验来使它起作用,但是似乎没有方法或适当的过程可以执行此操作。 Should I be using the 'on delete set NULL or cascade delete settings'? 我应该使用“在删除集上设置NULL还是级联删除设置”吗? I don't know which to use and worried it might delete important data in the other tables. 我不知道该使用哪个,担心会删除其他表中的重要数据。

Do people even bother setting the relationship constraints in their databases? 人们是否还要在数据库中设置关系约束? I mean it would seem a lot easier to do a delete then write some extra lines to update the other tables that were linked to it in the PHP code. 我的意思是,执行删除操作然后编写一些额外的行来更新PHP代码中与其链接的其他表似乎容易得多。

Any help would be much appreciated. 任何帮助将非常感激。

The database should be treated as its own fiefdom in that it should not trust the data outside itself. 应该将数据库视为自己的领地,因为它不应信任自身之外的数据。 Too often I have seen systems where direct connectivity to the database was necessary and broke whatever rules were setup by the original application. 我经常看到需要直接连接数据库的系统,并且破坏了原始应用程序设置的任何规则。 Databases often morph into use by multiple applications and thus it is imperative that it implements its own data integrity. 数据库通常会被多个应用程序使用,因此必须实现自己的数据完整性。 Assume that other developers will completely bypass your application or middle-tier. 假设其他开发人员将完全绕过您的应用程序或中间层。 In addition, databases tend to stay in service far longer than the application that was originally created to service it. 另外,数据库的使用寿命比原始创建的服务寿命更长。

Thus, yes, it is crucial to incorporate foreign key constraints, proper use of nullability constraints and so on in your database design. 因此,是的,在数据​​库设计中纳入外键约束,正确使用可空性约束等至关重要。 Those referential integrity constraints you are encountering are there to protect the data against orphaned rows. 您遇到的那些参照完整性约束可以保护数据免受孤立行的侵害。 In addition, they provide documentation about how the tables relate to each other. 此外,它们提供有关表之间如何关联的文档。

If you have a child entity which logically should be removed when the parent is removed then you could consider cascading deletes. 如果您有一个子实体,在逻辑上应在删除父实体时将其删除,则可以考虑级联删除。 I tend on the side of caution by not cascading deletes unless all the code that deletes the parent is already coded to delete the child in which case, you might as well cascade deletes. 我倾向于谨慎行事,除非将删除父级的所有代码都已编码为删除子级,否则不要级联删除,在这种情况下,您最好也级联删除。 Cascade updates are obviously safer and there generally isn't a reason not to implement them unless you are worried about performance or are unable to implement them due to some restriction in the design. 级联更新显然更安全,除非您担心性能或由于设计中的某些限制而无法实现,否则通常没有理由不实现它们。

It really depends on the type of data that you are storing, do you really need the data to have referential integrity? 这真的取决于您存储的数据类型,您是否真的需要数据具有参照完整性? For most 'information system' this is a must. 对于大多数“信息系统”,这是必须的。

Referring to your second paragraph: 参考第二段:

I mean it would seem a lot easier to do a delete then write some extra lines to update the other tables that were linked to it in the PHP code. 我的意思是,执行删除操作然后编写一些额外的行来更新PHP代码中与其链接的其他表似乎容易得多。

Yes it is perhaps easier when you're only making a small scale system, but imagine that your application is successful, and you have more customers, more features needed to be implemented, more programmers needed, then you will inevitably forget to update/delete/insert relevant data to your database, that will be the moment that you'll understand that Codd wasn't joking when he said that 是的,当您仅制作一个小型系统时,想象您的应用程序成功了,您拥有更多的客户,需要实现更多的功能,需要更多的程序员,这也许会更容易,那么您将不可避免地忘记更新/删除/将相关数据插入数据库,这将是当您了解Codd并不是在开玩笑的那一刻

Integrity constraints must be specified separately from application programs and stored in the catalog. 完整性约束必须与应用程序分开指定,并存储在目录中。 It must be possible to change such constraints as and when appropriate without unnecessarily affecting existing applications. 必须在适当的时候更改这些约束,而不会不必要地影响现有应用程序。

read up codd's rule here 在这里阅读科德规则

If you need to control referential integrity (and I´d say you do in the vast majority of cases) it´s always better to let the DBMS to do the work for you. 如果您需要控制参照完整性(在绝大多数情况下,您会这样做),最好让DBMS为您完成工作。

Not only because there may be other applications (now or in the future) using the same data and you should not trust them to be as careful as you may be in your code but also for efficiency reasons. 不仅因为可能有其他应用程序(现在或将来)使用相同的数据,而且您不应该相信它们像在代码中那样谨慎,而且出于效率方面的考虑。

A good DBMS will always check the constraints (eg checking that insertions of foreign keys hold a valid a value, ie a value that exists in the referenced table) faster than your code. 一个好的DBMS总是比您的代码更快地检查约束(例如,检查外键的插入是否持有有效的值,即引用表中存在的值)。 Since this kind of checkings are the core of relational databases, DBMS are very optimized for thsi 由于这种检查是关系数据库的核心,因此DBMS已针对此进行了非常优化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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