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: Why aren't databases 0-indexed(ie IDs start at 1)?

3 pointsby cpolisover 11 years ago
It seems like most everything else in computer science is based on counting from zero.

7 comments

byoung2over 11 years ago
It could be problematic to have an identity column with a 0 value because you could pass in a value that evaluates to 0 (e.g. an empty string or single space). An error in your code or some other condition that doesn&#x27;t pass a proper id, but that evaluates to 0, would wipe out the first entry in the table.<p>Identity columns are more closely tied with the data in the row than array indices are, so wiping out array index 0 is not as bad as wiping out a database row with id 0.<p>EDIT: Here&#x27;s an example: <a href="http://sqlfiddle.com/#!2/dd3ed/6" rel="nofollow">http:&#x2F;&#x2F;sqlfiddle.com&#x2F;#!2&#x2F;dd3ed&#x2F;6</a>
morkfromorkover 11 years ago
Arrays start at 0. Databases are databases and IDs are not always numbers, just unique values.
patmccover 11 years ago
When things begin at 0, it&#x27;s often because they&#x27;re abstracting an offset baked into the data structure - array[0] is 0 steps from the start of the array, array[4] is 4, etc.<p>Databases aren&#x27;t storing things based on offsets, they&#x27;re mystical and strange under the hood (by design - we worry about the schema, the database engine worries about the implementation). IDs are meant to be unique and that&#x27;s about it. As mentioned by someone else, it&#x27;s probably best if we don&#x27;t use a value like 0 that can be misinterpreted.
LarryMade2over 11 years ago
<a href="http://xkcd.com/163/" rel="nofollow">http:&#x2F;&#x2F;xkcd.com&#x2F;163&#x2F;</a><p>I think for most lay persons starting with one makes sense, but more technical folk learn to start with 0, so depending on who wrote it or who it was written for that usually determines the start index.
dragonwriterover 11 years ago
They are, if the column being indexed is an attribute of the entity represented that itself is 0-based.<p>Otherwise they generally aren&#x27;t, because of what a database is.
kogirover 11 years ago
In MSSQL it&#x27;s arbitrary - you set the start number and the step size, but (1, 1) is the default.
SamReidHughesover 11 years ago
IDs are not indices.