- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Add an autoincrement column with a custom start value in MySQL
To add a new column to an already created table, use ALTER TABLE and ADD COLUMN. Use AUTO_INCREMENT to set auto increment custom value.
Let us first create a table −
mysql> create table DemoTable -> ( -> StudentName varchar(20) -> ); Query OK, 0 rows affected (0.63 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Adam'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------------+ | StudentName | +-------------+ | Robert | | Adam | | Mike | +-------------+ 3 rows in set (0.00 sec)
Following is the query to add an autoincrement column with start value −
mysql> alter table DemoTable add column StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,AUTO_INCREMENT=1000; Query OK, 0 rows affected (1.83 sec) Records: 0 Duplicates: 0 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+-------------+-----------+ | StudentName | StudentId | +-------------+-----------+ | Robert | 1000 | | Adam | 1001 | | Mike | 1002 | +-------------+-----------+ 3 rows in set (0.00 sec)
- Related Articles
- Add a temporary column with a value in MySQL?
- Increment column value ‘ADD’ with MySQL SET clause
- Selecting a value in custom order from another column in a MySQL table with a single query
- Add user defined value to a column in a MySQL query?
- MySQL ORDER BY with custom field value
- How to Reset MySQL AutoIncrement using a MAX value from another table?
- Add to existing value in MySQL column using CONCAT function?
- Add a new value to a column of data type enum in MySQL?
- Create table query with manual AUTO_INCREMENT start value in MySQL?
- Add a positive integer constraint to an integer column in MySQL?
- Order randomly in MySQL with a random value column?
- MySQL SELECT to sum a column value with previous value
- Set custom messages on the basis of a column with student marks in MySQL
- Add a new column and index to an existing table with ALTER in a single MySQL query?
- ALTER table by adding AUTOINCREMENT in MySQL?

Advertisements