06

Updated 4 Oct 2026

ER-to-Relational Mapping: 7-Step Recipe

Step 1: Regular/Strong Entity Types

อันแรกง่าย ๆ ก็เอาพวก Strong Entity มาเขียนก่อน (สี่เหลี่ยมธรรมดา)

  • เอา Simple attributes อย่างเดียว (ถ้าเป็น Composite ให้แยกออกมา)
  • อันที่มีขีดเส้นใต้ก็คือ Primary Key เขียนกำกับไว้ด้วย
  • ถ้ามี Derived attributes (คำนวณได้) ให้ทิ้งซะ ไม่ต้องเอา
    • Multivalued (กลม ๆ 2 วง) ก็ยังไม่ต้องใส่ เอาไปทำ Step 6

Step 2: Weak Entity Types

Weak = สี่เหลี่ยมสองวง

  • Create a relation R for each weak entity type W
  • Include all attributes of the weak entity
  • Include a foreign key (FK) from the primary key (PK) of the owner entity

เอา Primary Key ของ Strong มาใส่ Weak ด้วย แล้วขีดเส้นใต้ด้วย แล้วถือว่าเป็น PK ด้วย

  • The PK of the new relation = combination of owner's PK + partial key of weak entity

Step 3: Binary 1:1 Relationships

ไม่ต้องทำ ถ้ามี ternary ให้ไปทำ steps 7 เพราะสำคัญกว่า

  • ถ้าเจอที่เป็น 0 กับ 1 อย่าลืมเขียน Double line แปลว่า “must” in English.
    • แล้วไอ่ตัวที่มี Double line จะถือว่าเป็น Main Determination
    • เอา Primary Key ของอีกฝั่ง (ฝั่งรอง) เอามาใส่
  • แต่ว่าใน 1:1 นี้จะไม่ใส่ PK ให้กับ Unique ID ของฝั่งรองนะ (แอบ make sense เพราะว่าฝั่งหลักยังไงมีแค่นั้นก็ไม่ซ้ำอยู่ละ)
  • ถ้ามี Attribute ใน Relationship ให้เอาไปต่อเป็น attribute นึงฝั่ง Main

Step 4: Binary 1:N Relationships

  • หาฝั่ง N (ลูกไก่)
  • เอา FK ของฝั่ง 1 (แม่ไก่) มาใส่ให้ลูกไก่
  • เอา attributes ของความสัมพันธ์มาใส่ที่ลูกไก่

ง่าย ๆ ก็คือหาฝั่ง Main ก่อน เสร็จปุ๊ปเอา PK ของลูก มาใส่เป็น Foreign Key ในตารางของแม่

จำง่าย ๆ: ลูกไก่ต้องรู้ว่าแม่คือใคร = FK ไปที่ N

Step 5: Binary M:N Relationships

  • Create a new relation E3 to represent the relationship R

  • Include FKs in E3 obtained from the PKs of both participating entities

  • Use the combination of these FKs to form the PK of E3

  • Include any relationship attributes in E3

  • สร้าง table ใหม่ให้ความสัมพันธ์

  • เอา PK จาก 2 ฝั่งมาใส่ กลายเป็น PK, FK บน ตัวเชื่อมอะเนอะ

  • เอา attributes ของความสัมพันธ์มาใส่

จำง่าย ๆ: M:N = Many-to-Many = ยุ่งเหยิง = ต้องมี table กลาง

Step 6: Multi-Valued Attributes

  • For each multivalued attribute A, create a new relation R

  • Include an attribute corresponding to A plus the PK of the relation that A belongs to

  • The PK of R = combination of A and the original entity's PK

  • สร้าง table ใหม่ให้ attribute ที่เป็น Multi-value

  • เอา PK ของ entity เดิมมาด้วย

  • PK ใหม่ = PK เดิม + attribute ที่เป็น Multivalue (ก็คือตัวที่เอาจาก entity เดิมจะเป็น FK,PK ส่วนของอัน multivalue ก็จะ PK ธรรมดา)

Step 7: N-ary Relationships (n > 2)

  • Create a new relation E to represent the n-ary relationship R

  • Include FKs obtained from the PKs of all participating entities

  • Include any attributes of the n-ary relationship in E

  • The PK is typically the combination of all FKs (unless specified otherwise)

  • เปิดตารางใหม่ของ Relation นั้น ที่มันอยู่ตรงกลาง (n > 2)

  • เอา Primary ของทุกอันรอบ ๆ มาใส่ ให้เป็น Composite (ก็คือ PK + FK)

  • Include Attribute ของตัวมันเองด้วย (ถ้ามีนะ)

Step 8:

Warning


ระวังนะ อย่าทำ Relation ซ้ำ เช่น Weak ไปแล้ว (แล้วมันเชื่อมกันด้วย 1:N) ก็อย่าไป Apply ซ้ำในขั้นตอน Step 4 อีกเลยล่ะ

Example:

Quick Reference Summary

  • Step 1: Strong entity → PK
  • Step 2: Weak entity → Composite PK, FK
  • Step 3: 1:1 Relationship → PK, FK
  • Step 4: 1:M Relationship → PK, FK
  • Step 5: M:N Relationship → Composite PK, FK
  • Step 6: Multivalued Attribute → Composite PK, FK
  • Step 7: N-ary Relationship → Composite PK, FK

Key Exam Tips

  1. Always identify primary keys first
  2. Foreign keys must reference existing primary keys
  3. Composite primary keys use parentheses: (attr1, attr2)
  4. For 1:1 relationships, choose the side with total participation
  5. For 1:N relationships, FK goes on the N-side
  6. M:N relationships always create a new junction table
  7. Multivalued attributes always become separate tables