Should I use a unique string as a primary key, or should I use it as a separate INT auto-increment?

Possible duplicates:
Surrogate Vs. Natural / Business Keys
When not to use surrogate primary keys?

So, I would prefer to have INT as a primary key with AI. But, here is the reason why I am considering making a unique string as a primary key :. I should not query related tables to retrieve the primary key, since I already know it

For example:
I have a lot of relationships:

Customer - Order - Product

Let's say I want to add a new customer and a new order, and I already know what they bought. I have to query the product table to get an INT, but if I have a string (unique) that is the primary key I shouldn't make a query (this seems cleaner than me, I'm not talking about optimization / runtime, not at all )

+3
source share
3 answers

If you do not care about optimization, the main 2 criteria for the primary key are:

  • Uniqueness

  • Constant (never changes)

. - , 2 , (, Norton Antivirus → Symantec Antivirus ), .

100% , .

, - , , , , ID .

+4

, VARCHAR ( ), - .

(, : SQL Server), . , . , , VARCHAR .

+3

Er...

  • Your logical model and design will use a unique string. This is a natural key.
  • Actual implementation may use numeric automatic column number (surrogate key) due to architecture / performance
0
source

Source: https://habr.com/ru/post/1732685/


All Articles