简体   繁体   English

处理这些MySQL数据库关系的最佳方法是什么?

[英]What is the best way to handle these MySQL database relationsships?

I'm building a small website that let users recommend their favourite books to eachother. 我正在建立一个小型网站,允许用户向彼此推荐自己喜欢的书。 So I have two tables, books and groups. 所以我有两个表,书和组。 A user can have 0 or more books in their library, and a book belongs to 1 or more groups. 用户在其图书馆中可以拥有0本书或更多图书,而一本书属于1个或多个群组。 Currently, my tables look like this: 目前,我的表格如下所示:

books table
|---------|------------|---------------|
| book_id | book_title | book_owner_id |
|---------|------------|---------------|
| 22      | something  | 12         |
|---------|------------|---------------|
| 23      | something2 | 12         |
|---------|------------|---------------|

groups table
|----------|------------|---------------|---------|
| group_id | group_name | book_owner_id | book_id |
|----------|------------|---------------|---------|
| 231      | random     | 12            | 22      |
|----------|------------|---------------|---------|
| 231      | random     | 12            | 23      |
|----------|------------|---------------|---------|

As you can see, the relationsships between users+books and books+groups are defined in the tables. 如您所见,在表中定义了用户+书籍与书籍+组之间的关系。 Should I define the relationsships in their own tables instead? 我应该在自己的表中定义关系吗? Something like this: 像这样:

books table
|---------|------------|
| book_id | book_title |
|---------|------------|
| 22      | something  |
|---------|------------|
| 23      | something2 |
|---------|------------|

books_users_relationsship table
|---------|------------|---------|
| rel_id  | user_id    | book_id |
|---------|------------|---------|
| 1       | 12         | 22   |
|---------|------------|---------|
| 2       | 12         | 23   |
|---------|------------|---------|

groups table
|----------|------------|
| group_id | group_name |
|----------|------------|
| 231      | random     | 
|----------|------------|

groups_books_relationsship table
|----------|---------|
| group_id | book_id |
|----------|---------|
| 231      | 22      |
|----------|---------|
| 231      | 23      |
|----------|---------|

Thanks for your time. 谢谢你的时间。

The second form with four tables is the correct one. 具有四个表的第二种形式是正确的。 You could delete rel_id from books_users_relationsship as primary key might be composite with both user_id and book_id , just like in groups_books_relationsship table. 您可以从books_users_relationsship删除rel_id ,因为主键可能与user_idbook_id组合在一起,就像在groups_books_relationsship表中一样。

You do not need a "relationship table" to support a relationship. 您不需要“关系表”来支持关系。 In Databases, implementing a Foreign Key in a child table defines the Relation between the parent and the child. 在数据库中,在子表中实现外键可定义父级与子级之间的关系。 You need tables only if they contain data, or to resolve a many-to-many relationship (and that has no data other than the Primary Keys of the parents). 仅当表包含数据或要解决多对多关系(并且除了父级的主键之外没有其他数据)时,才需要表。

The second problem you are facing, the reason the Relations become complex, and even optional, is due to the first two tables not being Normalised. 您面临的第二个问题是,关系变得复杂甚至是可选的原因是由于前两个表未规范化。 Many problems ensue from that. 随之而来的是许多问题。

  • if you look closely at book , you may notice that the same book (title) gets repeated 如果您仔细book ,您可能会注意到同一book (书名)被重复

  • likewise, There is no differentiation between (a) a book in terms of its existence in the world and (b) a copy of a book, that is owned by a member, and available for borrowing 同样,(a)一本书在世界上的存在与(b)一本由会员拥有并可以借用的书籍的副本之间没有区别。

    • eg. 例如。 the review is about an existing book , once, and applies to all copies of a book ; 审查一次是关于一本现有book ,并且适用于该book所有副本; not to an owned book . 不要拥有自己的book
      .
  • your "relationship" tables also have data in them, and the data is repeated. 您的“关系”表中也有数据,并且重复数据。

  • All this repeated data needs to be maintained and kept in synch. 所有这些重复的数据都需要维护并保持同步。

  • all those problems are eliminated if the data is Normalised. 如果将数据标准化,则所有这些问题都将消除。

