新增 Home

leo 2024-04-20 13:46:07 +08:00
commit c60410b0b6

148
Home.md Normal file

@ -0,0 +1,148 @@
# 資料庫
## 介紹
### 資料庫是結構化的資訊或資料集合,通常以電子方式儲存在電腦系統中。資料庫通常由資料庫管理系統 (DBMS) 控制。資料和 DBMS 以及與之關聯的應用程式統稱為資料庫系統,通常簡稱為資料庫。
### 現今操作中最常見的資料庫類型中的資料通常以一系列表中的行和欄建模,以提高處理和資料查詢的效率。然後可以輕鬆地存取、管理、修改、更新、控制和組織資料。大多數資料庫使用結構化查詢語言 (SQL) 來編寫和查詢資料。
##
### 資料庫種類:
### 關聯式資料庫(RDBMS)/非關聯式資料庫(NOSQL)
##
## 關聯式資料庫-RDBMS
* 由Table組成
* Schema需先定義且只接受一開始設定格式的資料
* 可使用join連接多個table
* 具有ACID特性
* 使用SQL語法
#### ACID
* Atomicity 原子性
* Consistency 一致性
* Isolation 隔離性
* Durability 持續性
#### Schema
* 描述資料庫內中的表格結構、欄位名稱等日後不符合Schema的規範會報錯
##
## 非關聯式資料庫-NOSQL
* 資料庫由 collection 組成
* collection 中每筆資料為一份 documentdocument 的資料格式不需一致
* 以 CAP theorem 為概念設計
* 常用於分散式雲端系統
#### CAP
* 一致性Consistency
* 可用性Availability
* 分區容錯性Partition tolerance
##
## 資料庫應用
### 實驗室目前大多使用 MySQL/PostgreSQL/SQLite 這些關聯式資料庫
### 幾乎都透過Dbeaver去管理資料庫所以基本用不太到太多的資料庫語法
##
## Dbeaver 教學
### 下載 相關應用程式 [link](https://dbeaver.io/download/)
### 若是初學者,建議先連接實驗室開放的資料庫,先不用自架資料庫
若這邊看不懂可以下載PPT [link](http://140.125.21.65:8418/Education/SQL/src/commit/77f62e0b1ab96080dc4f401464b8dd2b9e3bb8c6/PPT/SQL.pptx)
#### 設定連線:
資料庫類型 : MariaDB
Server Host : 140.125.21.65
Port : 3307
Database :VIP125
Username : VIP125
Password : @VIPvip125
##
## 基礎SQL語法
### 建議打開資料庫一起操作
### 新增資料
```
INSERT INTO teaching_sql_2 (student_id,person_id,student_birthday) # 選擇好資料表及覽未明成
Values("M11112029","C123456789","900801") #將參數按照順序放入
```
### 更新資料
```
UPDATE teaching_sql_2 SET student_birthday='890801' # 將變動的資料設置好
WHERE student_id='M11112030'; #尋找student_id 為 M11112030 的參數
```
### 刪除資料
```
DELETE FROM teaching_sql_2 # 選擇要刪除的table表
WHERE student_id='M11112029' #刪除studen_id為M1112029
```
### 搜尋資料(最需要花時間學的地方常常會遇到需要串接多個table的時候)
#### 所有資料
```
select * FROM teaching_sql_1
```
#### 輸出:
| student_id | student_name | student_class |
| -------- | -------- | -------- |
| M11112030| 張威勝 | EL125 |
| M11112031| 黃曉明 | EL125 |
| M11112029| 林大偉 | EL124 |
#### 指定欄位
```
select student_name FROM teaching_sql_1
```
|student_name |
| -------- |
| 張威勝 |
| 黃曉明 |
| 林大偉 |
#### 多個指定欄位
```
select student_name,student_id FROM teaching_sql_1
```
| student_name | student_id |
| -------- | -------- |
|張威勝|M11112030|
|黃曉明|M11112031|
|林大偉|M11112029 |
#### 查詢符合條件式的資料
```
select * FROM teaching_sql_1 WHERE student_id='M11112030'
```
| student_id | student_name | student_class |
| -------- | -------- | -------- |
|M11112030| 張威勝| EL125|
#### 查詢符合條件式的資料(and)
```
select * FROM teaching_sql_1 WHERE student_id='M11112030' AND student_class='EL125' # 尋找同時符合 student_id='M11112030' 及 student_class='EL125'的資料
```
| student_id | student_name | student_class |
| -------- | -------- | -------- |
|M11112030| 張威勝| EL125|
#### 查詢符合條件式的資料(or)
```
select * FROM teaching_sql_1 WHERE student_id='M11112030' or student_class='EL124' # 尋找只要符合 student_id='M11112030' 或 student_class='EL124'的資料
```
| student_id | student_name | student_class |
| -------- | -------- | -------- |
|M11112030| 張威勝| EL125|
|M11112029|林大偉|EL124|
#### 串接2個資料表
```
SELECT teaching_sql_1.student_id, # 找尋teaching_sql_1的 student_id
teaching_sql_1.student_class, # 找尋teaching_sql_1的 student_class
teaching_sql_2.person_id # 找尋teaching_sql_2的 person_id
from teaching_sql_1 join teaching_sql_2 # 將teaching_sql_2加入
WHERE teaching_sql_1.student_id = teaching_sql_2.student_id # 指定條件
```
|student_id|student_class|person_id|
| -------- | -------- | -------- |
|M11112030| EL125| A123456789|
|M11112031| EL125| B123456789|