TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: GUID, int, or both for primary key?

5 pointsby Fsp2WFuHabout 7 years ago
If both I&#x27;d just use int as primary key, and short guid as unique indexed key.<p>I like being able to get last n records if my primary key is int. At the same time I don&#x27;t want to show to the public my auto incrementing int, I&#x27;d rather do a form of short guid. Reason is to prevent someone form randomly incrementing to next record or something similar. Most of my tables also have a created datestamp. Not sure, any thoughts on this?

4 comments

coreyp_1about 7 years ago
You actually answered your own question, which I&#x27;m glad for because that means that you are thinking about security.<p>Use numeric IDs internally (for foreign keys, etc.), and GUIDs for anything public-facing. NEVER expose the internal IDs to the end user, but, conversely, only use IDs internally in the database. As long as you are consistent, then it will never be a problem.<p>[edit: added this sentence] The ID will be the primary key, but the GUID can be a key as well, but it does not have to be part of the primary key.<p>As a side note, it&#x27;s not just about a person being able to increment to the next record, but that it exposes information about your system. If a person has just joined your website, and they know that their ID is 7, then they know that there is nobody else on the site (and that&#x27;s potentially catostrophic). If their order # is 5, then they know that nobody else is buying from you. Essentially, exposing IDs is, in essence, an information leak.
eesmithabout 7 years ago
Various discussion about something similar at <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=16946557" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=16946557</a> . This concerned the essay &quot;User IDs probably shouldn&#x27;t be passed around as ints&quot; at <a href="https:&#x2F;&#x2F;rachelbythebay.com&#x2F;w&#x2F;2018&#x2F;04&#x2F;27&#x2F;uid&#x2F;" rel="nofollow">https:&#x2F;&#x2F;rachelbythebay.com&#x2F;w&#x2F;2018&#x2F;04&#x2F;27&#x2F;uid&#x2F;</a> .<p>The author argues that auto incrementing ints have additional failure modes that sparse integers don&#x27;t have, even if those sequential values aren&#x27;t user-facing.<p>If they have a created datestamp then if you index them then it&#x27;s easy to get last-n.
daleholborowabout 7 years ago
There are additional arguments against using ints as ids in the database, if you ever plan to shard etc, as in &quot;you&#x27;d still have to do a lot of work to migration data from an integer-key-based-system to something distributed&quot;. Various articles describe this. Keep in mind that many RDBMS systems now support some concept of a &quot;sequential guid&quot; - but keep in mind that they all store these in different ways. Plug: here&#x27;s some code I pulled together, with links to the reference material, that will generate sequential guids for various databases:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;daleholborow&#x2F;iayos.sequentialguid" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;daleholborow&#x2F;iayos.sequentialguid</a><p>Speaking from personal experience, in theory I would always choose a (sequential)GUID as PK.. BUT... they ARE a pain in the bollocks to work with when you are developing and writing sql by hand to query data. E.g.<p>a) select * from Thingies where Id = 5<p>vs<p>b) select * from Thingies where Id = &#x27;12331-432Aafds-32424-df&#x27;
jstewartmobileabout 7 years ago
If you do GUID PK or clustered index in an RDBMS, probably want to turn it into a monotonic GUID.<p>Many DBs order the table by PK. If the GUID value moves backward, that&#x27;s a slow insert into the middle instead of a fast append to the end.<p><a href="https:&#x2F;&#x2F;dba.stackexchange.com&#x2F;a&#x2F;17212" rel="nofollow">https:&#x2F;&#x2F;dba.stackexchange.com&#x2F;a&#x2F;17212</a>