前回の記事です
MySQLをMacに導入して基礎を学ぶ(2)
前回はテーブルを作成してフィールドを制限付きで定義しました。
今回はレコードを作成していきます。
とっととmysqlの基礎は終えたいですね
前回まで
前回は条件付きフィールドを作ったということで、ターミナルから
$ mysql -u dbuser -p
use app_test
show tables
でテーブルaccounts
を作成
なお
desc accounts
mysql> desc accounts;
+--------------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-----------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_name | varchar(255) | YES | MUL | NULL | |
| user_email | varchar(255) | YES | UNI | NULL | |
| phone_number | char(20) | YES | | NULL | |
| password | char(30) | YES | | NULL | |
| sex | enum('male','female') | YES | | male | |
| created | datetime | YES | | NULL | |
+--------------+-----------------------+------+-----+---------+----------------+
7 rows in set (0.01 sec)
と内容を確認できます。
レコードの挿入
insert into accounts(user_name,user_email,phone_number,password,created) values('yamageni','yama@yama.com','080-333-333','aaa','2015-09-17 00:00:00');
これでそれぞれのフィールド名accounts()
に対して対応するvalue()
が挿入されます。
select * from accounts;
さてわかったことは
- idのauto_incrementはエラーが出て挿入失敗しても数える
auto_incrementは任意に値を挿入すると次はその値を参考にする
datetimeの型に注意
2015-09-17 00:00:00
レコードをまとめて挿入
次はデータベースらしくデータの抽出など行います
まずはまとめてレコードを挿入します。
insert into score(create_datetime,name,test_score,class,email) value(
'2015-2-2 00:00:00','山田',79,'A','yamada@mail.com'),
('2015-3-3 00:00:00','山本',100,'B','yamamoto@mail.jp'),
('2015-4-3 00:00:00','田中',90,'C','tanaka@mail.com'),
('2015-5-3 00:00:00','佐藤',50,'B','satoh@mail.jp'),
('2015-6-3 00:00:00','佐々木',60,'B','sasaki@mail.jp');
select * from score;
+----+---------------------+-----------+------------+-------+------------------+
| id | create_datetime | name | test_score | class | email |
+----+---------------------+-----------+------------+-------+------------------+
| 1 | 2015-02-02 00:00:00 | 山田 | 79 | A | yamada@mail.com |
| 2 | 2015-03-03 00:00:00 | 山本 | 100 | B | yamamoto@mail.jp |
| 3 | 2015-04-03 00:00:00 | 田中 | 90 | C | tanaka@mail.com |
| 4 | 2015-05-03 00:00:00 | 佐藤 | 50 | B | satoh@mail.jp |
| 5 | 2015-06-03 00:00:00 | 佐々木 | 60 | B | sasaki@mail.jp |
+----+---------------------+-----------+------------+-------+------------------+
レコードの抽出
レコードの抽出について説明します
先ほどから使っていた
select * from `テーブル名`
ですが、任意のレコードを取り出すのに使うsql分です。
まずはざっくりと
select `フィールド名` from `テーブル名` where `条件式`;
と覚えます。
意味は英語そのままですね
*
はすべてのをさします
なので、
select * from score;
全てのフィールドに置いて、レコードを持ってくる処理ですね。
フィールド名を指定する
では
select create_datetime, name from score;
を実行しましょう。
name と create_datetimeが表示されます。
条件の指定
- 大小比較
select * from score where score =< 95;
>
<
<=
>=
=
!=
等==
でないことに注意
またdatetimeにも使えます。
select * from score where create_datetime > '2015-05-15 00:00:00';
select * from score where create_datetime > '2015-05-15';
- in句
in句は()内の条件に等しくなるものを抽出します。
select * from score where test_score in(100,50,60);
not inもあります
select * from score where test_score not in(90,79);
- 曖昧検索のlike
select * from score where email like '%@mail.com';
%は任意の長さの文字列を示してます。
また
select * from score where email like 'satoh%';
など後ろにつけたり
select * from score where email like 'sa%mail.__';
など途中に加えることができます。
正規表表現に近い形になっています。
なお_は一文字の任意のものです
- or文、and文
and,orで条件をつなげることができます
select * from score where email like 'sa%mail.__' and test_score in(50,100);
select * from score where email like 'sa%mail.__' or test_score in(50,100);
要素の並べ替え
要素を並べ替えるのはorder句です
select * from score order by test_score;
小さい順に並びます
データ自身が並び変わったわけではありません
where句とも使えます
その場合はwhereが先です
select * from score where class = 'B' order by test_score;
逆は最後にdesc
をつけます
select * from score where class = 'B' order by test_score;
今回はここまでにします。
内容はめちゃめちゃ簡単なのに自分でかくと時間かかりますね。
まあエンジニアの近道は遠回りということで次回もがんばります。