繁体   English   中英

基本SQL建模问题

[英]Basic SQL Modelling question

我面临着需要引用不同类型数据的情况。

简而言之,这是一个通知系统,可以在其中通知用户有关他们关注的网页上的新评论或对他们关注的评论进行回复的信息。

我有以下(精简版)布局

表:注释

comment_id (int) PRIMARY # primary key
entry_id (int) NULL # id of the webpage (entry), null if reply to  existing comment
comment_parentid NULL # parent id of comment if reply, null if root comment

表:关注者

user_id (int) # the user following
entry_id (int) NULL # webpage the user follows, null if it this entry is only for following replys to a specific comment
comment_parentid NULL # comment the user follows replys to, null if the user if this following entry represents following of the complete post on the webpage (notification for all new comments)

如您所见,只有正好填充一个相关的字段(entry_id,comment_parentid),这才有意义。

在查找下表的主键时,我也遇到问题。 因为主键不能包含可为空的字段。

因此,我以为我只创建了一个名为“ parent_id”或“ followed_item_id”的字段,并设置了一个附加的枚举标志,它是哪种父ID。

因此,下表如下所示:

user_id (int) PRIMARY 
followed_item_id (int) PRIMARY
followed_item_type ENUM('entry','comment') PRIMARY

应用相同的建模技术,注释表将如下所示:

comment_id (int) PRIMARY # primary key
comnent_parentid_type ENUM('entry','comment') # parent id type. entry or comment.
comment_parentid (int) # parent id representing comment or entry

但是我记得我曾经遇到过一个问题,我想做类似的事情,但是有很多反对的理由。 我只是不记得了,也找不到帖子。

那么构造这种数据的好方法是什么?

我都用UNION语句选择的Maby两个表?

反对“ combo”表的论点是,您不能具有任何参照完整性,而您必须自己管理任何级联删除。

好像制作两个表可以工作,例如followed_entries和followed_comments。

祝好运。

暂无
暂无

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

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