I'm making a review type website, and I would like for there to be 2 types of users. One would be the majority, reviewers while the other would be the person the review is about. How, in terms of database design would I differentiate the two. Should I have separate tables for reviewers and reviewies or simply assign a different range of ID's for both?
-
Thanks for the quick responses. The information will be all the same for both types of users. Some context, not the same thing but think ratemyprofessor.com, with the ability for the professor to respond to specific comments and reviews.CNJ– CNJ2010-01-26 04:48:12 +00:00Commented Jan 26, 2010 at 4:48
7 Answers
This is a classic super-type/sub-type situation. Users and Reviewers are both people, and relational bigots would say you should have one "people" table containing all of the common person fields. You can then have explicit one-to-one foriegn key relationship to a reviewers table, which contains the reviewer-specific fields and related keys to other reviewer-specific tables. You check to see if a person is a reviewer by joining to the Reviewers table.
You can of course add other types of people with different data to this approach. This is a good way to model things for relational bigots like me who like to avoid null-valued fields and undeclared foreign keys that reference one of several tables. MySQL jockeys would just cram it all into one big table with lots of nulls, magic numbers, and comma-separated lists in varchar fields.
If you know the requirements will not change in the future (ha!), and there is no difference in schema between users and reviewers, just use a single person_type field and keep it simple.
Never use "key ranges" for any reason. That's just hackery which will only cause a massively painful recoding effort for you or someone else in a few years. Primary keys should be without semantics and invariant.
4 Comments
Given the vagueness of your question, I would say that it depends on the kind of information that either party would be accessing/inserting. As I understand it, the structure of your database should reflect the manner in which you are organizing your data, not necessarily the relationship between the users of your database. For example, you could have a separate tabled called "User Status" with 0->Reviewer, 1->Review-ee, and then list User Status as a foreign key in your Users table as a column that indicates whether a given User is a Reviewer or a Reviewee, thereby eliminating the need for separate User tables.
Comments
That depends rather largely on what data you're keeping about each of these user types. If very similar, a single table could be okay, but you'd want a differentiating field (column) rather than assigning a range of IDs.
If you need different information about reviewers and reviewees, and it otherwise makes sense to keep that information together, then you'd do well to separate them into at least two tables.
One time, here on SO someone said, "ALWAYS put like things in the same table."
This is true because that SO'ers definition of 'like' is so ambiguous that he can always create a definition that's true.
So said, you should do the same here. The fact that Reviewers and Reviewee are both 'people' is irrelevant. The real question is can anyone ever be both? If that's not true then you'll have no problem and actually should use separate tables.
Think of it this way.
Let's say that's it's true reviewers can never be reviewees. Somewhere you'll have a table with both Reviewer_ID and Reviewee_ID. You'll build a FK back to those two tables. Those Constraints GUARANTEE that you'll have one reviewer and one reviewee... you'll never have 2 of one.
But if they can 'cross dress' so to speak, and you build two tables, you'll either have to put the same person in both tables with different id's an no way (besides adding more layers) to know they are the same person or else you'd have to recast to a Super-type/sub-type style.
Put like things in the same table. And what like really means is behaviorally. Things which can be reviewed should go into one table even if they are professors and films. How you handle their disparate attributes (either Nullable columns or Supertype/subtype) is determined by more information than you've provided).