1 / 29

資料庫管理 Database Management 建立 銀行資料庫及查詢 範例

資料庫管理 Database Management 建立 銀行資料庫及查詢 範例. 系級:物理四 學號: 49814201 姓名:吳嘉峰 授課老師: 楊維邦 教授. 主題 說明. 利用 phpMyAdmin 在 M ySQL 中建立簡單的銀行資料庫。 依照 範例情境,練習對銀行資料庫作查詢。. 目錄. 檢視銀行資料庫. Banking Database. Example: Banking Database. 1. branch. 2. customer. 3. depositor. 客戶(存款 戶, 貸款 戶). 分公司.

tevin
Télécharger la présentation

資料庫管理 Database Management 建立 銀行資料庫及查詢 範例

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 資料庫管理Database Management建立銀行資料庫及查詢範例 系級:物理四 學號:49814201 姓名:吳嘉峰 授課老師:楊維邦 教授

  2. 主題說明 • 利用phpMyAdmin在MySQL中建立簡單的銀行資料庫。 • 依照範例情境,練習對銀行資料庫作查詢。

  3. 目錄

  4. 檢視銀行資料庫 Banking Database

  5. Example: Banking Database 1. branch 2. customer 3. depositor 客戶(存款戶, 貸款戶) 分公司 存款戶 5. account 4. borrower 貸款戶 6. loan 存款帳 貸款帳 ※資料來源:Database System Concepts, Silberschatz etc. 2006 (Fifth Ed.)

  6. Primary Key & Foregin Key • Primary Key:該欄位不得為空值且為唯一。 • ForeginKey:該欄位的值需參考其他表格中的欄位,當被參考表格中不存在該值時便不可輸入該筆資料,被參考的資料也不可任意的更新(UPDATE)或刪除(DELETE)。

  7. Schema Diagram • Primary Key and foreign key can be depicted by schema diagram • Schema Diagram for the Banking Enterprise Foreign Key 箭號方向:Primary Key ※資料來源:Database System Concepts, Silberschatz etc. 2006 (Fifth Ed.)

  8. 建立銀行資料庫 建立Database及Table並輸入資料

  9. 建立Banking Database 資料庫 CREATE DATABASEBanking_Database; 新增一個名為“Banking_Database”的資料庫 指令格式: CREATE DATABASE資料庫名稱

  10. 建立及輸入Table內資料 指令格式: CREATE TABLE 表格名稱 ( 欄位名稱1資料型別(資料長度), 欄位名稱2 資料型別(資料長度), PRIMARY KEY(欄位名稱), FOREIGN KEY(本table欄位名稱)REFERENCES 參照table名稱(參照欄位) ); 設為PRIMARY KEY 表示該欄位不得為空值 且為唯一(選用) 新增多筆資料 指令格式: INSERT INTO `表格名稱` (`欄位1`, `欄位2`) VALUES (資料1, 資料2), (資料3, 資料4); 設為FOREIGN KEY 表示該欄位的值需參考其他表格中的欄位,當被參考表格中不存在該值時便不可輸入該筆資料(選用)

  11. back 1. branch (分公司) CREATE TABLE branch ( branch_namechar(16), branch_citychar(16), assets int(10), PRIMARY KEY(branch_name) ); INSERT INTO `branch` (`branch_name`, `branch_city`, `assets`)VALUES ('Brighton', 'Brooklyn', 7100000), ('Downtown','Brooklyn', 9000000), ('Mianus', 'Horseneck',400000), ('North Town', 'Rye', 3700000), ('Perryridge', 'Horseneck',1700000), ('Pownal', 'Bennington', 300000), ('Redwood', 'Palo Alto',2100000), ('Round Hill','Horseneck', 8000000);

  12. back 2. customer (客戶:存款戶、貸款戶) INSERT INTO `customer` (`customer_name`,`customer_street`, `customer_city`) VALUES ('Adams', 'Spring', 'Pittsfield' ), ('Brooks', 'Senator', 'Brooklyn'), ('Curry', 'North', 'Rye'), ('Glenn', 'Sand Hill', 'Woodside'), ('Green', 'Walnut', 'Stamford'), ('Hayes', 'Main', 'Harrison'), ('Johnson', 'Alma', 'Palo Alto'), ('Jones', 'MAin', 'Harrison'), ('Lindsay', 'Park', 'Pittsfield'), ('Smith', 'North', 'Rye'), ('Turner', 'Putnam', 'Stamford'), ('Williams', 'Nassau', 'Princeton'), ('Jackson', NULL, NULL);#特別注意! CREATE TABLE customer ( customer_namechar(16), customer_streetchar(16), customer_citychar(16), PRIMARY KEY(customer_name) );

  13. back 3. account (存款帳) CREATE TABLE account ( account_numberchar(16), branch_namechar(16), balance int(10), PRIMARY KEY (account_number), FOREIGN KEY (branch_name) REFERENCESbranch(branch_name) ); INSERT INTO `account` (`account_number`,`branch_name`, `balance`) VALUES ('A_101', 'Downtown', 500), ('A_102', 'Perryridge', 400), ('A_201', 'Brighton',800), ('A_215', 'Mianus',700), ('A_217', 'Brighton',750), ('A_222', 'Redwood',700), ('A_305', 'Round Hill', 350);

  14. back 4. loan (貸款帳) INSERT INTO `loan` (`loan_number`,`branch_name`, `amount`) VALUES ('L_11', 'Round Hill', 900), ('L_14', 'Downtown', 1500), ('L_15', 'Perryridge', 1500), ('L_16', 'Perryridge', 1300), ('L_17', 'Downtown', 1000), ('L_23', 'Redwood', 2000), ('L_93', 'Mianus', 500); CREATE TABLE loan ( loan_numberchar(16), branch_namechar(16), amount int(10), PRIMARY KEY (loan_number), FOREIGN KEY (branch_name) REFERENCESbranch(branch_name) );

  15. back 5. depositor (存款戶) INSERT INTO `depositor` (`customer_name`,`account_number`) VALUES ('Hayes', 'A_102'), ('Johnson', 'A_101'), ('Johnson', 'A_201'), ('Jones', 'A_217'), ('Lindsay', 'A_222'), ('Smith', 'A_215'), ('Turner', 'A_305'); CREATE TABLE depositor ( customer_namechar(16), account_numberchar(16), FOREIGN KEY (customer_name) REFERENCEScustomer(customer_name), FOREIGN KEY (account_number) REFERENCESaccount(account_number) );

  16. back 6. borrower (貸款戶) INSERT INTO `borrower` (`customer_name`,`loan_number`) VALUES ('Adams', 'L_16'), ('Curry', 'L_93'), ('Hayes', 'L_15'), ('Jackson', 'L_14'), ('Jones', 'L_17'), ('Smith', 'L_11'), ('Smith', 'L_23'), ('Williams', 'L_17'); CREATE TABLE borrower ( customer_namechar(16), loan_numberchar(16), FOREIGN KEY (customer_name) REFERENCES customer(customer_name), FOREIGN KEY (loan_number) REFERENCESloan(loan_number) );

  17. 檢視各表格中的資料

  18. 查詢範例練習 範例說明及演練

  19. 查詢範例說明 • 找出住在North且有貸款的客戶。 • 查詢這些客戶的貸款金額。 • 確認貸款分公司及所在地。 • 是否有存款? • 存款分公司所在地? • 住在North同時有存款及貸款的客戶之帳號及金額。

  20. 手動解題 4.是否有存款? 5.存款分公司所在地? 3.確認貸款分公司及所在地。 找出住在North且有貸款的客戶名稱。 2.查詢這些客戶的貸款金額。 1. branch 2. customer 3. depositor 客戶(存款戶, 貸款戶) 分公司 存款戶 5. account 4. borrower 貸款戶 6. loan 存款帳 貸款帳

  21. SQL解題1:住North且有貸款的客戶

  22. SQL解題2:貸款金額

  23. SQL解題3:貸款分公司及所在地

  24. SQL解題4:是否有存款?

  25. SQL解題5:存款分公司所在地?

  26. SQL解題6:住在North同時有存款及貸款的客戶之帳目資料SQL解題6:住在North同時有存款及貸款的客戶之帳目資料

  27. 心得感想 實做練習心得

  28. 心得感想 • 這次為了建立好銀行資料庫,必須要把它的schema diagram弄清楚,了解各個key的作用,參考時需注意欄位型別及長度。 • 若是不參考各key之間的關係,銀行資料庫依然可以順利建立完成,但是這種銀行資料庫可能會在更新、刪除後出現錯誤或資料一致性問題。 • 資料輸入必須謹慎,需注意是否有打字錯誤。 • 真實的資料庫系統中,我們比較不可能使用手動查詢的方式來解決問題,因為真實系統中的資料數量遠高於這次的範例。 • 解決同一問題的SQL query,通常不只一種。

  29. The End. Thank you!

More Related