Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Two Tables Referencing Eachother
Old 12-15-2007, 09:51 PM Two Tables Referencing Eachother
Invisionary's Avatar
Novice Talker

Posts: 9
Name: Nerosuran Sieronodeski
Location: U.K.
Trades: 0
I have two tables in a DB.

users
  • ID
things
  • ID
  • rating
  • rated_by
The users will be rating the things and as such I want to keep track of what users rated what things and with what "rating".

What I originally had planned was to have the "rated_by" field be a delimited array string of user "IDs" and have a parallel delimited array string in the "rating" field of the associated "rating" that user gave that stuff.

Like so:

users
  • ID
    • 1
    • 2
    • 3
  • name
    • Adam
    • Bob
    • Charlie
things
  • ID
    • 1
    • 2
    • 3
  • name
    • apple
    • banana
    • chocolate
  • rating
    • 6.5 5.0 4.5
    • 2.5 4.0 8.0
    • 9.0 1.5 7.5
  • rated_by
    • 1 2 3
    • 2 3 1
    • 3 1 2
Example:
user "ID" 1 gave thing "ID" 2 a "rating" of 8.0

However, I've been told that using delimited strings to store information like this is bad form.

The users and things tables will change at randomly different rates. (as users join and things are added by the users)

This wouldn't normally be a problem except that I want to do things like take the average rating of a particular thing on the fly and more importantly sort the things table by highest average rating. Likewise, I want to be able to able to do a mySQL command that selects all the user "names" that rated a particular thing.

Is the only way to do this is to keep that special information (obtained and/or calculated from existing info) in it's own table column, or is there a better way to construct the two tables? If so, what's the better method?

Is there some syntax that is like the explode() command in PHP in mySQL?
__________________

Please login or register to view this content. Registration is FREE
Invisionary is offline
Reply With Quote
View Public Profile Visit Invisionary's homepage!
 
 
Register now for full access!
Old 12-15-2007, 10:44 PM Re: Two Tables Referencing Eachother
Extreme Talker

Posts: 238
Location: United States
Trades: 0
Quote:
Originally Posted by Invisionary View Post
things
  • ID
    • 1
    • 2
    • 3
  • name
    • apple
    • banana
    • chocolate
  • rating
    • 6.5 5.0 4.5
    • 2.5 4.0 8.0
    • 9.0 1.5 7.5
  • rated_by
    • 1 2 3
    • 2 3 1
    • 3 1 2
Example:
user "ID" 1 gave thing "ID" 2 a "rating" of 8.0
Quote:
Originally Posted by Invisionary View Post
Is the only way to do this is to keep that special information (obtained and/or calculated from existing info) in it's own table column, or is there a better way to construct the two tables? If so, what's the better method?
A much better solution in my opinion would be to create a third table, such as thing_ratings with fields: id, thing_id, rated_by, and rating. This way each rating has its own row. You won't have to mess with delimiters or serialization, which should make for cleaner code, as well as likely improve performance.

Quote:
Originally Posted by Invisionary View Post
This wouldn't normally be a problem except that I want to do things like take the average rating of a particular thing on the fly and more importantly sort the things table by highest average rating. Likewise, I want to be able to able to do a mySQL command that selects all the user "names" that rated a particular thing.
Using the third table, the queries would be on the simpler side compared to dealing with delimiters or unserializing data. I wrote a few below, but I didn't test them.

Average would be something like:
SELECT thing_id, AVG(rating) FROM thing_ratings GROUP BY thing_id

Order by average:
SELECT thing_id, AVG(rating) as rate FROM thing_ratings GROUP BY thing_id ORDER BY rate DESC

Users who rated an item (where $id is the id of the item)
SELECT users.name FROM thing_ratings JOIN users ON things_ratings.rated_by=users.id WHERE thing_id=$id
__________________
The interlocking pieces of web development: usability, performance, accessibility, and standards.

Last edited by frost; 12-15-2007 at 10:45 PM..
frost is offline
Reply With Quote
View Public Profile
 
Old 12-16-2007, 03:09 AM Re: Two Tables Referencing Eachother
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
Frost gave a very good answer, probably worthy of some reputation, but more importantly, this is really worth a lot of consideration.

For most people, relational database theory is a pretty foreign concept when they first approach it. What's really important at the end of the day is that modern databases are meant to be able to achieve the things you've said you need, and to do it very well using relational schemas, like Frost designed. Apart from what you'd like to do now, this approach will make it easy to do do plenty of other types of analysis that might be useful down the line.

Using a delimited list in one column in one row instead of multiple rows - often in a different table - won't make most analysis impossible, but will drastically slow everything down. That sounds counter-intuitive, especially if you've done procedural or object oriented development, but the way rdbms's work is very different.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 12-16-2007, 10:09 AM Re: Two Tables Referencing Eachother
Average Talker

Posts: 29
Name: Jean
Trades: 0
Hi,

+1 : a "join table" ("table de jointure" in french) is probably here the most logical and efficient way : a user can be associated with 0 to n things, and a thing with 0 to m users, rating being a property from this relation, not from a user or from a thing.
MarvinLeRouge is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Two Tables Referencing Eachother
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.28191 seconds with 12 queries