MySQLの基礎

開発環境の準備

「mysql」へのログイン (パスワード未設定時)

mysql -u root

「mysql」へのログイン (パスワード設定時)

mysql -u root -p

パスワード「hoge」の設定方法

mysql> SET PASSWORD FOR root@localhost=PASSWORD('hoge');

「help」の表示

mysql> help;

ログアウト

\q
exit;

用語を理解する

  • データベース:excelにおけるbook
    • テーブル:excelにおけるsheet
      • フィールド:excelにおける列
      • レコード:excelにおける行

データベースを操作する

データベースを表示する

mysql> show databases;

データベース「blog_app」を作成する ※データベース名は大文字/小文字を区別する

mysql> create database blog_app;

データベース「blog_app」を削除する

mysql> drop database blog_app;

一般ユーザーを作る

「blog_app」の全ての操作権限を持つ一般ユーザー「dbuser001」を作成する (アクセス元は「localhost」、パスワードは「dbpwd0001」)

mysql> grant all on blog_app. * to dbuser001@localhost identified by 'dbpwd0001';

テーブルを表示する

一般ユーザー「dbusr001」でログイン

mysql -u dbusr001;

データベース「blog_app」を使用する

mysql> use blog_app;

テーブルを見る

mysql> show tables;

テーブルを作成する

テーブル「users」を作成。フィールド(名)を追加しそのデータ型を指定する

mysql> create table users (id int, name varchar(50), email varchar(255), password varchar(16));

テーブル「users」の中身を確認する

mysql> desc users;

テーブルを削除する

テーブル「users」を削除する

mysql> drop table users;

レコードを挿入する

mysql> insert into users (id, name, email, password) values (1, 'kimura', 'kimura@foo.com', 'xxxxx');
mysql> insert into users (id, name, email, password) values (1, 'tanaka', 'tanaka@foo.com', 'xxxxx');
mysql> insert into users (id, name, email, password) values (1, 'yoshida', 'yoshida@foo.com', 'xxxxx');
idnameemailpassword
1kimurakimura@foo.comxxxxx
1tanakatanaka@foo.comxxxxx
1yoshidayoshida@foo.comxxxxx

レコードを確認する (「*」は全てのユーザー)

mysql> select * from users;

レコードを更新する

「name」フィールドが「tanaka」の「id」フィールドを「2」に、
「name」フィールドが「yoshida」の「id」フィールドを「3」に変更する

mysql> update users set id = 2 where name = 'tanaka';
mysql> update users set id = 3 where name = 'yoshida';
idnameemailpassword
1kimurakimura@foo.comxxxxx
2tanakatanaka@foo.comxxxxx
3yoshidayoshida@foo.comxxxxx

レコードを削除する

「id」フィールドが「3」のレコードを削除する

mysql> delete from users where id = 3;
idnameemailpassword
1kimurakimura@foo.comxxxxx
2tanakatanaka@foo.comxxxxx

レコードを扱えるデータ型について

create table users (
       id int, # 整数型
       weight floot, # 小数点(doubleも可)
       code char(10), # 固定長文字列
       name varchar(50), # 可変長文字列
       name text, # 可変長文字列 (最大値大)
       email varchar(255),
       password varchar(16),
       created datetime, # 日付型
       sex enum('male', 'female') # 列挙型
);

テーブル作成時のオプション (create table文)

create table文のオプション

create table users (
       id int NOT NULL, # NULLではない
       created datetime DEFAULT '2011-11-11 10:00:00', # デフォルト値を設定する
);

インデックスとは? (create table文)

インデックスをフィールドに設定すると検索が高速化する

create table users (
       id int NOT NULL primary key, # idにふる
       email varchar(255) unique, # ユニークなフィールドにふる
       KEY code (code) # KEY インデックス名 (検索キーワード)
       /* 検索を高速化させたい対象にふる */
);

IDを自動でふる (create table文)

create table users (
       id int NOT NULL primary key auto_increment, # 自動的にidを連番でふる
);

「id」フィールドに「null」を入れても自動的に「id」が連番でふられる

insert into users (id, ...) values (null, ...);
insert into users (weight, ...) values (55.5, ...);
*/ 

表示するフィールドを指定する (select文)

フィールドを指定して表示させる (表示させる順番は変えることも可能)

mysql> select id, name from users;

フィールドを縦に表示させる (フィールドが多い場合に使用する)

mysql> select id, name from users\G;

表示する条件を指定する (select文)

where句を活用する

mysql> select * from users where id = 1;
mysql> select * from users where id != 1;
mysql> select * from users where id > 1;
mysql> select * from users where name = 'yoshida';
mysql> select * from users where name like = 'yoshi%'; # 部分文字列検索

表示する順番を指定する (select文)

名前順にソート(昇順)

mysql> select * from users order by name;

名前順にソート(降順)

mysql> select * from users order by name DESC;

「wehre句」と「order by」を組み合わせる

mysql> select * from users where id > 1 order by name DESC;

表示する件数を指定する (select文)

表示件数を2件に指定する

mysql> select * from users limit 2;

「limit」と「order by」を組み合わせて「id」でソートした上位2件を表示させる

mysql> select * from users order by id limit 2;

フィールドを追加、削除する (alter table文)

フィールド「created」を追加

mysql> alter table users add created datetime;

フィールド「created」を追加

mysql> alter table users drop created;

フィールドを変更する (alter table文)

現在のデータ型を確認する

mysql> desc users;

「email」フィールドの名前・データ型を変更する

mysql> alter table users change email email varchar(100);

テーブル名を変更する (alter table文)

テーブル名を「users」から「blog_users」へ変更

mysql> alter table users rename blog_users;

length関数を使う

replace関数を使う

substring関数を使う

数学関数を使う

randを使う

日付関数を使う

外部ファイルにSQLを記述する

外部ファイルからSQLを実行する

外部ファイルからデータを取り込む

取り込んだデータを確認する

複数のテーブルを準備する

複数のテーブルからselectする

条件を組み合わせてselectする

group byを使う


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2013-08-10 (土) 01:45:51 (3912d)