[MySQL] よく使うコマンドまとめ

○目次

●項目別

・データベース
・ユーザー
・テーブル
・ビュー
・データ

●操作別

・作成
・削除
・変更
・確認

参考サイト:https://www.dbonline.jp/mysql/

●項目別

○データベース

・作成
 mysql> create database (if not exists) [database];

・削除
 mysql> drop database (if exists) [database];

・変更(char set、照合順序の変更)
 mysql> alter database [database] character
    -> set [charset] (collate [collation]);

・確認
 mysql> show databases;
 mysql> show create database [database];
 mysql> select * from information_schema.schemata\G

○ユーザー

・作成
 mysql> create user [user]@[host]
    -> identified by '[password]';

・削除
 mysql> drop user [user]@[host];

・変更
 (ユーザー名の変更)
 mysql> rename user [olduser]@[host] to [newuser]@[host];

 (権限の付与・削除)
 mysql> grant [権限] on [database].[table] to '[user]'@'[host]'
 mysql> revoke [権限] on [database].[table]
    -> from '[user]'@'[host]'

・確認
 (ユーザー名・ホストの確認)
 mysql> select user,host from mysql.user;

 (権限の確認)
 mysql> show grants for '[user]'@'[host]';

○テーブル

・作成
 (カラムを指定して新たに作成)
 mysql> create table (if not exists) [table]
    -> ([column1] [型・属性], [column2]...);

 (既存のテーブルの一部or全部が同じものを作成)
 mysql> create table [table2] select ... from [table1];
  ※インデックスやキーは継承されない

 (既存のテーブルと同じ定義のものを作成)
 mysql> create table [table2] like [table1];
  ※データは継承されない

・削除
 mysql> drop table (if exists) [table];

・変更(https://www.dbonline.jp/mysql/table/index17.html)
 (名前の変更)
 mysql> alter table [old_name] rename to [new_name];

 (文字セットと照合順序の変更)
 mysql> alter table [table] character
    -> set [charset] (collate [collation]);

 (カラムの追加・削除・変更)
 mysql> alter table [table] add
    -> [column1] [型・属性] (first/after [column2]);
 mysql> alter table [table] drop (column) [column];
 mysql> alter table [table] change
    -> [旧column] [新column] [新column型・属性]
  ※not null とか primary keyとかは継承される。

・確認
 (テーブル一覧の確認)
 mysql> show tables (from [database]);

 (テーブル作成のコマンド確認)
 mysql> show create table [table]\G

 (テーブルのステータスの確認)
 mysql> show table status\G

 (カラムの確認)
 mysql> show columns from [table] (from [database]);

○ビュー

・作成
 mysql> create view [view](column1,...) as select ...;

・削除
 mysql> drop view (if exists) [view];

・変更
 mysql> alter view [view](column) as select ...;

・確認
 mysql> show tables (from [database]);
 mysql> show create view [view]\G

○データ

・挿入
 mysql> insert into [table] values ([value1], [value2].....);

・削除
 mysql> delete from [table] (where ...);
  (条件絞らなければ全消し)
 mysql> truncate table [table];

・変更
 mysql> update [table] set [column] = [value] (where ...);
  (条件に合えば複数行の更新も可)

・確認
 mysql> select [column1],[column2]... from [table];

●操作別

○作成

・データベース
 mysql> create database (if not exists) [database];

・ユーザー
 mysql> create user [user]@[host]
    -> identified by '[password]';

・テーブル
 mysql> create table (if not exists) [table]
    -> ([column1] [型・属性], [column2]...);
 mysql> create table [table2] select ... from [table1];
 mysql> create table [table2] like [table1];

・ビュー
 mysql> create view [view](column1,...) as select ...;

・データ
 mysql> insert into [table] values ([value1], [value2].....);

○削除

・データベース
 mysql> drop database (if exists) [database];

・ユーザー
 mysql> drop user [user]@[host];

・テーブル
 mysql> drop table (if exists) [table];

・ビュー
 mysql> drop view (if exists) [view];

・データ
 mysql> delete from [table] (where ...);
 mysql> truncate table [table];

○変更

・データベース
 mysql> alter database [database] character
    -> set [charset] (collate [collation]);

・ユーザー
 mysql> rename user [olduser]@[host] to [newuser]@[host];
 mysql> grant [権限] on [database].[table] to '[user]'@'[host]'
 mysql> revoke [権限] on [database].[table]
    -> from '[user]'@'[host]'

・テーブル
 mysql> alter table [old_name] rename to [new_name];
 mysql> alter table [table] character
    -> set [charset] (collate [collation]);
 mysql> alter table [table] add
    -> [column1] [型・属性] (first/after [column2]);
 mysql> alter table [table] drop (column) [column];
 mysql> alter table [table] change
    -> [旧column] [新column] [新column型・属性]

・ビュー
 mysql> alter view [view](column) as select ...;

・データ
 mysql> update [table] set [column] = [value] (where ...);

○確認

・データベース
 mysql> show databases;
 mysql> show create database [database];
 mysql> select * from information_schema.schemata\G

・ユーザー
 mysql> select user,host from mysql.user;
 mysql> show grants for '[user]'@'[host]';

・テーブル
 mysql> show tables (from [database]);
 mysql> show create table [table]\G
 mysql> show table status\G
 mysql> show columns from [table] (from [database]);

・ビュー
 mysql> show tables (from [database]);
 mysql> show create view [view]\G

・データ
 mysql> select [column1],[column2]... from [table];

コメントを残す

メールアドレスが公開されることはありません。