SQL AUTO INCREMENT
SQL AUTO INCREMENT is a feature used in SQL databases to automatically generate a unique value for a column in a table when a new row is inserted. This is particularly useful for primary keys.
Example Using MySQL
-
Creating a Table with AUTO_INCREMENT:
CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL );
Here, the
id
column will automatically increment by 1 each time a new row is inserted into theusers
table. TheAUTO_INCREMENT
keyword is used to specify that theid
column should automatically increment, andPRIMARY KEY
ensures that eachid
is unique. -
Inserting Data into the Table:
INSERT INTO users (username, email) VALUES ('john_doe', 'john@example.com'); INSERT INTO users (username, email) VALUES ('jane_doe', 'jane@example.com');
-
Table Data after Insertions:
SELECT * FROM users;
Output:
+----+-----------+-----------------+ | id | username | email | +----+-----------+-----------------+ | 1 | john_doe | john@example.com| | 2 | jane_doe | jane@example.com| +----+-----------+-----------------+
As you can see, the
id
column values are automatically incremented.
Example Using PostgreSQL
-
Creating a Table with SERIAL:
CREATE TABLE users ( id SERIAL PRIMARY KEY, username VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL );
In PostgreSQL, the
SERIAL
keyword is used to create an auto-incrementing integer column. -
Inserting Data into the Table:
INSERT INTO users (username, email) VALUES ('john_doe', 'john@example.com'); INSERT INTO users (username, email) VALUES ('jane_doe', 'jane@example.com');
-
Table Data after Insertions:
SELECT * FROM users;
Output:
+----+-----------+-----------------+ | id | username | email | +----+-----------+-----------------+ | 1 | john_doe | john@example.com| | 2 | jane_doe | jane@example.com| +----+-----------+-----------------+
Example Using SQLite
-
Creating a Table with AUTOINCREMENT:
CREATE TABLE users ( id INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL );
In SQLite,
AUTOINCREMENT
ensures that theid
column values are unique and increment automatically. -
Inserting Data into the Table:
INSERT INTO users (username, email) VALUES ('john_doe', 'john@example.com'); INSERT INTO users (username, email) VALUES ('jane_doe', 'jane@example.com');
-
Table Data after Insertions:
SELECT * FROM users;
Output:
+----+-----------+-----------------+ | id | username | email | +----+-----------+-----------------+ | 1 | john_doe | john@example.com| | 2 | jane_doe | jane@example.com| +----+-----------+-----------------+
In each case, the id
column automatically increments by 1 for each new row inserted, ensuring each row has a unique identifier.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.