كل المقررات

الرئيسية / مقرر قاعدة البيانات / الفصل 4 الجزء 1

الفصل 4 (الجزء 1): Mapping EER to Relations

Entities, relationships and multivalued attributes

على طول: إزاي نحوّل رسمة ER لجداول حقيقية، خطوة بخطوة.

فيديو المحاضرة

فيديو المحاضرة

بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.

رسوم متحركة

تحويل علاقات 1:N و M:N

أمثلة SQL

أمثلة قصيرة وبسيطة

الكود قصير والنتيجة اتجرّبت فعلاً. عدّل في المحرر واضغط شغّل.

المثال ١: 1:N بـ foreign key

Sample output (press Run to produce your own):
name | n
-----+--
IT   | 2
Math | 1
(2 rows)

المثال ٢: M:N بجدول جديد

Sample output (press Run to produce your own):
name | title            | grade
-----+------------------+------
Mona | Databases        | A    
Mona | Machine Learning | B    
Omar | Databases        | A    
(3 rows)

المثال ٣: Multivalued attribute

Sample output (press Run to produce your own):
name | phones
-----+-------
Mona | 2     
Omar | 1     
(2 rows)

افتح الموضوع المقابل في صفحة LiU TDDD12

اختبار قصير

اختبر فهمك

1. In a 1:N relationship, the foreign key goes on:

2. A M:N relationship becomes:

3. A multivalued attribute becomes:

4. The primary key of a M:N relationship table is:

5. A composite attribute is mapped to:

تمارين برمجة

جرّب بإيدك

التمرين 1

طبّق قاعدة 1:N: مؤلف (author) عنده كتب كتير (book). اعمل الجدولين وضيف كتابين لنفس المؤلف.

Apply the 1:N rule: an author has many books. Create both tables and add two books for one author.

(output appears here)

التمرين 2

طبّق قاعدة M:N: فيلم (movie) وممثل (actor). اعمل جدول cast الوسيط.

Apply the M:N rule: movies and actors. Create the cast table in the middle.

(output appears here)

مشروع صغير في آخر المحاضرة

مكتبة: من رسمة ER لجداول

الداتا: Library sample data: 2 members, 3 books, 4 loans. Written for this course (our own data, free to reuse).

License / source: Our own sample data; SQLite is in the public domain. Dataset page

الخطوات

  1. الرسمة: MEMBER و BOOK بينهم علاقة M:N اسمها LOAN (فيها loan_date). وـ BOOK ليه multivalued attribute اسمه topics.
  2. حوّل: member و book و loan (key مركّب) و book_topic.
  3. ضيف الداتا من الـ starter.
  4. اعرض كل عضو وعدد الكتب اللي استلفها. المتوقع: Mona = 3 و Omar = 1.
Expected output (press Run to produce your own):
name | loans
-----+------
Mona | 3    
Omar | 1    
(2 rows)

Credits: the topic order follows the lecture list of the course TDDD12 Database Technology at Linköping University (https://www.ida.liu.se/~TDDD12/fo/), whose reading guide is based on the textbook Fundamentals of Database Systems by Elmasri and Navathe. We found no reuse licence on that page, so we link to it and copy nothing from it: the explanations, diagrams, data and code in this lecture are our own. Reference: Elmasri and Navathe, Fundamentals of Database Systems, the chapters on mapping ER and EER to relations.