These are ways to automatically create unique numbers for new rows in a table. They help give each row a special ID without you typing it.
AUTO_INCREMENT vs SERIAL vs IDENTITY in SQL
CREATE TABLE table_name ( id INT AUTO_INCREMENT PRIMARY KEY ); -- OR -- CREATE TABLE table_name ( id SERIAL PRIMARY KEY ); -- OR -- CREATE TABLE table_name ( id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY );
AUTO_INCREMENT is mainly used in MySQL.
SERIAL is a shortcut in PostgreSQL for auto-incrementing integers.
IDENTITY is the standard SQL way, used in SQL Server and newer PostgreSQL versions.
CREATE TABLE users ( user_id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) );
CREATE TABLE products ( product_id SERIAL PRIMARY KEY, product_name TEXT );
CREATE TABLE orders ( order_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, order_date DATE );
This example creates a table with an AUTO_INCREMENT id in MySQL, adds two employees, and shows their IDs.
CREATE TABLE employees ( emp_id INT AUTO_INCREMENT PRIMARY KEY, emp_name VARCHAR(100) ); INSERT INTO employees (emp_name) VALUES ('Alice'); INSERT INTO employees (emp_name) VALUES ('Bob'); SELECT * FROM employees;
Each method automatically creates a unique number for new rows.
AUTO_INCREMENT is MySQL-specific; SERIAL is PostgreSQL-specific but considered legacy; IDENTITY is the modern standard.
IDENTITY allows more control over how numbers are generated compared to SERIAL.
AUTO_INCREMENT is used in MySQL for automatic numbering.
SERIAL is a PostgreSQL shortcut for auto-incrementing integers.
IDENTITY is the SQL standard and used in SQL Server and newer PostgreSQL versions.