LoginSignup
9
10

More than 5 years have passed since last update.

MySQLでの基本コマンド

Last updated at Posted at 2016-06-12

以前上げた記事Node.js導入->MySQLへ接続でMySQLへの接続をやりました
Arduinoのセンサーから得た値をinsert する事は出来たので
よく使うMySQLコマンドをまとめておきます。

#基本コマンド sshやシェルからDBへ接続

$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 108836
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
#-Dオプションをつけてデータベースを指定して接続も出来る
$ mysql -u root -D test -p

#データベース名の確認
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| fess_db            |
| fess_robot         |
| mysql              |
| passwords          |
| sample_db          |
| secret             |
| test               |
+--------------------+
8 rows in set (0.00 sec)
#DBの作成
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| passwords          |
| test               |
+--------------------+
3 rows in set (0.00 sec)

mysql> create database sample_db;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| passwords          |
| sample_db          |
| test               |
+--------------------+
4 rows in set (0.00 sec)

#DBの選択
mysql> use test;
Database changed
#テーブル名の表示
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test           |
| test_table     |
| test_table2    |
+----------------+
3 rows in set (0.00 sec)
#テーブルからデータを全選択して表示 ここではtest_tableを選択
mysql> select * from test_table;
+------+------+----------------------+------------+
| id   | memo | datetime             | temprature |
+------+------+----------------------+------------+
|    1 | 1st  | NULL                 |       NULL |
|    2 | 2nd+ | NULL                 |       NULL |
| NULL | NULL | test                 |          1 |
| NULL | NULL | 2016/05/26 12?08?27? |          0 |
| NULL | NULL | 2016/05/26 12?10?09? |          0 |
| NULL | NULL | 2016/05/26 12?11?25? |          0 |
| NULL | NULL | 2016/05/26 12:17:45  |      26.26 |
.
.
#INSERT INTO "テーブル名" (カラム名) VALUES("値");でレコードが挿入出来る こんな感じ
mysql> INSERT INTO test_table (datetime,temprature) VALUES(1,1);
Query OK, 1 row affected (0.00 sec)

mysql> select * from test_table;
.
.
.
 NULL | NULL | 1465564176613        |      26.72 |
| NULL | NULL | 1465564186599        |      26.72 |
| NULL | NULL | 1465564196589        |      26.72 |
| NULL | NULL | 1465564206574        |      26.72 |
| NULL | NULL | 1465564216560        |      26.72 |
| NULL | NULL | 1                    |          1 |
| NULL | NULL | 1                    |          1 |
+------+------+----------------------+------------+
108709 rows in set (0.11 sec)


# テーブルの作成(userデータベースを作成したので上とは違うDBです)
mysql> create table user(
    -> id int(11) not null auto_increment,
    -> name varchar(30)
    -> ,
    -> age int(11),
    -> pass varchar(50),
    -> created_date datetime,
    -> primary key(id));
Query OK, 0 rows affected (0.06 sec)

mysql> show tables;
+---------------------+
| Tables_in_test_user |
+---------------------+
| user                |
+---------------------+
1 row in set (0.00 sec)
#DB削除する危険なコマンド
#下手に打つとDB毎死ぬので熟練者以外は自宅環境の検証以外では使わない方が良い

mysql> drop database sample_db;
Query OK, 3 rows affected (0.18 sec)

その他使ったコマンドを追記していきます。

参考
Mysqlコマンド
シェルスクリプト練習 -MySQLのテストDBにランダムに生成したデータを入れる-

9
10
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
9
10