2
I have a question on SQL Server. I have the following table, ID (integer Key), Name (nvachar), CPF (nvachar, with index unico).
If I insert with the CPF that already exists, SQL returns with error and this is OK. Now if I do the Insert and it succeeds, what I noticed is that the ID column jumps and it’s not in sequence.
| ID | Nome | CPF |
| 1 | João | 1234 |
| 2 | Pedro | 1235 |
| 3 | Judas | 1236 |
| 5 | João A | 12347 |
From what I understood, when I tried to insert and gave error, SQL itself inserted and then deleted the record, so the next ID is 5 and not 4. Is there any way that this does not occur? Or the best way to treat it is to check before inserting?
Thank you
Assuming that your field
id
whether auto-increment type so this is the expected behavior, there may be "holes" in the sequence.– anonimo
If you need to keep order. It is interesting to check in the application. Validate whether or not the user can be inserted. Although the database does not allow.
– Gabriel Santana