简体   繁体   English

将筛选条件保存在SQL数据库上

[英]Save filter criteria on a SQL database

I have a Products table as follows: 我有一个产品表,如下所示:

create table dbo.Product ( 
  Id int not null
  Name nvarchar (80) not null,
  Price decimal not null
)

I am creating Baskets (lists of products) as follows: 我正在创建购物篮(产品列表),如下所示:

create table dbo.Baskets ( 
  Id int not null
  Name nvarchar (80) not null
)

create table dbo.BasketProducts ( 
  BasketId int not null,
  ProductId int not null,
)

A basket is created based on a Search Criteria using parameters: 根据搜索条件使用以下参数创建购物篮:

  1. MinimumPrice; MinimumPrice;
  2. MaximumPrice; MaximumPrice;
  3. Categories (can be zero to many); 类别(可以是零到很多);
  4. MinimumWarrantyPeriod MinimumWarrantyPeriod

I need to save these parameters so later I know how the basket was created. 我需要保存这些参数,以便以后知道篮子是如何创建的。

In the future I will have more parameters so I see 2 options: 将来我将有更多参数,所以我看到2个选项:

  1. Add MinimumPrice, MaximumPrice and MinimumWarrantyPeriod as columns to Basket table and add a BasketCategories and Categories tables to relate a Basket to Categories. 将MinimumPrice,MaximumPrice和MinimumWarrantyPeriod作为列添加到Basket表中,并添加basketCategories和Categories表以将Basket与Category相关联。

  2. Create a more flexible design using a Parameters table: 使用参数表创建更灵活的设计:

    create table dbo.BasketParameters ( BasketId int not null, ParameterTypeId int not null, Value nvarchar (400) not null ) 创建表dbo.BasketParameters(BasketId int不为null,ParameterTypeId int不为null,值nvarchar(400)不为null)

    create table dbo.ParameterType ( Id int not null Name nvarchar (80) not null ) 创建表dbo.ParameterType(Id int不为null名称nvarchar(80)不为null)

Parameter types are MinimumPrice, MaximumPrice, Categories, MinimumWarrantyPeriod, etc. 参数类型为MinimumPrice,MaximumPrice,类别,MinimumWarrantyPeriod等。

So for each Basket I have a list of BasketParameters, all different, having each on value. 因此,对于每个Basket,我都有一个BasketParameters列表,所有这些都各不相同,每个都有值。 Later if I need for parameter types I add them to the ParameterType table ... 以后,如果我需要参数类型,可以将它们添加到ParameterType表中。

The application will be responsible for using each Basket Parameters to build the Basket ... I will have, for example, a Categories table but will be decoupled from the BasketParameters. 该应用程序将负责使用每个购物篮参数来构建购物篮...例如,我将拥有一个Categorys表,但将从BasketParameters中分离出来。

Does this make sense? 这有意义吗? Which approach would you use? 您将使用哪种方法?

Your first option is superior (especially since you are using a relational data store. Ie SQL Server), since it is properly referential. 您的第一个选择是高级的(特别是因为您正在使用关系数据存储。即SQL Server),因为它是正确的引用。 This will be much easier to maintain and query as well as far more performant. 这将更易于维护和查询,并且性能更高。

Your second solution is equivalent to an EVA table: https://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80%93value_model 您的第二个解决方案等效于EVA表: https : //en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80%93value_model

Which are usually a terrible idea (and if you need that type of flexibility you should probably use a Document Database or other NoSQL Solution instead). 通常这是一个糟糕的主意(如果您需要这种灵活性,则应该改用文档数据库或其他NoSQL解决方案)。 The only benefit to this is if you need to add/remove attributes regularly or based on other criteria. 这样做的唯一好处是,如果您需要定期或基于其他条件添加/删除属性。

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

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