0

When creating a many to many table, relational database. If for example you are enabling users to scrape images form around the web and tag them. Would it be better to:

  1. Check to see if the image is already in the database and if it is, create a link in a relational table and if it is not create a new image.

  2. Create a unique instance of the image for every user and when looking to display the most popular images SELECT AND ORDER BY the image with the most duplicates

I hope this makes sense. Thanks in advance for you help.

1 Answer 1

1

I assume you have something equivalent to a USERS table and a PICTURES table. Also a table to break up the many to many relationship. U2P I will call it.

The option you listed as option 1 would seem to be the preferred way. Check to see if the picture is in the DB, if it is get primary key from PICTURES corresponding to it. If not, put the picture in the PICTURES table.

Regardless of if it is a new image, or one that is already in there, you will insert the event into the U2P table. This would reference the USERS primary key and the PICTURES primary key corresponding to the event. You would also record other data such as time etc...

2
  • Thanks NR, i was just thinking about when displaying popular images, if each user can give the image a title, when displaying the image in a 'most popular' list which title do you use. I was just looking to gather opinions really, finding out some "unknown unknowns" if you will. Thanks again for your help. Dec 12, 2012 at 14:03
  • you have some choices then, the easiest will be to put the title column in the event table. To get the most popular it is as simple as a select count(*) from the event tables where the picture pk matches. To get them all at the same time you can use a JOIN and GROUP BY to get them all in a single query, sorted however you like. There are still options for saving the title. If multiple users call it different things for example, which will you use? This may require more thought. Easiest may be to use the first title given. Dec 12, 2012 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.