Skip to content
Advertisement

Auto-increment vs manual incrementing in SQL [closed]

I’m not a database expert, but I’m curious about auto-incrementing in SQL.
Is it efficient, a friend of mine, his opinions, he’s not using auto-incrementing instead he’s taking the last id every time he insert something. I’m in doubt whether to use his ideas, which I guess is less efficient or go with auto-incrementing provided by the database itself.

Advertisement

Answer

I would use auto-increment every time. It is, by far, the best way to ensure that you have unique values.

If you use the previous highest value, unless you are careful, there is a chance that the same value may be written twice – for example if when two instances of the application retrieve the same previous highest value, add one and insert the new row. (I have seen it happen..)

Both SQL Server and My SQL have built in the auto-increment functionality for a reason. You should only avoid them if you have a specific need for something else.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement