tags:

views:

32

answers:

2

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, there'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?

A: 

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
Sugerman
So how would I select all the flagged users? something along the lines of...select * FROM flagged where userid !=null?
ggfan
I forgot the type column in the second option which is necessary only if it is possible for different Object types to share the same ID.
Sugerman
+1  A: 

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.

Risha
thanks! I failed to see such a simple solution.
ggfan