[英]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.