数据库设计,两种类型的网站用户

时间:2021-08-06 17:02:31

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?

我正在做一个评论类型的网站,我希望有两种类型的用户。一个是大多数,审稿人,另一个是审稿人。在数据库设计方面,我如何区分这两者。我应该为审阅者和reviewies提供单独的表,还是仅仅为两者分配不同的ID范围?

7 个解决方案

#1


1  

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.

这是典型的超类型/子类型情况。用户和审阅者都是人,关系偏执者会说,您应该有一个包含所有common person字段的“people”表。然后,您可以拥有与审阅者表的显式一对一的foriegn键关系,该表包含特定于审阅者的字段,以及其他特定于审阅者的表的相关键。通过加入审稿人表,您可以检查一个人是否是审阅人。

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.

当然,您可以向此方法添加具有不同数据的其他类型的人员。这是为像我这样喜欢避免使用空值字段和引用几个表中的一个的未声明外键的关系偏执者建模的好方法。MySQL jockeys会把所有这些都塞到一个大表中,其中包含大量的null、神奇的数字和varchar字段中的逗号分隔列表。

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.

如果您知道将来的需求不会改变(ha!),并且用户和审稿人之间的模式没有区别,只需使用一个person_type字段并保持简单。

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.

不要因为任何原因而使用“key ranges”。这只是一种黑客行为,只会给你或其他人带来巨大痛苦的重新编码。主键应该没有语义和不变量。

#2


1  

I recommend you to create one table for users and other for usertype, with a foreign key to users, in case you later need users with both type.

我建议您为用户创建一个表,为usertype创建另一个表,为用户创建一个外键,以防以后需要两个类型的用户。

#3


1  

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.

考虑到你的问题的模糊性,我想说,这取决于任何一方会访问/插入的信息。据我所知,数据库的结构应该反映您组织数据的方式,而不是数据库用户之间的关系。例如,您可以有一个单独的名为“User Status”的列表,其中包含0->Reviewer, 1->Review-ee,然后将用户状态作为一个外键列出在用户表中,作为一个列,该列指示给定的用户是Reviewer还是Reviewee,因此不需要单独的用户表。

#4


0  

You could have a column 'type' with values "reviewer" and "reviewee"

你可以有一个列'type'带有值"reviewer"和"reviewee"

#5


0  

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.

这在很大程度上取决于您保存这些用户类型的数据。如果非常相似,单个表可能是可以的,但是您需要一个区分字段(列),而不是分配一个id范围。

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.

如果您需要关于审阅者和reviewees的不同信息,并且将这些信息放在一起是有意义的,那么您最好将它们划分为至少两个表。

#6


0  

If the attributes describing these two different users is different, then you may want to separate them. If they share same attributes like firstName, lastName etc, then you can keep them in the same table.

如果描述这两个不同用户的属性不同,那么您可能希望将它们分开。如果它们共享相同的属性,如firstName、lastName等,那么您可以将它们保存在相同的表中。

#7


0  

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'ers对like的定义是如此模棱两可,以至于他总能找到一个正确的定义。

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.

假设这是真的评论家永远不会被评论。在某些地方,您将拥有一个包含Reviewer_ID和Reviewee_ID的表。您将构建一个FK回到这两个表。这些约束保证您将有一个审阅人和一个审阅人……你永远不会有2个1。

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.

但如果他们能“变装”这么说,和你建立两个表,你要么把同一个人在两个表有不同的id是一个没有办法(除了增加层),知道他们都是一样的人,否则你必须重塑一个超类/子型风格。

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).

把东西放在同一张桌子上。真正的意思是行为。即使是教授和电影,那些可以被审查的东西也应该放在一张桌子上。如何处理它们的不同属性(可空列或超类型/子类型)取决于比您提供的更多的信息)。

#1


1  

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.

这是典型的超类型/子类型情况。用户和审阅者都是人,关系偏执者会说,您应该有一个包含所有common person字段的“people”表。然后,您可以拥有与审阅者表的显式一对一的foriegn键关系,该表包含特定于审阅者的字段,以及其他特定于审阅者的表的相关键。通过加入审稿人表,您可以检查一个人是否是审阅人。

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.

当然,您可以向此方法添加具有不同数据的其他类型的人员。这是为像我这样喜欢避免使用空值字段和引用几个表中的一个的未声明外键的关系偏执者建模的好方法。MySQL jockeys会把所有这些都塞到一个大表中,其中包含大量的null、神奇的数字和varchar字段中的逗号分隔列表。

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.

如果您知道将来的需求不会改变(ha!),并且用户和审稿人之间的模式没有区别,只需使用一个person_type字段并保持简单。

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.

不要因为任何原因而使用“key ranges”。这只是一种黑客行为,只会给你或其他人带来巨大痛苦的重新编码。主键应该没有语义和不变量。

#2


1  

I recommend you to create one table for users and other for usertype, with a foreign key to users, in case you later need users with both type.

我建议您为用户创建一个表,为usertype创建另一个表,为用户创建一个外键,以防以后需要两个类型的用户。

#3


1  

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.

考虑到你的问题的模糊性,我想说,这取决于任何一方会访问/插入的信息。据我所知,数据库的结构应该反映您组织数据的方式,而不是数据库用户之间的关系。例如,您可以有一个单独的名为“User Status”的列表,其中包含0->Reviewer, 1->Review-ee,然后将用户状态作为一个外键列出在用户表中,作为一个列,该列指示给定的用户是Reviewer还是Reviewee,因此不需要单独的用户表。

#4


0  

You could have a column 'type' with values "reviewer" and "reviewee"

你可以有一个列'type'带有值"reviewer"和"reviewee"

#5


0  

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.

这在很大程度上取决于您保存这些用户类型的数据。如果非常相似,单个表可能是可以的,但是您需要一个区分字段(列),而不是分配一个id范围。

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.

如果您需要关于审阅者和reviewees的不同信息,并且将这些信息放在一起是有意义的,那么您最好将它们划分为至少两个表。

#6


0  

If the attributes describing these two different users is different, then you may want to separate them. If they share same attributes like firstName, lastName etc, then you can keep them in the same table.

如果描述这两个不同用户的属性不同,那么您可能希望将它们分开。如果它们共享相同的属性,如firstName、lastName等,那么您可以将它们保存在相同的表中。

#7


0  

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'ers对like的定义是如此模棱两可,以至于他总能找到一个正确的定义。

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.

假设这是真的评论家永远不会被评论。在某些地方,您将拥有一个包含Reviewer_ID和Reviewee_ID的表。您将构建一个FK回到这两个表。这些约束保证您将有一个审阅人和一个审阅人……你永远不会有2个1。

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.

但如果他们能“变装”这么说,和你建立两个表,你要么把同一个人在两个表有不同的id是一个没有办法(除了增加层),知道他们都是一样的人,否则你必须重塑一个超类/子型风格。

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).

把东西放在同一张桌子上。真正的意思是行为。即使是教授和电影,那些可以被审查的东西也应该放在一张桌子上。如何处理它们的不同属性(可空列或超类型/子类型)取决于比您提供的更多的信息)。