What is AUTO_INCREMENT in MySQL?

CREATE TABLE students (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(50)
);

INSERT INTO students (name) VALUES ('Robin'), ('Maria');
SELECT LAST_INSERT_ID();

The ids are 1 and 2 without us writing them. LAST_INSERT_ID() gives the id of the last inserted row. Deleted numbers are not used again, and TRUNCATE restarts the counter.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.