Вопрос

We are building a web database system and we need to allow some products to be made of other products. For example combining 2 or more products as a new product. We are using CakePhp and MySQL.

Here is the data structure diagram of our database: https://www.dropbox.com/s/ksv22rt45uv69k9/Data%20Structure%20Diagram.png

Would we need to have self referencing products table or create a new table?

Это было полезно?

Решение

You can do either. There are pros and cons to both. Either way you will need a cross reference table.

The cross reference table can refer itself.

products in item

+---------------------+----------------------------+------------+
| product_identifier  |   product_identifier_child |   quantity |
+---------------------+----------------------------+------------+
|          1          |              1             |    1       |
|          2          |              1             |    1       |
|          2          |              2             |    2       |
|          3          |              2             |    1       |
+---------------------+----------------------------+------------+

On the bright side, this method means you only have one table of data and only one new cross reference table, and you can add new products as you see fit (along with multiple of the same products, say, with a gift basket). On the downside, your table will be trying to do two different things at the same time. Products that have other products in them may not have a model number. Also, how will you determine whether to check the inventory table? Are you going to have inventory for products that are made out of products, or would you sooner check stock on individual products in order to see if your combo products are in stock? The latter is much more flexible, and you can still reserve inventory this way. It just allows all of your inventory to be in the same unit scale in your inventory table.

To add more flexibility, you can create another table, base products, which has values only the building block products are going to have.

base products

+--------------------------+----------+--------------+
|  base product identifier |  brand   | model number |
+--------------------------+----------+--------------+

You could then attach your inventories to your base products table, and your cross reference table would be products to base products.

The negative here is that now instead of two tables, you have three. However, I am a fan of more tables with fewer columns thanks to increased flexibility. Even if the table tasks are not completely different, letting each table specialize completely can make things a lot easier.

There are numerous ways to go but optimal situation is the one that requires no data duplication and no NULL values. Without stressing yourself out about getting all the way there, try to keep your NULL values out of indexed columns and make sure your name values are only showing up in one place.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top