Jump to content

Recommended Posts

Assuming you have two tables, photo and user, and each has an id  (photo_id, user_id)  as the primary key, you put a table between the two.  Let's assume you call this table "photorating"

 

 

photorating
--------------
photorating_id (primary key, autoincrement)
users_id  (fk to users table)
photo_id (fk to photo table)
rating tinyint
created_on timestamp

 

You stick a row into this table for a rating.  Add a unique index on user_id, photo_id and it becomes impossible for a user to have more than one rating row in the table.  You have a variety of options for how you want to deal with the rating ... you can look up the rating, or simply try the update and handle the failure/exception that will occur due to the unique index.

 

 

Link to comment
https://forums.phpfreaks.com/topic/230887-rating-photos/#findComment-1188540
Share on other sites

It was just shorthand for me pointing out that the datatype and name should match the name of the primary key in each of the associated tables.  There are other benefits to defining a true foreign key constraint, but with mysql many people use the default myisam engine which has no support for constraints or foreign keys.

 

Often people will do this:

 

mytable

id (primary key)

 

 

anothertable

-----------------

id

mytable_id  (foreign key to mytable)

 

 

So the actual name doesn't have to be the same in each table.  Whatever name you use for it, the important thing is that the datatype of the column should be exactly the same.  Since you will also be joining on that table, it's important to have a non-unique index on it as well.

 

 

Link to comment
https://forums.phpfreaks.com/topic/230887-rating-photos/#findComment-1188753
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.