数据建模:车辆的年份、品牌和型号?
我正在尝试在基础级别上对车辆进行建模。以下是我查看数据的方式:
- 一个“年份”(例如 2010 年、2011 年)有 0 个或多个“品牌”(例如 Nissan、Honda)
- 一个“品牌”有 0 个或多个“型号”(例如 Nissan 有 Sentra、Altima、Maxima) )
拥有一个仅包含 1 列的“year”表是没有意义的,所以我认为它将与“make”结合起来创建:
TABLE: year_make
- year
- make
我猜“year”和“make”列将组成一个复合键。
然后我会有一个“模型”表,它以某种方式与“year_make”表关联。问题是我不知道在“year_make”中放入“model”中的内容来关联这两个表。
我是否创建 PK:year_make->year_make_id 并使用它?这意味着“年份”和“制造”列不再构成复合键,对吧?
更新:
我想我应该有一个查找表“lookup_make”,然后“year_make”将有一个“lookup_make_id”而不是“make”列。
更新2:
Pernate c:
TABLE: make
- make_id
- name
TABLE: model
- model_id
- make_id
- name
TABLE: model_year
- model_id
- year
I'm trying to model vehicles at a basic level. Here's how I see the data:
- A "year" (e.g. 2010, 2011) has 0 or more "make" (e.g. Nissan, Honda)
- A "make" has 0 or more "model" (e.g. Nissan has Sentra, Altima, Maxima)
It wouldn't make sense to have a "year" table containing just 1 column, so I think it would be combined with "make" to create:
TABLE: year_make
- year
- make
I guess that "year" and "make" columns would make up a composite key.
Then I would have a "model" table that somehow is associated to "year_make" table. The problem is that I don't know what in "year_make" to put in "model" to associate the 2 tables.
Do I make a PK: year_make->year_make_id and use that? It would mean that "year" and "make" columns no longer make a composite key, right?
UPDATE:
I suppose I should have a lookup table "lookup_make", then "year_make" would have a "lookup_make_id" instead of "make" column.
UPDATE 2:
Per nate c:
TABLE: make
- make_id
- name
TABLE: model
- model_id
- make_id
- name
TABLE: model_year
- model_id
- year
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为如果您创建一些示例数据,您将为设计做好更好的准备。目标是提供具有代表性的样本数据。您可以从代表性示例数据中学到很多东西。 (如果您的样本数据不具有代表性,您可能会犯一些相当昂贵的错误。)
同一栏?
请抵制在此类数据中添加少量 ID 号的诱惑。首先确定键和功能依赖性。根据键和依赖项进行规范化。
I think you'll be better prepared for design if you create some sample data. The goal is to come up with representative sample data. You can learn a lot from representative sample data. (And you can make some pretty expensive mistakes if your sample data isn't representative.)
same column?
Resist the temptation to throw a handful of id numbers at data like this. Identify keys and functional dependencies first. Normalize based on the keys and dependencies.
在查看了更多内容之后,我认为您将这一年建模为多对多加入是正确的。我用这两种方式只是为了展示我正在谈论的内容的例子。如果您有一个涵盖多年来整个模型的属性,例如汽车类别(经济型、卡车型、豪华型等),您将需要一个多对多表来进行规范化并避免数据重复。
...所以#1将是更“正确”和更稳健的方法,特别是如果您计划在模型上保留独立于其制造年份的属性。无论哪种方式都会让你得到相同的查询。事实上,连接规范化表比非规范化表更难/更多工作(不是真正在你理解之后)。但这不是重点。您放入数据库是因为您希望数据正确(我希望)。
注意:即使您使用自动增量主键,这些也是真实主键。您可能希望将主键更改为唯一键以确保数据一致性,并将引用外键更改为整数。
After looking at some more I think you are right to model the year as a many to many join. I put both ways just to show as examples of what of I am talking about. If you have an attribute that covers the whole model over years such as car class (economy, truck, luxury, etc.) You would need a many to many table for normalization and to avoid data duplication.
... So #1 would be the more 'correct' and robust way, especially if you planning to hold attributes on the model that would be independent of what year it was made. Either way would get you the same queries. In fact, its harder / more work (not really after you get it down) to join the the tables of a normalized one than a non-normalized one. But that is not the point. You are putting in the DB because you want your data to be correct (I hope).
Note: those are the real primary keys even if you use auto-inc primary keys. You would want to change the primary keys to unique ones to ensure data consistency and change the referencing foreign keys to integers.
IMO,多年来我不会做一张桌子。对整数列(例如年份)执行 SQL 的速度非常快,尤其是在使用正确的索引的情况下。比加入快得多。
然而,我确实看到了为 Make 建立一个单独的表并基于该表进行连接的优点。这样你就可以输入制造商的详细信息,如历史、生产年份、网站等。
所以我会这样做:
IMO, I wouldn't make a table for the years. Doing SQL on an integer column (such as year) is incredibly fast, especially with the right indexes in place. Much faster than a join.
However, I do see the merit to having a separate table for the Make and doing join based on that. This way you'd be able to enter manufacturer details like history, years of production, website, etc.
So I'd do:
到目前为止看起来不错,就像你的想法一样。但在 cars 表中,如果您必须输入 Camry 2001、camry 2002、canry 2003、camree 2004。似乎您将拥有冗余数据以及拼写错误导致数据完整性受损的风险。 IMO,您还需要某种具有 makeID 和 modelID 的实体,对于 modelID,它仅输入一次,或者模型仅在表中输入一次。 “Camry”将始终是用 modelID 表示的“Camry”。
Looks good so far, like your thinking. In the cars table though, if you have to enter Camry 2001, camry 2002 canry 2003, camree 2004. Seems like you would have redundant data along with the risk of data integrity with misspelling. IMO you would also need somekind of Entity with makeID and modelID, with modelID it is entered just once or model is just entered once in a table. 'Camry' will always be 'Camry' represented with modelID.
如果我理解正确,您需要创建一个
make
表和一个model
表。model
表将包含id
、make_id
、name
、year
列。显然,make_id 将是指向
make
表的外键。这实际上取决于您的情况以及您需要多少程度来规范化数据。
If I'm understanding you correctly, you would need to create one
make
table and onemodel
table.The
model
table would have columns forid
,make_id
,name
,year
.Obviously, make_id would be the foreign key pointing to the
make
table.It really depends on what your situation is and how much you would need to normalize your data.