数据库架构和Sql获得5星评级

时间:2014-07-03 01:10:02

标签: mysql sql database database-design

要求:

1. All "Users" have 5 fixed set of same "Services" which they are supposed to rate.
2. Given the userId, display all the Services and their corresponding "Ratings". 
3. Rating would be from a scale of 1 - 5 only
4. Ratings can be updated anytime by the user for any of his Service.

我不是SQL / DB专家,我在堆栈中有这个问题,我从Raj那里得到了答案ER Model of User Ratings

这是我最终确定的ER(打开以进行更正)。enter image description here

我的获取查询:获取userId的所有服务及其对应的信息(比如userId = 1)

SELECT service.service_id, service.name, usr.ratings 
FROM services service, 
     ratings ratings, 
     userservices_ratings usr,
     user user,
     user_services us
where 
    us.userid = user.uid and 
    us.serviceid = service.serviceid and
    usr.usid = us.id and
    usr.rid = ratings.rid and
    user.id = 1
  1. 只有在用户对服务进行评级后,上述查询才会返回所有服务和评级。对于未评级的服务,不要将其设置为结果集。所以我假设我应该预先填充未分级的服务,默认值为0?

  2. 所有用户都会有固定的5个服务,每当创建用户时,我是否会编写一个触发器来创建他进入User_Services表的条目?

  3. 如果例如Userid = 1,对于serviceId = 1,rates = 3,创建/更新查询将如何显示?

  4. 架构中有任何缺陷吗?

1 个答案:

答案 0 :(得分:1)

您可以在此处使用标准格式的评分方案https://schema.org/Rating 您可以从此

中选择所需的列