LoginSignup
22
25

More than 5 years have passed since last update.

MySQLコマンド備忘録

Last updated at Posted at 2016-02-23

MySQLのコマンド一覧

サンプルコマンド

  • データベースの作成
mysql> create database hoge;  # 通常時
mysql> create database hoge character set utf8;  # 文字コードの指定
  • テーブルの作成
mysql> create table user (
    -> user_id INT not null primary key auto_increment,
    -> user_name VARCHAR(30),
    -> user_address VARCHAR(50) not null,
    -> user_domain VARCHAR(50),
    -> fulltext index (user_address)
    -> ) engine = mroonga default charset utf8;

mysql> create table mailing_list (
    -> mailing_list_id INT not null primary key auto_increment,
    -> mailing_list_address VARCHAR(50) not null,
    -> mailing_list_domain VARCHAR(50),
    -> FULLTEXT INDEX (mailing_list_address)
    -> ) engine = mroonga default charset utf8;

mysql> create table mailing_list_member (
    -> user_id INT not null, 
    -> mailing_list_id INT not null, 
    -> primary key(user_id, mailing_list_id), 
    -> foreign key (user_id) references user(user_id), 
    -> foreign key (mailing_list_id) references mailing_list(mailing_list_id)
    -> ) engine = mroonga default charset utf8;
  • テストデータの挿入
mysql> insert into user 
    -> (user_id, user_name, user_address) 
    -> values 
    -> (0, 'hoge', 'hoge@hoge.co.jp');

mysql> insert into mailing_list 
    -> (mailing_list_id, mailing_list_address) 
    -> values 
    -> (0, 'hoge_list@hoge.co.jp');

mysql> insert into mailing_list_member
    -> (user_id, mailing_list_id)
    -> values
    -> (0, 0);

  • 問い合わせ
mysql> select user.user_address from user, mailing_list_member, mailing_list
    -> where mailing_list_member.mailing_list_id =
    -> (select mailing_list_id 
    -> from mailing_list
    -> where mailing_list_address = 'hoge_list@hoge.co.jp')
    -> and mailing_list_member.user_id = user.user_id;
  • 利用するデータベースの選択
mysql> use db_name;

SELECTコマンド

  • 行数を確認
mysql> select count(*) from table_name;
mysql> select count(column_name) from table_name;

確認コマンド

  • MySQL内のデータベース一覧を表示
mysql> show databases;
  • データベース内のテーブル一覧を表示
mysql> show tables;
  • テーブルのカラム一覧を表示
mysql> show columns from table_name;

ユーザ処理コマンド

  • ユーザの一覧を表示
mysql> select host, user from mysql.user;
  • ユーザの追加
mysql> create user 'hoge'@'localhost' identified by 'hogehoge';
  • ユーザの削除
mysql> drop user 'hoge'@'localhost';
  • ユーザの権限確認
mysql> show grants for 'hoge'@'localhost';
  • ユーザの権限追加
mysql> grant all privileges on hoge_db.* to 'hoge'@'localhost';
  • ユーザの権限削除
mysql> revoke all privileges on hoge_db.* from 'hoge'@'localhost';
  • ユーザのパスワード変更
mysql> set password for 'root'@'localhost'=password('hoge');
  • ユーザの確認
mysql> select user,host from mysql.user;

削除コマンド

  • データベースの削除
mysql> drop database db_name;
  • テーブルの削除
mysql > drop table table_name;
  • テーブルのレコードの削除
mysql > delete from table_name where hoge = 'hoge';
mysql > delete from table_name;  # where句を指定しない場合、レコードがすべて削除されるので注意が必要

テーブルの変更コマンド

  • テーブル名の変更
mysql > alter table old_tbl_name rename to new_tbl_name; #old_tbl_nameテーブルをnew_tbl_nameに変更
  • テーブルのカラム名を変更
mysql > alter table tbl_name change old_column new_column int; # old_columnカラムをnew_columnカラムに変更(型はINT)
  • テーブルにカラムを追加
mysql > alter table tbl_name add column_name int;

SQLファイルの読み込み

mysql > source SQL_file_PATH

または

$ mysql -u hoge -p < SQL_file_PATH

キャンセルコマンド

以下のようにSQL文実行前に誤って改行などをしてしまった場合にSQLのキャンセルができる。

mysql > select hoge where hoge_tbl fuga
     ->
mysql > select hoge where hoge_tbl fuga
     -> \c
mysql > 

コマンドラインの処理

  • データベースを指定
$ mysql -u hoge -D target_db -p
  • パスワードを直接入力
$ mysql -u hoge -p[password]

参考文献

  1. MySQLの使い方, http://www.dbonline.jp/mysql/, Online; accessed 23-Feburary-2016.
22
25
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
25