Advanced EER & Mapping Advanced EER & Mapping (التحويل)
Concluding the EER model with Hierarchies and Union Types, then learning the critical skill of transforming ERD into Relational Schemas. استكمال نموذج EER مع الهرميات (Hierarchies) و Union Types، ثم تعلم المهارة الحاسمة لتحويل ERD إلى Relational Schemas.
Hierarchies vs. Lattices Hierarchies vs. Lattices
Specialization Hierarchy Specialization Hierarchy
Every subclass has only ONE superclass. It's a strict tree structure. Single Inheritance. كل subclass لديه superclass واحد فقط. هيكل شجري صارم. Single Inheritance.
Specialization Lattice Specialization Lattice
A subclass can have MULTIPLE superclasses. This is known as Multiple Inheritance.
Example: Engineering_Manager inherits from both Engineer and Manager.
يمكن للـ subclass أن يكون لديه متعدد superclasses. يُعرف هذا بـ Multiple Inheritance.
مثال: مدير_هندسي يرث من كل من Engineer و Manager.
Union Types (Categories) Union Types (Categories)
The Concept المفهوم
Sometimes a single subclass/relationship needs to connect to multiple distinct entity types that play the same role.
Example: An OWNER of a vehicle can be a PERSON, a BANK, or a COMPANY.
أحياناً يحتاج subclass/علاقة واحدة للاتصال بأنواع كيانات متميزة متعددة تلعب نفس الدور.
مثال: OWNER (المالك) لمركبة يمكن أن يكون PERSON، أو BANK، أو COMPANY.
In Category (Union), the entity must exist in AT LEAST ONE of the superclasses (OR). في Shared Subclass (Lattice)، الكيان يجب أن يوجد في كل الـ superclasses (AND).
في Category (Union)، الكيان يجب أن يوجد في واحد على الأقل من الـ superclasses (OR).
ER-to-Relational Mapping Algorithm خوارزمية ER-to-Relational Mapping
How do we turn bubbles and diamonds into tables? Follow the 7-Step Algorithm. كيف نحول الفقاعات والمعينات إلى جداول؟ اتبع الخوارزمية المكونة من 7 خطوات.
Regular Entities Regular Entities
Create a relation (table) for each strong entity. Simple attributes become columns. Composite attributes are flattened. أنشئ علاقة (جدول) لكل كيان قوي. السمات البسيطة تصبح أعمدة. السمات المركبة يتم تسطيحها (flattened).
Weak Entities Weak Entities
Create a relation. Include attributes. Add Owner's Primary Key (PK) as Foreign Key (FK). The PK of this table is (Owner PK + Partial Key). أنشئ علاقة. ضمن السمات. أضف الـ Primary Key (PK) للمالك كـ Foreign Key (FK). الـ PK لهذا الجدول هو (Owner PK + Partial Key).
Binary 1:1 Relationships Binary 1:1 Relationships
Foreign Key approach:نهج Foreign Key: Add PK of one side as FK to the other. Prefer adding to the side with Total Participation to avoid NULLs. أضف الـ PK من أحد الجانبين كـ FK للآخر. يفضل الإضافة للجانب ذو Total Participation لتجنب الـ NULLs.
Binary 1:N Relationships Binary 1:N Relationships
Take PK from the "1-side" and put it as FK in the "N-side". (e.g., Dept PK goes into Employee table). خُذ الـ PK من "جانب 1" وضعه كـ FK في "جانب N". (مثال: Dept PK يذهب إلى جدول Employee).
Binary M:N Relationships Binary M:N Relationships
Create a new relation (Associative Entity). Include PKs of both sides as FKs. The combination of these FKs becomes the PK. أنشئ علاقة جديدة (Associative Entity). ضمن PKs من كلا الجانبين كـ FKs. مزيج هذه الـ FKs يصبح هو الـ PK.
Multivalued Attributes Multivalued Attributes
Create a new relation. Include attribute value + FK of parent entity. PK is the combination of both. أنشئ علاقة جديدة. ضمن قيمة السمة + FK للكيان الأب. الـ PK هو المزيج بينهما.
N-ary Relationships (>2) N-ary Relationships (>2)
Create a new relation. Include PKs of all participating entities as FKs. أنشئ علاقة جديدة. ضمن PKs لجميع الكيانات المشاركة كـ FKs.
Mapping EER Concepts Mapping EER Concepts
How to map Superclass/Subclass? There are 4 options (A, B, C, D). كيف يتم تحويل Superclass/Subclass؟ هناك 4 خيارات (A, B, C, D).
Best for: Any specialization (Total/Partial, Disjoint/Overlap). أنشئ جدولاً للـ Superclass. أنشئ جداول للـ Subclasses (الـ PK = الـ PK للـ Superclass).
الأفضل لـ: أي specialization (Total/Partial, Disjoint/Overlap).
Best for: Total Participation + Disjoint. أنشئ جداول للـ Subclasses فقط. ضمن سمات الـ Superclass فيها.
الأفضل لـ: Total Participation + Disjoint.
Best for: Disjoint + Few attributes. جدول واحد للكل. أضف عمود "Type" للتمييز. قيم NULL للسمات غير القابلة للتطبيق.
الأفضل لـ: Disjoint + سمات قليلة.
Best for: Overlapping. جدول واحد. أعمدة boolean متعددة (IsManager?, IsEngineer?) للتمييز.
الأفضل لـ: Overlapping.
The Exam Vault خزنة الاختبار
Professor's Secrets & Trap Avoidance أسرار البروفيسور وتجنب الفخاخ
TRAP: 1:N Mapping Direction فخ: اتجاه 1:N Mapping
Where does the Foreign Key go?
It always goes to the N-side (Child side).
If Employee(N) works for Dept(1), the Dept_ID goes into the Employee table. If you put Employee_ID into Dept table, you can't handle multiple employees per dept (violates 1NF).
أين يذهب الـ Foreign Key؟
دائماً يذهب إلى الجانب N (جانب الطفل).
إذا كان الموظف Employee(N) يعمل في قسم Dept(1)، فإن Dept_ID يذهب إلى جدول Employee. إذا وضعت Employee_ID في جدول Dept، لن تتمكن من التعامل مع عدة موظفين في القسم الواحد (ينتهك 1NF).
TRAP: Multivalued Attributes فخ: Multivalued Attributes
"Can I just add columns like Phone1, Phone2?"
NO. That is bad design.
You MUST create a separate table for the multivalued attribute. The PK of this new table is a composite key (ParentFK + Value).
"هل يمكنني فقط إضافة أعمدة مثل Phone1, Phone2؟"
لا. هذا تصميم سيء.
يجب عليك إنشاء جدول منفصل للسمة متعددة القيم. الـ PK لهذا الجدول الجديد هو composite key (ParentFK + القيمة).
SECRET: Surrogate Keys سر: Surrogate Keys
If a Category (Union Type) has superclasses with different Primary Key types (e.g., String SSN vs Int CompanyID), you cannot inherit the PK. You must create a new Surrogate Key (artificial ID) for the Category. إذا كان للـ Category (Union Type) كيانات superclasses بأنواع Primary Key مختلفة (مثال: نص SSN مقابل رقم CompanyID)، لا يمكنك وراثة الـ PK. يجب عليك إنشاء Surrogate Key (معرف اصطناعي) جديد للـ Category.
KEY CONCEPT: Mapping Weak Entities مفهوم أساسي: Mapping Weak Entities
The Primary Key of a Weak Entity table is always composite. It consists of the Foreign Key from the Owner table PLUS the Weak Entity's own Partial Key. الـ Primary Key لجدول الـ Weak Entity هو دائماً مركب (composite). يتكون من الـ Foreign Key من جدول المالك بالإضافة إلى الـ Partial Key الخاص بالـ Weak Entity.