【问题标题】:Basic SQL Modelling question基本 SQL 建模问题
【发布时间】:2011-08-03 03:46:07
【问题描述】:

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

说白了就是一个通知系统,用户可以在他们关注的网页上收到新的 cmets 通知或回复他们关注的 cmets。

我有以下(减少的)布局

表:cmets

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

应用与 cmets 表相同的建模技术:

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 语句选择的表?

【问题讨论】:

    标签: mysql sql database-design relational-database modeling


    【解决方案1】:

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

    似乎制作两张桌子会起作用,例如follow_entries 和 follow_cmets。

    祝你好运。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-01
      相关资源
      最近更新 更多