Auto Increment in sql

What is autoincrement?
Autoincrement is a function that works with numeric data types. It automatically generates consecutive numeric values ​​each time a record is inserted into the table for a field defined as autoincrement.
When to use autoincrement?
In the Database Normalization tutorial, we looked at how you can store data with minimal redundancy by storing data in many small tables that are related to each other using primary and foreign keys.
The primary key must be unique because it uniquely identifies a row in the database. But how can we guarantee that the primary key is always unique? One possible solution would be to use a formula to generate a primary key that checks if the key exists in the table before adding data. This may work well, but as you can see, the approach is complex and not error-proof. To avoid this complexity and ensure that the primary key is always unique, we can use MySQL's auto-increment feature to generate primary keys. Auto increment is used with the INT data type. The INT data type supports both signed and unsigned values.Unsigned data types can only contain positive numbers. It is recommended to define an unsigned constraint on an auto-increment primary key.
Autoincrement Syntax
Let's now look at the script used to create the movie category table.
CREATE TABLE `categories` ( `category_id` int(11) AUTO_INCREMENT, `category_name` varchar(150) DEFAULT NULL, `remarks` varchar(500) DEFAULT NULL, PRIMARY KEY (`category_id`) );
Notice the "AUTO_INCREMENT" in the category_id field. This causes the category ID to be automatically generated each time a new row is added to the table. It is not provided when inserting data into a table, MySQL generates it.
Let's take a look at the current contents of the category table.
SELECT * FROM `categories`;
Running the above script in MySQL Workbench against myflixdb gives us the following results.
category_id | category_name | remarks |
---|---|---|
one | Comedy | Movies with humor |
2 | Romantic | love stories |
3 | epic | story ancient movies |
4 | Horror | NULL |
5 | science fiction | NULL |
6 | Thriller | NULL |
7 | action | NULL |
eight | romantic comedy | NULL |
Let's now insert a new category into the categories table.
INSERT INTO `categories` (`category_name`) VALUES ('Cartoons');
Running the above script for myflixdb in MySQL Workbench gives us the following results shown below.
category_id | category_name | remarks |
---|---|---|
one | Comedy | Movies with humor |
2 | Romantic | love stories |
3 | epic | story ancient movies |
4 | Horror | NULL |
5 | science fiction | NULL |
6 | Thriller | NULL |
7 | action | NULL |
eight | romantic comedy | NULL |
9 | Cartoons | NULL |
Notice that we didn't provide a category ID. MySQL automatically generated it for us because the category id is defined as auto-increment.
If you want to get the last insert ID generated by MySQL, you can use the LAST_INSERT_ID function to do this. The script below gets the last id that was created.
SELECT LAST_INSERT_ID();
Running the above script yields the last auto increment number generated by the INSERT query. The results are shown below.
Summary
- The autoincrement attribute, when specified on a column with numeric data types, generates numbers sequentially each time a new row is added to the database.
- Auto increment is commonly used to generate primary keys.
- The defined auto-increment data type must be large enough to accommodate many records. Specifying TINYINT as the data type for an auto-increment field limits the number of records that can be added to the table to only 255, since any values ​​beyond this will not be accepted by the TINYINT data type.
- It is recommended to specify an unsigned constraint for auto-increment primary keys to avoid negative numbers.
- When a row is removed from a table, its automatically incremented identifier is not reused. MySQL keeps generating new numbers sequentially.
- The default initial value for AUTO_INCREMENT is 1, and it will increase by 1 for each new entry.
- To allow the AUTO_INCREMENT sequence to start with a different value, use AUTO_INCREMENT = 10