基本操作

終了

\q / exit / quit / Ctrl-d

ヘルプ

\h / help

ユーザー操作

ユーザーを作成

create user user@host;
create user user@host identified by 'password';

セキュリティ上、パスワードを設定したほうが良い

ユーザーのパスワードを変更

自分のパスワードを変更:

set password = password('password');

指定ユーザーのパスワードを変更:

set password for user@host = password('password');

ユーザー名を変更

rename user user@host to new_user@new_host

ユーザー一覧を表示

select User,Host from mysql.user;

ユーザーの権限を表示

自分の権限を表示:

show grants;

指定ユーザーの権限を表示:

show grants for user@host;

ユーザーに権限を追加

grant permissions on level user@host;

例(test_databeseデータベースのtest_tableテーブルでselectupdateをする権限をuser@hostに与える:

grant select,update on test_database.test_table to user@host;

permissionsで設定できる項目の一部:

all すべて
select select
update update
delete delete
create create table
drop drop table
alter alter table
create user create user, drop user, rename user, revoke all privileges
usage 権限なし

levelで設定できる項目:

grant permissions on *.* user@host; すべて
grant permissions on database.* user@host; 指定データベース
grant permissions on database.table user@host; 指定データベースのテーブル
grant permissions (column, ...) on database.table user@host; 指定データベースのテーブルのカラム

オプション:

grant permissions on level to user with grant option; 他のユーザーの権限を変更(自分にある権限のみ)
grant permissions on level to user with max_queries_per_hour num; 1時間にユーザーが行えるクエリ数
grant permissions on level to user with max_updates_per_hour num; 1時間にユーザーが行える更新数
grant permissions on level to user with max_queries_per_hour num; 1時間にユーザーがログインできる回数
grant permissions on level to user with max_user_connections num; 同時接続数

ユーザーから権限を削除

revoke permissions on level from user@host;

データベース操作

データベースを作成

create database database_name;

データベース一覧を表示

show databases;

データベースを削除

drop database database_name;

データベースの切り替え

use database_name;

テーブル操作

テーブルを作成

create table table_name(column type, ...);
create table users(id int, name varchar(255), email varchar(255), password char(32));

テーブル一覧を表示

show tables;

テーブルを削除

drop table users;

テーブル名の変更

alter table [テーブル名] rename to [新テーブル名];

テーブルの構造を表示

.schema [テーブル名]

テーブル名を指定しない場合は、全テーブルを表示。

カラム操作

カラムを末尾に追加

alter table [テーブル名] add column [カラム名];

カラムのオプション

[カラム名] integer primary key autoincrement 自動連番
[カラム名] text not null 空欄エラー
[カラム名] text unique ユニークな文字列以外エラー
[カラム名] integer default 20 デフォルト値
[カラム名] check(カラム名>0) 0より小さいとエラー(例)

テーブル作成時にも使用可能。

挿入場所を指定:

[カラム名] first 最初
[カラム名] after [カラム名2] カラム名2の後に追加

データ操作

データを追加

insert into [テーブル名](カラム1, カラム2...) values(値1, 値2...);
insert into users values('yamada', 25);
insert into users(age) values(25);

シングルクォーテーションを書く場合は、2回続けて('It''s a pen.')書く。

nullはシングルクォーテーションで囲わない。

データを変更

update [テーブル名] set [カラム名] = [値] where [条件];
update users set name = 'tanaka' where name = 'yamada';
update users set name = 'tanaka', score = 500 where name = 'yamada';

データを削除

delete from [テーブル名] where [条件];
delete from users where score <= 100;

ROWID

データ番号

取得:

select ROWID, * from users;

削除:

delete from users where ROWID = 4;

データ取得(select)

データを取得

select [カラム名] from [テーブル名];
select * from users;
select name from users;

並び替え昇順(A→ Z) / 降順(Z→ A)

order by [カラム名] / order by [カラム名] desc
select * order by score;
select * order by score desc;

表示件数を指定

limit [数]

select * order by score desc limit 5;

条件を指定

where

select * where score >= 200 order by score;
select * where score is null;

nullを取得するには、is null (isnull)is not null (not null, notnull)を使う。

演算子:+, -, *, /, %=, ==, !=, <>, <, <=, >, >=and, or, not

重複行を削除

distinct

select distinct team from users;

countやsumをグループごとに行う

group by [カラム名]

select team, sum(score) from users group by team;

表示するカラム名を変更

as [別名]

select id, name as username from users;

複数テーブルにまたがる操作

select id, name, team, sum(score) from users, games where users.id = games.user_id group by users.id;

組み込み関数

データの個数を表示

count(*)

select count(*) from users;

最大値 / 最小値を表示

max(カラム名) / min(カラム名)

select max(*) from users;
select max(*) from users;

乱数を生成

random()

select * from users order by random() limit 1;   -- 1つランダムに選ぶ

文字列の長さを表示

length(カラム名)

select name, length(name) from users;

合計を計算

sum(カラム名)

select sum(score) from users;

平均を計算

avg(カラム名)

select avg(score) from users;

データ型を表示

typeof(カラム名)

select name, typeof(name) from users;

日付・時刻

日付 / 時刻 / 日付と時刻

current_date / current_time / current_timestamp

insert into users(name, created) values('yamada', current_timestamp);

日付 / 時刻を指定フォーマットに変換

strfttime()

select strfttime('%Y年%m月%d日', current_timestamp);
%Y
%m 月(01-)
%d 日(00-)
%W 年の初めからの週(00-53)
%j 年の初めからの経過日数(001-366)
%w 曜日(0-6, 日-土)
%H 時(00-24)
%M 分(00-59)
%S 秒(00-59)
%f 秒+ミリ秒(SS.SSS)
%s 1970/1/1からの経過秒数
%% %

画面表示の変更

ヘッダー(カラム名)を表示 / 非表示

.header [on|off]

現在の設定内容を表示

.show

モード(selectの出力形式)を変更

.mode [モード]
mode 書式 メモ
csv カンマ区切り 文字列は""で囲われる
column 指定幅で表示 幅は.widthで変更可能
html HTMLのTABLE <table>は出力されない
insert insert文
line カラムごとに行を分けて中央揃え カラム名と値の間は=
list 区切り文字で区切る 区切り文字は.separatorで変更可能
tabs タブ区切り タブは8文字
tcl |で区切る すべて文字列扱い(""で囲われる)

columnの幅を指定

.width 10
.width 10 5 30

区切り文字を指定

.separator ,

ファイル操作

外部ファイルから読み込み

.import [ファイル名] [テーブル名]

.import users.txt users

区切り文字を.separatorで変更しておく必要がある。

SQL文で書き出し

.dump [テーブル名]

.dump users

出力先ファイルを.outputで指定しておく必要がある。

SQL文を読み込み

.read [ファイル名]

.read users.txt

設定

MySQL起動パスワードの設定

mysql> set password for user_name@host_name=password('new_password');

用語

データベース

表計算:1つのファイル

テーブル

表計算:1つのシート

フィールド / カラム

レコード