![]() |
Primary keys in databases uniquely identify each record, ensuring data integrity and efficient retrieval. AUTO_INCREMENT, a feature in MySQL, automatically assigns unique numeric identifiers to new rows, simplifying data management. Understanding these concepts is crucial for designing robust database schemas that optimize data storage and retrieval. This introduction briefly covers the importance of primary keys, AUTO_INCREMENT functionality, and their relevance in database design. Solutions for AUTO_INCREMENT IDs in MySQLSo we have some common solutions with some built-in functions and some with procedures(Triggers) that we will create. Let’s first understand them.
1. AUTO_INCREMENT with Create StatementIn this, we will look to Create a table and with it, we will mention the “AUTO_INCREMENT” As discussed it will manage the id by itself. Syntax:
ExampleCREATE TABLE AutoIncrement ( Now insert some values into it to see whether the id is generating by itself INSERT INTO AutoIncrement (name, age) Output:
So we have not given the values for the id column but as we have set the column to AUTO_INCREMENT it will automatically assign the value according to the last value used for the table. Now we will delete the row with DELETE FROM AutoIncrement WHERE id = 3; Output:
Now add another row by using the below query and apply the select statement. INSERT INTO AutoIncrement(name, age) Output:
So it is clear from the above query that AUTO_INCREMENT doesn’t take the last ID from the database but it will take the ID based on the last inserted ID. We can also change the AUTO_INCREMENT starting value other than 1 by using giving AUTO_INCREMENT = value property.
2. AUTO_INCREMENT with Alter StatementNow let’s say we have a table that was created without Let’s Create a table without AUTO_INCREMENT CREATE TABLE AutoIncrementAlt ( Insert the Rows into it INSERT INTO AutoIncrementAlt (id, name, age) Output:
Now we will add AUTO_INCREMENT to this and remember if we give only AUTO_INCREMENT then it will start from 1 but here we want it to start from 5 ALTER TABLE AutoIncrementAlt And now we can add rows INSERT INTO AutoIncrementAlt (id, name, age) Output:
By this, we can use AUTO_INCREMENT to achieve the auto-incrementing primary key. 3. Create AUTO_INCREMENT Id Without Triggerso now we won’t Auto_Increment with column. But we will mimic this behavior with triggers. The trigger will automatically be called when an insert query is applied. First, let’s create another table for better understanding. CREATE TABLE AutoIncrementWithTrigger ( Now we will create a trigger which will be called Before Insert. DELIMITER // Let’s See what this trigger is doing.
Now we will insert the rows to look at the results. INSERT INTO AutoIncrementWithTrigger (name, age) Output:
By this, we can use AUTO_INCREMENT to achieve the auto-incrementing primary key. Thus we can use triggers to calculate the id and assign it to new records. ConclusionSo, Using this approach we can create a column with auto increment. This helps when we want to create a record from our backend API or service. At that time we reduce our Database calls because we don’t have to worry much about the last id. Also, it will remove the chances of any mismatch with the already present ID. FAQs on AUTO_INCREMENT in MySQLWhat is AUTO_INCREMENT and how does it work?
Can a table have multiple primary keys?
What is the difference between PRIMARY KEY and UNIQUE constraint?
When should I use a surrogate key versus a natural key?
|
Reffered: https://www.geeksforgeeks.org
Databases |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 17 |