فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
رسوم متحركة
أمثلة 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)
اختبار قصير
اختبر فهمك
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)
sql('''
CREATE TABLE author (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE book (id INTEGER PRIMARY KEY, title TEXT, author_id INTEGER REFERENCES author(id));
INSERT INTO author VALUES (1, 'Naguib');
INSERT INTO book VALUES (1, 'Book A', 1), (2, 'Book B', 1);
SELECT a.name, b.title FROM book b JOIN author a ON a.id = b.author_id;
''')
Output
name | title -------+------- Naguib | Book A Naguib | Book B (2 rows)
التمرين 2
طبّق قاعدة M:N: فيلم (movie) وممثل (actor). اعمل جدول cast الوسيط.
Apply the M:N rule: movies and actors. Create the cast table in the middle.
(output appears here)
sql('''
CREATE TABLE movie (id INTEGER PRIMARY KEY, title TEXT);
CREATE TABLE actor (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE cast_member (movie_id INTEGER REFERENCES movie(id), actor_id INTEGER REFERENCES actor(id), PRIMARY KEY (movie_id, actor_id));
INSERT INTO movie VALUES (1, 'M1'), (2, 'M2');
INSERT INTO actor VALUES (1, 'A1'), (2, 'A2');
INSERT INTO cast_member VALUES (1,1), (1,2), (2,1);
SELECT m.title, a.name FROM cast_member c JOIN movie m ON m.id = c.movie_id JOIN actor a ON a.id = c.actor_id;
''')
Output
title | name ------+----- M1 | A1 M1 | A2 M2 | A1 (3 rows)
مشروع صغير في آخر المحاضرة
مكتبة: من رسمة 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
الخطوات
- الرسمة: MEMBER و BOOK بينهم علاقة M:N اسمها LOAN (فيها loan_date). وـ BOOK ليه multivalued attribute اسمه topics.
- حوّل: member و book و loan (key مركّب) و book_topic.
- ضيف الداتا من الـ starter.
- اعرض كل عضو وعدد الكتب اللي استلفها. المتوقع: 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.