LoginSignup
0
0

More than 3 years have passed since last update.

「初心者向け」ターミナルからmysqlでカラム一覧をみる方法。

Last updated at Posted at 2021-03-13

記事を探すのに手間取ったので投稿します。

基本知識
database>table>colums
という階層となっていることに注意。

<ターミナルでmysqlを起動>

% mysql -u root -p;         
Enter password: 

パスワードを入力したら
以下のように表示が変わる

mysql>

<データベース一覧を表示>
*注意 コマンドの最後に" ; "(セミコロン)を忘れないように!

mysql> show databases;

結果こうなる

+----------------------------+
| Database                   |
+----------------------------+
| blog_development           |
| information_schema         |
| mysql                      |
| performance_schema         |
| Recommend_spot_development |
| Recommend_spot_test        |
| recospo_development        |
| recospo_test               |
| sample_app_development     |
| sample_app_test            |
| sample_app_test-0          |
| sample_app_test-1          |
| sample_app_test-2          |
| sample_app_test-3          |
| sys                        |
+----------------------------+
15 rows in set (0.01 sec)

<databaseを指定する>
いきなりテーブルやカラムを取り出そうとするとdatabase選択されてないぞと怒られます。
以下のように指定する。

mysql> use recospo_development(データベース名);

<テーブルを確認>

mysql> show tables from recospo_development(データベース名);

結果がこちら

+-------------------------------+
| Tables_in_recospo_development |
+-------------------------------+
| active_storage_attachments    |
| active_storage_blobs          |
| ar_internal_metadata          |
| microposts                    |
| relationships                 |
| schema_migrations             |
| users                         |
+-------------------------------+
7 rows in set (0.00 sec)

<カラム一覧表示>

mysql> show columns from microposts(テーブル名);

結果がこちら

+------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| id         | bigint      | NO   | PRI | NULL    | auto_increment |
| content    | text        | YES  |     | NULL    |                |
| user_id    | bigint      | NO   | MUL | NULL    |                |
| created_at | datetime(6) | NO   |     | NULL    |                |
| updated_at | datetime(6) | NO   |     | NULL    |                |
+------------+-------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)

おわり!!

こんな基礎のことなのに、記事探してもあんまり見つからなかったので、備忘録にかきました。

0
0
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
0
0