数据库设计:与汽车型号或内饰相关?

发布于 2024-11-19 22:15:41 字数 755 浏览 0 评论 0原文

我正在重新问一个之前在 SO 中删除的问题,因为它不是“编程问题”。希望这篇文章比上一篇文章更加“编程”。

首先,一些定义:

  • 型号 - 2011 Nissan Sentra
  • 装饰 - 2011 Nissan Sentra LX

一般来说,特定的车辆将有一个列表,例如可用的颜色设备选项。因此,2011 Nissan Sentra 可能有以下颜色可供选择:

  • 黑色
  • 白色
  • 红色

然后,制造商可能制作了一种仅适用于 2011 Nissan Sentra LX 装饰的特殊颜色:

  • 粉红色和黄色圆点

如果我正在构建一个汽车网站,我想在其中捕获此信息,我应该执行以下哪项操作:

  • 将颜色与模型相关联?
  • 将颜色与装饰相关联?
  • 将颜色与模型和装饰相关联?

我的直觉是将其与模型关联起来就足够了。与装饰相关联意味着重复(例如,2011 Nissan Sentra LX2011 Nissan Sentre SE 的颜色均为“黑色”)。试图将颜色与模型和装饰联系起来可能有点矫枉过正。

建议?

I'm re-asking a question that was previously deleted here in SO for not being a "programming question". Hopefully, this is a bit more "programming" than the last post.

First, a few definitions:

  • model - 2011 Nissan Sentra
  • trim - 2011 Nissan Sentra LX

Generally, a particular vehicle would have a list of, say, available colors or equipment options. So a 2011 Nissan Sentra may be available in the following colors:

  • Black
  • White
  • Red

Then, the manufacturer may have made a special color only available to the 2011 Nissan Sentra LX trim:

  • Pink with Yellow Polka Dots

If I were building a car website wherein I wanted to capture this information, which of the following should I do:

  • Associate the colors to the model?
  • Associate the colors to the trim?
  • Associate the colors to the model and trim?

My gut feeling is that associating it to the model would be sufficient. Associating to trim would mean duplicates (e.g. 2011 Nissan Sentra LX and 2011 Nissan Sentre SE would both have "Black" as a color). Trying to associate colors to model and trim might be overkill.

Suggestions?

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

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

发布评论

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

评论(5

剩余の解释 2024-11-26 22:15:42

根据我的评论的要求...

我只会将“颜色”设置为自由格式的文本字段,可能会带有一个预先填充的下拉列表,显示数据库中当前流行的颜色。主要优点是它使您的数据库模式更加简单,并防止您的汽车模型/颜色研究人员发疯。但它也允许进行制造商根本无法提供的定制油漆工作。

manufacturers
-------------
id

models
------
id
manufacturer (FK to manufacturers.id)
model_name   (VARCHAR)

trims
-----
id
model  (FK to models.id)

cars
-------
id
trim   (FK to trims.id)
year   INT
color  VARCHAR

As requested in response to my comment...

I would just make 'color' a free-form text field, possibly with a pre-populated drop-down showing current popular colors in the database. The main advantage is that it makes your DB schema much simpler, and keeps your car model/color researchers from going insane. But it also allows for custom paint jobs that aren't available from the manufacturer at all.

manufacturers
-------------
id

models
------
id
manufacturer (FK to manufacturers.id)
model_name   (VARCHAR)

trims
-----
id
model  (FK to models.id)

cars
-------
id
trim   (FK to trims.id)
year   INT
color  VARCHAR
箜明 2024-11-26 22:15:42

如果我正在构建一个汽车网站,我想在其中捕获此内容
信息

,那么您必须构建一个捕获该信息的逻辑模型。 (这有多难?)这意味着你必须对这些事实进行建模。

  • 某些颜色适用于模型。
  • 某些颜色适用于装饰套件。
  • (我敢打赌我能找到一家制造商,其中某些颜色适用于
    品牌。)
  • 而且我敢打赌,所有这些颜色也与年份有关。)

捕获所有已知的要求是一回事。实施它们是另一回事。一旦您了解了颜色的实际工作原理,

  • 您就可以自由地忽略任何您想要的现实世界行为。

但是,正如博士。菲尔经常说:

  • “当你选择行为时,你就选择了后果。”

简化已知的要求——忽略某些颜色仅适用于一两个装饰包的事实——意味着您设计数据库时故意允许无效数据。您的数据库最终可能会包含有关“粉色带黄色圆点”Nissan Altima 或“Copper”2002 Nissan Sentra 的信息。 (我认为 Nissan 于 2004 年推出了铜。)

所以这是真正的问题。

  • 您可以容忍多少不良数据?

这始终取决于应用程序。收集有关您的汽车颜色信息的社交媒体网站比销售补漆的公司更能容忍不可能的颜色选择。

