How to set auto increment in MySQL?

Auto Increment is a constraint that automatically generates a unique number while inserting a new record into the table. Generally, it is used for the primary key field in a table. In MySQL, we can set the value for an AUTO_INCREMENT column using the ALTER TABLE statement as follows:

ALTER TABLE table_name AUTO_INCREMENT = value;

To set auto-increment in MySQL, you can use the AUTO_INCREMENT attribute when defining a column in a table. Here’s the general syntax:

CREATE TABLE table_name (
column_name INT AUTO_INCREMENT PRIMARY KEY,
other_column_name datatype,
...
);

Or if you’re altering an existing table:

ALTER TABLE table_name
MODIFY column_name INT AUTO_INCREMENT PRIMARY KEY;

This will automatically generate a unique value for the specified column whenever a new row is inserted into the table. The auto-incremented column typically serves as the primary key for the table.