Love澤's Room

技術系ネタをまとめていたブログ。現在はカテゴリにこだわらず更新中。

MariaDB(MySQL)でテーブルの定義を調べる3つの方法

新サイトへ移転しました
約3秒後に自動的にリダイレクトします。


たまにテーブルの制約の状況を確認したいのだけど、よく忘れてググるのでメモ。ググる時に、サイトによって確認するコマンドが異なるのが覚えにくい原因なのかも(と言い訳しておく)。

以下に2つ3つの例を示す。全く同じ事を確認できるのだけど、もしかしたら、本当は細かに違いがあるのかもしれない。(2014/08/14追記:3つ目の確認方法=describe文を見つけたので追記しています。)

MariaDB [test_db]> show columns from test_table;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| name     | varchar(20) | NO   |     | NULL    |                |
| birthday | date        | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

MariaDB [test_db]> show fields from test_table;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| name     | varchar(20) | NO   |     | NULL    |                |
| birthday | date        | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

MariaDB [test_db]> describe test_table;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| name     | varchar(20) | NO   |     | NULL    |                |
| birthday | date        | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

他のコマンドでも同様のことを確認できる場合は追記していく。

ちなみに、以下の様な環境で実行した。

MariaDB [(none)]> show databases;  // データベース一覧
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test_db            |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> use test_db;  // データベースの選択
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [test_db]> show tables;  // テーブル一覧
+-------------------+
| Tables_in_test_db |
+-------------------+
| test_table        |
+-------------------+
1 row in set (0.00 sec)