我想创建一个'comments'表,所以保存text,created,userId等......
但是用户可以评论很多事情。
所以我不能把一个外键放到'Post'表中,因为我还需要能够对'Picture'表进行评论,对此最好的做法是什么?
所以基本上是一个多表链接表?
答案 0 :(得分:1)
评论表:
comment(comment_id,text, created, userid, other_field,...)
链接表
link(comment_id,comment_type, refID, )
其中comment_type
将是您评论的表的名称(“图片”,“帖子”,...)。 comment_id
将是用户发表的评论的ID。 refID
是用户评论过的表的id
(Post_id,Picture_id,...)
假设您有这些表条目:
POST(id, some_text,other_stuff,...):
(1, "Hello Hello", "something",...)
(2, "Hey there", "something_else",...)
...
(57, "TEST TEST", "another something",...)
PICTURE(pic_id, description, other_stuff):
(4, "A cat", "something",...)
(2, "Another cat", "something_else",...)
...
(57, "finally a dog", "another something",...)
当用户刊登新评论时:
注释
(1,"What a cute cat", 2015-10-02, user24)
(3,"That something is awesome", 2015-10-02, user87)
链路
(1,"Picture", 4) //That comment is about the cat (Picture ID 4)
(3, "Post",57) //That comment is about the post with ID 57