そろばんのしょ(第3版第2刷)

雑魚い見習いDBAの日々学んだことや、どーでもいいことを。基本は自分の備忘録。

MySQLのHELP構文ってやつ

この記事は MySQL Advent Calendar 2019の16日目です。 昨日はdupont-kedamaさんのMySQL5.5からMySQL8.0にマイグレーションしたゆるい話 でした。


MySQLにはHELP構文というものがあって、構文とかをサクッと調べられるということは聞いたことあったけど、ちゃんと使ったことがなかったのでこの際に調べてみた。

dev.mysql.com

基本構文は下記。

mysql> HELP '<検索文字列>'

mysqlスキーマのhelp関連のテーブルを返しているらしい。

mysql> show tables like 'help%';
+-------------------------+
| Tables_in_mysql (help%) |
+-------------------------+
| help_category           |
| help_keyword            |
| help_relation           |
| help_topic              |
+-------------------------+
4 rows in set (0.00 sec)

このあたりかな。

コンテンツ一覧を見るにはHELP 'contents', 型の一覧を見るにはHELP 'data_types'を利用する。

HELP 'contents'を実行した時に出力されるコンテンツを選んで実行するとさらに項目を選ぶように言われる。

例えばDROP ROLEの項目を見たいとしたら、

mysql> HELP 'contents'                                                                                                                             
You asked for help about help category: "Contents"
For more information, type 'help <item>', where <item> is one of the following
categories:
   Account Management
   Administration
   Components
   Compound Statements
   Contents
   Data Definition
   Data Manipulation
   Data Types
   Functions
   Geographic Features
   Help Metadata
   Language Structure
   Plugins
   Storage Engines
   Table Maintenance
   Transactions
   User-Defined Functions
   Utility

mysql> HELP 'Account Management'
You asked for help about help category: "Account Management"
For more information, type 'help <item>', where <item> is one of the following
topics:
   ALTER RESOURCE GROUP
   ALTER USER
   CREATE RESOURCE GROUP
   CREATE ROLE
   CREATE USER
   DROP RESOURCE GROUP
   DROP ROLE
   DROP USER
   GRANT
   RENAME USER
   REVOKE
   SET DEFAULT ROLE
   SET PASSWORD
   SET RESOURCE GROUP
   SET ROLE

mysql> HELP 'DROP ROLE'
Name: 'DROP ROLE'
Description:
Syntax:
DROP ROLE [IF EXISTS] role [, role ] ...

DROP ROLE removes one or more roles (named collections of privileges).
To use this statement, you must have the global DROP ROLE or CREATE
USER privilege. When the read_only system variable is enabled, DROP
ROLE additionally requires the CONNECTION_ADMIN or SUPER privilege.
(以下略)

HELP DROP からでも辿れるっぽい

> HELP DROP
Many help items for your request exist.
To make a more specific request, please type 'help <item>',
where <item> is one of the following
topics:
   ALTER TABLE
   ALTER TABLESPACE
   DEALLOCATE PREPARE
   DROP DATABASE
   DROP EVENT
   DROP FUNCTION
   DROP FUNCTION UDF
   DROP INDEX
   DROP PREPARE
   DROP PROCEDURE
   DROP RESOURCE GROUP
   DROP ROLE
   DROP SCHEMA
   DROP SERVER
   DROP SPATIAL REFERENCE SYSTEM
   DROP TABLE
   DROP TABLESPACE
   DROP TRIGGER
   DROP USER
   DROP VIEW

mysql> HELP DROP ROLE 
Name: 'DROP ROLE'
Description:
Syntax:
DROP ROLE [IF EXISTS] role [, role ] ...
(以下略)

地味に便利だ。

その他にも組み込み関数とかもサクッと調べてられて引数とか忘れたときに便利。

mysql> > HELP format_bytes
Name: 'FORMAT_BYTES'
Description:
FORMAT_BYTES(count)

Given a numeric byte count, converts it to human-readable format and
returns a string consisting of a value and a units indicator. The
string contains the number of bytes rounded to 2 decimal places and a
minimum of 3 significant digits. Numbers less than 1024 bytes are
represented as whole numbers and are not rounded.

URL: https://dev.mysql.com/doc/refman/8.0/en/performance-schema-functions.html

Examples:
mysql> SELECT FORMAT_BYTES(512), FORMAT_BYTES(18446644073709551615);
+-------------------+------------------------------------+
| FORMAT_BYTES(512) | FORMAT_BYTES(18446644073709551615) |
+-------------------+------------------------------------+
|  512 bytes        | 16.00 EiB                          |
+-------------------+------------------------------------+

ただ、完全一致じゃないと表示はしてくれないらしい。

mysql> help format_byte

Nothing found
Please try to run 'help contents' for a list of all accessible topics

「%」を使ったワイルドカード検索はできるのでこっちを使えばよいのかな。

mysql> help %ormat_byte%
Name: 'FORMAT_BYTES'
Description:
FORMAT_BYTES(count)
(以下略)

今後は困ったら積極的に使っていきたい。