veksen
veksen

Reputation: 7003

MySQL best approach to having one table tied to multiple tables?

I'm not too sure how to word my question, but here it is.

Let's say I have multiple tables, dogs, cats, and birds. Each of them have uploads related to them. Each can have many, so I figured it would be best to be on its own table.

Which is a better approach?

  1. a single uploads table with for taking the table (say dogs), and for_id referencing the id of that column
  2. a single uploads table with a columns dogs_id, cats_id, birds_id, with one of them filled with the relative id
  3. multiple tables, named uploads_dogs, uploads_cats and uploads_birds

Or maybe there is a better appraoch?

Thank you!

Upvotes: 0

Views: 62

Answers (1)

E-Rock
E-Rock

Reputation: 180

You could use a single uploads table with a type id (birds = 1, cats = 2, etc.) and an object id column containing the ID of the cat/bird/dog. So when you want to find all uploads for a given cat:

    SELECT * FROM uploads WHERE typeId = 2 AND objectId = [catId]

Upvotes: 2

Related Questions