0
0

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 1 year has passed since last update.

mysqlコマンド

0
Last updated at Posted at 2022-04-25
パス初期化
sudo mysql_secure_installation

権限付与
grant select on *.* to 'user名'@'localhost';

ユーザー作成
create user 'user'@'localhost' IDENTIFIED BY 'pass';

全権限付与
grant all on *.* to user名@localhost;

ユーザー一覧の確認
select user, host from mysql.user;

データベースに接続(上記コマンド入力後にパスワードを入力)
sudo mysql -u root -p

データベース作成
create database データベース名;

データベースに権限を付与(ユーザー名:dbuser、サーバ:localhost)
grant all on (データベース名).* to 'user'@'localhost' identified by 'パスワード';

使うデータベースを切り替える
use database名;

現在のデータベースを確認
mysql>select database();

データベースの中身(一覧)を確認
mysql>show database;

データベースを削除
mysql>drop database larabook;


テーブルを確認する
mysql>show tables;


テーブルの作成(CREATE)
create table [テーブル名] (
    column [データ型] [その他オプション],
    column2 [データ型] [その他オプション],
    column3 [データ型] [その他オプション],
);


表示用の列名は省略可能
select * from mebers;

列名で四則演算(+, -, *, /)ができる。
select price * 1.08 from price;

重複を排除して選択(DISTINCT)
select distinct 列名 from mebers;

条件指定して抜き出す(WHERE)
select 列名 from mebers where 条件式;

例
select * 
from user
where
   ID = 1
   OR ID = 3
   OR ID = 5


select *
from user
where 年齢 >= 30 OR 出身地 = "九州"

#東京都と千葉県と埼玉県のみ検索
select * from board_member
where prefecture in ('東京都','埼玉県','千葉県');


よく使う条件指定。
値 --2つの値の間に含む

列名 is between 値 and 
列名 is in (値, 値, ...) --列挙した中のどれかと一致する
列名 is null --空欄を選択
列名 is not null --空欄以外を選択
等

ソート(order by)
order 列名 --昇順でソート(並び替え)
order by)列名 DESC --降順でソート(並び替え)

データの挿入(INSERT)


#行をテーブルの一番下に挿入する
insert into mebers values (値, 値); 

#列名を指定して行を挿入
insert into mebers (列名, 列名, ...) values (値, 値); 

#データの変更(update) --条件と一致する列の値を更新する
update mebers set 列名 = 値 where 条件式;

#データの削除(delete)--条件と一致する行を削除する
delete from mebers where 条件式; 

#削除


drop colum 列名;

#制約を追加
add 制約名;

#制約を削除
drop 制約名; 

テーブルの削除(drop)
drop table テーブル名;

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?