Frage

Ok so we have Table 1 (T1) and Table 2 (T2)

Structure:

T1 (main table):
ID (Auto-Increment)
Name
Properties

T2 (properties table):
ID
Property Name

Let's say table 1 has two entries. Table 2 has 4 properties. What i want to do is set the properties of entry 1 in table 1 to map to property 1, 2 and 3 in table 2. For entry 2 in table 1, i want to map it to property 2, 3 and 4 in table 2.

For example, let's say we have two products, a BALL and a CUBE. Those would be listed in table 1. Now let's say table 2 holds all available COLORS.

Table 2:
ID COLOR
1  RED
2  BLUE
3  GREEN
4  YELLOW

Table 1:
ID NAME AVAILABLE_COLORS
1  BALL (TABLE 2 ENTRY 1, TABLE 2 ENTRY 2, TABLE 2 ENTRY 3, in other words RED BLUE GREEN)
2  CUBE (TABLE 2 ENTRY 2, TABLE 2 ENTRY 3, TABLE 2 ENTRY 4, in other words BLUE GREEN YELLOW)

How would i get the available colors to point to the colors table? Basically i want to make sure if i change a property (say color red->black), it gets automatically changed for all table 1 entries.

usually if i would only need to assign one color to one item, and that color cannot be used in another item, i would simply add a column in table 2 with "belongs to" or something like that, and this way i can set multiple colors to match the same entry in table 1, however, i can only assign a color to one entry that way... i cannot assign per say red to both ball and cube.

Anyone got any idea?

War es hilfreich?

Lösung

What I usually do is create a third table to track which products are available in which colors. Let's say a catalog table. All it stores is an primary ID and the ID of products and the ID of colors.

Table 3:  Catalog
ID ColorsID ProductsID
1 1 1  // row one connects with colors and products and this is a red ball 
2 2 1  // blue ball
3 1 2  // red cube
4 4 2  // yellow cube

Doing it this way you can connect as many attributes to products as you need. It requires more work to keep track of your catalog but makes things much easier in the long run. Remove the available colors from products and only store the product ID and product name in that table. Any else that is standard like description that will not necessarily change with color. This way if you change your available colors from 3 to say 10 you do not have to add new fields to track. You can also take the catalog and add extra fields like active or in-stock that tracks if product is currently available in that color. In catalog you can also store inventory numbers. What if your price is different based on color? White balls are cheaper? All good reasons for normal the data.

Normalization is about making sure each table has the only minimal fields and to get rid of dependencies. View this accepted answer for a good explanation of normalization on the theory level. Normalization in MYSQL

You can then use JOINS to connect them all to loop through the catalog table.

SELECT Colors.ColorID as Color, Products.ProductID as Product
FROM Catalog
    INNER JOIN Colors
        ON Catalog.ColorsID = Colors.ColorID
    INNER JOIN Products 
        ON Catalog.ProductsID = Products.ProductID

More information on JOINS can be found here - http://en.wikipedia.org/wiki/Join_%28SQL%29

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top