Should I design a table with a primary key of varchar or int?

后端 未结 13 1180
清酒与你
清酒与你 2020-11-27 13:45

I know this is subjective, but I\'d like to know peoples opinions and hopefully some best practices that I can apply when designing sql server table structures.

I pe

13条回答
  •  清歌不尽
    2020-11-27 14:39

    I'd agree that in general an INT (or identity) field type is the best choice in most "normal" database designs:

    • it requires no "algorithm" to generate the id/key/value
    • you have fast(er) joins and the optimizer can work a lot harder over ranges and such under the hood
    • you're following a defacto standard

    That said, you also need to know your data. If you're going to blow through a signed 32-bit int, you need to think about unsigned. If you're going to blow through that, maybe 64-bit ints are what you want. Or maybe you need a UUID/hash to make syncing between database instances/shards easier.

    Unfortunately, it depends and YMMV but I'd definitely use an int/identity unless you have a good reason not to.

提交回复
热议问题