汎用
mysql -u root -p
Mysqlにログインする。
mysqldump -u root -p データベース名 > ファイルパス
ファイルにエクスポートする。
指定したデータベースが存在しなくてもファイルは生成される。
\c
入力途中の取り消し
exit もしくは quit
Mysqlを抜ける
\G
縦表示にするには、最後に;の代わりに\Gを付ける。
全体操作コマンド
show character set
利用可能な文字セット一覧を返す。
show variables
現在の設定値を返す。多いので、likeを利用するとよい。
source ファイルパス
mysqlファイルをインポートする。先にデータベースをuseしておく。
status
mysqlの状態を返す。
update user set password=null where host='localhost' and user='root'
パスワード初期化。再度rootでログインしてパスワードを設定する。
レコード操作コマンド
select カラム名 from テーブル名 where カラム名='値';
データを取得する
insert into テーブル名 (カラム名,カラム名,カラム名) values ('値','値','値');
データベースのデータを挿入する
insert into テーブル名 values ('値','値','値','値','値');
データベースのデータを挿入する
update テーブル名 set カラム名='値',カラム名='値' where カラム名='値';
データベースのデータを更新する
delete from テーブル名 where カラム名='値';
データベースのデータを削除する
データベース操作コマンド
use データベース名
指定したデータベースを利用する。
show create database データベース名
指定したデータベースの文字コードを調べる。
show databases
データベースの一覧を返す。
show tables
テーブルの一覧を返す。
show table status from データベース名
指定したデータベースの状態を表示する。
show columns from テーブル名
現在のデータベースの指定したテーブル内のカラムの情報を表示する。
show indexes from テーブル名
指定したテーブルのインデックス設定(PrimaryKeyやUniqueKey)を確認する。
create database データベース名
データベースを作成する。
create database データベース名 character set utf8
utf8に文字セットを指定してデータベースを作成する。
create table テーブル名 (カラム1 型,カラム2 型,...)
テーブルを作成する。
create unique index インデックス名 on テーブル名 (カラム名)
ユニークキーを作成する。
drop database データベース名
データベースを削除する。
drop table テーブル名, テーブル名...
テーブルを削除する。
alter table テーブル名 rename to 新しいテーブル名
テーブル名を変更する。
alter table テーブル名 add カラム名 型 not null
テーブルの最後にカラムを追加する。
alter table テーブル名 add カラム名 型 not null first
テーブルの一番目にカラムを追加する。
alter table テーブル名 add カラム名 型 not null after 直前のカラム名
あるカラム名の後にカラムを追加する。
alter table テーブル名 change カラム名 新しいカラム名 型
カラム名を変更する。
※先にuseデータベースで切換が必要。
※型まで入れないと変更出来ない。
alter table テーブル名 modify カラム名 型 not null
Null有りをNot Nullに変更
alter table テーブル名 add unique (カラム名1,カラム名2...)
複合ユニークを設定する。キー名はカラム名1になる。
alter table テーブル名 drop カラム名
カラムを削除する。
alter table テーブル名 drop index インデックス名 (,drop index インデックス名...)
指定したテーブルの指定したインデックス(UniqueKey)を削除する。
alter table テーブル名 drop primary key
指定したテーブルのPrimary Keyを削除する。
truncate table テーブル名
テーブルの全レコードを削除する。
PrimaryKeyは最初に戻る。
create index インデックス名 on テーブル名 (`カラム名`)
テーブルにindexを追加する。
ユーザー操作コマンド
create user ユーザ名@ホスト名 identified by 'パスワード';
ユーザを作成する。新規作成時には権限は何も与えられてない。パスワードの'は必要。無ければエラーになる。
set password = password ('〜')
現在ログイン中のユーザにパスワードを設定・変更する。
set password for ユーザ名@ホスト名 = password ('〜')
指定したユーザにパスワードを設定・変更する。
rename user ユーザ名@ホスト名 to 新ユーザ名@ホスト名;
ユーザ名を変更する。
show grants
現在ログイン中のユーザの権限を表示する。
show grants for ユーザ名@ホスト名;
指定したユーザの権限を表示する。
revoke all privileges, grant option from ユーザ名@ホスト名;
ユーザの権限を全て削除する。
grant select,insert,update,delete on データベース名.* to ユーザ名@ホスト名;
ユーザに指定したデータベースのselectとinsertとupdateとdeleteのみ権限を与える。
drop user ユーザ名@ホスト名
ユーザを削除する。
select user,host,password from mysql.user order by user ASC;
順序有りでユーザ情報を確認
update mysql.user set host=変更後のホスト名 where host=現在のホスト名;
ホスト名の変更