
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to work with auto incrementing column in MySQL?
To work with auto incrementing column, you can set it as AUTO_INCREMENT while creating the table.
Let us first create a table. Here, we have set the Id field as column since that would be our auto increment column −
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> FirstName varchar(20), -> LastName varchar(20) -> ); Query OK, 0 rows affected (0.71 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(FirstName,LastName) values('John','Smith'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(FirstName,LastName) values('Chris','Brown'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(FirstName,LastName) values('Carol','Taylor'); Query OK, 1 row affected (0.21 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+----+-----------+----------+ | Id | FirstName | LastName | +----+-----------+----------+ | 1 | John | Smith | | 2 | Chris | Brown | | 3 | Carol | Taylor | +----+-----------+----------+ 3 rows in set (0.00 sec)
- Related Articles
- How to reset auto-incrementing column in MySQL?
- How to create a table with auto-increment column in MySQL using JDBC?
- MySQL date column auto fill with current date?
- How to handle fragmentation of auto increment ID column in MySQL?
- How to add auto-increment to column in MySQL database using PhpMyAdmin?
- How does auto property work in margin: 0 auto in CSS?
- How to Auto-Fit Column Width in Excel?
- How to Auto-Number a Column in Excel?
- How to work with MySQL in Lua Programming?
- How to set the initial value of an auto-incremented column in MySQL using JDBC?
- How to work with array variable in MySQL?\n
- How to Auto Generate Database Diagram in MySQL?
- How to change auto increment number in MySQL?
- How to work with “!=” or “not equals” in MySQL WHERE?
- Set MySQL int column to auto increment by 1 beginning at 10000?

Advertisements