###### tags: `database` `note` `thu` {%hackmd theme-dark %} # ER Diagram至Schema的轉換 本頁將會聚焦在將ER Diagram轉換至Schema的演算法。 詳細請參見課本Ch9。 ## 簡介(~~幹話~~) 我們既然在把資料轉換成Diagram的時候就已經把資料分類成三大類, 那我們就也把轉換成schema的步驟分成三大類: Entity、Relationship、Attributes。 ## 1. ***Entity*** ### a. 建一個table叫Employee。 ### b. 把attribute們放進去當column的欄位(composite的打散)。 ### c. 選一個key attribute當primary key。 ### d. (Weak Entity) 把它依存的Regular Entity的Primary用"Foreign Key"的方式複制進來。 ### Example 1 (Regular)  #### 變成了  ### Example 2 (Weak)  #### 變成了  - 這裡的ESSN為Employee的SSN(Foreign key)。 ## 2. ***Relationship*** ### a. Relationship relation - 直接生出一個table來記錄誰參與了該relationship。 - 記錄時使用Foreign key。 - 而attribute們就照常放入。 ### b. (Binary 1:N) Foreign key approach - 在N side (數量比較多的那一邊)裡把1 side用Foreign key的形式記錄下來。 ### c. (Binary 1:1 total) Just merge - 當兩邊都是total participation時,就可以直接合併成同一個table。 ### Note: 可以的話就用b,不能的話再用a。 ### Example 1: SUPPLY (n-ary)  #### 變成了  ### Example 2: WORKS_ON (Binary M:N)  #### 變成了  - Essn指向Employee的Ssn - Pno指向Project的Pnumber ### Example 3: WORKS_FOR (Binary 1:N)  #### 變成了  ### Example 4: MANAGES (Binary 1:1)  #### 變成了  ## 3. ***Attributes*** ### a. Multivalued Attributes (多值屬性) - 另外為該Attribute開一個table。 - 其中一個column對應到Entity的Primary key。 - 剩下的就把他原本的值也開一個column放入。 - (注意) 有的新版relational model會有array這種資料型態,就不要再傻傻的另開一個table了。 ### Example  #### 變成了  - 這裡的Dnumber為來自Department的Foreign key。
×
Sign in
Email
Password
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