目次
とある日
MySQL 8.0.27がリリースされました。
追加機能がいくつかあるのですが、その中のBINARYオペレータ
について調べます。
MySQL 8.0.27での変更(2021-10-19、一般提供)
重要な変更
Important Change: The
BINARY
operator is now deprecated, and subject to removal in a future release of MySQL. Use ofBINARY
now causes a warning. UseCAST(... AS BINARY)
instead.重要な変更:
BINARY
オペレータが廃止され、およびMySQLの将来のリリースで除去対象。BINARY
nowを使用すると、警告が発生します。CAST(... AS BINARY)
代わりに使用してください 。
SELECT binary 'a' = 'A';
といったSQLの実行が今後除去対象となるため実行時に警告が表示されるようになった変更です。
文字列の厳格な比較をする際にバイナリに変換して比較する際によく使用するイメージ。
検証
MySQL8.0.27 で BINARYオペレーターを使うと警告が表示されるらしいので簡単な内容ですが検証してみます。
MySQL 8.0.26
root@5afddc4dcabd:/# mysql -u docker -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 8.0.26 MySQL Community Server - GPL Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> SELECT binary 'a' = 'A'; +------------------+ | binary 'a' = 'A' | +------------------+ | 0 | +------------------+ 1 row in set (0.00 sec) mysql> show warnings; Empty set (0.00 sec)
MySQL 8.0.27
root@61a2a5737d7a:/# mysql -udocker -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 10 Server version: 8.0.27 MySQL Community Server - GPL Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> SELECT binary 'a' = 'A'; +------------------+ | binary 'a' = 'A' | +------------------+ | 0 | +------------------+ 1 row in set, 1 warning (0.01 sec) mysql> show warnings ; +---------+------+----------------------------------------------------------------------------------------------+ | Level | Code | Message | +---------+------+----------------------------------------------------------------------------------------------+ | Warning | 1287 | 'BINARY expr' is deprecated and will be removed in a future release. Please use CAST instead | +---------+------+----------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
'BINARY expr' is deprecated and will be removed in a future release. Please use CAST instead
実行すると警告が発生します。
解説
現在のMySQLではバイナリ変換する方法が3つある。
名前 | 説明 | 非推奨 |
---|---|---|
BINARY |
文字列をバイナリ文字列にキャストします | 8.0.27 |
CAST() |
特定のタイプとして値をキャストする | |
CONVERT() |
特定のタイプとして値をキャストする |
CAST
とCOVERT
の違いとしてはCOVERT
のほうができることが多い。
ただ、CAST
はANSI規格らしい。
どっちがいいとかは調べてみたけどあまりわからなかった。
BINARY
オペレータ
BINARY
expr
BINARY
オペレータは、バイナリ文字列(持つ文字列に式を変換binary
文字セットとbinary
照合します)。の一般的な使用法BINARY
は、文字列の比較を、文字ごとではなく数値のバイト値を使用してバイトごとに実行することです。BINARY
また、オペレータは、重要であるとの比較で末尾のスペースが発生します。文字セットの照合と非バイナリ文字セットのbinary
照合の 違いについては、 10.8.5項「_bin照合と比較したバイナリ照合」を参照してください。binary``_bin
mysql> SELECT binary 'a' = 'A'; +------------------+ | binary 'a' = 'A' | +------------------+ | 0 | +------------------+ 1 row in set, 1 warning (0.01 sec)
CAST(... AS BINARY)
CAST(expr AS type [ARRAY])
BINARY[(N)]
VARBINARY式exprが空(長さがゼロ)の場合、結果の型がBINARY(0)。であることを除いて 、データ型の文字列を生成し ます 。オプションの長さNが指定されている 場合、 キャストは引数のバイト以下を使用 します。バイトより短い値には、長さ。までのバイトが埋め込まれます。オプションの長さの場合BINARY(N)NN0x00NNが指定されていない場合、MySQLは式から最大長を計算します。提供または計算された長さが内部しきい値より大きい場合、結果のタイプは BLOBです。それでも長さが長すぎる場合、結果のタイプはLONGBLOBです。
mysql> SELECT cast('a' as binary) = cast('A' as binary); +-------------------------------------------+ | cast('a' as binary) = cast('A' as binary) | +-------------------------------------------+ | 0 | +-------------------------------------------+ 1 row in set (0.00 sec) mysql> show warnings ; Empty set (0.00 sec)
CAST
では警告は表示されていない。
今後は、BINARY変換はこっちを推奨?してるみたい。
CONVERT(expr
,type
),
CONVERT(expr
USING transcoding_name
)
CONVERT() および CAST() 関数には、任意の型の式が指定され、指定された型の結果値が生成されます。
mysql> SELECT CONVERT('a',binary) = CONVERT('A',binary) ; +-------------------------------------------+ | CONVERT('a',binary) = CONVERT('A',binary) | +-------------------------------------------+ | 0 | +-------------------------------------------+ 1 row in set (0.00 sec) mysql> show warnings ; Empty set (0.00 sec)
こちらも、警告は表示されていない。
〆
多要素認証の追加について調べようとしたけど、検証できなさそうという理由からバイナリの変更を調べました。
多要素認証の追加は面白そうですが、使うタイミングがあるのか不思議ですね。
他にも変更があるのでゆっくりと追っていこうかなと。