• Now we want to convert the conceptual ER schema into a relational database schema
  • This step is called Mapping
  • We then express this relational schema using SQL code

Relational Data Model

![[Data Models in Databases#23-relational-model-ุงู„ุฃุดู‡ุฑ|2.3 Relational Model (ุงู„ุฃุดู‡ุฑ)]]

Constraints

We talked about Participation Constraints In relationships

ER to Relational Mapping

Mapping Steps

Summary

Mapping TypeSolution
1. Strong EntitiesCreate a new table for each strong entity
2. Weak EntitiesCreate a new table and add a foreign key for the strong entity
3. Binary Relationships
- 1:1 Must-MustCombine in one table (no new table needed)
- 1:1 May-MustForeign key on the โ€œMustโ€ side (no new table needed)
- 1:1 May-MayForeign key on either side (no new table needed)
- 1:M Must from โ€œManyโ€ sideForeign key on the โ€œManyโ€ side (no new table needed)
- 1:M May from โ€œManyโ€ sideForeign key on the โ€œManyโ€ side (no new table needed)
- M:NCreate a new linking table for the relationship
4. Ternary RelationshipsCreate a new linking table with three foreign keys
5. Unary RelationshipsUse a self-referencing foreign key or create a new table depending on the relationship type

1. Mapping of regular(Strong) Entity Types

1) Composite Attribute

ุนุดุงู† ุฃุธู‡ุฑู‡ุง ุจุนู…ู„ู‡ุง ุนู„ู‰ ุญุณุจ ุงู„ู€ Constraint File ุฃูˆ ูƒู†ุง ู‚ูˆู„ู†ุง ุนู„ูŠู‡ Equations File ูู„ุงุฒู… ุฃูƒุชุจ ููŠู‡ุง ุญุงุฌุฉ ุฒูŠ Composite ุงู†ู‡ุง ู‡ุชุธู‡ุฑ ุงู„ุฅุณู… (Mahmoud Feshar) ูŠุนู†ูŠ ุจูƒุชุจ ุงู„ู…ุนุงุฏู„ุฉ ู†ูุณู‡ุง ุฃูˆ ุงู„ู€ Equation

2) Multivalued Attributes

ุงู„ู€ Complex Attribute ุฒูŠู‡ุง ุจุฑุถูˆ ุจุงู„ุธุจุท

3) Mapping of Derived Attributes

ุจู†ุนู…ู„ Ignore ู„ู„ู€ Derived ูˆู†ูƒุชุจ ุงู„ุฃุตู„ูŠุฉ ุจุณ ู…ู…ูƒู† ููŠ ุงู„ุจูŠุฒู†ูŠุณ ูŠุฎู„ูŠู†ุง ู†ุนู…ู„ู‡ุง Column ุนุงุฏูŠ

ุจู†ูƒุชุจ ุจุฑุถูˆ ุงู„ู…ุนุงุฏู„ุฉ ุจุชุงุนู†ุง ููŠ ุงู„ู€ Equations File ูŠุนู†ูŠ ู…ุซู„ู‹ุง Age = CurrentDate - DOB ู„ูˆ ูƒู„ ู…ุฑุฉ ู‡ุญุณุจ ุงู„ู…ุนุงุฏู„ุฉ ุฏูŠ ูˆุฃู†ุง ู…ุด ู…ุนุงูŠุง Resources ูุงุนู…ู„ู‡ุง Column ุนุงุฏูŠ ุฃูˆ ู„ูˆ ุนุงูŠุฒ ุฃุนู…ู„ ู…ุซู„ู‹ุง ุงู„ุฃุจู„ูƒูŠุดู† ูŠุจู‚ุง ุณุฑูŠุน ู„ุฃู†ู‡ ู…ุด ู‡ุนู…ู„ ุงู„ุนู…ู„ูŠุฉ ุฏูŠ ุฒูŠ ุงู„ุฃุจู„ูƒูŠุดู†ุฒ ุงู„ู„ุงูŠุช

ู„ุงุฒู… ุชุนู…ู„ ู…ู†ุดู† ููŠ ุงู„ู€ Equation ู„ูˆ ู‡ูŠ ู…ุนุชู…ุฏุฉ ุนู„ู‰ ุฏุงุชุง ู…ู† Table ุชุงู†ูŠ

2. Mapping of Weak Entity Type

ุงูุชูƒุฑ ุงู† ุงู„ู€ Weak Entity ู…ุจูŠุจู‚ุงุด ููŠู‡ุง Primary key ูˆุจูŠุจู‚ุง ููŠู‡ุง Partial Key

3. Mapping Relationships

1) Binary M:N Relationship

2) Binary 1:M Relationship

Approach 1 (Must)

Approach 2 (May)

3)Binary 1:1 Relationship

Approach 1 (Total-partial)

Approach 2 (Total-total)

Approach 3 (Partial-partial)

Summary

4. Mapping of N-ary Relationship

5. Mapping of Unary Relationship

ุจุนู…ู„ู‡ุง ููŠ ู†ูุณ ุงู„ู€ Table ูˆุฃุฒูˆุฏ FK ููŠู‡ุง ุจูŠุดุงูˆุฑ ุนู„ู‰ PK ุฏุง ููŠ ุญุงู„ุฉ ุงู„ู€ 1:M

