1
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の導入基礎

Last updated at Posted at 2020-06-06

本記事では、MySQLの基礎コマンドをXAMPP環境で検証したものを、
備忘録として記録しています。

  • MySQL環境構築
  • テーブル操作
  • 基礎コマンド

#検証環境
OS: Windows 10 Home
XAMPP Ver: Windwos向けXAMPP 7.4.6
MariaDB Ver: 10.4.11

公式マニュアル

引用元: MySQL(英語版)
引用元: MySQL(日本語版)

準備編

1.XAMPPのインストール
2.MySQLの起動
3.ディレクトリの移動

XAMPPのインストール、およびMySQLの起動

XAMPPをインストールするには、以下の記事が参考になります。
また、XAMPP Control PanelからMySQLを起動する方法も記載されています。

引用元:今さら聞けない!XAMPPをインストールする方法【超初心者向け】

ディレクトリの移動
  • コマンドプロンプトを起動する
  • 以下のコマンドを入力し、Enterキーで実行する
cd C:¥xampp¥mysql¥bin

プロンプトの表記が、C:¥xampp¥mysql¥bin>に変更されればOK。
ここまでで、【準備編】が完了。

#MySQLサーバーへ接続
デフォルトで、MySQLのrootユーザーがパスワードなしで設定されているので、
以下のコマンドで接続する。

mysql -u root

プロンプトがMariaDB [(none)]>に切り替われば、OKです。
ちなみに、以下のコマンドも同様の結果が確認できます。

mysql -u root -h localhost

つまり、-h localhostは省略可能で、
-hオプションを省略した場合、localhostからMySQLサーバーへアクセスすることとなります。

バージョン確認方法

1.MySQLへの接続時
mysql -u root

上記コマンド実行後、Server Version:が表示されます。

2.MySQLへ接続後
status;

上記コマンド。もしくは、以下のコマンドでも確認できます。

select version();
3.MySQLサーバー接続前
mysql --version

そのほか、phpMyAdminからも確認できますが、本記事では割愛します。

引用元:[【MySQL入門】バージョン確認をする方法を紹介!ツール利用にも対応]
(https://www.sejuku.net/blog/82533)

基礎コマンド

ここでは、SQLの基本コマンドを確認します。

プロンプトがMariDB[(none)]となり、MySQLへ接続できていることを確認してください。

現在のユーザーを確認する
select user();
データベース一覧を確認する
show databases;
データベースを作成する
create database dababase名;
データベースを削除する
drop database database名;
現在の操作対象データベースを確認する
select database();
操作するデータベースを選択する
use database名;
作業用ユーザーを作成する

user名: dbuser
password: Pass

create user dbuser@localhost identified by 'Pass';
作業用ユーザーに対して、操作対象DB全ての操作権限を与える

database名: myproject

grant all on myproject.* to dbuser@localhost;

docker コンテナの場合、

grant all on `myproject`.* to dbuser@'%';
作業用ユーザーの作成と、操作対象DBの操作権限を同時に行う

database名: myproject
user名: dbuser
password: Pass

grant all on myproject.* to dbuser@localhost identified by 'Pass';
作成した作業用ユーザーで、データベースにアクセスする

database名: myproject
user名: dbuser
password: Pass
MySQLからは、¥qもしくはquitで抜けておく。

mysql -u dbuser [-h localhost] -p myproject
Enter Password:

※[]内は省略可。

作業用ユーザーを削除する

事前にmysql -u rootコマンドでルートユーザーで接続しておく。

drop user dbuser@localhost;
テーブル一覧を確認する

事前にuse database名コマンドでデータベースを選択しておく。

show tables;
テーブルの構造を確認する
desc table名;

または、

show columns from table名;
テーブルを削除する
drop table table名;

ここまでで、SQLの基礎コマンドは完了です。

外部ファイルを使用する方法

事前準備

C:¥xampp¥mysql¥binディレクトリにdatabaseディレクトリを作成する。
このC:¥xampp¥mysql¥bin¥databaseディレクトリ内にファイルを作成していくものとする。

実行方法
  • 方法1: リダイレクション
mysql -u root < file_path

実行例:

cd C:¥xampp¥mysql¥bin
mysql -u root < ./database/create_myproject.sql
  • 方法2: sourceコマンド
source file_path

実行例:

cd C:¥xampp¥mysql¥bin
mysql -u root
Enter Password:

source ./database/create_myproject.sql

sourceのコマンドは、¥.(バックスラッシュ + ピリオド)でも代用可能です。

データベースと作業用ユーザーを作成する【外部ファイル】

SQLファイルの作成

ファイル名: create_myproject.sql
データベース名: myproject
ユーザー名: dbuser
パスワード: Pass

create_myproject.sql
drop database if exists myproject;
create database myproject;
grant all on myproject.* to dbuser@localhost identified by 'Pass';
作成したファイルを実行する
cd C:¥xampp¥mysql¥bin
mysql -u root
¥. ./database/create_myproject.sql
実行後、作成したデータベースに、作成した作業用ユーザーで接続する
cd C:¥xampp¥mysql¥bin
mysql -u dbuser -p myproject
Enter Password:

テーブルを作成する【外部ファイル】

SQLファイルの作成

ファイル名: myproject.sql
テーブル名: users

myproject.sql
drop table if exists users;
create table users (
  id int unsigned primary key auto_increment,
  name varchar(255),
  point float
);
作成したファイルを実行する
¥. ./database/myproject.sql

実行後、作成したテーブルができているか、テーブル構造が意図した通りか確認する

show tables;
desc users;

テーブルの構造を変更する

特定のフィールドの後に、新たなフィールドを追加する

追加するフィールド: email

myproject.sql
alter table users add column email varchar(255) after name;
フィールドを削除する
myproject.sql
alter table users drop column email;
フィールド名の変更、フィールドのデータ型の変更
myproject.sql
alter table users modify name varchar(20); -- データ型のみ変更
alter table users change name user_name varchar(80); -- データ型、フィールド名変更
テーブル名の変更
myproject.sql
alter table users rename members;

テーブル操作の基本

レコードの抽出
myproject.sql
select * from users where id > 1;
select name, point from users;
select * from users where id in (1. 3);
select * from users where point between 2.0 and 6.0;
select * from users where name like '%a%'; -- %はワイルドカード
select * from users where name like binary 'S%'; -- binaryは厳密に大文字のSから開始される

レコードの挿入、更新、削除
myproject.sql
insert into users (name, point) values ('sato', 4.9);
update users set point = point * 1.2 where id % 2 = 0; -- idが偶数のuser
delete from users where id < 5;
抽出したレコードの並び替え、件数制限、関数の利用
myproject.sql
select * from users order by point;
select * from users order by point desc limit 3; -- point上位3人(組み合わせ)
select * from users order by rand() limit 1; -- 乱数で並び替える
select name, length(name) users order by length(name) desc; -- nameフィールドの文字数が大きい順に並び替える

以上、随時加筆修正します。

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