
- 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
Display auto increment user id sequence number to begin from 001 in MySQL?
For this, use ZEROFILL and alter the table to begin from the same sequence −
alter table yourTableName change yourColumnName yourColumnName int(3) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT PRIMARY KEY;
To understand the above syntax, let us first create a table −
mysql> create table DemoTable1958 ( UserId int, UserName varchar(20) ); Query OK, 0 rows affected (0.00 sec)
Here is the query to alter generated sequence number to begin from 001:
mysql> alter table DemoTable1958 change UserId UserId int(3) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT PRIMARY KEY; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0
Let us check the table description:
mysql> desc DemoTable1958;
This will produce the following output −
+----------+--------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+--------------------------+------+-----+---------+----------------+ | UserId | int(3) unsigned zerofill | NO | PRI | NULL | auto_increment | | UserName | varchar(20) | YES | | NULL | | +----------+--------------------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1958(UserName) values('Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1958(UserName) values('David'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1958;
This will produce the following output −
+--------+----------+ | UserId | UserName | +--------+----------+ | 001 | Chris | | 002 | David | +--------+----------+ 2 rows in set (0.00 sec)
- Related Articles
- Auto increment in MongoDB to store sequence of Unique User ID?
- How do I begin auto increment from a specific point in MySQL?
- How can I set my auto-increment value to begin from 1 in MySQL?
- How to get the next auto-increment id in MySQL?
- How to handle fragmentation of auto increment ID column in MySQL?
- How to change auto increment number in MySQL?
- MySQL query to set my auto increment column ( id ) to zero or reset the value of auto increment field?
- How to change auto increment number in the beginning in MySQL?
- Passing NULL to MySQL for auto increment?
- Change the Auto Increment counter in MySQL?
- Set custom Auto Increment with ZEROFILL in MySQL
- Extract the user ID from the username only in MySQL?
- How to auto increment with 1 after deleting data from a MySQL table?
- How to set initial value and auto increment in MySQL?
- How to make MySQL table primary key auto increment?

Advertisements