YAK
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
###### tags: `Data Base` # 第十章: Relational Database Design I ## Measures of Quality - 讓 attribute semantics 是清楚易懂的 - 減少重複訊息 - 減少 NULL 數量 - 不允許虛假的 tuple 出現的可能性 ## 重複資訊會造成的異常現象(anomalies) - Anomalies 的問題 - 造成多餘的工作被執行 - Anomalies 種類 - Example: EMP_PROJ(Emp#, Proj#, Ename, Pname,No_hours) - Insertion anomaly: 有條件的新增值 - Cannot insert a project unless an employee is assigned to it. - Deletion anomaly: 刪除不能刪除的資料 - When a project is deleted, it will result in deleting all the employees who work on that project. - Update anomaly: 需要重複更新多筆一樣的資料 - Changing the name of project number P1 from “Billing” to “Customer Accounting” may cause this update to be made for all 100 employees working on project P1. ## NULL Values in Tuples - NULL 的問題 - 浪費儲存空間 - 造成資料沒有可讀性 - 使用 NULL 的時機 - Attribute 不適用或無效 - e.g. 男生有沒有懷孕? - Attribute value 是 unknown - e.g. 期末考成績還沒出來 - Value 存在,但不可用 ## Spurious(虛假) Tuples - JOIN 後產生錯誤的 tuple(無意義的) - lossless join 指保證 join 後出現的資料都是有意義的 ## Functional Dependencies(FDs) - 功用: 能夠偵測 Spurious 與 anomalies - 定義: 限制兩個 attribute setes 之間的關係 - 何謂FD? - (if x 能決定唯一 y) => X FD Y - FD 用於推導現實世界的 attribute 的 constraint - E.g. SSN -> ENAME , {SSN, PUNMBER} -> HOURS ## Use of FDs - 使用 FDs 測試 relation 是否滿足 F of FDs set,如果滿足,則稱 r satisfies F ## Inference Rules for FDs - Armstrong's inference rules: - IR<small>1</small>(Reflexive) if Y subset-of X, then X->Y - IR<small>2</small>(Augmentation) if X -> Y, then XZ -> YZ - (Notation: XZ stands for X $\cup$ Z) - IR<small>3</small>(Transitive) if X->Y and Y -> Z, then X -> Z - Armstrong's inference rules 完備了 inference rules - 還有額外的 inference rules(可推導): - Decomposition: if X -> YZ, then X -> Y and X -> Z - Union: if X -> Y and X->Z, then X -> YZ - Prove: 1. X → Y (given) 2. X → Z (given) 3. X → XY (using IR2 on 1 by augmentation with X. Where XX = X) 4. XY → YZ (using IR2 on 2 by augmentation with Y) 5. X → YZ (using IR3 on 3 and 4) - Psuedotransitivity: if X -> Y and WY -> Z, then WX -> Z ## Closure - Closure of set F of FDs => 從 F 可被推導的 FD - Closure of set F of attribute X => 所有可被 attribute X 的 attributes ## Equivalence of Sets of FDs - if 所有 FD in F 可以被 G 推導 - if 所有 FD in G 可以被 F 推導 - 兩個 sets pf FDs F and G are equivalent => F+ = G+ - Definition(Covers): - 如果 FD in G 可以被 F 推導 => F covers G - F cover G and G cover F => F and G 是等價 ## Minimal Sets of FDs(1) - 定義: - 等號右邊有一個 attribute - 每個 FD 都不能拿走 - 不能用 R1 代替 ## Normalization of Relations - 2NF, 3NF, BCNF - based on keys and FDs of a relation schema - 4NF - based on keys, multi-valued dependencies: MVDs; - 5NF - based on keys, join dependencies: JDs 大部分做到 3NF, BCNF, 4NF ## 要求目標 1. Nonadditive join property(lossless join) 2. Dependency perservation property ## Promblem with Decompositions - normalize 的代價 - queries 時間變長 - denormalize 不一定變回原來的 schema - tradeoff: these issue v.s. redunancy ## Keys and Attributes - prime attribute 是指存在某些 candidate key 的 attribute - nonprime attribute 是指不存在任何 candidate key 裡的 attribute ## First Normal Form - Disallows: - composite attributes - multivalue attributes - nested relations: non-atomic attribute - method: - 將重複的資料項分別儲存到不同的記錄中, 並加上適當的主鍵 Example: multivalue resolve ![](https://i.imgur.com/cCr55zQ.png) nested relations ![](https://i.imgur.com/qCCPTzi.png) ## Second Normal Form - Uses the concepts of FDs, primary key - 目標: - 符合1NF - 每個非主鍵屬性必須完全相依於主鍵 - method: - 分割資料表:將部分相依的欄位分割出去,另組成新的資料表 - Definitions: - Prime attribute: 指存在某些 candidate key 的 attribute - Full functional dependency: 拿掉任何 attribute 的 Y 皆無法滿足 Y -> Z 的 FD - Example: - ![](https://i.imgur.com/cm4BIGr.png) - 2NF: 所有 non-prime attribute A in R(table) 都是 fully functionally dependent on the primary key - R 可以藉由 2NF normalization 分解成 2NF relations ![](https://i.imgur.com/QA3o14U.png) (以 candidate 為主軸,一個個把不同 prime attribute 的表組合起來) (只最少的 attribute 就可以辨認出單一 attribute ,把它變成表) ## Third Normal Form - 目標: - 符合 2NF - 各個欄位與主鍵之間沒有遞移相依的關係 - 如何找出遞移關係 - 若要找出資料表中各欄位與「主鍵」之間的遞移相依性, 最簡單的方法就是從左到右掃瞄資料表中各欄位有沒有『與主鍵無關的相依性』存在 - 可能的情況如下: 1.如果有存在時,則代表有「遞移相依」的關係 2.如果有不存在時,則代表沒有「遞移相依」的關係 - method: - 檢查有無遞移關係 - 將遞移關係分割出去,組成新的資料表 - Transitive dependency: FD X -> Z 可以從 X -> Y and Y -> Z推導 - 如果表 R 是 2NF 且沒有 non-prime attribute A in R 是跟 pk 有 tansitively dependent 關係,稱之為 3NF - 利用 3NF normalization 可做出 3NF relations - 例如: X -> Y 且 Y -> Z 且 X $\in$ PK,接下來考慮是否 Y $\in$ candidate key - 若 Y 是 candidate key, 則沒有 transitive dependency - 若不是, 把它變成另一個表,並以 Y 為 PK - Example: - ![](https://i.imgur.com/OEvLjAf.png) - It is fully functionally dependent on every key of R. - 跟每個 key 都是 FD - It is nontransitively dependent on every key of R. - 沒有 transitively dependent ## Normalization into 2NF and 3NF ![](https://i.imgur.com/clSvJX2.png) ![](https://i.imgur.com/A0TXM47.png) ![](https://i.imgur.com/D7ddeu4.png) ## Normal Forms Defined Informally - 1NF: All attributes depend on the key - 2NF: All attributes depend on the whole key - 3NF: All attribute depend on nothing but the key ## BCNF(Boyce-Codd Normal Form) - 如果主鍵是由多個欄位組成,則進行 Boyce-Codd 正規化 - 目標: - 符合 3NF 的格式 - 主鍵不可相依於其他非主鍵的欄位 - 當表 R 的 FD X -> A, X 是 R 的 superkey 時,是為 BCNF ![](https://i.imgur.com/5rlkMWl.png) ## Modeling Temporal Data - Temporal data 是增加時間區段 - 沒有 accepted standard - Temporal functional dependency: 假如 X 包含全部的 legal instance Y 的 snapshot ,則 X -> Y FD 存在於表 R 中。 - 在實務上,time attribute 會增加 start 與 end ![](https://i.imgur.com/bN5Nzy0.png) - FK 會參考到某個時間點的 tuple

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully