
- 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
ALTER table by adding AUTOINCREMENT in MySQL?
To add AUTOINCREMENT in MySQL, you can use the ALTER command.
ALTER TABLE yourTableName change yourColumName yourColumnName dataType AUTO_INCREMENT PRIMARY KEY;
To understand the above concept, create a table with a column. Let us create a table −
mysql> create table AlterTableToAddAutoIncrement -> ( -> StudentId int -> ); Query OK, 0 rows affected (0.57 sec)
Implement the above syntax to change “StudentId” with AUTOINCREMENT. The query is as follows −
mysql> alter table AlterTableToAddAutoIncrement change StudentId StudentId int AUTO_INCREMENT Primary key; Query OK, 0 rows affected (1.93 sec) Records: 0 Duplicates: 0 Warnings: 0
We have added AUTO_INCREMENT to column name ‘StudentId’. Now you can verify whether autoincrement is working or not by inserting records without giving any value.
The query is as follows −
mysql> insert into AlterTableToAddAutoIncrement values(); Query OK, 1 row affected (0.16 sec) mysql> insert into AlterTableToAddAutoIncrement values(); Query OK, 1 row affected (0.16 sec) mysql> insert into AlterTableToAddAutoIncrement values(); Query OK, 1 row affected (0.33 sec)
Now you can display all records which will be displayed in sequence of 1, 2, 3, 4, etc. The query is as follows −
mysql> select *from AlterTableToAddAutoIncrement;
The following is the output −
+-----------+ | StudentId | +-----------+ | 1 | | 2 | | 3 | +-----------+ 3 rows in set (0.00 sec)
- Related Articles
- Adding unique constraint to ALTER TABLE in MySQL
- How can we use MySQL ALTER TABLE command for adding comments on columns?
- How to Reset MySQL AutoIncrement using a MAX value from another table?
- ALTER TABLE to add a composite primary key in MySQL?
- Alter a table column from VARCHAR to NULL in MySQL
- How do I alter a MySQL table column defaults?
- What MySQL returns when we remove all the columns from a table by using ALTER TABLE command with DROP keyword?
- How can we alter table to add MySQL virtual GENERATED COLUMNS?
- How can we alter table to add MySQL stored GENERATED COLUMNS?
- Set auto increment initial value for MySQL table using ALTER command
- How to alter the database engine of a MySQL database table?
- Adding new enum column to an existing MySQL table?
- How can I update a field in a MySQL database table by adding a value in the second table with a value from the first table?
- Alter row_format to dynamic in MySQL?
- How to use ALTER TABLE statement for changing the size of a column in MySQL?

Advertisements