1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MySQLのコマンド

Last updated at Posted at 2019-01-20

コマンド

起動

mysql -u root -p

指定

mysql -u dbuser -p blog_app

データベース作成

create database cakephp_blog;

データベース一覧

show databases;

データベース選択

use blog_app;

データベース削除

drop database blog_app;

開く

show tables;

テーブル列表示

desc users

テーブル列追加

alter table users add full_name varchar(255) after name;

テーブル列変更

alter table users change full_name full_name varchar(100);

テーブル列削除

alter table users drop full_name;

索引追加

alter table users add index name (name);

索引削除

alter table users drop index name;

テーブル名変更

alter table users rename blog_users;

削除

drop table users;

全データ表示

select * from users;

抽出

select * from users where email like '%.ne.jp’; 

1つ抽出

select * from users limit 1;

並び替え

select * from users order by name desc;

カウント取得

select count(*) from users;

特定列抽出

select distinct email from users;

合計

select sum(score) from users;

ランダム表示

select rand();

長さ取得

select email,length(email) from users;

2つ表示

select concat(name,'(',email,')') from users;

2つ表示タイトル付け

select concat(name,'(',email,')') as label from users;

特定の数だけ表示

select name,substring(name,1,1) from users;

日付

select now();

作成した日付

select name,month(created) from users;

更新

upadate users set email = 'hoge@docomo.ne.jp' where id = 5;

削除

delete from users where score <= 3.0;

バックアップ

mysqldump -u dbuser -p blog_app > blog_app.dump.sql

データ復元

mysql -u dbuser -p blog_app < blog_app.dump.sql

初期設定の手順

mysql -u root -p
create database cakephp_blog;

// データベースのid,pwd設定
// grant all on データベース名.* to ユーザー名@localhost identified by '設定したいパスワード';
grant all on blog_app.* to hoge@localhost identified by 'password’; 

例)
create table users (
     id int not null auto_increment primary key,
     name varchar(255),
     email varchar(255) unique,
     password char(32),
     score double,
     sex enum('male', 'female') default 'male',
     memo text,
     created datetime,
     key score (score)
);

create table users (
     id int(11) not null auto_increment primary key,
     name varchar(50),
     email varchar(255),
     password varchar(16)
);

// テーブルにデータ挿入(複数)
insert into users (name,email,password) values
     ('yamada','yamada@gmail.com','taro'),
     ('sato','sato@gmail.com','jiro');

// テーブルにカラム追加
alter table テーブル名 add カラム名 型
alter table posts add name varchar(255);
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?