문제

I'm implementing a shop using the Play! framework. Play uses Ebean as ORM. Right now I'm creating a rating system where a user may rate products. A user is allowed to rate a product only once. I was just wondering about best practice how to design the RATING table.

  1. Use user_id and product_id as primary key or
  2. Use a seperate id as primary key an set a unique constraint on the foreign keys user_id and product_id

What are the pros/cons?

User
---------------------
id (PK)
name
...
---------------------

Product
---------------------
id (PK)
name
...
---------------------

Rating
---------------------
user_id (FK)
product_id (FK)
rating
comment
PK(user_id, product_id)

OR:

Rating
---------------------
id (PK)
user_id (FK)
product_id (FK)
rating
comment
UNIQUE (user_id, product_id)

Thank you,

Nick

도움이 되었습니까?

해결책

The first you listed because it is simpler. Both absolve the same task so why complicate your life? Read this: KISS

다른 팁

I think is better to use the second you listed because in the future, if you would like to reference the "rating" you wouldn't need to create two FKs in the new table to reference it. In that case you will only need to use the "rating" id. That's good if you care about memory economy and, in my particular opinion, this practice makes the understanding of relations easier.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top