简体   繁体   English

MySQL将一个表绑定到多个表的最佳方法是什么?

[英]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 . 假设我有多张桌子, dogscatsbirds Each of them have uploads related to them. 他们每个人都有与他们相关的uploads 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 一个uploadsfor获取表(比如dogs ), for_id引用该列的id
  2. a single uploads table with a columns dogs_id , cats_id , birds_id , with one of them filled with the relative id 单个uploads表,其中包含dogs_idcats_idbirds_id列,其中一列填充了相对ID
  3. multiple tables, named uploads_dogs , uploads_cats and uploads_birds 多个表,名为uploads_dogsuploads_catsuploads_birds

Or maybe there is a better appraoch? 或者也许有更好的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. 您可以使用具有类型ID(birds = 1,cats = 2等)的单个上载表和包含cat / bird / dog的ID的对象id列。 So when you want to find all uploads for a given cat: 因此,当您想要查找给定猫的所有上传时:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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