فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
رسوم متحركة
أمثلة SQL
أمثلة قصيرة وبسيطة
الكود قصير والنتيجة اتجرّبت فعلاً. عدّل في المحرر واضغط شغّل.
المثال ١: Entity وattributes كجدول
Sample output (press Run to produce your own): student_id | name | year -----------+------+----- 1 | Mona | 2 2 | Omar | 3 (2 rows)
المثال ٢: علاقة واحد لكتير
Sample output (press Run to produce your own): dept | employees ------+---------- IT | 2 Sales | 1 (2 rows)
المثال ٣: علاقة كتير لكتير
Sample output (press Run to produce your own): name | title | grade -----+------------------+------ Mona | Databases | A Mona | Machine Learning | B Omar | Databases | B (3 rows)
اختبار قصير
اختبر فهمك
1. In an ER diagram, a key attribute is shown:
2. A relationship is drawn as:
3. A student can take many courses and a course has many students. The cardinality is:
4. The grade of a student in a course is best an attribute of:
5. Total participation means:
تمارين برمجة
جرّب بإيدك
التمرين 1
اعمل جدولين author و book، وكل كتاب له مؤلف واحد (علاقة واحد لكتير)، وضيف بيانات واعرض اسم المؤلف مع عنوان الكتاب.
Create author and book tables where each book has one author (1:N). Insert data and show author name with book title.
(output appears here)
sql('''
CREATE TABLE author (author_id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE book (book_id INTEGER PRIMARY KEY, title TEXT, author_id INTEGER NOT NULL REFERENCES author(author_id));
INSERT INTO author VALUES (1, 'Mona'), (2, 'Omar');
INSERT INTO book VALUES (1, 'SQL Basics', 1), (2, 'ER Models', 1), (3, 'Indexes', 2);
SELECT a.name, b.title FROM book b JOIN author a ON a.author_id = b.author_id;
''')
Output
name | title -----+----------- Mona | SQL Basics Mona | ER Models Omar | Indexes (3 rows)
التمرين 2
جرّب تضيف كتاب بمؤلف مش موجود. إيه اللي بيحصل؟
Try to insert a book whose author does not exist. What happens?
(output appears here)
sql('''
CREATE TABLE author (author_id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE book (book_id INTEGER PRIMARY KEY, title TEXT, author_id INTEGER NOT NULL REFERENCES author(author_id));
INSERT INTO author VALUES (1, 'Mona');
INSERT INTO book VALUES (1, 'Ghost Book', 99);
SELECT COUNT(*) AS books FROM book;
''')
Output
Error: FOREIGN KEY constraint failed books ----- 0 (1 row)
مشروع صغير في آخر المحاضرة
نموذج مكتبة بعلاقة كتير لكتير
الداتا: Library sample data: 3 members, 3 books and 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
الخطوات
- الـ entities: member و book. العلاقة LOAN بين الاتنين كتير لكتير وليها attribute اسمه loan_date.
- اعمل الجداول الثلاثة، والـ loan بـ primary key من (member_id, book_id).
- ضيف الداتا اللي في الـ starter.
- اعرض اسم العضو وعنوان الكتاب وتاريخ الاستعارة. المتوقع: 4 صفوف.
Expected output (press Run to produce your own): name | title | loan_date -----+------------+----------- Mona | SQL Basics | 2026-01-05 Mona | ER Models | 2026-01-09 Omar | SQL Basics | 2026-02-01 Nour | Indexes | 2026-02-10 (4 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 ER and EER modeling.