使用数据库的主键作为业务对象标识符是一个坏主意吗? [英] Is it a bad idea to use a database's primary key as business object identifier?

查看:123
本文介绍了使用数据库的主键作为业务对象标识符是一个坏主意吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,使用自动递增主键作为业务实体标识符(例如Partner IdAccount Number)是不好还是个好主意?

I wonder, is it bad or good idea to use auto increment primary key as business entity identifier such as Partner Id or Account Number?

此外,如果我选择这种方法,我会遇到什么陷阱?

Also, what pitfalls I can face if I'll choose that approach?

推荐答案

我不认为每个人都有相同的看法,但我确实认为这是不好的做法.我认为,将ID作为密钥"传递给用户是不好的,原因有很多:

I don't think everyone shares the same opinion, but I do think it is bad practice. Passing ID's to the user as the 'key' is bad in my opinion, for a number of reasons:

  • ID对用户来说不是自然的.他们不是在谈论项目"1474623",而是在谈论项目"ABC".他们不是在谈论"363528"人,而是在谈论帕特里克·霍夫曼"(Patrick Hofman);
  • ID很脆弱.您不能真正依靠它们不变.如果您选择移动到另一个数据库平台或当前平台的新版本,并且希望使用'insert'语句移动所有数据,可以删除ID字段.

在我们的产品中,我们总是在主键旁边使用自然键" ,这是人类可以理解的密钥.

In our products, we always use a 'natural key', next to the primary key, a key that is understood by humans.

如果没有可用的人类可理解的自然键(例如,当它是日志表时),则可以还原为人工键.

If there is no human understandable natural key available, for example when it is a logging table, you can revert to a artificial key.

这篇关于使用数据库的主键作为业务对象标识符是一个坏主意吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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