Solutions for AUTO_INCREMENT IDs in MySQL
So we have some common solutions with some built-in functions and some with procedures(Triggers) that we will create. Let’s first understand them.
- AUTO_INCREMENT: This is the built-in property that is used to solve the above issue. It will add the new ID after the last one automatically and we don’t have to give anything related to the primary key in the Insert statement.
- Trigger: This approach will require the understanding of triggers. It will trigger the procedure as soon as the insert query is run. We will create a Trigger which will run before actual insertion in the database so that we can count the id and then we can append the id by ourselves.
How to Create id with AUTO_INCREMENT in MySQL?
In all tables, we always have a primary key which is used to identify rows uniquely. For that, we generally use integers as a type of column. When adding each row, we assign a new integer to it. Whenever we insert a new row, we don’t want to manually get the last integer and use it in the INSERT query. Instead, we want an option that can automatically generate a new ID every time we try to insert it.