O
O
Oleg2021-08-04 15:55:04
MySQL
Oleg, 2021-08-04 15:55:04

How to properly design a database relationship between two products?

Hello!
There is a product (linen), the product has size options (double, single).
Task #1 ( DONE !): split the product into options (create a separate product for each option with its own URL). As a result, we have two products:
- ID 10 Double bed linen
- ID 15 Single

bed linen In fact, they are different products.

Question: how can I design a table for the relationships between these products. To have each of these products refer to each other?
I see it like this:

linking_options (table)
product_id | linking_product_id
--------------------------------------
10 | 15
15 | 10

In this case, we link two goods. But, if you need to link 3 or 4 products together? Then add additional columns (stupid as for me)....

Tell me, please, how best to implement the table.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Akina, 2021-08-04
@Akina

Question: how can I design a table for the relationships between these products. To have each of these products refer to each other?

CREATE TABLE groups_of_goods (
    group_id BIGINT UNSIGNED NOT NULL,
    product_id BIGINT UNSIGNED NOT NULL,
    PRIMARY KEY (group_id, product_id),
    FOREIGN KEY fk_product (product_id) REFERENCES product (product_id)
);

Accordingly, if your shorts are in the same group and refer to each other, then there will be 2 entries in the table - (123, 10) and (123, 15).
By the way, such a scheme also provides the principle of "vassal of my vassal ...". Those. "crimson jacket" can refer to "crimson trousers" (via group 456) and "striped jacket" (via group 789), but the latter two will not refer to each other, because they are different groups.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question