• 沒有找到結果。

第四章 系統實作與展示

4.2 系統實作展示

4.2.1 實例情境說明與轉換機制

在本章節中,我們先以 private table 資料庫 schema 架構的多租戶實例來介紹本研 究。之後,介紹將 JDBC 驅動程式加上 SQL 語句自動轉換功能的實作與展示,

展示將資料庫 schema 架構修改成 chunk table 之後程式操作的案例。

4.2.1 實例情境說明與轉換機制

在實作之前,系統開發人員必需先事先準備完成 private table schema 資料庫 架構的多租戶應用程式,並安裝搭配 MySQL 的 JDBC 驅動程式。在本節,我們 以 private table 多租戶應用程式為基礎,以各種 SQL 實例來說明本系統的設計,

實際展示 SQL 語句轉換機制如何將 DDL (CREATE、ALTER、DROP)及 DML (SELECT、INSERT、UPDATE、DELETE)的等動作轉換成 chunk table schema 架 構的多租戶應用程式。

AccountID Name

1 林小萱

2 周小芳

圖 4.1 一租戶一表格之範例

根據以上的表格設計,如果我們要查詢 Tenant 31 裡 AccountID 為 1 的 Major 欄位資料,可用 SQL 指令「SELECT Major FROM Tenant31 WHERE AccountID = 1」來完成。接著我們以此範例為基礎,介紹幾種常見的一租戶一表格使用情境。

1. 實例情境說明與分析

本節第一個部分先說明 private table 架構下,常見的 DDL 如 CREATE、

ALTER、DROP 等,SQL 語句的實際操作例子及語法作用,第二個部分再說明 DML 常見的操作語法,如 SELECT、INSERT、UPDATE、DELETE 等,SQL 語 句實際操作例子及語法作用。

第一部份,我們將 DDL 操作語法依下列幾種常見的陳述式來做討論:

‧ 國

立 政 治 大 學

N a tio na

l C h engchi U ni ve rs it y

CREATE 陳述式:CREATE TABLE tenant32_student (address varchar(50), city varchar(50), country varchar(25), age integer(8))。語法執行的目的是要新增一個資 料表名稱為 tenant32_student,資料表欄位包含有 address 屬於 varchar 型別、city 屬於 varchar 型別、country 屬於 varchar 型別及 age 屬於 integer 型別,在 private table schema 架構下執行的結果如圖 4.2,會協助我們建立一個新的資料表。

圖 4.2 CREATE 語法執行結果

ALTER add 陳述式:ALTER table tenant32_student add project varchar(10)。

語法執行的目的是要在資料表 tenant32_student 新增一個欄位名稱為 project 屬於 varchar 型別的欄位,在 private table schema 架構下執行的結果如圖 4.3,會協助 我們新增一個欄位。

圖 4.3 ALTER add 語法執行結果

‧ 國

立 政 治 大 學

N a tio na

l C h engchi U ni ve rs it y

ALTER modify 陳 述 式 : ALTER table tenant32_student modify project integer(3)。語法執行的目的是要在資料表 tenant32_student 修改欄位名稱為 project 的欄位型別,更改為 Integer 型別,在 private table schema 架構下執行的結果如圖 4.4,會協助我們修改欄位型別。

圖 4.4 ALTER modify 語法執行結果

ALTER change 陳述式:ALTER table tenant32_student change project proj varchar(50)。語法執行的目的是要在資料表 tenant32_student 修改欄位名稱為 project 的欄位名稱與型別,更改名稱為 proj 型別為 varchar,在 private table schema 架構下執行的結果如圖 4.5,會協助我們修改欄位名稱與型別。

圖 4.5 ALTER change 語法執行結果

‧ 國

立 政 治 大 學

N a tio na

l C h engchi U ni ve rs it y

ALTER drop 陳述式:ALTER table tenant32_student drop proj。語法執行的目 的是要在資料表 tenant32_student 刪除欄位名稱為 proj 的欄位,在 private table schema 架構下執行的結果如圖 4.6,會協助我們移除指定的欄位。

圖 4.6 ALTER drop 語法執行結果

DROP 陳述式:DROP TABLE tenant32_student。語法執行的目的是要刪除資 料表 tenant32_student 包含所有欄位名稱與型別,會協助我們移除指定的資料表。

第二部份,我們將說明 DML 操作語法在 private table 上的實例可依下列幾 種常見的陳述式來做討論:

SELECT 陳述式,依照不同使用情境共提出了多欄位查詢、SELECE *查詢、

多 Table 查詢、JOIN 查詢和多對多關係查詢等,幾種常用的查詢關係來做說明。

首 先 , 說 明 多 欄 位 查 詢 語 句 : SELECT tenant31.accountid, tenant31.name, tenant31.major, tenant31.grade FROM tenant31 WHERE tenant31.accountid= 1。語 法執行的目的是要查詢資料表 tenant31 裡,accountid 是 1 的 accountid、name、

