Syntax error Display auto increment user id sequence number to begin from 001 in MySQL?

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)
Updated on: 2019-12-31T07:14:27+05:30

727 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements