4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【コピペ用】MySQLのCOMMENT構文メモ

Posted at

クォートが必要だったり不要だったりで覚えられないので、コピペできるように覚え書きです。

テーブル

最大2048文字のコメントを残すことができる。

table_sample.sql
-- コメントの作成
CREATE TABLE `t_user_info` (
    `user_id` INT NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(100) NOT NULL,
    PRIMARY KEY (`user_id`)
) COMMENT 'ユーザ情報テーブル';

-- コメントの変更
ALTER TABLE t_user_info COMMENT '変更後のコメント';

-- コメントの確認
SHOW TABLE STATUS LIKE 't_user_info';

-- 出力
+-------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+--------------------+----------+----------------+-----------------------------+
| Name        | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time | Check_time | Collation          | Checksum | Create_options | Comment                     |
+-------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+--------------------+----------+----------------+-----------------------------+
| t_user_info | InnoDB |      10 | Dynamic    |    0 |              0 |       16384 |               0 |            0 |         0 |              1 | 2021-07-18 22:37:25 | NULL        | NULL       | utf8mb4_unicode_ci |     NULL |                | ユーザ情報テーブル          |
+-------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+--------------------+----------+----------------+-----------------------------+

カラム

最大1024文字のコメントを残すことができる。

columns_sample.sql
-- コメントの作成
CREATE TABLE `t_user_info` (
    `user_id` INT NOT NULL AUTO_INCREMENT COMMENT 'ユーザーID',
    `name` VARCHAR(100) NOT NULL COMMENT 'ユーザー名',
    PRIMARY KEY (`user_id`)
) COMMENT 'ユーザ情報テーブル';

-- コメントの変更
ALTER TABLE t_user_info MODIFY user_id INT NOT NULL AUTO_INCREMENT COMMENT '変更後のコメント';

-- コメントの確認
SHOW FULL COLUMNS FROM t_user_info;

-- 出力
+---------+--------------+--------------------+------+-----+---------+----------------+---------------------------------+-----------------+
| Field   | Type         | Collation          | Null | Key | Default | Extra          | Privileges                      | Comment         |
+---------+--------------+--------------------+------+-----+---------+----------------+---------------------------------+-----------------+
| user_id | int(11)      | NULL               | NO   | PRI | NULL    | auto_increment | select,insert,update,references | ユーザーID      |
| name    | varchar(100) | utf8mb4_unicode_ci | NO   |     | NULL    |                | select,insert,update,references | ユーザー名      |
+---------+--------------+--------------------+------+-----+---------+----------------+---------------------------------+-----------------+

参考

4
2
1

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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?