If I were building a car website wherein I wanted to capture this
information

then you'd have to build a logical model that captured that information. (How hard was that?) And that means you have to model these facts.

  • Some colors apply to the model.
  • Some colors apply to the trim package.
  • (And I'll bet I can find a manufacturer where some colors apply to
    the make.)
  • (And I'll bet that all these colors also have something to do with the year.)

Capturing all the known requirements is one thing. Implementing them is another. Once you understand how the colors actually work,

  • you're free to ignore whatever real-world behavior you want to.

But, as Dr. Phil often says,

  • "When you choose the behavior, you choose the consequences."

Simplifying the known requirements--ignoring the fact that some colors apply only to one or two trim packages--means you design your database to deliberately allow invalid data. Your database might end up with information about a "Pink with Yellow Polka Dots" Nissan Altima, or a "Copper" 2002 Nissan Sentra. (I think Nissan introduced copper in 2004.)

So here's the real question.

  • How much bad data can you tolerate?

That's always going to be application-dependent. A social media site that collected information about your car color would be a lot more tolerant of impossible color choices than a company that sells touch-up paint.

合久必婚 2024-11-26 22:15:41

如果存在特殊情况,正如您所说,制造商只为特定装饰提供特殊颜色,例如“带有黄色圆点的粉色”强>“2011 Nissan Sentra LX装饰”

并且您想要存储这些特殊情况,您应该选择第二个选项。

因此,您的关系将是:

1 个制造商生产许多型号

1 个型号有许多装饰

1 个装饰可以有多种颜色,对于 1 种颜色,许多装饰都有它
(因此您需要一个用于此关系的关联表)

Manufacturer
    1\
      \
       \N
     Model
       1\
         \
          \N
         Trim          Colour
           1\           1/
             \          /
              \N       /M
              TrimColour

有关颜色的附加信息:

一种通用颜色可以由不同制造商命名为多种颜色,并且
一个制造商可以用各种颜色(名称)“洗礼”通用颜色

        Manufacturer         
         1/      1\           
         /         \         
        /N          \        
     Model           \     GeneralColour
       1\             \       1/
         \             \      /
          \N            \N   /M
         Trim           Colour
           1\           1/
             \          /
              \N       /M
              TrimColour

更清楚地思考,不需要额外的 Manufacturer-Colour 关系:

Manufacturer
    1\
      \
       \N               
     Model                 GeneralColour
       1\                     1/
         \                    /
          \N                 /M
         Trim           Colour
           1\           1/
             \          /
              \N       /M
              TrimColour

If there are special cases, as you say, where a manufacturer has made a special color only available to a specific trim, like "Pink with Yellow Polka Dots" for the "2011 Nissan Sentra LX trim"

and you want to have those special case stored, you should choose the 2nd option.

So, your relationships would be:

1 manufacturer makes many models

1 model has many trims

1 trim can have many colors and for 1 colour many trims have it
(so you'll need an association table for this relationship)

Manufacturer
    1\
      \
       \N
     Model
       1\
         \
          \N
         Trim          Colour
           1\           1/
             \          /
              \N       /M
              TrimColour

With additional information about colours:

One GeneralColour can be named as many Colours by different Manufacturers and
one Manufacturer can "baptize" a GeneralColour with various Colour (names)

        Manufacturer         
         1/      1\           
         /         \         
        /N          \        
     Model           \     GeneralColour
       1\             \       1/
         \             \      /
          \N            \N   /M
         Trim           Colour
           1\           1/
             \          /
              \N       /M
              TrimColour

Thinking more clearly, the extra Manufacturer-Colour relationship is not needed:

Manufacturer
    1\
      \
       \N               
     Model                 GeneralColour
       1\                     1/
         \                    /
          \N                 /M
         Trim           Colour
           1\           1/
             \          /
              \N       /M
              TrimColour
人心善变 2024-11-26 22:15:41

如果同一型号的不同装饰可能有不同的颜色选项(正如您所暗示的),那么您应该将颜色与装饰相关联,否则您将获得不正确/不兼容的信息。又名:如果“粉色带黄色圆点”与“2011 Nissan Sentra”车型相关联,那么您将错误地将其显示为 LX 以外的装饰选项。

If different trims for the same model may have different color options (as you imply) then you should associate the color to the trim, otherwise you will have incorrect/incompatible information. aka If "pink with yellow polka dots" is associated to the "2011 Nissan Sentra" model then you will incorrectly show it as an option for trims other than LX.

薄凉少年不暖心 2024-11-26 22:15:41

您缺少装饰与模型的关联;如果没有这个,我不知道你能否真正正确地完成你的联想。

You're missing the association of the trim to the model; without that, I don't know that you can really properly complete your associations.

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