Upgrade to Pro — share decks privately, control downloads, hide ads and more …

[CS Foundation] Web - 5 - Database

[CS Foundation] Web - 5 - Database

x-village

August 15, 2018
Tweet

More Decks by x-village

Other Decks in Programming

Transcript

  1. 關聯聯式資料庫的特性 ▸ A (Atomicity) - 不可部分完成性 ▸ C (Consistency) -

    ⼀一致性 ▸ I (Isolation) - 隔離性 ▸ D (Durability) - 耐久性
  2. Atomicity - 不可部分完成性 Product Price iPhone X 999 MacBook Pro

    1299 Product Price iPhone X 999 刪除 MacBook Pro A
  3. Atomicity - 不可部分完成性 Product Price iPhone X 999 MacBook Pro

    1299 Product Price iPhone X 999 Product Price iPhone X 999 MacBook Pro 刪除 MacBook Pro B A
  4. Atomicity - 不可部分完成性 Product Price iPhone X 999 MacBook Pro

    1299 Product Price iPhone X 999 Product Price iPhone X 999 MacBook Pro 刪除 MacBook Pro B A
  5. Atomicity - 不可部分完成性 Product Price iPhone X 999 MacBook Pro

    1299 Product Price iPhone X 999 Product Price iPhone X 999 MacBook Pro 刪除 MacBook Pro 全部做完或者不做,沒有做⼀一半的 B A
  6. Consistency - ⼀一致性 Product Price iPhone X 999 MacBook Pro

    1299 Product Price iPhone X 999 刪除 MacBook Pro A
  7. Consistency - ⼀一致性 Product Price iPhone X 999 MacBook Pro

    1299 Product Price iPhone X 999 Product Price iPhone X 1000 刪除 MacBook Pro B A
  8. Consistency - ⼀一致性 Product Price iPhone X 999 MacBook Pro

    1299 Product Price iPhone X 999 Product Price iPhone X 1000 刪除 MacBook Pro B A
  9. Consistency - ⼀一致性 Product Price iPhone X 999 MacBook Pro

    1299 Product Price iPhone X 999 Product Price iPhone X 1000 刪除 MacBook Pro 只有受到操作的資料會更更動,其他資料或設定應該維持⼀一致 B A
  10. Isolation - 隔離性 Product Price iPhone X 999 MacBook Pro

    1299 User A 更更新 MacBook Pro 售價為 1499 User B 更更新 MacBook Pro 售價為 1699
  11. Isolation - 隔離性 Product Price iPhone X 999 MacBook Pro

    1299 User A 更更新 MacBook Pro 售價為 1499 A User B 更更新 MacBook Pro 售價為 1699 Product Price iPhone X 999 MacBook Pro 1499
  12. Isolation - 隔離性 Product Price iPhone X 999 MacBook Pro

    1299 User A 更更新 MacBook Pro 售價為 1499 B A User B 更更新 MacBook Pro 售價為 1699 Product Price iPhone X 999 MacBook Pro 1499 Product Price iPhone X 999 MacBook Pro 1699
  13. Isolation - 隔離性 Product Price iPhone X 999 MacBook Pro

    1299 User A 更更新 MacBook Pro 售價為 1499 B A User B 更更新 MacBook Pro 售價為 1699 Product Price iPhone X 999 MacBook Pro 1499 多個指令同時操作同⼀一筆資料,後⾯面的指令要等前⾯面的先完成 Product Price iPhone X 999 MacBook Pro 1699
  14. Durability - 持久性 Product Price iPhone X 999 MacBook Pro

    1299 更更新 MacBook Pro 售價為 1499 完成 停電造成系統無預警關機
  15. Durability - 持久性 Product Price iPhone X 999 MacBook Pro

    1299 更更新 MacBook Pro 售價為 1499 完成 A 停電造成系統無預警關機 Product Price iPhone X 999 MacBook Pro 1499
  16. Durability - 持久性 Product Price iPhone X 999 MacBook Pro

    1299 更更新 MacBook Pro 售價為 1499 完成 B A 停電造成系統無預警關機 Product Price iPhone X 999 MacBook Pro 1499 Product Price iPhone X 999 MacBook Pro 1699
  17. Durability - 持久性 Product Price iPhone X 999 MacBook Pro

    1299 更更新 MacBook Pro 售價為 1499 完成 B A 停電造成系統無預警關機 Product Price iPhone X 999 MacBook Pro 1499 Product Price iPhone X 999 MacBook Pro 1699
  18. Durability - 持久性 Product Price iPhone X 999 MacBook Pro

    1299 更更新 MacBook Pro 售價為 1499 完成 B A 停電造成系統無預警關機 Product Price iPhone X 999 MacBook Pro 1499 只要指令完成,即使系統關閉,資料依然還在 Product Price iPhone X 999 MacBook Pro 1699
  19. Exercise 1 創建 Table 創建資料表 Product,包含三個欄欄位:
 • id ,型別為 Integer,設為

    Primary Key
 • name ,型別為 Text
 • price ,型別為 Integer
  20. 創建 Table 創建資料表 Sales,包含三個欄欄位:
 • id ,型別為 Integer,設為 Primary Key


    • customer_id ,設為 Foreign Key 對應 Customer 的 id
 • product_id ,設為 Foreign Key 對應 Product 的 id 範例
  21. 創建 Table 範例 CREATE TABLE Sales (id INTEGER, customer_id INTEGER,

    product_id INTEGER, PRIMARY KEY(id), FOREIGN KEY(customer_id) REFERENCES Customer(id), FOREIGN KEY(product_id) REFERENCES Product(id))
  22. 範例 Insert 資料 INSERT INTO Product(id, name, price) VALUES (1,

    'iPhone X', 999) INSERT INTO Product(id, name, price) VALUES (2, 'MacBook Pro', 1299)
  23. Insert 資料 INSERT INTO Customer(id, first_name, last_name) VALUES (1, 'Steven',

    'Jobs') INSERT INTO Customer(id, first_name, last_name) VALUES (2, 'Elon', 'Musk') Exercise 2
  24. Select 資料 範例 SELECT Product.id, Product.name, Product.price FROM Sales, Product

    WHERE Sales.customer_id=1 AND Sales.product_id=Product.id
  25. Select 資料 - JOIN 對於聯聯合資料表,SQL 有更更厲害的功能 - JOIN SELECT 欄欄位1,

    欄欄位2, ... FROM 資料表1
 [LEFT|RIGHT|INNER] JOIN 資料表2 ON 資料表1.欄欄位 = 資料表2.欄欄 位 JOIN 詳解!