LoginSignup
3
5

More than 5 years have passed since last update.

MySQLコマンド

Last updated at Posted at 2017-04-25

MySQLに接続

$ mysql -uroot -p

データベース

データベース一覧

show databases;

データベース作成

create database dbname collate utf8_general_ci;

データベース削除

drop database dbname;

データベース名前変更

コマンドない

テーブル

テーブル作成

create table goods(id int not null auto_increment, name varchar(10), primary key (id));

テーブル名変更

alter table table_name rename new_table_name;

テーブルを空にする

truncate table table_name

テーブル削除

drop table table_name;

フィールド

フィールド一覧

desc items;
show columns from items;
show full columns from items; //コメントも表示

フィールド追加

alter table goods add stock int not null default 10 after name;

フィールド変更

alter table goods modify hoge null;

フィールド名変更

alter table items change price_type price_type_id int not null;

フィールドの順番変更

alter table options modify unit varchar(5) not null after price;

フィールド削除

alter table goods drop column stock;

フィールドのコメント追加

alter table items modify price_type_id int not null comment 'コメント' after name;

データ挿入・編集・削除

データ挿入

insert into todos (title, created_at, updated_at) values ('ご飯を食べる', now(), now());

データ変更

update table_name set user_id=3 where hoge=3;

データ削除

delete from todos where id = 1;

全テーブルのデータ数を確認

select table_name, table_rows from information_schema.TABLES where table_schema = 'DB名';

データを縦向きにみる

select * from users where id=1 \G;

テーブルの状態をみる(次のautoincrementの値とか色々)

show table status where name="users" \G;

エクスポート

mysqldump -uusername -p -r backup_file_name --single-transaction database_name

インポート

mysql -uroot -p database_name < hoge.sql

ユーザ

ユーザ作成

create user 'user_name'@'localhost' identified by 'password'

権限付与

grant all on db_name.* to 'user_name'@'localhost'
3
5
0

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
3
5