5
8

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.

LaravelのMYSQLのコマンド一覧【随時更新予定】

Last updated at Posted at 2020-02-29

はじめに

MYSQLのコマンドを覚える度に記録しています。

目次

  • MYSQLの起動
  • MYSQLの接続
  • データベースの選定
  • テーブル名一覧
  • テーブル内構成一覧
  • テーブル内の保存データ一覧
  • テーブルの削除
  • テーブルを空にする

MYSQLの起動

$ sudo service mysqld start

余談ですが、下記エラーが出るとMYSQLが起動していない可能性があります。

$ SQLSTATE[HY000] [2002] Connection refused

MYSQLの接続

$ mysql -u root

接続に成功すると下記表示になります。

mysql> 

データベースの選定

mysql> use データベース名

テーブル名一覧

mysql> show tables;
+--------------------+
| Tables_in_myhiking |
+--------------------+
| admins             |
| migrations         |
| news               |
| password_resets    |
| profiles           |
| users              |
+--------------------+

テーブル内構成一覧

mysql> describe データベース名.テーブル名;
+-------------------+---------------------+------+-----+---------+----------------+
| Field             | Type                | Null | Key | Default | Extra          |
+-------------------+---------------------+------+-----+---------+----------------+
| id                | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| name              | varchar(255)        | NO   |     | NULL    |                |
| email             | varchar(191)        | NO   | UNI | NULL    |                |
| email_verified_at | timestamp           | YES  |     | NULL    |                |
| password          | varchar(255)        | NO   |     | NULL    |                |
| remember_token    | varchar(100)        | YES  |     | NULL    |                |
| created_at        | timestamp           | YES  |     | NULL    |                |
| updated_at        | timestamp           | YES  |     | NULL    |                |
| deleted_at        | timestamp           | YES  |     | NULL    |                |
+-------------------+---------------------+------+-----+---------+----------------+

テーブル内の保存データ一覧

mysql> select * from テーブル名
+----+----------+--------------------+-------------------+--------------------------------------------------------------+----------------+
| id | name  | email    | email_verified_at | password | remember_token | created_at          | updated_at          | deleted_at          
+----+----------+--------------------+-------------------+--------------------------------------------------------------+----------------+
|  1 | name1 | a@a.com  | NULL              | ***      | NULL           | 2020-02-21 00:32:05 | 2020-02-21 00:32:05 | NULL                
|  2 | name2 | b@b.com  | NULL              | ***      | NULL           | 2020-02-28 02:15:10 | 2020-02-29 02:01:49 | 2020-02-29 02:01:49 
|  3 | name3 | c@c.com  | NULL              | ***      | NULL           | 2020-02-28 02:22:16 | 2020-02-29 01:56:39 | 2020-02-29 01:56:39 
+----+----------+--------------------+-------------------+--------------------------------------------------------------+----------------+

特定のカラムを表示させたい場合

MYSQL> select カラム名1, カラム名2, カラム名3 from テーブル名;

例)

mysql> select id, name, deleted_at from users;                                                                                                                                                                                                  
+----+----------+---------------------+
| id | name  | deleted_at          |
+----+----------+---------------------+
|  1 | name1 | NULL                |
|  2 | name2 | 2020-02-29 02:01:49 |
|  3 | name3 | 2020-02-29 01:56:39 |
+----+----------+---------------------+

テーブルの削除

mysql> drop table テーブル名;

テーブルを空にする

mysql> delete from テーブル名;
5
8
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
5
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?