How to configure a specific field to auto-increment in MySQL?

In MySQL, to set a field to auto-increment, you need to use the AUTO_INCREMENT attribute. This can be done when creating a table or modifying its structure.

Set auto-increment field when creating a table.

CREATE TABLE table_name (
    id INT AUTO_INCREMENT PRIMARY KEY,
    column_name VARCHAR(50)
);

Set auto-increment field when modifying table structure.

ALTER TABLE table_name
MODIFY COLUMN id INT AUTO_INCREMENT;

In the example above, the id field is set as an auto-increment field, so that each time a new record is inserted, the value of the id field will automatically increase to maintain uniqueness.

广告
Closing in 10 seconds
bannerAds