LoginSignup
7
2

More than 1 year has passed since last update.

MySQL 8.0 から int の ZEROFILL 指定が deprecated

Last updated at Posted at 2022-02-17

僕の現場で、本件の warning が出ていて調べたこと、対応したことを簡単にまとめます。
ちゃんとわかって使っていれば問題にならないんですが、そうは言っても以下の背景のような場面ってあるよね、という前提で見てもらえるとありがたいです。

概要

  • int(x) ZEROFILL が deprecated
  • MySQL 8.0.19 から、 (x) は表示されない

放っておくことによる弊害

ありません。
が、deprecated なので順次消していくのが適切と言えます。

背景

(x) が保存桁数と勘違いされて運用される場面がある。
そのため、本来 ZEROFILL とセットで使うべき (x) が単体で用いられることがある。

MySQL 8.0 にバージョンアップした環境で CREATE TABLE したり、ADD COLUMN すると warning が出る。

MySQL 8.0.17 で廃止され、MySQL 8.0.19 以降は、CREATE TABLE からも (x) が表示されなくなる。
MySQL 公式

Display width specification for integer data types was deprecated in MySQL 8.0.17, and now statements that include data type definitions in their output no longer show the display width for integer types, with these exceptions:

The type is TINYINT(1). MySQL Connectors make the assumption that TINYINT(1) columns originated as BOOLEAN columns; this exception enables them to continue to make that assumption.

The type includes the ZEROFILL attribute.

公式

LPAD() 関数を使うか、CHAR カラムに入れましょうと書いてある。
https://dev.mysql.com/doc/refman/8.0/ja/numeric-type-syntax.html

MySQL 8.0.17 では、ZEROFILL 属性は数値データ型では非推奨です。将来のバージョンの MySQL ではサポートされなくなる予定です。 この属性の効果を生成する別の方法の使用を検討してください。 たとえば、アプリケーションでは、LPAD() 関数を使用して、必要な幅まで数値をゼロ埋めたり、書式設定された数値を CHAR カラムに格納したりできます。

warning

warning が出るテーブル定義で CREATE TABLE する。

mysql> create table zerofill_test(
    ->     id bigint unsigned primary key auto_increment,
    ->     zerofill_on int(10) zerofill not null,
    ->     zerofill_off int not null
    -> )
    -> ;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

mysql> show warnings\G
*************************** 1. row ***************************
  Level: Warning
   Code: 1681
Message: The ZEROFILL attribute is deprecated and will be removed in a future release. Use the LPAD function to zero-pad numbers, or store the formatted numbers in a CHAR column.
*************************** 2. row ***************************
  Level: Warning
   Code: 1681
Message: Integer display width is deprecated and will be removed in a future release.
2 rows in set (0.01 sec)

以下、warning を DeepL で直訳してみると、読んで理解できる内容であることがわかります。

ZEROFILL

The ZEROFILL attribute is deprecated and will be removed in a future release. Use the LPAD function to zero-pad numbers, or store the formatted numbers in a CHAR column.

  • 直訳

ZEROFILL属性は非推奨で、将来のリリースで削除される予定です。ゼロパッドの数値にはLPAD関数を使用するか、CHAR列にフォーマットされた数値を格納します。

int の (x)

Integer display width is deprecated and will be removed in a future release.

  • 直訳

整数の表示幅は非推奨であり、将来のリリースで削除される予定です。

対応

int(x) ZEROFILL

  • (x) ZEROFILL を削除
  • ZEROFILL が必要なら LPAD 関数を使うよう修正
    • 個人的にはビジネス要件が多いのでアプリケーションで対応したい

int(x)

  • (x) を削除
  • 他 (アプリケーションなど) を修正する必要はない(はず)

tinyint(1)

関連して、今まで boolean として tinyint(1) を使っていた場合も同様に、warning が出る。

よって、boolean として使いたいのであれば、明示的に boolean と指定する。

7
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
2