具有很多属性的数据库表设计

发布于 2024-12-10 17:53:12 字数 516 浏览 0 评论 0原文

我想知道在这种情况下数据库设计应该是什么样子。

在此处输入图像描述

我想到的是每个实体的表格,例如:

Engine  
EngineDesign  
ElectricalSystem  
Drivetrain  
etc.

但在我看来,有很多表。我的想法可以吗还是太过分了?

那么传动系统呢?现在我们有 8 个档位,但只要制造商推出不同的产品,它就可以改变。

应该是这样的:
表(列)

DrivetrainId | 1st | 2nd | 3rd | etc.

表(行)

DrivetrainId | Gears
1            | 1st
2            | 2nd
3            | 3rd
etc.

I'd like to know how the db design should look like in this case.

enter image description here

What comes to my mind is a table for every entity like:

Engine  
EngineDesign  
ElectricalSystem  
Drivetrain  
etc.

But it looks to me as a lot of tables. Is my idea ok or is it excessive?

And what about the Drivetrain? Today we have 8 gears but it can change whenever a manufacturer comes with something different.

Should it be like this:
Table (columns)

DrivetrainId | 1st | 2nd | 3rd | etc.

Or

Table (rows)

DrivetrainId | Gears
1            | 1st
2            | 2nd
3            | 3rd
etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

对不⑦ 2024-12-17 17:53:12

你不能通过计算表来判断数据库设计的合理性。不存在“表太多”范式或“表不足”范式之类的东西。也不存在“列太多”范式或“列不足”范式之类的东西。

就目前情况而言,为每个变速箱齿轮比设置一列可能仍会达到 5NF。这是因为它没有多个列的值来自同一域,这是一个问题。它有多个列,这些列的值来自同一域并且具有相同的含义,这是一个问题。

显然,8档和1档的含义是不同的。事实上,您可能会认为它们来自不同的领域。我的猜测是 0.67 不是 1 档的有效值,4.85 不是 8 档的有效值。

当它们存储在不同的列中时,

  • 很容易强制执行这样的约束:每行都有 8 个变速器齿轮比中的每一个的值,并且
  • 对每个齿轮比的有效值范围的约束非常简单。 (但是对于只有 5 或 6 个齿轮的后续设计,您必须容纳 NULL,这会引发标准化问题。)

当它们存储为行时,

  • 很难(也许不可能)强制执行每辆车都有一个值的约束对于 8 个传动比中的每一个,
  • 每个传动比的有效值范围的约束更加复杂。 (特别是当您稍后适应只有 5 或 6 个齿轮的设计时。)

You can't judge the soundness of a database design by counting tables. There's no such thing as "too many tables" normal form, or "not enough tables" normal form. There's also no such thing as "too many columns" normal form, or "not enough columns" normal form.

As it stands today, having one column for each of the transmission gear ratios might still get you to 5NF. That's because it's not having multiple columns whose values come from the same domain that's a problem. It's having multiple columns whose values come from the same domain and have the same meaning that's a problem.

Clearly, 8th gear and 1st gear have different meanings. In fact, you might consider them to be drawn from different domains. My guess is that 0.67 isn't a valid value for 1st gear, and that 4.85 isn't a valid value for 8th.

When these are stored in different columns,

  • it's easy to enforce the constraint that every row have a value for each of the 8 transmission gear ratios, and
  • constraints on the range of valid values for each gear ratio is really simple. (But you have to accommodate NULLs for later designs having only 5 or 6 gears, and that raises normalization issues.)

When they're stored as rows,

  • it's harder--perhaps impossible--to enforce the constraint that each vehicle have a value for each of the 8 transmission gear ratios, and
  • constraints on the range of valid values for each gear ratio is more complicated. (Especially when you later accommodate designs that have only 5 or 6 gears.)
走走停停 2024-12-17 17:53:12

我的第一反应是用属性表和汽车表做一些事情(我假设是汽车?)

Attributes
AttributeId | AttributeCategory | AttributeDesc

Cars    
CarId | AttributeId | AttributeValue

编辑:请参阅下面的评论 @HLGEM 的解释为什么实体属性值表在本例中不是一个好主意。

My first instinct would be to do something with an Attributes table and a Cars table (I'm assuming cars?)

Attributes
AttributeId | AttributeCategory | AttributeDesc

Cars    
CarId | AttributeId | AttributeValue

Edit: See comments below @HLGEM's explanation why an Entity-Attribute-Value table is not a good idea in this example.

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