私はさまざまなタイプのデータを参照する必要がある状況に直面しています。基本的なSQLモデリングの質問
通知システムは、ユーザーが自分のフォローしているウェブページ上の新しいコメントや、それに続くコメントに返信することができる通知システムです。
私は、次の(減少)レイアウト
表を持っている:コメント
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)
あなたは内のフィールドの正確ONE場合にのみ意味があります見ての通り質問(entry_id、comment_parentid)が入力されます。
また、次の表の主キーを見つける際に問題があります。主キーにはNULL可能なフィールドを入れることができないためです。
したがって、私は "parent_id"または "following_item_id"と呼ばれるフィールドを1つだけ作成し、追加の列挙型フラグをどのような親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文で2つのテーブルを選択していますか?