问题
数据库 ID “毫无意义”是一个好的经验法则吗?相反,以一种一眼就能认出的方式构建 ID 是否有显着的好处?优缺点都有什么?
背景
我刚刚与我的同事就我们数据库中 ID 的一致性进行了辩论。我们有一个利用 Spring 的数据驱动应用程序,因此我们很少需要更改代码。这意味着,如果出现问题,数据更改通常是解决方案。
我的论点是,通过使 ID 保持一致和可读,我们可以为自己节省大量时间和长期的麻烦。一旦设置了 ID,它们就不必经常更改,如果操作正确,未来的更改将不会很困难。我同事的立场是,ID 永远不重要。将信息编码到 ID 中违反了数据库设计策略,并且保持它们有序需要额外的工作,“我们没有时间去做”。我在网上找不到任何支持这两种立场的东西。所以我要求助于 SA 的所有大师!
例子
想象一下这个表示杂货店食物的简化数据库记录列表,第一组表示在 ID 中编码的具有含义的数据,而第二组则没有:
ID的含义:
Type
1 Fruit
2 Veggie
Product
101 Apple
102 Banana
103 Orange
201 Lettuce
202 Onion
203 Carrot
Location
41 Aisle four top shelf
42 Aisle four bottom shelf
51 Aisle five top shelf
52 Aisle five bottom shelf
ProductLocation
10141 Apple on aisle four top shelf
10241 Banana on aisle four top shelf
//just by reading the ids, it's easy to recongnize that these are both Fruit on Aisle 4
ID没有意义:
Type
1 Fruit
2 Veggie
Product
1 Apple
2 Banana
3 Orange
4 Lettuce
5 Onion
6 Carrot
Location
1 Aisle four top shelf
2 Aisle four bottom shelf
3 Aisle five top shelf
4 Aisle five bottom shelf
ProductLocation
1 Apple on aisle four top shelf
2 Banana on aisle four top shelf
//given the IDs, it's harder to see that these are both fruit on aisle 4
概括
What are the pros and cons of keeping IDs readable and consistent? Which approach do you generally prefer and why? Is there an accepted industry best-practice?
-------- edit ( helpful background info from comments, below ): --------
In our tables, the Primary Key is always an ID field containing a unique integer. At first, that integer was arbitrary. Over time, some of these IDs naturally took on meaning among developers/testers. During a recent refactor, certain developers also took time to make all IDs easier to recognize. It made everyone's job 100X easier. Some people (who don't actually use the data/code) vehemently disagreed for theoretical reasons. In practice, not one of those objections are holding true. Moreover, all developers using the data agree that it's now significantly easier to maintain.
I'm looking for (but haven't seen) a defensible argument against using immediately recognizable IDs in a data-centric environment.