Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why SQL primary key index begin at 1 and not at 0?

was wondering, why does the SQL primary key index begin at 1, and why not at 0 ? Is there a reason ?

I don't know if this is the good place to ask a question like this, but found no answer on the web.

like image 361
A.Vinuela Avatar asked Dec 05 '18 11:12

A.Vinuela


Video Answer


1 Answers

Counting in SQL generally starts as "1". For instance, the substring operations count characters in a string from 1 (and not 0). row_number() enumerates rows in a group, starting from 1 (and not 0).

The reason for this is simple. Humans start counting at 1 -- just ask many four-year olds. We don't start counting at zero until we learn programming. As a species, we counted from "1" for many millennia before adding zero to the pantheon of numbers.

Counting from 1 is much more intuitive when you are counting rows from tables: "1" for the first row requires little explanation, in particular for people who are not programmers. "0" for the first row requires explanation.

It is easy to forget, but SQL was devised for non-programmers. Much of the verbosity is because it was intended for business users.

like image 57
Gordon Linoff Avatar answered Nov 14 '22 23:11

Gordon Linoff