我应该使用多个到许多“链接”中的索引列。表? [英] Should I use an index column in a many to many "link" table?

查看:89
本文介绍了我应该使用多个到许多“链接”中的索引列。表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表,产品类别有多对多的关系,所以我添加将包含 category_id product_id products_categories / p>

我应该添加另一个(自动递增)索引列还是使用两个现有索引列作为主键?

$ p
$ b

  • 你看到的数据更多的是一组对象关系数据库只是
    a存储介质)或者通过关系代数原生表示和分析的事实集合。


  • 一些ORM /框架/工具没有对多列主键的良好支持。
    如果你碰巧使用其中的一个,你将需要额外的id列。


  • 如果它只是一个多对多关系没有额外的数据相关联,
    最好避免额外的id列,并将两列作为主键。


  • 对这个关联的附加信息,那么它将变成
    ,然后两个实体的多对多关系。
    所以它成为它自己的实体,如果它有自己的id
    独立于它连接的实体会更方便。



I have two tables, products and categories which have a many to many relationship, so I'm adding a products_categories table which will contain category_id and product_id.

Should I add another (auto-incrementing) index column or use the two existing ones as primary key?

解决方案

That depends.

  • Are you seeing your data more as set of objects (and relational database is just a storage medium) or as set of facts represented and analyzed natively by relational algebra.

  • Some ORMs/Frameworks/Tools don't have good support for multicolumn primary keys. If you happen to use one of them, you'll need additional id column.

  • If it's just a many-to-many relationship with no additional data associated with it, it's better to avoid additional id column and have both columns as primary key.

  • If you want to add some additional information to this association, then it'll become something more then many-to-many relationship of two entities. So it becomes entity in it's own and it'd be more convenient if it had it's own id independent to entities it connects.

这篇关于我应该使用多个到许多“链接”中的索引列。表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