我应该使用多个到许多“链接”中的索引列。表? [英] Should I use an index column in a many to many "link" table?
问题描述
我有两个表,产品
和类别
有多对多的关系,所以我添加将包含 category_id
和 product_id
的 products_categories
/ 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屋!