ููŠ ุญุงู„ุฉ ุงู„ู€ M:N ุจู†ุนู…ู„ Table ุฌุฏูŠุฏุฉ ุจูŠุจู‚ุง ููŠู‡ุง ุงุชู†ูŠู† FK ุจูŠุดุงูˆุฑูˆุง ุนู„ู‰ ู†ูุณ ุงู„ู€ PK ุงู„ุญูˆุงุฑ ุฏุง ุงุณู…ู‡ Junction Table

Junction Table

ุงู„ู€ Junction Table ุจุจุณุงุทุฉ ู‡ูˆ ุฌุฏูˆู„ ุจู†ุนู…ู„ู‡ ููŠ ู‚ูˆุงุนุฏ ุงู„ุจูŠุงู†ุงุช ุนู„ุดุงู† ู†ู‚ุฏุฑ ู†ูˆุตู„ ุจูŠู† ุฌุฏูˆู„ูŠู† ุนู†ุฏู‡ู… ุนู„ุงู‚ุฉ Many-to-Many. ุทูŠุจ ู„ูŠู‡ ุจู†ุญุชุงุฌู‡ุŸ ู„ุฃู† ู‚ุงุนุฏุฉ ุงู„ุจูŠุงู†ุงุช ู…ุง ุชู‚ุฏุฑุด ุชุนู…ู„ ุงู„ุนู„ุงู‚ุฉ ุฏูŠ ู…ุจุงุดุฑุฉุŒ ูู„ุงุฒู… ู†ุนู…ู„ ุฌุฏูˆู„ ุซุงู„ุซ ูŠูˆุตู„ ุจูŠู†ู‡ู….

ุทูŠุจ ู†ุงุฎุฏ ู…ุซุงู„ ุจุณูŠุทุŸ

ุฎู„ูŠู†ุง ู†ู‚ูˆู„ ุฅู† ุนู†ุฏู†ุง ู‚ุงุนุฏุฉ ุจูŠุงู†ุงุช ู„ู„ู…ุฏุฑุณุฉุŒ ูˆุนู†ุฏู†ุง ุฌุฏูˆู„ูŠู† ุฑุฆูŠุณูŠูŠู†:

  1. ุฌุฏูˆู„ Students (ุงู„ุทู„ุงุจ)ุŒ ููŠู‡ ุจูŠุงู†ุงุช ุนู† ูƒู„ ุทุงู„ุจ ุฒูŠ ุงุณู…ู‡ ูˆุฑู‚ู… ุงู„ุทุงู„ุจ.
  2. ุฌุฏูˆู„ Courses (ุงู„ู…ูˆุงุฏ)ุŒ ููŠู‡ ุจูŠุงู†ุงุช ุนู† ูƒู„ ู…ุงุฏุฉ ุฒูŠ ุงุณู… ุงู„ู…ุงุฏุฉ ูˆุฑู‚ู… ุงู„ู…ุงุฏุฉ.

ุทูŠุจ ุงู„ู…ุดูƒู„ุฉ ู‡ู†ุง ุฅู† ุงู„ุทุงู„ุจ ุงู„ูˆุงุญุฏ ู…ู…ูƒู† ูŠุงุฎุฏ ุฃูƒุชุฑ ู…ู† ู…ุงุฏุฉุŒ ูˆุงู„ู…ุงุฏุฉ ุงู„ูˆุงุญุฏุฉ ู…ู…ูƒู† ูŠูƒูˆู† ููŠู‡ุง ุฃูƒุชุฑ ู…ู† ุทุงู„ุจ. ูŠุนู†ูŠ ุนู†ุฏู†ุง ุนู„ุงู‚ุฉ Many-to-Many.

ู†ุญู„ ุงู„ู…ุดูƒู„ุฉ ุฅุฒุงูŠุŸ

ุจู†ุนู…ู„ ุฌุฏูˆู„ ุฌุฏูŠุฏ ู†ุณู…ูŠู‡ Students_CoursesุŒ ูˆุฏู‡ ู‡ูˆ ุงู„ู€ Junction Table ุงู„ู„ูŠ ู‡ูŠูˆุตู„ ุจูŠู† Students ูˆCourses.

ููŠ ุงู„ุฌุฏูˆู„ ุฏู‡ ุจู†ุญุท:

  1. ุงู„ู€student_id: ุฏู‡ ุฒูŠ ุงู„ู…ูุชุงุญ ุงู„ู„ูŠ ุจูŠุฑุจุทู†ุง ุจุฌุฏูˆู„ Students.
  2. ุงู„ู€course_id: ุฏู‡ ุงู„ู…ูุชุงุญ ุงู„ู„ูŠ ุจูŠุฑุจุทู†ุง ุจุฌุฏูˆู„ Courses.

ุดูƒู„ ุงู„ุจูŠุงู†ุงุช ููŠ ุงู„ุฌุฏูˆู„

ู‡ุชุจู‚ู‰ ุจุงู„ุดูƒู„ ุฏู‡:

student_idcourse_id
1101
1102
2101
3103