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
MySQL Data Types
MySQL CREATE DATABASE
MySQL USE and SHOW DATABASES
MySQL DROP DATABASE
MySQL CREATE TABLE
MySQL Constraints
MySQL DROP TABLE
Exercises & Assignments
Write a query to get all column names of a database table
Write a query to create a new table with the data and structure of another table
Interview Questions & Answers
Write a query to create a new table which consists of data and structure copied from the other table
What is a primary key in MySQL?
What is a foreign key in MySQL?
What is the difference between CHAR and VARCHAR in MySQL?
What is normalization in MySQL?
What is AUTO_INCREMENT in MySQL?
What is the difference between primary key and unique key?