简体   繁体   English

数据库结构,多个实体的一个大实体

[英]Database structure, one big entity for multiple entities

Suppose that I have a store-website where user can leave comments about any product. 假设我有一个商店网站,用户可以在其中留下关于任何产品的评论。

Suppose that I have tables(entities) in my website database: let it be 'Shoes', 'Hats' and 'Skates'. 假设我的网站数据库中有表(实体):让它成为'Shoes','Hats'和'Skates'。 I don't want to create separate "comments" table for every entity (like 'shoes_comments', 'hats_comments', 'skates_comments'). 我不想为每个实体创建单独的“评论”表(例如'shoes_comments','hats_comments','skates_comments')。 My idea is to somehow store all the comments in one big table. 我的想法是以某种方式将所有评论存储在一个大表中。

One way to do this, that I thought of, is to create a table: 我想到的一种方法是创建一个表:

table (comments): 
ID (int, Primary Key), 
comment (text),  
Product_id (int), 
isSkates (boolean), 
isShoes (boolean), 
isHats (boolean) 

and like flag for every entity that could have comments. 并且像每个可能有评论的实体的标志。

Then when I want to get comments for some product the SELECT query would look like: 然后,当我想获得某些产品的注释时,SELECT查询将如下所示:

SELECT comment 
FROM comments, ___SOMETABLE___
WHERE ____SOMEFLAG____ = TRUE 
  AND ___SOMETABLE___.ID = comments.Product_id

Is this an efficient way to implement database for needed functionality? 这是为所需功能实现数据库的有效方法吗? What other ways i can do this?> 还有什么方法可以做到这一点?>

我建议您为注释创建一个表,并在注释表中使用其他表的外键。

Sorry, this feels odd. 对不起,这感觉很奇怪。

Do you indeed have one separate table for each product type? 您确实为每种产品类型都有一个单独的表吗? Don't they have common fields (eg name, description, price, product image, etc.)? 他们不是有共同的领域(例如名称,描述,价格,产品形象等)吗?

My recommendation as for tables: product for common fields, comments with foreign key to product but no hasX columns, hat with only the fields that are specific to the hat product line. 我建议,作为表: product共同领域, comments与国外关键product ,但没有hasX列, hat ,只有特定于帽子生产线等领域。 The primary key in hat is either the product PK or an individual unique value (then you'd need an extra field for the foreign key to product ). 在该主键hat或者是product PK或个人独特的价值(然后你需要为外键的额外字段product )。

The "normalized" way to do this is to add one more entity (say, "Product") that groups all characteristics common to shoes, hats and skates (including comments) 执行此操作的“标准化”方法是添加一个实体(例如“产品”),该实体将鞋子,帽子和冰鞋共有的所有特征(包括注释)分组

              +-- 0..1 [Shoe]
              |
[Product] 1 --+-- 0..1 [Hat]
    1         |
    |         +-- 0..1 [Skate]
    *
[Comment]

Besides performance considerations, the drawback here is that there is nothing in the data model preventing a row in Product to be referenced both by a row in Shoe and one in Hat. 除了性能方面的考虑之外,这里的缺点是数据模型中没有任何内容可以防止Product中的行被Shoe中的行和Hat中的行引用。

There are other alternatives too (each with perks & flaws) - you might want to read something about "jpa inheritance strategies" - you'll find java-specific articles that discuss your same issue (just ignore the java babbling and read the rest) 还有其他选择(每个都有额外的好处和缺点) - 您可能想要阅读有关“jpa继承策略”的内容 - 您将找到讨论同一问题的特定于Java的文章(只需忽略java babbling并阅读其余内容)

Personally, I often end up using a single table for all entities in a hierarchy (shoes, hats and skates in our case) and sacrificing constraints on the altar of performance and simplicity (eg: not null in a field that is mandatory for shoes but not for hats and skates). 就个人而言,我经常最终为层次结构中的所有实体使用单个表(在我们的案例中使用鞋子,帽子和冰鞋),并牺牲对性能和简单性的祭坛的限制(例如:在鞋子必需的字段中不为空但是不适用于帽子和冰鞋)。

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

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