実行環境 CentOS6.9
ターミナルでのコマンド
実行 /etc/init.d/mysqld start
状況確認 /etc/init.d/mysqld status
管理することができるプログラム一覧
chkconfig --list
電源を立ち上げた時にプログラムを立ち上げる
chkconfig mysqld on
MySQL 内のコマンド
ログインしているユーザーを表示
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
使用できるDBを表示
mysql> show databases;
使用するDBを選択
mysql> use webapp
Database changed
Mysqlの環境を確認
mysql> status
カラムの一覧表示
使用するDB選択後
mysql> desc users;
入出力のリダイレクション
sqlの取り込み
mysql -u root -p < db.sql
selcet文
student テーブルからstudent_idを取り出す時
mysql> select student_id from student;
+------------+
| student_id |
+------------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 7 |
| 8 |
| 9 |
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
| 16 |
+------------+
studentテーブルからすべての情報を表示する場合
mysql> select * from student;
簡単検索
mysql > select * from table名 where カラム名 like '%検索ワード%';
任意の
linuxとワイルドカードを比較
Linux | Mysql | |
---|---|---|
任意の0文字以上の文字列 | * | % |
任意の1文字 | ? | _ |
join
ほとんどの場合は等価結合を用いる。
SELECT a_column,b_column, c_column
FROM a_table,b_table
WHERE a_table.b_table_id = b_table.b_table_id;
b_tableのb_table_idがa_tableのb_table_idに対応して、関連させることができる。
order_by
検索結果の並び替えをおこなうことができる。
select構文の指定順番
順番を覚えておこう。
順番を間違えるとエラーになってしまう。
SELECT ...
FROM ...
WHERE ...
GROUP BY ...
HAVING ...
ORDER BY ...
LIMIT ...
mysql work banch
mysqlが公式で出しているworkbenchというのが
設計する上で便利
https://www-jp.mysql.com/products/workbench/