skip to Main Content

For a small project i used id as auto-increment primary key.

But my teacher said it is wrong to use auto-increment in id because it skips number when a row is deleted. So what is the best practice for it? i can use uuid but doesn’t it affect performance? Also if i make a custom uniq key, will that be really unique?

2

Answers


  1. An auto-increment ID can sometimes cause problems, e.g. in situations where you are using replication. In these cases opt for a GUID.

    Login or Signup to reply.
  2. AUTO_INCREMENT guarantees uniqueness, nothing else.

    If you want consecutive values (no gaps), then you have to write a lot of code to close up gaps, etc. But why would you need that?

    Often there is a "natural" key that works for the PRIMARY KEY. An example is a table of countries. The PRIMARY KEY can be country_code CHAR(2).

    Auto_inc has many cases where a number is skipped, lost, burned, etc. Another example is INSERT IGNORE when it fails for any reason — the id was already reserved, then not used.

    UUIDs/UUIDs are guaranteed unique, but they are not consecutive, so why use it? Anyway, a uuid is much bulkier. And, for huge tables, inefficient because of how ‘random’ they are. The one argument for uuids is that they can be independently generated by different clients without the help of the database.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search