How To Add Auto-increment To Column In MySQL Database Using ...

  • Home
  • Whiteboard
  • Online Compilers
  • Practice
  • Articles
  • AI Assistant
  • Jobs
  • Tools
  • Corporate Training
  • Courses
  • Certifications
Menu Categories Login
  • Switch theme
  • SQL
  • HTML
  • CSS
  • Javascript
  • Python
  • Java
  • C
  • C++
  • PHP
  • Scala
  • C#
  • Tailwind CSS
  • Node.js
  • MySQL
  • MongoDB
  • PL/SQL
  • Swift
  • Bootstrap
  • R
  • Machine Learning
  • Blockchain
  • Angular
  • React Native
  • Computer Fundamentals
  • Compiler Design
  • Operating System
  • Data Structure and Algorithms
  • Computer Network
  • DBMS
  • Excel
Technical Questions and Answers
  • Data Structure Data Structure
  • Networking Networking
  • RDBMS RDBMS
  • Operating System Operating System
  • Java Java
  • MS Excel MS Excel
  • iOS iOS
  • HTML HTML
  • CSS CSS
  • Android Android
  • Python Python
  • C Programming C Programming
  • C++ C++
  • C# C#
  • MongoDB MongoDB
  • MySQL MySQL
  • Javascript Javascript
  • PHP 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
How to add auto-increment to column in MySQL database using PhpMyAdmin? MySQLMySQLi DatabasePHP

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/phpmyadmin

The 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: 0

Check 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 karthikeya Boyini Updated on: 2020-06-26T10:21:02+05:30

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started Print Page Previous Next Advertisements

Tag » Add Column Id Auto Increment Mysql