Auto Increment Column
In MySQL, the auto-increment column creates a unique ID automatically in the table. The auto-increment column can be created as below:
CREATE TABLE IT_Customers
( CUST_Id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
customer_name VARCHAR(50) NOT NULL
);
Here the CUST_ID is set as an Auto increment column and the default value is 1.
The auto-increment column can also be set with the starting value of our choice instead of the default value 1. For some reason, if we need the starting value to be another value than the default value, the below command can be used.
ALTER TABLE CUST_Id AUTO_INCREMENT=100;
How to Get the Identity of Last Inserted Row in MySQL?
The LAST_INSERT_ID() returns the identity value after an insert into the specific table.
Syntax: SELECT LAST_INSERT_ID();
Getting the ID of a column after an insert may be required during database programming and in this article, we will learn how to get the ID of the inserted row.
In MySQL, the Key column of a table can be set to auto-increment with the starting value. The auto-increment column helps to avoid duplicate IDs being created or inserted.
Contact Us