可以将字符值用作主键吗?

编程入门 行业动态 更新时间:2024-10-28 19:24:25
本文介绍了可以将字符值用作主键吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

与使用基于字符的字段相比,在数据库表中使用唯一的数字ID字段是否有性能提升或最佳实践?

Is there a performance gain or best practice when it comes to using unique, numeric ID fields in a database table compared to using character-based ones?

例如,如果我有两个表:

For instance, if I had two tables:

id ... 17 ,名称 ... Rickey Henderson , teamid ... 28

id ... 17, name ... Rickey Henderson, teamid ... 28

teamid ... 28 , teamname ... 奥克兰

teamid ... 28, teamname ... Oakland

如果 teamid 是"OAK"或"SD"而不是"28"或"31",那么具有成千上万名运动员的运动员桌将更易于阅读.让我们理所当然地认为 teamid 值在字符形式上保持唯一且一致.

The athlete table, with thousands of players, would be easier to read if the teamid was, say, "OAK" or "SD" instead of "28" or "31". Let's take for granted the teamid values would remain unique and consistent in character form.

我知道您可以使用字符,但是出于某种原因对索引,过滤等是一个坏主意吗?

I know you CAN use characters, but is it a bad idea for indexing, filtering, etc for any reason?

请忽略归一化参数,因为这些表比示例更复杂.

推荐答案

我会尽量避免使用文本作为键-将来要更改某个团队的团队ID时会发生什么?当主键可以避免的确切事情时,您就必须在整个数据中级联该键更改.另外,尽管我没有任何经验证据,但我认为INT键会比文本键快得多.

I'd stay away from using text as your key - what happens in the future when you want to change the team ID for some team? You'd have to cascade that key change all through your data, when it's the exact thing a primary key can avoid. Also, though I don't have any emperical evidence, I'd think the INT key would be significantly faster than the text one.

也许您可以为数据创建视图,使其更易于使用,同时仍使用数字主键.

Perhaps you can create views for your data that make it easier to consume, while still using a numeric primary key.

更多推荐

可以将字符值用作主键吗?

本文发布于:2023-11-13 21:11:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585330.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主键   字符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!