Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Ask HN: Why aren't databases 0-indexed(ie IDs start at 1)?
3 points by cpolis on Jan 29, 2014 | hide | past | favorite | 7 comments
It seems like most everything else in computer science is based on counting from zero.


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't pass a proper id, but that evaluates to 0, would wipe out the first entry in the table.

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.

EDIT: Here's an example: http://sqlfiddle.com/#!2/dd3ed/6


Arrays start at 0. Databases are databases and IDs are not always numbers, just unique values.


When things begin at 0, it's often because they'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.

Databases aren't storing things based on offsets, they'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's about it. As mentioned by someone else, it's probably best if we don't use a value like 0 that can be misinterpreted.


http://xkcd.com/163/

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.


They are, if the column being indexed is an attribute of the entity represented that itself is 0-based.

Otherwise they generally aren't, because of what a database is.


In MSSQL it's arbitrary - you set the start number and the step size, but (1, 1) is the default.


IDs are not indices.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: