简体   繁体   English

SQL表设计减少冗余

[英]SQL table design to reduce redundancy

I have two designs in mind. 我有两种设计。 Wanted to check which one is more optimum as per you guys. 想检查一下哪个更适合您。

So I have three tables offer, offer_type and offer_type_filter. 因此,我有三个表offer,offer_type和offer_type_filter。

Original Design of tables 桌子的原始设计

offer 提供

 id                      int(10) unsigned    
 code                    varchar(48)         
 offer_type_id           int(10) unsigned 
 start_date              datetime            
 exp_date                datetime            
 value                   int(10)             
 updated                 timestamp           
 created                 datetime            

offer_type offer_type

 id                      int(10) unsigned         
 name                    varchar(48)              
 condition   varchar(512)             

offer_type_filter offer_type_filter

 id                      int(10) unsigned 
 filter_type             varchar(20)      
 filter_value            varchar(50)      
 offer_type_id   int(10) unsigned 

Now as you all may guess that offer has a type and filter specifies in what specific cases offer will apply. 现在,您可能都已经猜到要约具有类型,并且过滤器指定要在什么情况下应用要约。 If you are wondering then offer_type.condition is mainly for 20$ off on purchase of min. 如果您想知道,那么offer_type.condition主要是在购买min时有20 $的折扣。 300$. 300美元。 Offer_type_filter is to apply this offer only for say McDonalds. Offer_type_filter仅适用于说麦当劳的人。 Offer can exist without filters. 优惠可以不带过滤器而存在。

One prob with current design is that every time I create new offer, even though type is same I have to create a duplicate entry in offer_type and then use that type in offer_type_filter (using current type will mess up existing offers). 当前设计的一个问题是,即使类型相同,每次创建新报价时,我也必须在offer_type中创建一个重复条目,然后在offer_type_filter中使用该类型(使用当前类型会破坏现有报价)。

So in terms of database re-design it is quite obvious that offer_type must not exist in offer_type_filter so I am convinced it has to change to something like this 因此,就数据库重新设计而言,很明显,offer_type一定不能存在于offer_type_filter中,所以我确信它必须更改为类似这样的内容

Redesign (Doing away with offer_type_filter and creating new table filter. It's basically renaming to something more appropriate) 重新设计(取消offer_type_filter并创建新的表格过滤器。它基本上是重命名为更合适的名称)

Filter 过滤

id   int(10) unsigned 
filter_type  varchar(20)      
filter_value     varchar(50)      
filter_type_set_id   int(10) unsigned 

For other tables I am thinking of these two options 对于其他表,我正在考虑这两个选项

Option 1 (offer_type_filter from redesign + other tables same from original design) 选项1(重新设计的offer_type_filter +原始设计的其他表相同)

offer 提供

id   int(10) unsigned    
code     varchar(48)         
offer_type_filter_mapping_id     int(10) unsigned    

offer_type_filter_mapping offer_type_filter_mapping

id   int(10) unsigned 
filter_type_set_id   int(10) unsigned     > from Filter table
offer_type_id    int(10) unsigned    

If I choose first design then I will have redundant entries in offer_type_filter_mapping. 如果我选择第一个设计,那么在offer_type_filter_mapping中将有多余的条目。 For offers which don't have filters, offer_type_filter_mapping will have entries of offer_type_id with null as filter_type_set_id. 对于没有过滤器的商品,offer_type_filter_mapping将具有offer_type_id的条目,其中null为filter_type_set_id。 Also then for each type I create, I will have to put an entry in mapping table. 同样,对于我创建的每种类型,我都必须在映射表中放置一个条目。 So I don't like this aspect of design. 所以我不喜欢这方面的设计。

Option 2 (offer_type_filter from redesign + other tables same from original design) 选项2(重新设计的offer_type_filter +原始设计的其他表相同)

offer 提供

id   int(10) unsigned    
code     varchar(48)         
filter_type_set_id   int(10) unsigned    > from Filter table

I came to Option 2 only because in this case there is redundant filter_type_set_id for each offer and in my case offer table is huge 我之所以选择选项2,是因为在这种情况下,每个要约都有多余的filter_type_set_id,而就我而言,要约表很大

Wanted your critique as to which design do you think is the least painful. 希望您对您认为哪种设计痛苦最小的批评。 Frequent Usecases: Creating lots of offers with and without filters. 常见用例:创建很多带有或不带有过滤器的商品。 We already have close to 40-50 Offer types. 我们已经有接近40-50种优惠类型。 The types table is not able to cover all scenario so we do create new types 10 % of the times. 类型表无法涵盖所有​​情况,因此我们确实有10%的时间创建了新类型。

Also I use Spring and Hibernate so you can think from that perspective too what my design constraints would be. 我也使用Spring和Hibernate,所以您也可以从这个角度考虑我的设计约束。

PS You might even add that in mysql it is not convenient to generate two id's per table as in offer_type_filter but I am thinking about it. 附言:您甚至可以在mysql中添加它,就像在offer_type_filter中那样为每个表生成两个id并不方便,但我正在考虑这一点。 Prob use a dummy table for generation or use an externally generated id. 可能使用虚拟表进行生成或使用外部生成的ID。

I see it this way, one offer can have only one offer type_filter, so it makes a 1:N relationship 我这样看,一个要约只能有一个要约type_filter,因此建立了1:N关系

在此处输入图片说明

and offer will take the offer_type attributes that u had before. 和offer将采用您之前拥有的offer_type属性。

the cardinality is N:M 基数为N:M

在此处输入图片说明

EDIT: 编辑:
for example, if you have in offer_type_filter. 例如,如果您在offer_type_filter中。

offer_type_filter_id = 1 and it's 30% off.
offer_type_filter_id = 2 and it's 10% off.
offer_type_filter_id = 3 and it's 0% off.
...
etc

and in your offer table you can have: 在报价表中,您可以拥有:

offer_id=1 and offer_filter_id=1 //this mean that product 1 has 30% off
offer_id=2 and offer_filter_id=1 //this mean that product 2 has 30% off
offer_id=3 and offer_filter_id=2 //this mean that product 2 has 10% off
offer_id=4 and offer_filter_id=3 //this mean that product 2 has 0% off

...

etc

If your cardinality is one offer can be have only one Offer type, is the first design. 如果您的基数是一个要约,则只能有一个要约类型,是第一个设计。

if your cardinality is one offer can have multiple discounts and the same discount for multiple products, I recommend the second design 如果您的基数是一个优惠,可以有多个折扣,并且多个产品具有相同的折扣,那么我建议您使用第二种设计

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

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