The AUTO_INCREMENT
attribute automatically generates a unique sequential number for new rows.
Itβs most commonly used with integer primary key columns to ensure each record has a distinct identifier.
CREATE TABLE users ( user_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, username VARCHAR(50) NOT NULL, email VARCHAR(100) );
user_id = 1
, next β 2
, and so on.AUTO_INCREMENT
column allowed per table, and it must be indexed (usually PRIMARY KEY).-- Start from 1000 instead of 1 ALTER TABLE users AUTO_INCREMENT = 1000;
You can manually specify a value for the AUTO_INCREMENT column; however, it should be β₯ current counter, or MySQL will adjust the counter to max(existing, inserted)+1.
INSERT INTO users (user_id, username, email) VALUES (500, 'alice', 'alice@example.com'); -- Next auto value becomes 501 (if higher than current)
AUTO_INCREMENT
for surrogate keys rather than business data.AUTO_INCREMENT
, but this can risk duplicates.AUTO_INCREMENT
is single-node.Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!