2009-04-16
DataBase vol.1
データベースとは何か?
データベースは電話帳のようなもの
database(電話帳DB)
電話帳(テーブル) 出身校(テーブル) 名前 ←関連性がある→ 名前 住所 学校 Tel 学年
イメージとしてはExcelの表。
- PHPと同じように各種型がある。
今回はMySQLを使う
MySQL :: MySQL 5.1 リファレンスマニュアル
DBの種類
MySQLを起動してみる。
$ mysql5 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.0.77 Source distribution Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> exit Bye $ mysql5 -uroot Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.0.77 Source distribution Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql>
授業用のDBを作ってみる。
mysql> create database sql_drill; Query OK, 1 row affected (0.36 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | sql_drill | | test | +--------------------+ 4 rows in set (0.02 sec) mysql>
改訂新版 反復学習ソフト付き SQL書き方ドリル (WEB+DB PRESS plusシリーズ)
- 作者: 羽生章洋,和田省二
- 出版社/メーカー: 技術評論社
- 発売日: 2007/05/18
- メディア: 大型本
- 購入: 21人 クリック: 308回
- この商品を含むブログ (50件) を見る
に付属しているCD-ROM内に入っているcreate_for_mysql.sqlを入れる。
$ mysql5 -uroot sql_drill < /Users/shota/class/sql/create_for_mysql.sql $ mysql5 -uroot sql_drill < /Users/shota/class/sql/data.sql
※事前にローカルにダウンロード済
中身を見てみる。
$ mysql5 -uroot sql_drill Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server version: 5.0.77 Source distribution Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> show tables; +---------------------+ | Tables_in_sql_drill | +---------------------+ | BelongTo | | Categories | | CustomerClasses | | Customers | | Departments | | Employees | | Prefecturals | | Products | | Salary | | Sales | +---------------------+ 10 rows in set (0.00 sec) mysql>
データベースを消す。
mysql> drop database sql_drill;
データを取り出す
SELECT文・・・データを取り出す構文
sqlファイルを作って書く。
SELECT CustomerName FROM Customers ;
大文字小文字を区別しない。
FROMは先に書く。
$ mysql5 -uroot sql_drill < /Users/shota/class/sql/part1/041601.sql 'CustomerName タマ ハナ ミケ キク ウメ トラネコ商会 クロ トラ スーパーニャンコ ぶち雑貨店 ミーストア タロ 雑貨サクラ チー シロ 株式会社スズナリ エリザベス商会 ポンタ ジロー ニャン太 ミミ ミミミ 有限会社チョコグリーン チャー みーすけ チロ パーコ株式会社 トニー商店 ギン プーストア
問題1-1
テーブルCustomersのAddress列を取り出しなさい。
SELECT Address FROM Customers ;
実行結果
$ mysql5 -uroot sql_drill < /Users/shota/class/sql/part1/041602.sql Address 江戸川区下小岩 江戸川区北小岩 館林市緑町 江戸川区西小岩 中野区ねこた町 札幌市南区 台東区浅草橋 川崎市多摩区 (以下略)
問題1-4
テーブルEmployeesのEmployeeName列を取り出しなさい。
SELECT EmployeeName FROM Employees ;
実行結果
$ mysql5 -uroot sql_drill < /Users/shota/class/sql/part1/041603.sql EmployeeName シマゴロー ゴッチン マキ子 マル ミチロー チー サラ みなみ プリン ぴー子 ごま (以下略)
複数の列を指定する。
複数アイテムの間はカンマで区切る。
SELECT ProductName , Price FROM Products ;
実行結果
$ mysql5 -uroot sql_drill < /Users/shota/class/sql/part1/041604.sql ProductName Price まぐろ 500 金魚 35 ぶり 350 あじ 200 あなご 150 ねずみ肉 120 とり肉 200 豚肉 300 バッタ肉 185 蜘蛛肉 98 ねこ草 120 大根 135 いちご 480 じゃがいも 300 しいたけ 150 チューチュークッキー 200 いなごチップス 180 ありせんべい 320 チューチューアイス 110 こねずみジャーキー 120 (以下略)
SELECTに*を指定すると全部データベース内がすべて表示される。
SELECT * FROM Products ;
実行結果
$ mysql5 -uroot sql_drill < /Users/shota/class/sql/part1/041605.sql ProductID ProductCode ProductName Price CategoryID 1 1001 まぐろ 500 1 2 1002 金魚 35 1 3 1003 ぶり 350 1 4 1004 あじ 200 1 5 1005 あなご 150 1 6 2006 ねずみ肉 120 2 7 2007 とり肉 200 2 8 2008 豚肉 300 2 9 2009 バッタ肉 185 2 10 2010 蜘蛛肉 98 2 11 3011 ねこ草 120 3 12 3012 大根 135 3 13 3013 いちご 480 3 14 3014 じゃがいも 300 3 15 3015 しいたけ 150 3 16 4016 チューチュークッキー 200 4 17 4017 いなごチップス 180 4 18 4018 ありせんべい 320 4 19 4019 チューチューアイス 110 4 20 4020 こねずみジャーキー 120 4 21 5021 鰹節 200 5 22 5022 ネコ缶ナンバーワン 300 5 23 5023 ネコ缶ダイエット 300 5 24 5024 ネコ缶ローヤル 450 5 25 5025 煮干 320 5 26 6026 ねこマンマ 480 6 27 6027 ツナおじや 460 6 28 6028 ねずみキッシュ 500 6 29 6029 ねこ草のごまあえ 230 6 30 6030 バッタのそぼろ煮 360 6 31 7031 ネコ砂 800 7 (以下略)
問題2-1
テーブルCustomersのCustomerName列、Address列を取り出しなさい。
SELECT CustomerName ,Address FROM Customers ;
実行結果
$ mysql5 -uroot sql_drill < /Users/shota/class/sql/part1/041606.sql CustomerName Address タマ 江戸川区下小岩 ハナ 江戸川区北小岩 ミケ 館林市緑町 キク 江戸川区西小岩 ウメ 中野区ねこた町 トラネコ商会 札幌市南区 (以下略)
問題2-5
テーブルProductsのProductCode列、Price列、ProductName列、ProductCode列を取り出しなさい。
SELECT ProductCode ,Price ,ProductName ,ProductCode FROM Products ;
実行結果
$ mysql5 -uroot sql_drill < /Users/shota/class/sql/part1/041607.sql ProductCode Price ProductName ProductCode 1001 500 まぐろ 1001 1002 35 金魚 1002 1003 350 ぶり 1003 1004 200 あじ 1004 1005 150 あなご 1005 2006 120 ねずみ肉 2006 2007 200 とり肉 2007 2008 300 豚肉 2008 2009 185 バッタ肉 2009 2010 98 蜘蛛肉 2010 3011 120 ねこ草 3011 3012 135 大根 3012 3013 480 いちご 3013 3014 300 じゃがいも 3014 3015 150 しいたけ 3015 (以下略)
Copyright © crazyup. All rights reserved.


