How To Add Auto-increment To Column In MySQL Database Using ...
Maybe your like
- Home
- Whiteboard
- Online Compilers
- Practice
- Articles
- AI Assistant
- Jobs
- Tools
- Corporate Training
- Courses
- Certifications
- Switch theme
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
You can add auto_increment to a column in MySQL database with the help of ALTER command.
The syntax is as follows −
ALTER TABLE yourTableName MODIFY yourColumnName INT NOT NULL AUTO_INCREMENT;To open PhpMyAdmin on localhost, you need to type the following on localhost and press enter −
localhost/phpmyadminThe screenshot is as follows −

Above, we already have a table “AutoIncrementDemo”. In that, we have a column “UserId” set as Primary key. Let’s say we need to add auto_increment to the same column.
For auto_increment, check the A.I as shown above. The same is marked below as well −

After that press the Save button.
Let us also see how to add auto_increment to MySQL database.
Here is the query to add an auto_increment column in MySQL database. First, create a table. The query to create a table is as follows −
mysql> create table AutoIncrementDemo -> ( -> UserId int -> ); Query OK, 0 rows affected (0.45 sec)Example
Now add an auto_increment column to MySQL database. The query is as follows −
mysql> alter table AutoIncrementDemo -> modify column UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY; Query OK, 0 rows affected (1.10 sec) Records: 0 Duplicates: 0 Warnings: 0Check the description of the table with the help of the DESC command. The query is as follows −
mysql> desc AutoIncrementDemo;Output
+--------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+---------+------+-----+---------+----------------+ | UserId | int(11) | NO | PRI | NULL | auto_increment | +--------+---------+------+-----+---------+----------------+ 1 row in set (0.04 sec)
karthikeya Boyini Updated on: 2020-06-26T10:21:02+05:30 10K+ Views
Kickstart Your Career
Get certified by completing the course
Get StartedTag » Add Column Id Auto Increment Mysql
-
MySQL 8.0 Reference Manual :: 13.1.9.3 ALTER TABLE Examples
-
Add Auto-Increment ID To Existing Table? - Mysql - Stack Overflow
-
How To Add Auto Increment Column In Existing Table In MySQL - Ubiq
-
SQL AUTO INCREMENT A Field - W3Schools
-
Add An AUTO_INCREMENT Column As PRIMARY KEY In MySQL ...
-
MySQL: Sequences (AUTO_INCREMENT) - TechOnTheNet
-
“how To Add Auto Increment Column In Existing Table In Mysql” Code ...
-
How To Work With Auto-Incrementing IDs In SQL - Retool
-
How To Add Auto Increment Column To Existing Table - YouTube
-
AUTO_INCREMENT - MariaDB Knowledge Base
-
Working With AUTO_INCREMENT Columns In MySQL
-
AUTO_INCREMENT - PingCAP Docs
-
Sql Server - Add An Auto Increment Column By An Order Condition
-
Setting Up MySQL Auto Increment Primary Key 101 - Hevo Data