一对多数据库关系
伙计们,如果有人能帮助我解决这个问题,我将非常感激。我有一个架构,其中包含与此问题书籍、组、阅读列表、评论相关的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
BookInGroupCommentNo
为整数
,序列号 ( 1,2,3..) 对于每个GroupID, BookID
组合。 轻松获得它创建新评论时可以使用从
ReadingList
和Comment< 中删除那些自增
ID
和ReadingListID
/代码> 表。The
BookInGroupCommentNo
isinteger
, serial number (1,2,3..) for eachGroupID, BookID
combination. It can be easily obtained when creating a new comment usingGet rid of those auto-increment
IDs
andReadingListID
fromReadingList
andComment
tables.您可以做的是将 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.
这是因为 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.
我建议:
I suggest: