TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: Why aren't databases 0-indexed(ie IDs start at 1)?

3 点作者 cpolis超过 11 年前
It seems like most everything else in computer science is based on counting from zero.

7 条评论

byoung2超过 11 年前
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>
morkfromork超过 11 年前
Arrays start at 0. Databases are databases and IDs are not always numbers, just unique values.
patmcc超过 11 年前
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.
LarryMade2超过 11 年前
<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.
dragonwriter超过 11 年前
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.
kogir超过 11 年前
In MSSQL it&#x27;s arbitrary - you set the start number and the step size, but (1, 1) is the default.
SamReidHughes超过 11 年前
IDs are not indices.