且构网

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

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

更新时间:2023-02-08 15:51:07

技术上是的,但如果一个字符串可以作为主键,那么你可能应该使用它.这一切都取决于您为其制作的表的大小以及将成为主键的字符串的长度(更长的字符串 == 更难比较).对于具有数百万行的表,我不一定会使用字符串,但是在较小的表上使用字符串所导致的性能下降量对于使用整数所带来的麻烦来说是微不足道的与数据无关.

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.