一对多数据库关系

发布于 2024-10-30 22:54:48 字数 779 浏览 0 评论 0原文

伙计们,如果有人能帮助我解决这个问题,我将非常感激。我有一个架构,其中包含与此问题书籍、组、阅读列表、评论相关的 4 个表。系统的用户可以加入群组并将书籍添加到该群组的阅读列表中。组的图书列表由表 ReadingList 表示:

ReadingList
-------------
Id (auto_increment)
ReadingListID
BookID (pk)
GroupID (pk)

BookID 和 GroupID 设置为复合主键,以确保任何一本书不能在组阅读列表中出现两次。这两个字段都为相关表 Books/Groups 定义了一个 fk。现在,当尝试在评论表和阅读列表之间建立关系时,我的问题出现了。理论上,阅读列表中的每个唯一条目都可以有许多评论 (1..*),因此基本上,组阅读列表中的一本书可以有许多与 in 相关的评论。评论表如下所示:

Comments
-----------
Id (pk, auto_increment)
ReadingListID
UserName
Comment
TimeStamp

我的逻辑是,一个 fk可以从注释(ReadingListID)设置到阅读列表(ReadingListID),但我显然有缺陷,因为我收到“引用表中没有主键或候选键”错误。

我尝试过各种各样的事情,比如制作 ReadingListID & Comments 表中的 Id 是复合键,并使 ReadingListID 成为 ReadingList 表中的 pk 等,但我就是无法理解这一点。如果我说得不够清楚,请告诉我。

非常感谢!

Guys I would really appreciate it if someone could help me with this. I have a schema with 4 tables relevant to this question Books, Groups, ReadingList, Comments. A user of the system can join a group and add books to that group's reading list. A group's book list is represented by the table ReadingList:

ReadingList
-------------
Id (auto_increment)
ReadingListID
BookID (pk)
GroupID (pk)

BookID and GroupID are set up as a composite primary key to ensure no book can appear on a groups reading list twice. Both these fields have a fk defined to the relevant table Books/Groups. Now my problem comes when trying to set up a relationship between the Comments table and the ReadingList. The theory is that each unique entry in the ReadingList can have many comments (1..*) so basically one book on a groups reading list can have many comments associated with in. The Comments table looks like this:

Comments
-----------
Id (pk, auto_increment)
ReadingListID
UserName
Comment
TimeStamp

My logic is that a fk be set up from Comments (ReadingListID) to ReadingList (ReadingListID) but I am clearly flawed as I get a 'no primary or candidate keys in referenced table' error.

I have tried a variety of things like making ReadingListID & Id in the Comments table a composite key and making ReadingListID a pk in the ReadingList table etc, but I just can't get my head round this. Please let me know if I haven't been clear enough.

Many thanks!

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

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

发布评论

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

评论(4

柒夜笙歌凉 2024-11-06 22:54:48

在此处输入图像描述

BookInGroupCommentNo整数,序列号 ( 1,2,3..) 对于每个 GroupID, BookID 组合。 轻松获得它

select
    coalesce(max(BookInGroupCommentNo), 0) + 1  
from ReadingList
where GroupID = some_group_id
  and BookID  = some_book_id ;

创建新评论时可以使用从 ReadingListComment< 中删除那些自增 IDReadingListID /代码> 表。

enter image description here

The BookInGroupCommentNo is integer, serial number (1,2,3..) for each GroupID, BookID combination. It can be easily obtained when creating a new comment using

select
    coalesce(max(BookInGroupCommentNo), 0) + 1  
from ReadingList
where GroupID = some_group_id
  and BookID  = some_book_id ;

Get rid of those auto-increment IDs and ReadingListID from ReadingList and Comment tables.

提笔书几行 2024-11-06 22:54:48

您可以做的是将 ReadingList 的主键从组合 (BookID+GroupID) 更改为 ReadingListID 列,然后您的 FK 将起作用。然后,您可以跨 BookID+GroupID 列创建唯一约束或索引来强制数据完整性。

不过,我很困惑为什么你的 ReadingList (Id) 上有一个自动增量,它与你的 ReadingListID 列是分开的。也许你应该放弃其中之一。在我看来,您的架构似乎是这样设置的,也许 ReadingList.Id 是 PK,而 Comments.ReadingListID 是该列的 FK。

What you can do is use change the primary key of ReadingList from the composite (BookID+GroupID) to the ReadingListID column, then your FK will work. You can then create a unique constraint or index across the BookID+GroupID columns to enforce that data integrity.

I'm confused, though, why you have an auto-increment on ReadingList (Id) which is separate from your ReadingListID column. Perhaps you should drop one or the other. In my mind, the way your schema seems to be set up, perhaps ReadingList.Id is the PK, and Comments.ReadingListID is a FK to that column.

原野 2024-11-06 22:54:48

这是因为 ReadingList 的主键实际上是一个组合键(BookID、GroupID)。您应该更新 Comments 表并拥有 BookID 和 GroupID 而不是 ReadingListID。

为什么有 ReadingListID?您不需要它,因为您已经定义了复合主键。

That's because ReadingList's Primary Key is actually a composite key (BookID, GroupID). You should update your Comments table and have BookID and GroupID instead of ReadingListID.

Why do you have ReadingListID? You don't need it since you already have your composite primary key defined.

难以启齿的温柔 2024-11-06 22:54:48

我建议:

ReadingList
-------------
ReadingListID (pk, auto_increment)
BookID (fk)
GroupID (fk)
+ Unique index on (BookID, GroupID)

Comments
-----------
CommentID (pk, auto_increment)
ReadingListID
UserName
Comment (maybe "Content", "Value" or "Body" could help avoid typing comments.comment)
TimeStamp

I suggest:

ReadingList
-------------
ReadingListID (pk, auto_increment)
BookID (fk)
GroupID (fk)
+ Unique index on (BookID, GroupID)

Comments
-----------
CommentID (pk, auto_increment)
ReadingListID
UserName
Comment (maybe "Content", "Value" or "Body" could help avoid typing comments.comment)
TimeStamp
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文