简体   繁体   English

如何正确构建数据库

[英]How to properly build database

I've come to a problem with which I would like to ask you for some directions. 我遇到了一个问题,我想问你一些方向。 I have database as in image below. 我有数据库,如下图所示。 在此输入图像描述

1 In my application I have products predefined, so they automaticaly popup in textbox, depends on first letter. 1在我的应用程序中,我有预定义的产品,因此它们在文本框中自动弹出,取决于第一个字母。 My problem is that every product available in multiple colors. 我的问题是每种产品都有多种颜色可供选择。 And i dont know how to make a proper table. 我不知道如何制作一张合适的桌子。 Should i make one table only with colors? 我应该只用颜色制作一张桌子吗?

2 Let's say Customer orders 5 monitors, and 4 keyboards that are all on same Order. 2假设客户订购了5台显示器和4个同一订单上的键盘。 Where should I have quantity? 我应该在哪里有数量? In my OrderProduct table? 在我的OrderProduct表中?

3 What if customer want a custom sticker or his logo printed on item selected. 3如果客户想要在所选项目上打印自定义标签或其徽标,该怎么办? If I select print on item or sticker in my application, where in database should this be stored? 如果我在我的应用程序中选择打印项目或贴纸,那么应该在数据库中存储哪些内容? Also on OrderProduct? 还在OrderProduct上?

  1. Add a table called Colors and have it have the fields ID, Feasible Name, etc. In the Product show a bitwise value for the Colors it supports. 添加一个名为Colors的表,并使其具有字段ID,可行名称等。在Product中显示它支持的Colors的按位值。

  2. Yes, you would have the Quantity in the OrderProduct table. 是的,您将拥有OrderProduct表中的数量。

  3. It depends on what kind of stickers he is getting. 这取决于他得到的贴纸类型。 Is the sticker going to be one he can pick from or is it something he can upload? 贴纸是他可以选择的贴纸还是他可以上传的东西?

If your client is going to need to be able to customize each item, then you should have each item as a separate insert into the OrderProduct table and not have a quantity field- once you do that just have a reference to the sticker id which could be predefined or user defined. 如果您的客户需要能够自定义每个项目,那么您应该将每个项目作为单独插入到OrderProduct表中而不是具有数量字段 - 一旦您这样做,只需引用标签ID即可预定义或用户定义。

Back to the colors- like I said if you're going to be able to customize each product then make a new insert for each product and remove a quantity column and then just add a ColorID column to the OrderProduct table where you will define what color or customization's that specific product would have. 回到颜色 - 就像我说的,如果你能够自定义每个产品,那么为每个产品制作一个新的插入并删除数量列,然后只需将ColorID列添加到OrderProduct表中,您将在其中定义颜色或定制的特定产品。


Response to comment 回应评论

The application needs to know what colors each item comes in, so you may have Black and Gray keyboards while having Black and Red monitors. 应用程序需要知道每个项目的颜色,因此您可以使用黑色和灰色键盘,同时使用黑色和红色显示器。 A bitwise value is like a flag system except with numbers. 除了数字之外,按位值就像标志系统。 Lets say you had... 让我们说你有......

1 | Black
2 | Gray
3 | Red

If a product could have Black and Gray you would do Black,Gray which goes to 1,2. 如果一个产品可能有黑色和灰色你会做黑色,灰色到1,2。 If a product has Black and Red you would do Black,Red which goes to 1,3. 如果产品有黑色和红色,你会做黑色,红色到1,3。 All a bitwise value does is convert it to value that a computer can comprehend easier. 所有按位值都将其转换为计算机可以更容易理解的值。

So to get the bitwise value for Black & Gray you square the ID and add it together (bor). 因此,为了获得Black&Grey的按位值,您可以对ID进行平方并将其添加到一起(bor)。 Once you have that value (which is the bitwise value) you can run band on it to see if it has that color. 一旦你有了这个值(这是按位值)你可以在它上面运行band以查看它是否具有该颜色。

This is a really good tutorial on bitwise for PHP, I recommend reading it for more information on the topic. 对于PHP来说, 是一个非常好的bitwise教程,我建议您阅读它以获取有关该主题的更多信息。

  1. The problem you describe - a "product" is available in multiple colors - is common; 您描述的问题 - “产品”有多种颜色可供选择 - 很常见; the standard solution is to introduce the concept of "product variant". 标准解决方案是引入“产品变体”的概念。 For instance, T-shirts are available in multiple colors and multiple sizes; 例如,T恤有多种颜色和多种尺寸可供选择; shoes are available in multiple sizes etc. 鞋子有多种尺寸等。

The database solution can vary; 数据库解决方案可以变化; see this answer on SO for my recommendation if you don't know in advance what the variant types will be, or this if you do. 看到对SO答案我的建议,如果你事先不知道的变量类型是什么,或者这个如果你这样做。

Depending on which solution you go with, you can then include the reference to the variant in the product_order table. 根据您使用的解决方案,您可以在product_order表中包含对变量的引用。

This solution allows you to extend the solution for future product types without polluting the "product_order" table - if you use @Hans's recommendation to include a bitmask to indicate colour, and you introduce size as another dimension, very quickly your "product_order" table starts to include a lot of knowledge of the product structure, and it becomes hard to manage/validate. 此解决方案允许您扩展未来产品类型的解决方案而不会污染“product_order”表 - 如果您使用@Hans的建议包含一个位掩码来指示颜色,并且您将尺寸作为另一个维度引入,很快您的“product_order”表开始包含很多产品结构知识,很难管理/验证。

  1. Yes, quantity is usually included in the order_product table (this is more commonly called "orderlines"). 是的,数量通常包含在order_product表中(这通常称为“订单”)。 It's also a good idea to include the unit price - the price of the product may change over time, and you want to record the price the customer agreed to when placing the order. 包含单价也是一个好主意 - 产品价格可能随时间变化,并且您希望记录客户在下订单时同意的价格。

  2. For customizations, you may want to create an "order_customization" table, with a link to the order_product table, and the selected customization, eg 对于自定义,您可能需要创建“order_customization”表,其中包含指向order_product表的链接以及所选的自定义,例如

    OrderID ProductID Customization 1 2 Blue sticker 1 1 Green sticker OrderID ProductID Customization 1 2蓝色贴纸1 1绿色贴纸

Presumably, not all items on an order would have customizations; 据推测,并非订单上的所有商品都可以进行自定义; if a single order_product line with a quantity > 1 could have different customizations for each item, you'd have to split it down even further... 如果一个数量> 1的order_product行可以为每个项目进行不同的自定义,那么您必须将其拆分得更远......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM