简体   繁体   English

在数据库中添加产品投票

[英]Add Vote of Product in database

On a database I am saving Baskets bought by Users: 在数据库上,我保存了用户购买的购物篮:

USERS
Id
Name

BASKETS
Id
UserId
Name

Each basket is a selection of predefined Packages: 每个购物篮都是预定义软件包的选择:

BASKET_PACKAGES
BasketId   
PackageId

PACKAGES
PackageId
Name

Each package is a selection of predefined Products: 每个包装都是预定义产品的选择:

PACKAGE_PRODUCTS
PackageId
ProductId

PRODUCTS
Id
Name

In each basket I need to define a VOTE value for each product. 在每个购物篮中,我需要为每个产品定义一个VOTE值。

Each user might vote a product differently in different occasions, eg, in different baskets. 每个用户在不同场合(例如在不同的购物篮中)可能会对产品进行不同的投票。

What would be the best way to do this? 最好的方法是什么?

Create entity like below 如下创建实体

BasketVote
BasketVoteId PK
BasketId    FK
ProductId   FK
UserId      FK
Vote

You can also use PackageProductId instead of ProductId, its all based on your requirement. 您也可以根据需要使用PackageProductId而不是ProductId。

I would create the table: 我将创建表:

create table basket_product_vote (
  basket_id int not null,
  product_id int not null,
  voted_on timestamp not null,
  vote_value int not null,
  constraint fk1_basket foreign key (basket_id) 
    references basket (id),
  constraint fk2_product foreign key (product_id) 
    references product (id)
);

The user_id could be included, but I decided not to add it by default since in principle it can be obtained using the foreign key. 可以包含user_id ,但是我决定默认情况下不添加它,因为原则上可以使用外键获取。

Also, you will probably want to create extra indexes to speed up searches. 另外,您可能需要创建额外的索引以加快搜索速度。

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

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