harsh8888
harsh8888

Reputation: 477

auto increment id practice

I wanted to know if adding an auto-increment primary index to each table (along or without any other relationship between keys of other table) a good practice?

Example:

whenever I create any table, I always keep a primary auto-increment key called id. Be it users or posts or comments, or any table. That is the default thing that I do. Then I may add other keys like commentid, postid etc. but 'id' always stays.

Is it a bad practice?

Upvotes: 0

Views: 156

Answers (1)

SajjadHashmi
SajjadHashmi

Reputation: 3695

It’s always good to keep your primary-ID as an auto-increment Identity this helps you in many ways like:

  • Maintaining unique primary key integrity throughout the records

  • Reduce the chances of manual entry errors, such as duplication etc.

  • Saves work

    • If it is not auto-incrementing than you will have retrieve the last ID in your code than add 1 to it and then save the record

Keeping it as an auto-increment identity saves you from all above.

Upvotes: 3

Related Questions