Home >Database >Mysql Tutorial >How do I customize MySQL autoincrement format to display zero-filled digits?

How do I customize MySQL autoincrement format to display zero-filled digits?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-29 02:03:021065browse

How do I customize MySQL autoincrement format to display zero-filled digits?

Customizing MySQL Autoincrement Format to Zero-Filled Digits

Autoincrement functionality in MySQL assigns unique sequentially increasing values to new records inserted into a table. By default, autoincrement values are displayed without leading zeros. However, there may be scenarios where you need the autoincrement format to be in a specific format, such as four-digit zero-filled numbers.

To achieve this, you can utilize the ZEROFILL attribute when creating the autoincrement column. Here's how:

<code class="sql">ALTER TABLE table_name
MODIFY column_name INT NOT NULL AUTO_INCREMENT ZEROFILL;</code>

By adding the ZEROFILL attribute to the column definition, MySQL will automatically prepend leading zeros to the autoincrement values to match the specified format. For example, instead of displaying '1,' the autoincrement value will become '0001.'

The above is the detailed content of How do I customize MySQL autoincrement format to display zero-filled digits?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn