Is there a way to combine these 3 tables? (mysql)
I'm creating a "flagged" func that flags users, postings, and comments.
I am currently using three tables: "flagged_users" "flagged_postings" and "flagged_comments".
And in each table, t开发者_运维知识库here's: flagged_id, user_id/posting_id/comment_id, reason.
Is there a way to combine into just one table? or is 3 tables the best?
well you could make one table with the following columns: flagged_id,type,id,reason where type = 1 for flagged_user, 2 for flagged_postings, 3 for flagged_comments. And id would refer to the id of the user or posting or comment. But then while querying in this table it you'd have to make an extra check on the type column.
If you want foreign keys you could create a Flags table like so and change the column you're inserting into based on the type:
FlagID - auto inc
UserID - default NULL
PostingID - default NULL
CommentID - default NULL
Reason - text
Or if you don't care about FKs in the database just use one column for the reference ID:
FlagID - auto inc
ObjectID - User/Post/Comment ID
Reason - text
精彩评论