---
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

與之相對的還有Document Model
---
### 分層簡化與抽象化:使維護更容易

* Physical level:資料如何被儲存在實體中。
* Logical level:定義資料的儲存形式與關聯性。
* View level:展示給不同用戶特定的內容,隱藏其他數據。
---
### Schemas(綱要或是結構的設計)
<br>

* Logical schemas:資料儲存的設計結構(title)
* Physical schemas:底層的物理實現
* Instances(實例):資料儲存的變數(各用戶資料)
### Data definition language(DDL)
把定義記在data dictionary(資料字典)中
* 統稱 **==Meta data(中繼資料)==** :用以描述資料的**型態**、**結構**、**關係**、**限制**等(如循序、索引檔)
* 索引 → **提升效率**(針對某個欄位的查詢)
---
### Data manipulation language(DML)

Pure 學術理論,以提升資料庫的功能
* Commercial ,SQL
* Procedural DML
* Declarative DML
* SQL select → from → where
---
### 讓 SQL 去Embedded(嵌入) 其他高階語言
* DDL:存入dictionary
* DML:evaluation plan(執行計畫)
* DBA:管理**軟體、系統、監控、備份、效率、底層、空間、定義schemas**
---
### 各種運算操作
Attribute:物件的特性或處理的資料
屬於Atomic(不可被分割) 單純的文數字資料
1.multivalued 每個value是一個集合
{ {09XX-XX,04-XXXX} } 裡面是集合(同類型)
2.composite 每個value是不同類型資料的集合
{ (04,XXXXXX) } 裡面是不同類型
Relation schema and instance
A1,A2,...,An are attribute
R = (A) is a relation schema
Instructor = (id,name,phone)
Schema is title or the first row
K是R的部分"集合"
K ⊆ R
Key 用於指出特定唯一的資料列
比如用id去指向特定資料(super key)
Primary key
在Superkey中的極小為candidatekey
Candidate key is minimal(極小)
當屬性有其一缺一不可的情況就是minimal
Foreign key (外部鍵)
Referencing relation → instructor
Referenced relation → department
從Instructor中的資料
透過Foreign key指定到department
Advisor
Primary key限制數量的對應關係
一個老師 → 可以有很多學生
一個學生 → 只能有一個指導老師
Relational Query Language
Pure language:Relational algebra...
Relational algebra:屬於Procedural
1.operator
2.優先度
Six basic operators
Two additional operatiors
Select operation
σ_p(r)
p: selection predicate(限制式)
and ∧or∨not﹁
Project operation
Π_{A_1,A_2...A_n}
把單或多個欄位選出來
Cartesian product ×
合併表格
重複 → 增加行數 分別寫上來源
或是透過Select與限制式 挑出合理資料列
↓σ_p(r) 與 × 合成為join operation
Join operation
r✉_Θs
---
* 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;
```

---
要加==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
```

挑群
```SQL
select dept_name, avg (salary) asavg_salary
from instructor
group by dept_name
having avg (salary) > 42000;
```
* where -> 挑列
* group by -> 生成群
* having -> 挑群
---
### Practice
<br>

```SQL
select ... , ...
avg(xxx),count(xxx) as 幫前面運算的結果取名
from ... , ... 從哪個地方來的
group by 依據什麼分群
having ... 用條件式挑群
```
<br>

<br>
---
### SET與in
<br>

---

---
### 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(?,?,?,?)");
// 預先給四個欄位的空間
```