advantages and disadvantages of database automatic number generator for each row vs manual numbering for each row

1257 views sql

Imagine two tables that implemented like the following description:

  1. The first table rows numbers created by database system administration automatically.
  2. The second table rows numbers created manually by the programmer in a sequential order.

The main question is what are the advantages and disadvantages of these two approaches?

answered question

2 Answers


When the database automatically create numbers, you habe less work. Think about a sign up system, you have fields like name, email, password and so one:

1.) the number is generated by the database, so you can just insert the data into the table.

2.) if this is not the case you have to get the last number, so before the insert into you have to get the last id so instead a insert into you have a select + insert into.

Another reason is, what happened when you delete a row in your table? Maybe in a forum, you want to delete the account but not all of his posts, so you can work with a workaround and when a post has a user_id not given you know this is/was a deleted or banned account - if you give a new user the number from a deleted user you will come in trouble.

posted this

One distinct advantage of having the database manage auto-numbering over manually creating them is that the database implementation is thread safe - and manually creating them is usually (99.9% of the cases) is not (It's hard to do it correctly).

On the other hand, the database implementation does not guarantee sequential numbering - there can be gaps in the numbers.

Given these two facts, an auto-increment column should be used only as a surrogate key, when the values of this column does not have any business meaning - but they are simple used as a simple row identifier.

Please note that when using a surrogate key, it's best to also enforce uniqueness of a natural key - otherwise you might get rows where all the data is duplicated except the surrogate key.

posted this

Have an answer?


Please login first before posting an answer.