major 與 grade 等,指定的欄位資料,在 private table schema 架構下執行的結果如 圖 4.7,會協助我們查詢指定的資料表與欄位。

圖 4.7 多欄位查詢語法執行結果

‧ 國

立 政 治 大 學

N a tio na

l C h engchi U ni ve rs it y

SELECE *查詢語句:select * from tenant31。語法執行的目的是要查詢資料 表 tenant31 裡,所有欄位資料,在 private table schema 架構下執行的結果如圖 4.8,

會協助我們查詢指定的資料表所有欄位。

圖 4.8 SELECT *查詢語法執行結果

多 Table 查詢語句:SELECT tenant31.major, tenant31_class.accountid FROM tenant31,tenant31_class WHERE tenant31.accountid= 1。語法執行的目的是要查詢 資料表 tenant31 裡 major 欄位與 tenant31_class 裡 accountid 欄位的資料,並限制 條件是資料表 tenant31 中 accountid 欄位為 1 的資料才取出,在 private table schema 架構下執行的結果如圖 4.9,會協助我們查詢指定的資料表與欄位的資料。

圖 4.9 多 Table 查詢語法執行結果

JOIN 查詢語句 : select name, classname, room from tenant31 inner join tenant31_class ON tenant31.accountid = tenant31_class.accountid。語法執行的目的 是要查詢符合指定等值連接的條件 accountid 相等的兩個資料表 tenant31 與 tenant31_class 裡 name、classname、room 欄位,並連接資料表 tenant31 與

‧ 國

立 政 治 大 學

N a tio na

l C h engchi U ni ve rs it y

tenant31_class,在 private table schema 架構下執行的結果如圖 4.10,會協助我們 連接不同資料表並查詢指定的欄位資料。

圖 4.10 JOIN 查詢語法執行結果

多對多關係查詢我們可以先建立如圖 4.11 的關聯表格,除了 Tenant31 原有 的資料表之外,另外又加上 tenant31_class 與 tenant31_score 資料表,如果我們今 天 要 查 詢 tenant31 學 生 所 修 過 的 課 及 每 堂 課 的 修 業 分 數 , 就 需 要 依 靠 tenant31_score 做為我們的連接資料表,將另兩份資料表的主索引作為自己的外 部索引鍵。例如,[學生] 資料表 tenant31 和 [開課資料] 資料表 tenant31_class 有多對多關聯性。一位學生可選修多堂課,而每堂課程可出現在許多學生的選課

圖 4.11 多對多資料表關聯圖

‧ 國

立 政 治 大 學

N a tio na

l C h engchi U ni ve rs it y

單上。連接資料表 tenant31_score 對另外 2 個資料表都有一對多的關聯性,靠內 含參照資訊即可與其他資料表發生關聯產生多對多的關係。

多 對 多 關 係 查 詢 語 句 : select tenant31.`name`, tenant31_class.classname, tenant31_score.score from tenant31 inner join tenant31_score on tenant31.accountid

= tenant31_score.accountid inner join tenant31_class on tenant31_score.classid = tenant31_class.classid 。 語 法 執 行 的 目 的 是 要 查 詢 兩 個 資 料 表 tenant31 與 tenant31_class 多 對 多 關 聯 的 name 、 classname 、 score 欄 位 , 並 將 資 料 表 tenant31_score 連接資料表 tenant31 與 tenant31_class,在 private table schema 架構 下執行的結果如圖 4.12,會協助我們連接不同資料表並查詢指定的欄位資料。

圖 4.12 多對多查詢語法執行結果

INSERT 陳述式:INSERT INTO tenant32 (`accountid`, `name`, `age`) VALUES (2, '林小白', 25)。語法執行的目的是要新增一筆資料列到資料表 tenant32 裡,欄 位 accountid、name、age 的值分別是 2、林小白、25,在 private table schema 架 構下執行的結果如圖 4.13,會協助我們查詢指定的資料表與欄位的資料。

圖 4.13 INSERT 語法執行結果

‧ 國

立 政 治 大 學

N a tio na

l C h engchi U ni ve rs it y

UPDATE / DELETE 陳述式:UPDATE tenant31 SET major=‘資訊管理系' WHERE tenant31.accountid=2。語法執行的目的是要修改一筆資料表 tenant31 裡 的資料列,當 accountid=2 時欄位 major 的值由應用數學系更改為資訊管理系,

執行結果會協助我們修改指定的資料表與欄位的資料。DELETE 語法:DELETE FROM tenant31 WHERE tenant31.accountid = 2 語法執行的目的是要刪除一筆資 料表 tenant31 裡的資料列,當欄位 accountid=2 時即刪除該筆資料,執行結果會 協助我們刪除指定的資料表與欄位的資料。