数据库设计查询:合并表还是分开?
我开始设计一个数据库驱动的网站。
我有两个类似的表,GPU供应商
和CPU供应商
。两者都有相同的列:id、vendor_name、vendor_url、vendor_bio
。
您会制作单独的表格还是将它们组合起来,也许添加一个类型列
?
我不希望我的经验不足阻碍我以后的设计。先想,后做! :)
I'm starting to design a database driven website.
I have two similar tables, GPU vendor
and CPU vendor
. Both have the same columns: id, vendor_name, vendor_url, vendor_bio
.
Would you make separate tables or combine them, perhaps adding a type column
?
I don't want my inexperience to hinder my design later on. Think first, do later! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果他们实际上是问题域中的“供应商”。我将创建一个表“VENDOR”并添加列“VENDOR_TYPE”以及其他列。
但如果它们是两个逻辑上不同的实体类型,我会倾向于创建两个单独的表。在您的情况下,它似乎是“VENDOR”而不是“GPUVendor”和“CPUVendor”
If they are actually 'Vendor' in problem domain. I'll create a table 'VENDOR' and add column 'VENDOR_TYPE' along with your other columns.
But if they are two logically different Entity types, i would lean towards creating two separate tables. In your case it seems to be 'VENDOR' rather than 'GPUVendor' and 'CPUVendor'
尤概述了答案。在大多数像您这样的情况下,我已将两个表合并为一个表,并使用类型列来指示每个条目属于哪种类型。
这取决于您将如何使用这些数据。如果您的许多查询将来自 GPU 类型的行与来自 CPU 类型的行组合起来,那么单表将产生更好的结果。如果几乎所有查询都专门针对 GPU 类型或专门针对 CPU 类型,那么您将从两个表设计中获得更好的结果。
归根结底,这还是YAU所说的:它们是两个实体还是一个实体。
我要补充一点,无论哪种方式,您都可以构建一个或两个视图,使数据看起来相反。如果创建单个 VENDORS 表,则可以创建仅选择 CPU 类型的 CPU_VENDORS 视图和仅选择 GPU 类型的 GPU_VENDORS 视图。然后您可以在方便时访问这些视图。
如果采用两个表设计,则可以创建一个 VENDORS 视图,即 GPU_VENDORS 和 CPU_VENDORS 的 UNION,并在需要时使用它。
除了性能效果之外,您还可以从视图中获得两全其美的效果。
YAU outlined the answer. In most cases like yours, I have combined the two tables into one, with a type column to indicate which type each entry belongs to.
It depends on how you will use the data. If many of your queries will combine rows from GPU types with rows from CPU types, then the single table table will yield better results. If almost all your queries are exclusively about GPU types or exclusively about CPU types, then you'll get better results from the two table design.
Ultimately, this boils down to what YAU said: are they two entities or one entity.
I'm going to add that, either way, you can build one or two views that make the data look the other way. If you create a single VENDORS table, you can create a CPU_VENDORS view that only selects the CPU types and a GPU_VENDORS view that only selects the GPU types. You can then access those views when convenient.
If you adopt a two table design, you can create a single VENDORS view that is the UNION of GPU_VENDORS and CPU_VENDORS, and use that when needed.
Except for performance effects, you'll get the best of both worlds out of the views.