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).
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
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?
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?
How would the create/update query look if for example a Userid = 1, rates = 3 for serviceId = 1?
Any flaw in the schema?