--- tags: 大三筆記 --- # 資料庫系統 ## 簡介DBMS (DataBase Management System) ### 資料庫管理系統 (動態資料 + 程式) * 功能:新增、刪除、修改、查詢 * 目的:效率、便利 ### 跟傳統程式語言的差別: * 傳統程式語言 → 強調指令、運算 * 資料庫語言 → 強調資料、資料抽象化(讓資料跟系統間互動) ### 一般程式處理檔案的問題: 1. Redundancy:不同格式間有重複的資料。 2. Inconvenience(重複資料的移動、變動造成不一致):透過傳播更新 (propagating update),讓重複的資料一同更新。 3. 孤立性:Multiple file and formats 4. ★Integrity constraints :整體資料的限制,能夠配合現實(帳目不可為負數等狀況),讓資料更加精準。 > 傳統程式語言可以做, > 但是會有處理上的困難。 * Atomicity of update 不可分割性 → inconsistent 一致性(配對) ## [Data models](https://ithelp.ithome.com.tw/articles/10240143) ### Relational model:Ted Codd ![](https://i.imgur.com/kEpGgiQ.png =400x) 與之相對的還有Document Model --- ### 分層簡化與抽象化:使維護更容易 ![](https://i.imgur.com/zEMT7ho.png =400x) * Physical level:資料如何被儲存在實體中。 * Logical level:定義資料的儲存形式與關聯性。 * View level:展示給不同用戶特定的內容,隱藏其他數據。 --- ### Schemas(綱要或是結構的設計) ![](https://i.imgur.com/oDjhgfX.png)<br> ![](https://i.imgur.com/pl2qVmI.png =500x) * Logical schemas:資料儲存的設計結構(title) * Physical schemas:底層的物理實現 * Instances(實例):資料儲存的變數(各用戶資料) ### Data definition language(DDL) 把定義記在data dictionary(資料字典)中 * 統稱 **==Meta data(中繼資料)==** :用以描述資料的**型態**、**結構**、**關係**、**限制**等(如循序、索引檔) * 索引 → **提升效率**(針對某個欄位的查詢) --- ### Data manipulation language(DML) ![](https://i.imgur.com/DbMxJM9.png =400x) Pure 學術理論,以提升資料庫的功能 * Commercial ,SQL * Procedural DML * Declarative DML * SQL select → from → where --- ### 讓 SQL 去Embedded(嵌入) 其他高階語言 * DDL:存入dictionary * DML:evaluation plan(執行計畫) * DBA:管理**軟體、系統、監控、備份、效率、底層、空間、定義schemas** --- ### 各種運算操作 #### Attribute(屬性) 屬於 **Atomic**(不可分割) 的單純文數字資料 ##### 分類 1. **Multivalued Attribute** - 每個 value 是一個集合,元素為同類型資料 - 範例:`{ {09XX-XX, 04-XXXX} }` 2. **Composite Attribute** - 每個 value 是不同類型資料的組合 - 範例:`{ (04, XXXXXX) }` --- #### Relation Schema and Instance - 一個 Relation Schema: $R = (A_1, A_2, \dots, A_n)$ 例如: `Instructor = (id, name, phone)` - **Schema**:結構定義(如表格標題或第一列) ##### Key(鍵) - **Superkey**:能唯一識別資料列的屬性組合 - **Candidate Key**:Superkey 中的極小集合(不可再刪減) - **Primary Key**:從 Candidate Key 中選定一個作為主鍵 --- #### Foreign Key(外部鍵) - **Referencing relation**:例如 `Instructor` - **Referenced relation**:例如 `Department` - 用於建立跨表格的關聯 --- #### Advisor 關聯範例 - 一位老師 → 可以指導多位學生 - 一位學生 → 只能對應一位老師 - 利用 Primary key 與 Foreign key 建立限制關係 --- #### Relational Query Language ##### 類型 - **Pure language**:如 Relational Algebra - **Relational Algebra**:屬於 **Procedural** 語言 ##### 基本要素 1. Operators(運算子) 2. 優先度 ##### 運算子 - **六大基本運算子** - **兩個額外運算子** --- #### 基本運算 ##### Select Operation(選擇) - 表示法: $\sigma_{p}(r)$ - `p`: selection predicate(限制條件) - 可用邏輯運算:`∧`、`∨`、`﹁` --- ##### Project Operation(投影) - 表示法: $\Pi_{A_1, A_2, \dots, A_n}(r)$ - 用來選取指定欄位 --- ##### Cartesian Product(笛卡兒積) - 表示法: $r \times s$ - 功能:合併表格,可能產生重複資料 → 行數增加 - 常與 **Select** 搭配,形成 **Join Operation** --- ##### Join Operation(連接) - 表示法: $r \Join_{\Theta} s$ - 實作方式: - `Cartesian Product ×` - 再結合 `Select σ` --- * intersect * union * except ```SQL (selectcourse_id from section where semester = 'Fall' and year = 2017) union (selectcourse_id from section where semester = 'Spring' and year = 2018) ``` --- ### Null operator IS NOT NULL ```SQL SELECT * FROM listofitem WHERE coname IS NOT NULL; ``` IS NULL ```SQL SELECT * FROM listofitem WHERE coname IS NULL; ``` ![](https://i.imgur.com/DXnPdhs.png) --- 要加==distinct==才能抓到唯一的老師 ```SQL number of instructors select count (distinct ID) ``` 抓全部 ```SQL select count (*) form course; ``` --- 依xx分群 輸出每群的資料 in each department ```SQL select dept_name,avg(salary) as avg_salary from instructor group by dept_name ``` ![](https://i.imgur.com/mdqPw0s.png) 挑群 ```SQL select dept_name, avg (salary) asavg_salary from instructor group by dept_name having avg (salary) > 42000; ``` * where -> 挑列 * group by -> 生成群 * having -> 挑群 --- ### Practice <br> ![](https://i.imgur.com/Vq8UC8Z.png) ```SQL select ... , ... avg(xxx),count(xxx) as 幫前面運算的結果取名 from ... , ... 從哪個地方來的 group by 依據什麼分群 having ... 用條件式挑群 ``` <br> ![](https://i.imgur.com/natTJQP.png) <br> --- ### SET與in <br> ![](https://i.imgur.com/al3ZQYF.png) --- ![](https://i.imgur.com/lzU8gxB.png) --- ### some, all ### 排序 SELECT * FROM student ORDER BY 'score' DESC ### 限制 SELECT * FROM student WHERE score > 90 and major = 'english' ## JDBC:Connecting to the DB with JAVA API 建立connect物件 -> 建立stmt物件(配置空間) -> 透過stmt的方法操作資料 ### Update ```=java try{ stmt.executeUpdate{ "insert into ..." }catch(SQLException sqle){ ... } // 每一次操作就新增一次 ``` ### Prepared Statement 先經過預處理分配空間,提升效率(SQL Injection)與安全性。 ```=java preparedStatement pStmt = conn.preparedStatement("insert...values(?,?,?,?)"); // 預先給四個欄位的空間 ```