コマンドプロンプトにてMySQLを動かす


コマンドプロンプトで使うコマンド

mysqlに接続、切断

C:\xampp\mysql\bin> mysql -u root -p    //mysqlのあるディレクトリ内で実行

MariaDB [(none)]>                   //このようになればOK
MariaDB [(none)]> exit               //mysqlから切断

ステータス確認

MariaDB [(none)]> status;
//いろんな情報が出てきます()

データベース確認

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| example            |
| information_schema |
| mysql              |
| performance_schema |
| phpmyadmin         |
| test               |
+--------------------+

データベースの作成、確認

MariaDB [(none)]> create database データベース名;
MariaDB [(none)]> show databases;
MariaDB [(none)]> create database test2;
Query OK, 1 row affected (0.002 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| example            |
| information_schema |
| mysql              |
| performance_schema |
| phpmyadmin         |
| test               |
| test2              | //追加されていれば成功
+--------------------+

使用するデータベースの指定

MariaDB [(none)]> use データベース名;
MariaDB [(none)]> use test2;
Database changed
MariaDB [test2]>  //[]のなかが変更されていれば成功 

テーブルの作成、確認

MariaDB [test2]> create table テーブル名(
-> テーブルのフィールド情報を入力
->);

MariaDB [test2]> show tables;
MariaDB [test2]> describe テーブル名;
MariaDB [test2]> create table students(
-> id int,
-> first_name varchar(60),
-> last_name varchar(60) //最後に「,」は不要
->);

MariaDB [test2]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| students       |
+----------------+

MariaDB [test2]> describe students;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id         | int(11)     | YES  |     | NULL    |       |
| first_name | varchar(60) | YES  |     | NULL    |       |
| last_name  | varchar(60) | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+

テーブルの中身(レコード)の追加、確認

MariaDB [test2]> insert into テーブル名 values
    -> ('フィールド1の要素','フィールド2の要素','フィールド3の要素');

MariaDB [test2]> select * from テーブル名;

MariaDB [test2]> insert into students values
    -> ('1','hogehoge','pugepuge');

MariaDB [test2]> select * from students;
+------+------------+-----------+
| id   | first_name | last_name |
+------+------------+-----------+
|    1 | hogehoge   | pugepuge  |
+------+------------+-----------+

テーブルの中身の表示(部分的に)

MariaDB [test2]> select * from テーブル名 limit 範囲指定;
MariaDB [test2]> select * from students;
+------+------------+-----------+
| id   | first_name | last_name |
+------+------------+-----------+
|    1 | user1      | pugepuge  |
|    2 | qqqq       | pppp      |
|    3 | bbb        | aaaa      |
+------+------------+-----------+
3 rows in set (0.000 sec)

MariaDB [test]> select * from students limit 0,1;
+------+------------+-----------+
| id   | first_name | last_name |
+------+------------+-----------+
|    1 | user1      | pugepuge  |
+------+------------+-----------+

テーブルの中身(レコード)の全削除

MariaDB [test2]> delete from テーブル名;
MariaDB [test2]> delete from user;
Query OK, 1 row affected (0.004 sec)

//確認してみる
MariaDB [test2]> select * from students;
Empty set (0.000 sec) // emptyとなっていれば成功

テーブルの中身(レコード)の指定して削除

MariaDB [test2]> delete from テーブル名 where テーブルの要素 = 消したいとこ;
MariaDB [test2]> delete from students where id = 1;
MariaDB [test2]> select * from students;
Empty set (0.000 sec) // emptyとなっていれば成功

テーブルの中身の更新(変更)

MariaDB [test2]> select * from students;
//現在の状態
+------+------------+-----------+
| id   | first_name | last_name |
+------+------------+-----------+
|    1 | hogehoge   | pugepuge  |
+------+------------+-----------+
//変更
MariaDB [test2]> update テーブル名 set 変更したいテーブルのフィールド名=変更後の名前 where 変更したいテーブルのフィールド名=そのまま入力;
MariaDB [test2]> update students set first_name='test' where last_name='pugepuge';
Query OK, 1 row affected (0.004 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [test2]> select * from students;
+------+------------+-----------+
| id   | first_name | last_name |
+------+------------+-----------+
|    1 | test       | pugepuge  |
+------+------------+-----------+
1 row in set (0.002 sec)

日付型と時刻型のデータ型

  • date
    • 日付
    • YYYY-MM-DD
  • datetime
    • 日付と時間

MariaDB [jikken]> create table simple_bbs(
-> id int auto_increment,
-> time datetime,
-> name varchar(60),
-> comment varchar(60)
-> ,
-> index(id)
-> );


こんなもんですかね。
実際xamppさんはかなり強い雰囲気を漂わせていて、上に記したMySQLのページに行くとデータベースなり、テーブルがGUIで作れたり、テーブルの中身見れたりと結構便利な感じがします。

あとはPHP書いてそこに作ったデータベースの情報を書いて終わり!!!

補足

xampp

xamppとは無料でMariaDBやPHPとかをインストールできるすごいやつ。
僕自身、xamppを使って環境構築をしました

URL系