iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 3
1
AI & Data

與資料庫共舞系列 第 3

Day 3 — 裝好MYSQL,寫下SQL

今天我們有兩個重要的任務: 在電腦上裝起 MySQL 然後寫下 SQL。讓我們開始吧!

把 MySQL 裝起來

一樣,本篇的重點是希望盡可能進到 SQL 本體,所以我沒有把 Linux 和 WSL 的安裝方法寫下來。另外,我猜 Mac 應該安裝方式大同小異,所以讓我們閃電安裝!

第一步: 下載安裝檔案

https://dev.mysql.com/downloads/windows/installer/8.0.html
選擇下面的那個做安裝

https://ithelp.ithome.com.tw/upload/images/20200903/20129829gUp77owtPY.png

點選後可以不用註冊,左下角有 "No thanks, jsut start my download" 的選項

https://ithelp.ithome.com.tw/upload/images/20200903/20129829ZrWEsE19Z0.png

第二步: 安裝本人

記得選 Custom,這樣可以不用裝一堆用不到的東西

https://ithelp.ithome.com.tw/upload/images/20200903/201298290gGA8sZxhK.png

首先我們要先選一個 Server,可以選擇最新的,按下右邊的綠色鍵投加入安裝列表

https://ithelp.ithome.com.tw/upload/images/20200903/20129829OJsCn1Kga0.png

https://ithelp.ithome.com.tw/upload/images/20200903/20129829ABUJFRw9gR.png

把Server 收合後,選擇 Application 底下,除了 Visual Studio 和 Excel 以外的所有套件做安裝,一樣選擇最新的就可以了。最後選擇 Documentation 底下,Samples and Examples (這個很重要!) 以及 MYSQL Documentation 兩個,一樣選擇最新的加入列表。

選完後應該會向右側那樣。這裡一定要確定有 Server, Workbench, Shell 和 Sample and Example 四個。確認之後按下下一步一直到下面這個畫面。

第三步: 設定 Server

選擇上面的按下一步

https://ithelp.ithome.com.tw/upload/images/20200903/20129829OLfhlrGeC4.png

不用更改設定下一步

https://ithelp.ithome.com.tw/upload/images/20200903/20129829KzgOuCiyi5.png

建立這個資料庫的密碼

https://ithelp.ithome.com.tw/upload/images/20200903/20129829RTjnSb9ma0.png

再下面新增資料庫管理員,設定好帳號密碼然後下一步

https://ithelp.ithome.com.tw/upload/images/20200903/20129829472IizvHGE.png

https://ithelp.ithome.com.tw/upload/images/20200903/20129829Wig5bYMbWS.png

不用改動下一步

https://ithelp.ithome.com.tw/upload/images/20200903/20129829T4Ph5duYDl.png

選擇執行然後安裝

https://ithelp.ithome.com.tw/upload/images/20200903/20129829C1D4z3V0s2.png
完成就會像這樣。

https://ithelp.ithome.com.tw/upload/images/20200903/20129829PzFUKDcqdS.png

第四步: 重複上述動作設定其餘的原件

https://ithelp.ithome.com.tw/upload/images/20200903/20129829ZXRm4OfrFf.png

不用更動按下一步

https://ithelp.ithome.com.tw/upload/images/20200903/20129829HKknDE5xUD.png

輸入資料庫密碼,連線成功後按下一步

https://ithelp.ithome.com.tw/upload/images/20200903/20129829fSUChh1nyU.png

https://ithelp.ithome.com.tw/upload/images/20200903/20129829VVhQ8HLWYq.png

https://ithelp.ithome.com.tw/upload/images/20200903/201298291a8FJo0hX7.png

基本上就完成安裝了!

用兩種方式執行SQL

這裡我們先用最傳統的方式執行 SQL。打開開始功能列選取 MySQL Command Line Client.

https://ithelp.ithome.com.tw/upload/images/20200903/20129829vkRe0W9t7Y.png

這時候輸入剛剛設定的密碼。應該就會出現右側的樣子。這裡就是編輯 mysql 的地方。

https://ithelp.ithome.com.tw/upload/images/20200903/20129829lxjeEIf5EV.png

因為剛剛再設定的時候我們有把MySQL 的範例資料放入資料庫管理系統中,所以我們可以來查詢一下到底有那些資料。要記得,資料庫管理系統可以管理多個資料庫。每個資料庫是獨立的!

show databases;

這個指令會請 mysql 把存在系統裡面的資料庫都列舉出來。SQL 的指令結尾一定要加分號!

https://ithelp.ithome.com.tw/upload/images/20200903/20129829Ti12AP9yeR.png

讓我們先選取 World 這個資料庫,使用的是 USE 這個關鍵字。

use world;

正確操作的話應該會顯示 Database changed. 還記得昨天我們再解釋的關聯式資料庫嗎? 關聯式資料庫中存在數個關聯表。讓我們來看看有那些關聯表

show tables;

https://ithelp.ithome.com.tw/upload/images/20200903/20129829K31ctmgb4k.png

這裡你會發現有三個表,分別式 city、country、countrylanguage。我幫大家偷吃步把這三個表用圖像的方式顯示出來了。下面這張圖可以看到三個表格中各自的綱要 (schema)

用昨天的方式重新敘述 City 就會像這樣:

city(ID: integer, Name: CHAR(35), CountryCode: CHAR(3), District: CHAR(20), Population: integer)

https://ithelp.ithome.com.tw/upload/images/20200903/20129829ABjbrxlDCs.png

也就是City 這個關係表中,有個主鍵 (Primary Key) 叫做 ID 用數字表示。City 有另外四個屬性(Attribute) 分別是 Name、 Country、 District 和 Population. 其中,Name、Country 和 District 的資料必須要是字元 (Character) 並各自有其上限。再來Population 一樣是數字。紅色菱形表示不可以是空白的外部鏈 (Foreign Key),而且這些欄位都不可以是空白的。

另外兩個表格就讓讀者自己推推看吧! (複習完畢!)

讓我們完成今天最後一個任務,也就是把資料從資料庫打印出來吧! 明天我會更仔細的解釋並操作SQL,但先讓大家過過癮:

Select * From city where Name="Taipei";

https://ithelp.ithome.com.tw/upload/images/20200903/20129829vLxbiodLAO.png

這裡我們就印出台北這筆資料了!

疑,另外一種執行資料的方法呢?

除了文字介面,MySQL 也有圖像顯示是介面可以使用。我們可以選取 MySQL Workbench,同樣的輸入帳號密碼。

https://ithelp.ithome.com.tw/upload/images/20200903/201298295C9Uih8G9t.png

這時候在左側側欄可以選擇 Schema 並把 World 展開,就可以發現我們剛剛所執行的

show databases;
use world;
show tables;

他都用收合的方式就可以一覽無疑。

https://ithelp.ithome.com.tw/upload/images/20200903/20129829oHL6ZBuAJ7.png

這時候我們再右側的指令區可以輸入,並按下閃電的按鈕來執行。就會看到與剛剛相同的資料出現了!

Use world; 
Select * From city where name = "Taipei"

https://ithelp.ithome.com.tw/upload/images/20200903/20129829cXbK1Mbbzv.png

明天我們開始玩轉SQL


上一篇
Day 2 — 有「關係」的資料
下一篇
Day 4 — 玩轉 SQL 的基本語法
系列文
與資料庫共舞30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言