包库的数据库结构

发布于 2024-12-10 18:57:12 字数 726 浏览 0 评论 0原文

问题。我有一个项目可以有单独的值或“包”值。

例如:

项目

Table Car
id INT
package_id INT
alloys BOOLEAN
aircon BOLLEAN
airbags TINYINT
transmission ENUM('auto','manual')

和包:

Table Package
package_id INT
alloys BOOLEAN
aircon BOLLEAN
airbags TINYINT
transmission ENUM('auto','manual')

客户可以创建任何包(字段比本示例中的字段多,例如 15 个),然后将其应用到任何汽车。它是一对多的关系(一个包可以应用于多辆车)。然而他们不必使用包。他们可以单独指定所有元素。现在我的问题是做什么更好?

  1. 与上面的操作类似,如果 Car 中的 package_idNULL,则使用 Car
  2. Store 中 Package 中每组更改的 值> 即使它本身不是包,而是自定义工作(并添加一些 is_package bool 来确定它是否应该显示在包列表中)。
  3. 创建单独的表来存储这些自定义的一次性选项?
  4. 还有其他想法吗?

提前致谢!

Question. I have an item that can have an individual values or a 'package' values.

e.g.:

Item

Table Car
id INT
package_id INT
alloys BOOLEAN
aircon BOLLEAN
airbags TINYINT
transmission ENUM('auto','manual')

And packages:

Table Package
package_id INT
alloys BOOLEAN
aircon BOLLEAN
airbags TINYINT
transmission ENUM('auto','manual')

Customers can create any package (there is more fields than in this example, say 15) and then apply it to any car. Its a one to many relation (one package can be applied to many cars). However they dont have to use packages. They can specify all elements individually. Now my question is what is better to do?

  1. Like its done above and if package_id in Car is NULL then use values from Car
  2. Store every set of changes in Package even if it's not the package per se but a custom work (and add some is_package bool to determinie if it should show in packages list).
  3. Create separate table for storing those custom, one time options?
  4. Any other idea?

Thanks in advance!

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

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

发布评论

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

评论(2

难理解 2024-12-17 18:57:12

如果我理解正确的话,你描述了一个简单的 1:n 关系。我更喜欢第二个选项并将 Car.package_id 设置为 NOT NULL

Table Car
---------
id INT PRIMARY KEY
package_id INT NOT NULL
FOREIGN KEY (package_id)
  REFERENCES Package (package_id)    

Table Package
-------------
package_id INT PRIMARY KEY
alloys BOOLEAN
aircon BOLLEAN
airbags TINYINT
transmission ENUM('auto','manual')

您仍然可以通过简单的查询找到哪些包是“自定义”的(使用仅在一辆车中)或被多辆车使用。

if I understand correctly, you describe a simple 1:n relationship. I'd prefer 2nd option and setting Car.package_id as NOT NULL :

Table Car
---------
id INT PRIMARY KEY
package_id INT NOT NULL
FOREIGN KEY (package_id)
  REFERENCES Package (package_id)    

Table Package
-------------
package_id INT PRIMARY KEY
alloys BOOLEAN
aircon BOLLEAN
airbags TINYINT
transmission ENUM('auto','manual')

You will still be able to find - with a simple query - which packages are "custom" (used only in one car) or used by many cars.

冬天旳寂寞 2024-12-17 18:57:12

基本上你有两种类型的汽车......定制汽车和来自包装的汽车。

这是一个可以使用继承来解决的问题。

当您对继承进行建模时,您有两种选择:
- 创建一个包含所有属性的表
- 为每个子项创建单独的表

它们都有各自的优点和缺点,但基本上,将它们分成不同的表可以让您在其中一个子项与不同表相关时更好地保持数据完整性。

例如:当您在 package_cars 表中看到一辆汽车时,您 100% 确定这辆汽车与某个包相关,并且它没有有自己的属性。但是,如果您使用单个表,则您的表更容易出现数据不一致的情况,因为您将依赖于“如果 Car 中的 package_id 为 NULL,则使用 Car 中的值”之类的规则,并且只能在业务层中进行控制,并且不在模型上。

为了说明这一点,假设您添加一个名为“类别”的表,规则是:“定制汽车有一个类别。但打包汽车没有,因为类别与包装相关”如果您使用单个表,则只需要向表中添加一个category_id,但现在您需要记住一个更复杂的规则:“如果package_id为NULL,则使用Car中的值并且category_id不应该为NULL”。如果有人犯了错误(为什么不呢?)并将category_id和package_id添加到同一辆车中,您就会遇到数据不一致的情况。

简而言之,当子级与其余表有不同的关系时,最好将它们分成不同的表,以避免使用可为空的外键,它们可能会造成混乱和不一致。

Basically you have TWO types of cars... custom cars, and cars that comes from a package.

It's a problem that could be solved using inheritance.

When you're modeling inheritance you have two options:
- create a single table with all the attributes
- create separate tables for each children

They both have their pro and cons but basically, separating them into different tables allows you to better preserve your data integrity when one of the child is related to different tables.

For example: when you see a car in the package_cars table, you are 100% sure that this car is related to a package and it doesn't have its own attributes. However, if you use a one single table, your table is more exposed to data inconsistency because you'll depend on rules like "if package_id in Car is NULL then use values from Car" and that can be only controlled in the business layer and not on the model.

To illustrate this, let's say that you add a table called "category" and the rule is: "customized cars have a category. But packaged cars don't, because the category is related to the package" If you use a single table you'd only need to add a category_id to the table, but now you'll need to remember a more complicated rule: "if package_id is NULL then use values from Car AND category_id should not be NULL". And if someone makes a mistake (why not?) and adds a category_id and a package_id to the same car, you'll have a data inconsistency.

In a few words, when children have different relations to the rest of the tables, its better to separate them into different tables to avoid using null-ables foreign keys they might be confusing and inconsistent.

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