2

Requirements:

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.

I am no SQL/DB expert and I had this question up in stacks for which I got answer from Raj here ER Model of User Ratings

This is the ER I finalized (open for correction).enter image description here

My Fetch Query: Get all the services and their corresponding for a userId (say 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. The above query will return all the services and ratings, only if User has rated the service. For services which are unrated don't make it to result set. So I assume I should pre-populate unrated services with a default value say 0?

  2. All users will have fixed set of 5 services all the time, do I write a trigger to create his entry into User_Services table whenever a User is created?

  3. How would the create/update query look if for example a Userid = 1, rates = 3 for serviceId = 1?

  4. Any flaw in the schema?

3
  • Plus 1 for the drawing. What you drew is pretty good also. The schema seems appropriate. Regarding question 3, you will be inserting records into UserServicesRatings. For question 1, assume nothing. Consult the spec you were given and if it's not addressed, ask the person for whom you are doing this.
    – Dan Bracuk
    Commented Jul 3, 2014 at 1:21
  • Re 1 Why would you "pre-populate"? If there aren't any rated services, there aren't any rated services. Your "My Fetch Query" English is ungrammatical and ambiguous; per DanBracuk's comment you need clarification, to either "rated services and ratings and ... for a user" or "all services and ... for a user and if rated their ratings otherwise ??" or whatever.
    – philipxy
    Commented Jul 3, 2014 at 9:40
  • Yes this is what I want "All services for a user, AND IF rated their ratings otherwise" my fetch query doesn't do that :(. We dont have any hard and fast requirements Commented Jul 3, 2014 at 14:06

1 Answer 1

3

You can standard format of Rating Schema here https://schema.org/Rating You can pick required columns from this

Not the answer you're looking for? Browse other questions tagged or ask your own question.