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.

Similar Reads

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:...

Getting the Identity Value

The LAST_INSERT_ID() function will return the last Identity value after an insert in the specified table....

Bulk Insert and Identity Value

When we copy data from one table to another table using the INSERT INTO command, the LAST_INSERT_ID(), will return only the ID of the first row (Last Insert Id is 1)....

Conclusion

In this article, we learned about auto-increment columns, what is Identity column in a table, and how to get the Identity of the last inserted row after a single or multiple rows are inserted into a Table in MySQL....

Contact Us