使用复合键或者总是使用代理键? [英] Use composite keys? Or always use surrogate keys?

查看:89
本文介绍了使用复合键或者总是使用代理键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


重复:多对多关系设计 - 交叉表设计

如果我有这些表(* =主键):

If I have these tables (* = primary key):

user
  id*
  name

group
  id*
  name

更好吗?

user_group
  user_id*
  group_id*

还是更好?

user_group
  id*
  user_id
  group_id


推荐答案

给定而不是user_id和group_id都已经是代理键,因此保证是唯一的(给定适当的应用程序实现),添加第三个id完全是多余的。

Given than both user_id and group_id are already surrogate keys and thus guaranteed to be unique (given proper implementation of the app), adding a third id is totally redundant.

除非你使用某种ORM(可悲的是)通常会使复合键的处理变得更加复杂。在这种情况下,您必须使用您选择的ORM来评估冗余成本与开发成本。

Unless you are using some kind of ORM which (sadly) usually make more complex the handling of composite keys. In that case you must evaluate the cost of redundancy versus the ease of development with your chosen ORM.

这篇关于使用复合键或者总是使用代理键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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