简体   繁体   English

我什么时候应该为字段创建一个单独的表?

[英]when should i create a separate table for fields?

Let's say i have this SQL table:假设我有这个 SQL 表:

CREATE TABLE `article` (
 `article_id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(255) NOT NULL,
 `family` varchar(255) NOT NULL,
 `sub_family` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
 `price` float NOT NULL,
 PRIMARY KEY (`article_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Here every family of articles has one or multiple sub_family这里每个文章family都有一个或多个sub_family

should i create another table that has all possible combinations of family and sub_family and refer to it using a foreing key ?我应该创建另一个包含familysub_family家庭所有可能组合的表并使用foreing key引用它吗?


Data example:数据示例:

在此处输入图像描述

I read your question and comments as well.我也阅读了您的问题和评论。 I think you can create two different tables that'll for sure reduce the data redundancy and complexity.我认为您可以创建两个不同的表,这肯定会减少数据冗余和复杂性。

You can create Family as a parent table: In Family table you can declare a unique key(which you can use further for foreign key referencing) for each Family and sub-Family pair for example:您可以将 Family 创建为父表:在 Family 表中,您可以为每个 Family 和子系列对声明一个唯一键(您可以进一步将其用于外键引用),例如:

Family:家庭:

ID   | Family_name | Subfamily
1       Beverage     Hot Drinks
2       Beverage     cold Drinks
3       main course  Pasta

And you can create another table for Articles which will be connected with parent via foreign key reference ID.您可以为 Articles 创建另一个表,该表将通过外键引用 ID 与父级连接。

Article:文章:

  ID  |  Family | Name       | Price
  1       2      Milk Shake    100.00
  2       1      Coffee        200.00

You can try something like this.你可以尝试这样的事情。 I hope you find this easy to understand.我希望你觉得这很容易理解。

在此处输入图像描述

If you want to fix redundancy completely, please try the following table:如果您想彻底修复冗余,请尝试下表:

create table `family` (
 `family_id` int(11) NOT NULL AUTO_INCREMENT,
 `family` varchar(255) NOT NULL,
 `parent_id` int(11) NOT NULL,
 PRIMARY KEY (`family_id`),
 index idx_family_sub (`family`, `parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `article` (
 `article_id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(255) NOT NULL,
 `family_id` int(11) not null,
 `price` float NOT NULL,
 PRIMARY KEY (`article_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

The data in there 2 table may look like: 2 表中的数据可能如下所示:

mysql> select * from family order by family_id;
+-----------+-------------+-----------+
| family_id | family      | parent_id |
+-----------+-------------+-----------+
|         1 | Main Course |         0 |
|         2 | Beverage    |         0 |
|         3 | Pasta       |         1 |
|         4 | Hot Drinks  |         2 |
|         5 | Cold Drinks |         2 |
+-----------+-------------+-----------+
5 rows in set (0.00 sec)

mysql> select * from article;
+------------+-----------+-----------+-------+
| article_id | name      | family_id | price |
+------------+-----------+-----------+-------+
|          1 | Spaghetti |         3 |   600 |
|          2 | Penne     |         3 |   500 |
|          3 | Coffee    |         4 |   100 |
|          4 | Penne     |         5 |   150 |
+------------+-----------+-----------+-------+
4 rows in set (0.00 sec)

The query to get all data like data example is:获取所有数据(如数据示例)的查询是:

mysql> select
    ->        a.article_id, a.name, f2.family as family, f.family as sub_family, a.price
    -> from `article2` a
    ->     join family2 f on a.family_id = f.family_id
    ->     join family2 as f2 on f.parent_id = f2.family_id;
+------------+-----------+-------------+-------------+-------+
| article_id | name      | family      | sub_family  | price |
+------------+-----------+-------------+-------------+-------+
|          1 | Spaghetti | Main Course | Pasta       |   600 |
|          2 | Penne     | Main Course | Pasta       |   500 |
|          3 | Coffee    | Beverage    | Hot Drinks  |   100 |
|          4 | Penne     | Beverage    | Cold Drinks |   150 |
+------------+-----------+-------------+-------------+-------+
4 rows in set (0.00 sec)

From my perspective, this table is used to save food menu data, and it should be very small.在我看来,这张表是用来保存食物菜单数据的,应该很小。 I think it's OK to create a dedicated table to save family and sub_family.我认为创建一个专门的表来保存family和sub_family是可以的。

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

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