数据库设计查询:合并表还是分开?

发布于 2024-11-09 00:37:04 字数 228 浏览 0 评论 0原文

我开始设计一个数据库驱动的网站。

我有两个类似的表,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南巷近海 2024-11-16 00:37:04

如果他们实际上是问题域中的“供应商”。我将创建一个表“VENDOR”并添加列“VENDOR_TYPE”以及其他列。

Table Vendor{
    id, 
    vendor_type,
    vendor_name, 
    vendor_url, 
    vendor_bio    
}

但如果它们是两个逻辑上不同的实体类型,我会倾向于创建两个单独的表。在您的情况下,它似乎是“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.

Table Vendor{
    id, 
    vendor_type,
    vendor_name, 
    vendor_url, 
    vendor_bio    
}

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'

就是爱搞怪 2024-11-16 00:37:04

尤概述了答案。在大多数像您这样的情况下,我已将两个表合并为一个表,并使用类型列来指示每个条目属于哪种类型。

这取决于您将如何使用这些数据。如果您的许多查询将来自 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文