且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

作为SQL数据库中的主键的字符串

更新时间:2023-02-08 16:03:25

技术上是肯定的,但如果字符串是主键,那么你应该使用它。这一切都取决于你为它的表的大小和字符串的长度将成为主键(更长的字符串==难以比较)。我不一定使用一个字符串的表有数百万行,但你通过使用较小的表上的字符串将获得的性能减慢的量将是微不足道的,你可以拥有一个整数,不是指与数据相关的任何东西。

Technically yes, but if a string makes sense to be the primary key then you should probably use it. This all depends on the size of the table you're making it for and the length of the string that is going to be the primary key (longer strings == harder to compare). I wouldn't necessarily use a string for a table that has millions of rows, but the amount of performance slowdown you'll get by using a string on smaller tables will be minuscule to the headaches that you can have by having an integer that doesn't mean anything in relation to the data.