简体   繁体   English

如何设计这个MySQL结构?

[英]How to design this MySQL Structure?

Ok so here is my problem. 好的,这是我的问题。

I have a selection of items, their names are; 我有一些项目,他们的名字是;

UAT-1 UAT-2 UAT-3 UAT-1 UAT-2 UAT-3

Each item has 'sections', however not every item has the same sections as the previous. 每个项目都有“部分”,但并非每个项目都与前一个项目具有相同的部分。 For example, UAT-1 will have the sections - 'Red', 'Green', 'Blue'. 例如,UAT-1将包含“红色”,“绿色”,“蓝色”部分。

UAT-2 will have the seconds - 'Red', 'Brown', 'Purple' UAT-2将有秒 - '红色','棕色','紫色'

UAT-3 will ahve the sections - 'Gold', 'Silver', 'Red' UAT-3将会出现这些部分 - “黄金”,“白银”,“红色”

What is the best way to design MySQL table(s) for this problem? 为这个问题设计MySQL表的最佳方法是什么? I was thinking something along the lines of; 我正在思考一些事情;

items 项目

+-----+-----------+
|id   |item_name  |
|-----|-----------|
|1    |UAT-1      |
|2    |UAT-2      |
+-----+-----------+

sections 部分

+-------------------------------+
|id   |section_name |belongs_to |
|-----|-------------|-----------|
|1    |Red          |1          |
|2    |Green        |1          |
|3    |Blue         |2          |
|4    |Purple       |1          |
|     |             |           |
|     |             |           |
+-------------------------------+

However how would I deal with a case where a two sections belong to one item? 但是,我如何处理两个部分属于一个项目的情况?

Thanks! 谢谢!

One approach would be to add an intermediate table. 一种方法是添加中间表。 This lets your "items" and "sections" live as distinct records. 这使您的“项目”和“部分”可以作为不同的记录存在。

Items: 项目:

+-----+-----------+
|id   |item_name  |
|-----|-----------|
|1    |UAT-1      |
|2    |UAT-2      |
+-----+-----------+

Sections: 部分:

+--------------------+
|id   |section_name  |
|-----|--------------|
|1    |Red           |
|2    |Green         |
|3    |Blue          |
|4    |Purple        |
+--------------------+

ItemsSections: ItemsSections:

+---------------+
|item  |section |
|---------------|
|1     |1       |
|1     |2       |
|1     |4       |
|2     |3       |
+---------------+

You would get the sections for a specific item with a query like: 您将获得具有以下查询的特定项目的部分:

SELECT
    Sections.*
FROM
    Sections
    RIGHT JOIN ItemsSections
        ON Sections.id = ItemsSections.Section
WHERE
    ItemsSections.item = 1

For an n:m relation you would usually have two base tables and one relation table: 对于n:m关系,通常有两个基表和一个关系表:

items
+-----+-----------+
|id   |item_name  |
|-----|-----------|
|1    |UAT-1      |
|2    |UAT-2      |
|3    |UAT-3      |
+-----+-----------+

sections
+-------------------+
|id   |section_name |
|-----|-------------|
|1    |Red          |
|2    |Green        |
|3    |Blue         |
|4    |Brown        |
|5    |Purple       |
|6    |Gold         |
|7    |Silver       |
+-------------------

item_sections
+--------------------+
|id_item |id_section |
|--------|-----------|
|1       |1          |
|1       |2          |
|1       |3          |
|2       |1          |
|2       |4          |
|2       |5          |
|3       |1          |
|3       |6          |
|3       |7          |
+--------------------+

UAT-1 will have the sections - 'Red', 'Green', 'Blue'. UAT-1将包含“红色”,“绿色”,“蓝色”部分。

UAT-2 will have the seconds - 'Red', 'Brown', 'Purple' UAT-2将有秒 - '红色','棕色','紫色'

UAT-3 will ahve the sections - 'Gold', 'Silver', 'Red' UAT-3将会出现这些部分 - “黄金”,“白银”,“红色”

+-----+-----------+  +-----+--------------+
|id   |item_name  |  |id   |section_name  |
|-----|-----------|  |-----|--------------|
|1    |UAT-1      |  |1    |Red           |
|2    |UAT-2      |  |2    |Green         |
|3    |UAT-3      |  |3    |Blue          |
+-----+-----------+  |4    |Brown         |
                     |5    |Purple        |
                     |6    |Gold          |
                     |7    |Silver        |
                     +-----+--------------+

+-------------------------------+
|id   |section_id   |item_id    |
|-----|-------------|-----------|
|1    |1            |1          |
|2    |2            |1          |
|3    |3            |1          |
|4    |1            |2          |
|5    |4            |2          |
|6    |5            |2          |
|7    |6            |3          |
|8    |7            |3          |
|9    |1            |3          |
+-------------------------------+

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

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