HackMD
  • Prime
    Prime  Full-text search on all paid plans
    Search anywhere and reach everything in a Workspace with Prime plan.
    Got it
      • Create new note
      • Create a note from template
    • Prime  Full-text search on all paid plans
      Prime  Full-text search on all paid plans
      Search anywhere and reach everything in a Workspace with Prime plan.
      Got it
      • Options
      • Versions and GitHub Sync
      • Transfer ownership
      • Delete this note
      • Template
      • Save as template
      • Insert from template
      • Export
      • Dropbox
      • Google Drive
      • Gist
      • Import
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
      • Download
      • Markdown
      • HTML
      • Raw HTML
      • Sharing Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • More (Comment, Invitee)
      • Publishing
        Everyone on the web can find and read all notes of this public team.
        After the note is published, everyone on the web can find and read this note.
        See all published notes on profile page.
      • Commenting Enable
        Disabled Forbidden Owners Signed-in users Everyone
      • Permission
        • Forbidden
        • Owners
        • Signed-in users
        • Everyone
      • Invitee
      • No invitee
    Menu Sharing Create Help
    Create Create new note Create a note from template
    Menu
    Options
    Versions and GitHub Sync Transfer ownership Delete this note
    Export
    Dropbox Google Drive Gist
    Import
    Dropbox Google Drive Gist Clipboard
    Download
    Markdown HTML Raw HTML
    Back
    Sharing
    Sharing Link copied
    /edit
    View mode
    • Edit mode
    • View mode
    • Book mode
    • Slide mode
    Edit mode View mode Book mode Slide mode
    Note Permission
    Read
    Owners
    • Owners
    • Signed-in users
    • Everyone
    Owners Signed-in users Everyone
    Write
    Owners
    • Owners
    • Signed-in users
    • Everyone
    Owners Signed-in users Everyone
    More (Comment, Invitee)
    Publishing
    Everyone on the web can find and read all notes of this public team.
    After the note is published, everyone on the web can find and read this note.
    See all published notes on profile page.
    More (Comment, Invitee)
    Commenting Enable
    Disabled Forbidden Owners Signed-in users Everyone
    Permission
    Owners
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Invitee
    No invitee
       owned this note    owned this note      
    Published Linked with GitHub
    Like BookmarkBookmarked
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    ###### tags: `Data Base` # Data Base 第六章 ## 資料庫系統開發生命週期 初步規劃 >>> 需求收集與分析 >>> 設計 >>> 實作 資料庫設幾可以分成兩部分 - 資料庫設計:依照一定程序,方法與技術,使用結構化的方式將概念資料模型轉換成資料庫的過程。 - 應用程式設計:這是建立使用者介面,將商業處理流程轉換成英用城市執行流程,以便使用者能夠很容易存取資訊,這就是[資料庫程式設計] ## 資料庫設計方法論 --- 三階段 **概念資料庫設計**:將資料庫需求轉換成概念資料模型的過程,不針對特定資料庫管理系統或資料庫模型 **邏輯資料庫設計**:將概念資料模型轉換成邏輯資料模型的過程,邏輯資料庫設計是針對特定資料庫模型來建立邏輯資料模型 **實體資料庫設計**:將邏輯資料模型轉換成關聯式資料庫管理系統的一系列SQL指令敘述 概念資料庫設計-->邏輯資料庫設計-->實體資料庫設計 ![](https://i.imgur.com/BnS4zt9.png) ## 資料庫設計方法論 --- 資料庫設計的主要工作 概念資料庫設計:實體關聯圖 邏輯資料庫設計的主要工作有兩項: 1. 將實體關聯圖轉換成關聯表綱要 2. 關聯表的正規化 實體資料庫設計 ## 實體關聯模型 - 「實體關聯模型」(Entity-Relationship Model)。實 - 體關聯模型使用實體(Entities)與關聯性(Relationship)描述資料和資料間的關係 ![](https://i.imgur.com/LiguJgg.png) - 實體關聯模型是將真實世界的資料模型化成邏輯關聯資料(Logically RelatedData),這就是我們儲存在資料庫的資料。 ![](https://i.imgur.com/MORMCVm.png) ## 實體關聯模型與資料庫設計 概念資料模型 ---> 實體關聯圖 邏輯資料模型 ---> **正規化**的實體關聯圖 --->關聯式資料庫 **實體可以對應關聯式資料庫模型的關聯表,關聯性就是外來鍵參考。** ## 建立實體關聯圖-步驟 - 實體關聯圖(Entity-Relationship Diagram, ERD)是使用圖形符號表示的實體關聯模型。 - 實體關聯圖的基本建立步驟為: Step 1:從系統需求找出實體型態。 Step 2:找出實體型態與其他實體型態間的關聯。 Step 3:定義實體型態間的關聯型態種類是:一對一、一對多或 多對多關聯型態。 Step 4:定義實體型態的屬性型態與主鍵。 ## 建立實體關聯圖-符號 ![](https://i.imgur.com/03Ccni3.png) ![](https://i.imgur.com/L5Wu5qA.png) ![](https://i.imgur.com/96m0bVL.png) ![](https://i.imgur.com/Ttl701m.png) ![](https://i.imgur.com/5bb1nJW.png) ## 實體型態---實體集合 每一位學生稱為實體型態的實例(Instances),或簡稱為實體,實例的集合稱為「實體集合」(Entity Set) ![](https://i.imgur.com/7ONzV8R.png) ## 弱實體型態 - 弱實體型態(Weak Entity Types)是一種需要依賴其他實體型態才能存在的實體形態,這是沒有主鍵的實體型態。例如:學生家長是一個弱實體,因為只有學生實體存在,家長實體才會存在。 ![](https://i.imgur.com/PPbrPGS.png) - 擁有主鍵的實體型態稱為「一般實體型態」(Regular Entity Type)或強實體型態(Strong Entity Type)。 - 弱實體型態(Weak Entity Types)雖然沒有主鍵,但可以擁有用來識別的屬性,稱為區別屬性(Discriminating Attribute)。用虛線底線來表示。 ![](https://i.imgur.com/orcMoQk.png) - 弱實體型態一定關聯到一個強實體型態,以便識別其身份,這個強實體型態稱為「識別實體型態」(Identifying Entity Type),使用的關聯型態稱為「識別關聯型態」(Identifying Relationship Type),使用雙框的菱形圖形符號來表示。 ![](https://i.imgur.com/3f80JqZ.png) ## 關聯型態 ### 關聯集合 關聯型態也可以建立實例,實例的集合稱為「關聯集合」(Relationship Set) ![](https://i.imgur.com/XWscbfG.png) ### 遞迴 實體型態本身會參與關聯自己的關聯型態,稱為「遞迴」(Recursive) ![](https://i.imgur.com/wCSRQDl.png) ## 關聯限制條件 ### 基數比限制條件 基數比限制條件是限制關聯實體型態的實例個數,可以分為三種: - 一對一關聯性(One-to-one Relationship, 1:1):指一個實體只關聯到另一個實體,例如:一位講師只能是學校的一位員工。 ![](https://i.imgur.com/6RxyEoS.png) - 一對多關聯性(One-to-many Relationship, 1:N):指一個實體關聯到多個實體,例如:一門課程擁有小考、期中考和期末考等多次考試。 ![](https://i.imgur.com/PoAVrzX.png) - 多對多關聯性(Many-to-many Relationship, M:N):指多個實體關聯到多個其他實體,例如:學生可以參加多次考試;反過來,考試可以讓多位學生應試。 ![](https://i.imgur.com/JzEFAmw.png) ### 基數限制條件 - 基數限制條件是在關聯型態更進一步標示實體允許參與關聯性的數量範圍:(1,N)、(0,N)、(1,1)和(0,1)等。例如:課程Courses關聯型態是以(0,N)範圍參與Has關聯型態;考試Exams關聯型態是(1,1)參與。 ![](https://i.imgur.com/JYKxWyG.png) ### 參與限制條件 - 是指實體集合的實例是全部或部分參與關聯型態,可以分為兩種: - 全部參與限制條件(Total Participation Constraints):所有實體集合的實例都參與關聯性,使用雙線來標示,也稱為「存在相依」(Existence Dependency)。 - 部分參與限制條件(Partial Participation Constraints):在實體集合只有部分實例參與關聯性,使用單線標示。 ![](https://i.imgur.com/Beip77E.png) - 雞爪符號 - 用來表示參與和基數條件。 - 如果連接線沒有使用任何雞爪符號,就表示是1-1關聯性。 - 關聯型態名稱直接置於連接線之上。 ![](https://i.imgur.com/MEK3mtm.png) ## 屬性 - 實體關聯圖的實體與關聯型態可以擁有零到多個屬性(Attributes)。屬性是使用橢圓型圖形符號的節點,使用線與實體(或關聯)型態連接 ![](https://i.imgur.com/Ddv05cy.png) - 屬性可以分成5種: - 單元值屬性型態(Atomic Attribute Types):這是實體與關聯型態的基本屬性型態,只擁有單元值。例如:Students實體型態的sid、name、GPA、birthday和tel屬性。 - 複合屬性型態(Composite Attribute Types):屬性是由多個單元屬性組成,使用樹狀的單元屬性圖形符號來表示。例如:員工Employees實體型態的address複合屬性是由street、city和ZIP單元屬性組成。 ![](https://i.imgur.com/qnnovim.png) - 多重值屬性型態(Multivalued Attribute Types):屬性值不是單元值,而是多重值,使用雙線的橢圓形符號來標示。例如:學生Students實體型態的address屬性,這是記錄學生多個通訊地址的多重值屬性 ![](https://i.imgur.com/6JV5PDQ.png) - 導出屬性型態(Derived Attribute Types):可以由其他屬性計算出的屬性,使用虛線的橢圓形符號來標示。例如:Exams實體型態的numOfStudents屬性是記錄參加考試的學生數,這個屬性值可以從Results關聯型態計算而 ![](https://i.imgur.com/gPW4Scb.png) - 鍵屬性型態(Key Attribute Types):實體型態中用來識別實例的屬性,其角色相當於關聯表的主鍵。鍵屬性型態是在名稱下加上底線來標示,例如:Students實體型態的主鍵是sid屬性。 ![](https://i.imgur.com/oJCG0Ww.png) ## 實體關聯圖的常見錯誤 - 因錯誤解釋實體間的關聯性造成實體關聯圖的錯誤,稱為「連接陷阱」(Connection Traps)。 - 實體關聯圖最常見的兩種連接陷阱錯誤:扇形陷阱,斷層陷阱。 ### 扇形陷阱 - 是指實體間的關聯性令人產生混淆,扇形陷阱通常是產生在實體型態擁有多個一對多的關聯型態,如同扇形散開(此名稱的由來),但是關聯到的各實體型態間的關聯性會產生令人混淆的問題。 - 例如:學生、講師與課程實體型態間擁有3個一對多關聯性,如下所示: - 講師可以教多位學生。 - 講師可以教多門課程。 - 學生可以選修多門課。(註: 以底下例子而言, 應改為課程有多位學生選修) ![](https://i.imgur.com/KJltx6t.png) - 學生選修多門課程的一對多關聯性並沒有辦法建立在講師與學生和講師與課程的2個一對多關聯性上,如此就會產生混淆 ![](https://i.imgur.com/Dicj2kR.png) - Solution 1:扇形陷阱發生的原因是誤解關聯的意義,關聯正確的解釋應該是以課程建立關聯性的橋樑 ![](https://i.imgur.com/Of4ylHw.png) - Solution 2:形成學生選修多門課和講師教多門課程的2個一對多關聯性。 ![](https://i.imgur.com/zU2n74h.png) ### 斷層陷阱 - 斷層陷阱(Chasm Traps)是指實體間應該存在的關聯根本不存在,2個實體間並沒有辦法找到一條路徑來連接。例如:一位學生可以修多門課程,每一位學生會購買多本教科書。 ![](https://i.imgur.com/PXPz84m.png) - 學生可能不買教科書(部份參與),而學生會修多門課,導致我們會發現有些教科書(T002)不知道是那一門課程的教科書 ![](https://i.imgur.com/JNZRh2W.png) - Solution :新增關聯型態,以此例是在Courses和Textbooks實體型態新增一對多的關聯型態來解決此問題 ![](https://i.imgur.com/sUTsPq1.png) - Courses和Textbooks實體型態間擁有一個新關聯型態Assign,表示課程指定的教課書有那幾本。 ![](https://i.imgur.com/JuHv7HL.png) ### 扇形陷阱與斷層陷阱比較表 ![](https://i.imgur.com/WnSteai.png) **------- 概念資料模組的實體關聯圖 ------> 邏輯資料模組的關聯表綱要 -------** ## 將實體關聯圖轉換成關聯表綱要 ### 實體轉關聯表 在實體關聯圖的強實體型態(即一般實體型態)是對應關聯表,也就是將強實體型態轉換成關聯表綱要,其規則如下所示: - 建立新關聯表綱要,其名稱是實體型態名稱。 - 在關聯表綱要包含單元值屬性型態和複合屬性型態。 - 關聯表綱要不包含多重值屬性型態、外來鍵和導出屬性型態。 - 將鍵屬性(Key Attribute)指定為關聯表綱要的主鍵。 ![](https://i.imgur.com/qjl8qXo.png) - Employees(SSN, name, address-city, address-street, tel) ### 關聯轉關聯表 實體關聯圖的關聯型態可以轉換成關聯表綱要的外來鍵,在關聯表綱要新增參考其他實體型態的外來鍵,可以分為三種: - 一對一關聯型態: - 一對一關聯型態轉換成關聯表綱要的規則如下: - [ ]在參與關聯性的關聯表綱要新增參考到另一個關聯表綱要的外來鍵(FK)。 - [ ]若關聯型態擁有單元值屬性,也一併加入新增外來鍵的關聯表綱要。 ![](https://i.imgur.com/r1vNqJY.png) - 一對多關聯型態 - 一對多關聯型態轉換成關聯表綱要的規則如下: - [ ]在N端的關聯表綱要新增參考到1端關聯表綱要的外來鍵(FK)。 - [ ]若關聯型態擁有單元值屬性,也一併加入新增外來鍵的關聯表綱要。 - 多對多關聯型態 - 多對多關聯型態轉換成關聯表綱要的規則如下: - [ ]將關聯型態建立成新的關聯表綱要,名稱為關聯型態名稱,在新關聯表綱要擁有兩個外來鍵(FK),分別參考關聯到的實體型態。 - [ ]若關聯型態擁有單元值屬性,一併加入新的關聯表綱要。 - [ ]關聯型態建立的關聯表綱要主鍵是兩個外來鍵的組合鍵;有時,可能需要新增幾個關聯型態的屬性作為主鍵。 ![](https://i.imgur.com/EePvZn9.png) - 關聯型態可能擁有3個或更多實體型態間的關聯性,稱為「多重關聯型態」(Ternary Relationship Type),其轉換規則與多對多關聯型態轉換一樣。 ![](https://i.imgur.com/dQ3CMuS.png) ![](https://i.imgur.com/Y2MNwO2.png) ### 多重值屬性轉換成關聯表 - 多重值屬性轉換成關聯表綱要的規則,如下所示: - 建立新的關聯表綱要,名稱可以是屬性名稱或實體與屬性結合的名稱。 - 在新關聯表綱要新增參考到實體型態主鍵的外來鍵。 - 新關聯表綱要的主鍵是外來鍵加上多重值屬性,如果多重值屬性是複合屬性,可能需要加上其中一個屬性或是全部屬性。 ### 弱實體型態轉換成關聯表 - 弱實體型態轉換成關聯表綱要,因為弱實體型態一定擁有一個對應的識別實體型態(Identifying Entity Type),所以在轉換上稍有不同,其規則如下所示: - 建立新的關聯表綱要,其名稱為弱實體型態的名稱。 - 新關聯表綱要包含單元值屬性型態。 - 在新關聯表綱要新增**識別實體型態的主鍵**作為參考的外來鍵。 - 將弱實體型態的「部分鍵」(Partial Key)加上外來鍵指定成新關聯表綱要的主鍵。 ![](https://i.imgur.com/8gosiOZ.png)

    Import from clipboard

    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 lost their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template is not available.


    Upgrade

    All
    • All
    • Team
    No template found.

    Create custom template


    Upgrade

    Delete template

    Do you really want to delete this template?

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

    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

    Tutorials

    Book Mode Tutorial

    Slide Mode Tutorial

    YAML Metadata

    Contacts

    Facebook

    Twitter

    Feedback

    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

    Versions and GitHub Sync

    Sign in to link this note to GitHub Learn more
    This note is not linked with GitHub Learn more
     
    Add badge Pull Push GitHub Link Settings
    Upgrade now

    Version named by    

    More Less
    • Edit
    • Delete

    Note content is identical to the latest version.
    Compare with
      Choose a version
      No search result
      Version not found

    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. Learn more

         Sign in to GitHub

        HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.

        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
        Available push count

        Upgrade

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Upgrade

        Danger Zone

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

        Syncing

        Push failed

        Push successfully