Search code examples
mysqlsqlmatchalter-tablezerofill

how to ensure mysql column has 4 character


how to ensure mysql column has 4 character ?

Due to using the MATCH function with minimum 4 char, i want my primary key which is a INT to be at least 4 char.

How can i do it ?

eg. 1 will be 0001 in sql table


Solution

  • Use ZEROFILL attribute of column. Alter your Primary key column to have zerofill enabled.

    Try this:

    ALTER TABLE `tablename` CHANGE
    COLUMN `id` `id` INT(4) ZEROFILL NOT NULL AUTO_INCREMENT ;