Ms SQL

tags: Ms SQL SQL Sever
學習附件 http://csyue.nccu.edu.tw/ch/bigdata1081(SQL&R).pdf

壹、認識資料庫

資料庫的階層是有循序的關係,也就是由小到大的排列,其最小的單位是
Bit(位元),而最大的單位則是 Data Base(資料庫)。資料依其單位的大小與
相互關係的層次如下:
Bit(位元) → Byte(字元) → Field(資料欄) → Record(資料錄) → Table(資料表)
→ Data Base(資料庫)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


貳、主鍵( Primary Key )、外鍵( Foreign Key )

  • 主鍵 ( Primary Key )
    (1) 用來辨識記錄的欄位, 具有「唯一性」不允許重複,因此不是每個欄位都適合作主鍵。
    (2) 資料表不一定要有主鍵,但一般來說資料建立時一定都會設計成有主鍵。
    (3) 每個資料表通常只有一個欄位會設定為主鍵, 但有時可能沒有一個欄位特別具唯一性, 此時可考慮使用兩個或多個欄位組合起來做為主鍵。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • 外鍵( Foreign Key )
FK Key 又稱外來鍵、連外鍵與外部鍵的名稱。
    (1) 連結其他資料表,資料表間的關係都是藉由外來鍵所建立的。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • 動腦時間 Q1

    如果我想知道這個BusinessEntityID,這個員工曾經待過的部門(DepartmentID),以及他的名字(FirstName),那麼需要用到哪兩個表,這兩個表之間又是用什麼欄位產生關連的呢?

  • Ans 1

    在 HumanResources.EmployeeDepartmentHistory 可以知道該員工曾經待過的部門 DepartmentID,而
    Person.Person可以知道員工的名字。兩個資料表可以透過就 BusinessEntityID 欄位做合併。
    (註解:如果需要知道部門 DepartmentID 的名字,可以用HumanResources.
    Department 資料表做關聯。)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

參、SQL 資料庫結構

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

肆、SQL資料檔案類型-正規化(Normalization)

正規化的精神就是讓資料庫中重複的欄位資料減到最少,並且能快速的找到資料,以提高關聯性資料庫的效能

  • 正規化的目的 :
    1. 降低資料的重複性( Data Redundancy) (Data Redundancy)。
    2. 避免資料更新異常

參考資料:https://hackmd.io/@TSMI_E7ORNeP8YBbWm-lFA/rykcj8kmM?type=view

伍、SQL 資料型別

1. 浮點數

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

2. Unicode 字串

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

3. 其他

  • 二進位字串

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • 貨幣精確數值

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

陸、SQL 語法

SELECT 查詢 - 欄位名稱
INTO 表名
FROM來 - 表名
WHERE 在哪裡 - 條件敘述
GROUP BY 查詢 - 欄位名稱 (此查詢方法會指定兩個以上的欄位,且所有欄位值皆要符合相同資料才會被分為同一組)
ORDER BY 查詢 - 欄位名稱 ( 將 SELECT 取得的資料集依某欄位來作排序 )

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. 查詢資料庫
  • < Ex1 >

    使用 HumanResources.Department 表,挑出 DepartmentID, Name 這兩個欄位的資料,並依照 DepartmentID 排序。

  • < Ans1 >

SELECT DepartmentID, Name /* 挑出兩個欄位的資料 */ FROM HumanResources.Department /* 從資料表抓資料 */ ORDER BY DepartmentID /* 預設是由小到大,可以加上 DESC 改為 由大到小*/

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. 建立資料庫
CREATE DATABASE 教學資料庫 ON (NAME = 教學資料庫, FILENAME = 'D:\Sql_system_database\教學資料庫.MDF')

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

柒、MVC 架構

  • MVC模式(Model–View–Controller) 是軟體工程中的一種軟體架構模式,把軟體系統分為三個基本部分:

    • 模型(Model) - 程式設計師編寫程式應有的功能(實現演算法等等)、資料庫專家進行資料管理和資料庫設計(可以實現具體的功能)。
    • 視圖(View) - 介面設計人員進行圖形介面設計
    • 控制器(Controller) - 負責轉發請求,對請求進行處理。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

捌、SQL 指令

  • DDL (Data Definition Language) 資料定義語言用作開新資料表、設定欄位、刪除資料表、刪除欄位,管理所有有關資料庫結構的東西,常見的指令有

    1. Create:建立資料庫的物件。
    2. Alter:變更資料庫的物件。
    3. Drop:刪除資料庫的物件。
  • DML (Data Manipulation Language) 資料操作語言用作新增一筆資料,刪除、更新等工作,常見的指令有

    1. Insert:新增資料到 Table 中。
    2. Update:更改 Table 中的資料。
    3. Delete:刪除 Table 中的資料。
  • DQL (Data Query Language) 資料查詢語言只能取回查詢結果,指令只有1個

    1. Select:選取資料庫中的資料。
  • DCL (Data Control Language) 資料控制語言用作處理資料庫權限及安全設定,常見的指令有

    1. Grant:賦予使用者使用物件的權限。
    2. Revoke:取消使用者使用物件的權限。
    3. Commit:Transaction 正常作業完成。
    4. Rollback:Transaction 作業異常,異動的資料回復到 Transaction 開始的狀態

image alt

玖、關聯式資料庫模型

image alt

image alt