ローカルにSQLをインストールして実行してみた

はじめに
自宅にあるiMacでは、SQLを実行できる環境が整っている。
しかし、現在私は快活Clubにいる。
気分転換と運動を兼ねて、外をぶらぶらしており、疲れて休憩中だ。
そこで、ブログを書こうとなり、どうせならSQLに関する記事を書こうとなった。
最近、仕事でSQLのサブクエリや、テーブル結合に触れる機会があったからだ。
参考にする記事
こちらの記事を参考にする。
ローカルにSQL環境を構築してSQLの練習をする方法(XAMPP使用版) (ynmarketing.jp)
XAMPPをインストールする
以下のサイトからインストールしていこう。
Download XAMPP (apachefriends.org)
バージョンは最新のPHP8.2.12でいいだろう。

ダウンロードボタンを押すと以下の画面に遷移した。

右上のダウンロードで、保存をクリックしよう。
そうすると、以下の画面になる。

開くを押すと、以下の画面になった。

antivirus running(アンチウイルスが走っている)ので、In some cases, this may slow down the installation of the software(ソフトウェアのインストールが遅くなる場合がある)と書いてある。
迷わず「はい」をクリックすると、以下の画面になった。

Because an activated User Accont Control on your system some functions of XAMPP are possibly restricted(ユーザーアカウントコントロールシステムが作動したことにより、あなたのXAMPPのいくつかの機能が制限された可能性があります)ということが書かれている。
きっと快活ClubにあるPCのセキュリティシステムによるものだろう。
OKを押すと、問題なくセットアップウィザードが起動した。

Nextを押して、以下の画面では、MySQLとphpMyAdminを選択した。

インストール先フォルダは、デフォルトのままにしておいた。

言語は日本語が選べなかったので、デフォルトの英語にした。
インストールが始まった。

完了したら、以下の画面になった。

MySQLを起動してみた
MySQLのStartを押してみた。

現在カーソルが当たっているところに、データベースを作成するコマンドを打ってみた。
Shellをクリックして出てきたコマンドプロンプトにも打ってみたけど、データベースを作成することはできなかった。
テーブルの作成手順(MySQL) #DB – Qiitaを参考にして、以下のコマンドを打ってみていた。
CREATE DATABASE test;
そもそも、mysqlにログインしていないことが考えられる。
phpMyAdminを起動したい
XAMMPのコントロールパネルには、phpMyAdminのショートカットキーがなかったので、右のExplorerをクリックしてみた。

この中に、phpMyAdminのフォルダがあった。
phpMyAdminがあることは確認できた。
どうやって起動するのか?
Apacheも起動してみた
参考サイトには、Apacheも起動するように書いてあった。

そのため、ApacheのStartボタンも押した。

MySQLのAdminを押すと、phpMyAdminを起動することができた
これは分からなかった。

でも、MySQLのAdminをクリックすると無事に、phpMyAdminの起動画面を開くことができた。

testデータベースを利用する
もともとあったtestデータベースを使用していこう。

支出を管理するテーブル「expense」を作成する。

TEXT型のitemカラムと、INT型のpriceカラムを作成する。

無事、作成できたようだ。

ダミーデータを投入してみる
表示タブに作成したカラムがある。

しかし、データは空だ。
ダミーデータを投入するSQL文を書いていこう。

SQLタブに移動する。

ここで、SQL文を書いていこう。
刈谷のジャンカラの平日昼料金のデータを追加する。

警告が出ていたが、挿入できたようだ。

警告は、testデータベースのexpenseテーブルのitemカラムに挿入した文字列の値が正しくないという意味。
表示を確認すると、itemが????になっていた。

文字化けを解消していこう
SQLでデータを追加すると日本語部分が?となり表示されない。 | プログラミング学習サイト【侍テラコヤ】 (sejuku.net)を確認すると、文字コードがUTF-8になっていないことが原因のようだ。
また、phpMyAdminで解決する方法について、有り難き記事を見つけた。

この記事と同じ方法で、照合順序を設定した。
そして、先ほどのデータは以下のSQL文で削除。

(expense)だとエラーになった。
()を付けずに以下のDELETE文で、正常に実行できた。

カッコつけないほうが上手くいくようだ。
素直に生きていこう。
エラーもなく正常に挿入できたようだ。

ただ、先ほどと同じく、itemが????のままだった。

itemカラムの照合順序が元の状態だったので、ここを変更してみる。

utf8-general_ciに変更して、先ほどのデータをDELETE文で削除して、同じデータをINSERT文で挿入した。
上手く行ったことが確認できた。

また一つ、成功体験を得た。
こうやって、着実に前に進んでいきたい。
コメントを残す