
- 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
Set auto increment initial value for MySQL table using ALTER command
To set auto increment initial value for MySQL table, use ALTER command. The first step would be
alter table yourTableName modify yourColumnName int NOT NULL AUTO_INCREMENT PRIMARY KEY,add index(yourColumnName);
The second step is as follows
alter table yourTableName AUTO_INCREMENT=yourStartingValue;
To understand the above syntax, let us create a table. The query to create a table is as follows
mysql> create table setAutoIncrementDemo -> ( -> UserId int, -> UserName varchar(20) -> ); Query OK, 0 rows affected (0.75 sec)
Now implement the above two steps to set auto increment initial value for MySQL table.
Step 1 -The query is as follows
mysql> alter table setAutoIncrementDemo modify UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,add index(UserId); Query OK, 0 rows affected (1.51 sec) Records: 0 Duplicates: 0 Warnings: 0
Step 2 -The query is as follows
mysql> alter table setAutoIncrementDemo AUTO_INCREMENT=1000; Query OK, 0 rows affected (0.34 sec) Records: 0 Duplicates: 0 Warnings: 0
Insert some records in the table using insert command.
The query is as follows
mysql> INSERT INTO setAutoIncrementDemo(UserName) values('John'); Query OK, 1 row affected (0.14 sec) mysql> INSERT INTO setAutoIncrementDemo(UserName) values('Carol'); Query OK, 1 row affected (0.12 sec) mysql> INSERT INTO setAutoIncrementDemo(UserName) values('Sam'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from setAutoIncrementDemo;
The following is the output
+--------+----------+ | UserId | UserName | +--------+----------+ | 1000 | John | | 1001 | Carol | | 1002 | Sam | +--------+----------+ 3 rows in set (0.00 sec)
- Related Articles
- How to set initial value and auto increment in MySQL?
- Truncate a MySQL table and then set a custom value to auto increment
- MySQL query to set my auto increment column ( id ) to zero or reset the value of auto increment field?
- How to set the initial value of an auto-incremented column in MySQL using JDBC?
- Set custom Auto Increment with ZEROFILL in MySQL
- Passing NULL to MySQL for auto increment?
- How to make MySQL table primary key auto increment?
- How can we use MySQL ALTER TABLE command for adding comments on columns?
- How to create a table with auto-increment column in MySQL using JDBC?
- How can I set my auto-increment value to begin from 1 in MySQL?
- How to set auto-increment to an existing column in a table using JDBC API?
- How to auto-increment value of tables to lower value in MySQL?
- Changing the current count of an Auto Increment value in MySQL?
- Set MySQL int column to auto increment by 1 beginning at 10000?
- Increment column value ‘ADD’ with MySQL SET clause

Advertisements