2015-11-02 75 views
1

我想创建一个'评论'表,所以保存文本,创建,用户ID等...评论表mySQL

但是用户可以评论很多事情。

所以我不能把'外部'表的外键,因为我也需要能够评论'图片'表,这是最佳做法是什么?

所以基本上是一个多表链接表?

+0

添加列'类型'或'rel_table'或其他东西 – Alex

+0

我会考虑2个表:post_comment和picture_comment,外键回到各自的父表。试图使用一种适合所有方法的方法可以工作,但也会导致数据完整性头痛。 – JRD

回答

1

评语表:

comment(comment_id,text, created, userid, other_field,...) 

链接表

link(comment_id,comment_type, refID,) 

其中comment_type将是你( “图片”, “后”,...)评论表的名称。 comment_id将是用户所做评论的ID。​​是用户上(POST_ID,Picture_id,...)评论表的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