2015-11-02 74 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