What is the AUTO_INCREMENT attribute in MySQL?

Answer

AUTO_INCREMENT is a MySQL attribute that automatically generates a unique, incrementing integer value for a column when a new row is inserted, typically used for primary keys. Declaration: id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY. On each INSERT without specifying the id column (or inserting NULL/0), MySQL assigns the next available integer. The current AUTO_INCREMENT value can be seen in SHOW TABLE STATUS LIKE "tablename";. Key behaviors: the counter only increases — deleting rows does not reuse their IDs; if you insert a specific value that is larger than the current counter, the counter jumps to that value; the counter is not reset on ROLLBACK of a transaction (gaps in IDs are normal and expected). Reset the counter: ALTER TABLE tablename AUTO_INCREMENT = 1; (only works if the new value is greater than the current max ID). For a 32-bit INT UNSIGNED, max is ~4.29 billion; use BIGINT UNSIGNED for very large tables (max ~18.4 quintillion). Gap behavior: don't rely on AUTO_INCREMENT IDs being consecutive — gaps are normal due to failed inserts, rollbacks, and explicit gaps.