# Database structure

**URL:** <https://www.ruby-forum.com/t/database-structure/69220>\
**Category:** Rails\
**Created:** [September 19, 2006, 2:06pm UTC](https://www.ruby-forum.com/t/database-structure/69220 "2006-09-19T14:06:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [September 19, 2006, 2:06pm UTC](https://www.ruby-forum.com/t/database-structure/69220/1 "2006-09-19T14:06:22Z")

</div>

Database structure question.

I’ve already got this sorted but I just want to check that there isn’t a  
better alternative.

If I have 5 fixed ratings per review as columns in the table of reviews  
and then 5 fixed ratings (an average of all the reviews) as columns in  
the table of restaurants, is this alright?

Or should I make a separate ratings table with 5 fields. In my mind  
there is little point making a separate table for ratings if there would  
only ever at most be one row belonging to a review. However it would  
mean less columns in the reviews and restaurants tables. Thoughts?

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [September 19, 2006, 2:52pm UTC](https://www.ruby-forum.com/t/database-structure/69220/2 "2006-09-19T14:52:47Z")

</div>

If there will always be five columns then it is more correct to just add  
those on to the restaurants table. You could make another table for the  
ratings, but it would add overhead and serve no purpose other than  
reducing the number of columns in your main table.

You’re talking about reviews though? If there will ever be more than one  
review, then you should make that a separate table.

Jason

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [September 19, 2006, 3:22pm UTC](https://www.ruby-forum.com/t/database-structure/69220/3 "2006-09-19T15:22:21Z")

</div>

Jason N. wrote:

> If there will always be five columns then it is more correct to just add  
> those on to the restaurants table. You could make another table for the  
> ratings, but it would add overhead and serve no purpose other than  
> reducing the number of columns in your main table.
> 
> You’re talking about reviews though? If there will ever be more than one  
> review, then you should make that a separate table.
> 
> Jason

Thanks this is what I thought, so it is good to hear.

Yeah I already have a table for the actual reviews themselves.

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [September 19, 2006, 4:36pm UTC](https://www.ruby-forum.com/t/database-structure/69220/4 "2006-09-19T16:36:30Z")

</div>

Richard wrote:

> Database structure question.
> 
> I’ve already got this sorted but I just want to check that there isn’t a  
> better alternative.
> 
> If I have 5 fixed ratings per review as columns in the table of reviews  
> and then 5 fixed ratings (an average of all the reviews) as columns in  
> the table of restaurants, is this alright?
> 
> Or should I make a separate ratings table with 5 fields. In my mind  
> there is little point making a separate table for ratings if there would  
> only ever at most be one row belonging to a review. However it would  
> mean less columns in the reviews and restaurants tables. Thoughts?

Just make sure that your average rating never gets out of step with the  
ones stored on the review table. An alternative would be to get the  
average from the reviews table with some SQL that uses the avg function.  
That way your average will always be accurate.

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [September 19, 2006, 4:45pm UTC](https://www.ruby-forum.com/t/database-structure/69220/5 "2006-09-19T16:45:21Z")

</div>

Jonathan T. wrote:

> Richard wrote:
> 
> > Database structure question.
> > 
> > I’ve already got this sorted but I just want to check that there isn’t a  
> > better alternative.
> > 
> > If I have 5 fixed ratings per review as columns in the table of reviews  
> > and then 5 fixed ratings (an average of all the reviews) as columns in  
> > the table of restaurants, is this alright?
> > 
> > Or should I make a separate ratings table with 5 fields. In my mind  
> > there is little point making a separate table for ratings if there would  
> > only ever at most be one row belonging to a review. However it would  
> > mean less columns in the reviews and restaurants tables. Thoughts?
> 
> Just make sure that your average rating never gets out of step with the  
> ones stored on the review table. An alternative would be to get the  
> average from the reviews table with some SQL that uses the avg function.  
> That way your average will always be accurate.

Well everytime someone submits a review it calculates and updates the  
average, so that should be ok right?