Therefore (since you are seeking the "best way"), the sequence is to normalise the data first, after which (no surprise) the Relations are easy and not complex, and no data is repeated (in either the tables or the relations). 因此(因为您正在寻找“最佳方法”),顺序是首先对数据进行规范化,然后(毫不奇怪),关系很简单且不复杂,并且没有重复数据(在表或关系中) 。

  • when Normalising, it is best to model the real world (not the entire real world, but whatever parts of it that you are implementing in the database). 进行规范化时,最好为真实世界建模(不是整个真实世界,而是您要在数据库中实现的任何部分)。 That insulates your database from the effects of change, and functional extensions to it in future do not require the existing tables to be changed. 这样可以使数据库免受更改的影响,并且将来对数据库的功能扩展不需要更改现有表。

  • It is also important to use accurate names for tables and columns, for the same reason. 出于相同的原因,对表和列使用准确的名称也很重要。 group in non-specific and will cause a problem in future when you implement some other form of grouping. group的方式不明确,将来会在您实施其他某种形式的分组时引起问题。

  • The relations can be now defined at the correct "level", between the correct tables. 现在可以在正确的表之间以正确的“级别”定义关系。

  • The need to stick an Id column on everything that moves severely hinders your ability to understand the data and thus the Normalisation process, and robs the database of Relational power. 需要在严重移动的所有事物上附加一个Id列,这会妨碍您理解数据的能力,从而影响规范化过程,并破坏关系能力数据库。

    • Notice that the existing keys are already unique and meaningful, short and efficient, no additional surrogate keys (and their additional index) is required. 请注意,现有键已经是唯一且有意义的,简短而高效的,不需要其他代理键(及其附加索引)。

    • ReviewerId, OwnerId and BorrowerId are all MemberIds`, as Foreign Keys, showing the explicit Role in which they are used. ReviewerId, OwnerId和BorrowerId are all MemberIds(作为外键),显示了使用它们的显式角色。

  • Note that your problem space is not as simple as you think, it is used as a case study and shipped with tutorials for SQL (eg. MS SQL, Sybase). 请注意,您的问题空间并不像您想象的那么简单,它只是作为案例研究使用,并随SQL教程(例如MS SQL,Sybase)一起提供。

▶Social Library Data Model◀ ▶社会图书馆数据模型◀

Readers who are unfamiliar with the Standard for Modelling Relational Databases may find ▶IDEF1X Notational◀ useful. 不熟悉关系数据库建模标准的读者可能会发现▶IDEF1X Notational◀非常有用。

I have provided the structure required to support borrowing, to again illustrate how easy it is to implement Relations on Normalised data, and to show the correct tables upon which borrowing depends (it is not between any book and any person; only owned book can be borrowed). 我提供了支持借阅所需的结构,以再次说明实现标准化数据关系有多么容易,并显示借借所依赖的正确表(不在任何书与任何人之间;只有拥有的书才可以借来的)。

  • These issues are very important because they define the Referential Integrity of the database. 这些问题非常重要,因为它们定义了数据库的引用完整性。
  • It is also important to implement that in the database itself, which is the Standard location (rather than in app code all over the place). 在数据库本身(标准位置)(而不是在各处的应用程序代码中)中实现它也很重要。 Declarative Referential Integrity is part of IEC/ISO/ANSI Standard SQL. 声明性引用完整性是IEC / ISO / ANSI标准SQL的一部分。 And the question has a database design tag. 这个问题有一个数据库设计标签。

  • Referential Integrity cannot be defined or enforced in the non-SQLs (sometimes it can be defined but it is not enforced, which is confusing and fraudulent). 引用完整性不能在非SQL中定义或强制执行(有时可以定义但不能强制执行,这会造成混淆和欺诈)。 Nevertheless, you can design and implement whatever parts of a database your particular Non-SQL supports. 但是,您可以设计和实现特定的Non-SQL支持的数据库的任何部分。

If there is anything you do not understand, feel free to ask questions, either as a comment, or as an edit to your question. 如果您不了解任何内容,请随时提出问题,作为评论或对问题的编辑。

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

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