简体   繁体   中英

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!

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM