良好的数据库表设计:一个表混合不同实体或每个实体的单独表

问题描述:

什么是更好的数据库设计?

What is better database design?

拥有一个可以包含不同类型记录的大表,例如:员工,汽车,手机和按顺序要识别每种类型的记录,我们有一个名为type的列。

Having one big table that could contain different "types" of records eg: employees, cars, cell-phones, and in order to identify each type of record, we have a column called type.

因此,表格中将会显示如下列:

So the table would have columns that look like

1 |车|福特

2 |车|丰田

3 |电话|摩托罗拉

3 | phone | motorola

4 |员工|杰克

5 |员工| Aneesh

5 | employee | Aneesh

6 |电话|诺基亚

7 |电话|摩托罗拉

7 | phone | Motorola

或每种类型有不同的表格

or have different tables for each type

例如:

id |名称

id |名字

id | name

id | name

这些表可能有其他表的外键引用。现在如果每个表都有不同的列,那么这个决定很简单,你不能在同一个表中。因此,选项1可能被排除(除非所有不常见的列都为空)。但是,如果这些不同的实体有类似的列,那么在这种情况下,什么是更好的设计?

These tables could have foreign key references from other tables. Now if each table had different columns the decision would have been simple that you can't have that in the same table. So option 1 is probably ruled out (unless all columns that are not common are nullable). But what if these different entities had similar columns, in that case what is better design?

什么可能是每个的参数?

What might be the arguments for and against each?

我同意每个人 - 绝对使用单独的表。你没有任何东西通过分开的表 - 只是因为你有更多的表,你的数据库不会得到任何更慢或更少的可管理。

I agree with everyone - definitely use separate tables. You loose nothing by having separate tables - just because you have a few more tables, your database won't get any slower or less manageable.

但你获得了很多 - 你不必有很多对一种类型的实体没有意义的领域,等等。你坚持2NF,许多人都指出,这绝对是一件好事!

But you gain a lot - you don't have to have lots of fields that make no sense for one type of entity, and so on. You adhere to 2NF as many have pointed out, and that's definitely a good thing!

查看这个有趣的文章,简单的谈话叫

Check out this interesting article on Simple Talk called

五个简单的数据库设计错误以及如何避免他们

Five Simple Database Design Errors and how to avoid them

错误#1是作者称之为常见查找表,这听起来很像你想要做的 - 但是真正的实时数据。

Error #1 is what the author calls the "common lookup table", which sounds a lot like what you're trying to do - but for real live data.

阅读文章,内部化所有要求 - 优秀的内容和强烈推荐!

Read the article, internalize all its requirements - excellent stuff and highly recommended!