فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
رسوم متحركة
أمثلة SQL
أمثلة قصيرة وبسيطة
الكود قصير والنتيجة اتجرّبت فعلاً. عدّل في المحرر واضغط شغّل.
المثال ١: Weak entity
Sample output (press Run to produce your own): emp_id | dep_name | age -------+----------+---- 2 | Lina | 7 (1 row)
المثال ٢: جدول لكل class
Sample output (press Run to produce your own): name | gpa -----+---- Mona | 3.6 (1 row)
المثال ٣: جدول واحد بعمود type
Sample output (press Run to produce your own): name | kind | gpa | office -----+---------+------+------- Mona | student | 3.6 | NULL Omar | teacher | NULL | B-204 (2 rows)
اختبار قصير
اختبر فهمك
1. The primary key of a weak entity table is:
2. Option A for specialization uses:
3. A single table with a type column has the drawback of:
4. A category (union type) usually needs:
5. ON DELETE CASCADE on a weak entity means:
تمارين برمجة
جرّب بإيدك
التمرين 1
اعمل weak entity اسمه room داخل building. الـ key هو (building_id, room_no).
Create a weak entity room inside building. The key is (building_id, room_no).
(output appears here)
sql('''
CREATE TABLE building (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE room (building_id INTEGER REFERENCES building(id) ON DELETE CASCADE, room_no INTEGER, seats INTEGER, PRIMARY KEY (building_id, room_no));
INSERT INTO building VALUES (1, 'A'), (2, 'B');
INSERT INTO room VALUES (1, 101, 30), (1, 102, 20), (2, 101, 50);
SELECT b.name, r.room_no, r.seats FROM room r JOIN building b ON b.id = r.building_id;
''')
Output
name | room_no | seats -----+---------+------ A | 101 | 30 A | 102 | 20 B | 101 | 50 (3 rows)
التمرين 2
vehicle superclass وـ car subclass بخيار A. اعرض نوع العربية وعدد أبوابها.
A vehicle superclass and a car subclass using option A. Show the plate and the doors of each car.
(output appears here)
sql('''
CREATE TABLE vehicle (id INTEGER PRIMARY KEY, plate TEXT);
CREATE TABLE car (id INTEGER PRIMARY KEY REFERENCES vehicle(id), doors INTEGER);
INSERT INTO vehicle VALUES (1, 'ABC-1'), (2, 'XYZ-9');
INSERT INTO car VALUES (1, 4);
SELECT v.plate, c.doors FROM vehicle v JOIN car c ON c.id = v.id;
''')
Output
plate | doors ------+------ ABC-1 | 4 (1 row)
مشروع صغير في آخر المحاضرة
شركة: weak entity ووراثة
الداتا: Company sample data: 3 employees, 2 managers, 3 dependents. Written for this course (our own data, free to reuse).
License / source: Our own sample data; SQLite is in the public domain. Dataset page
الخطوات
- الرسمة: EMPLOYEE عنده subclass اسمه MANAGER (فيه bonus)، وـ DEPENDENT weak entity تابع للـ EMPLOYEE.
- حوّل لـ employee و manager (خيار A) و dependent بـ key مركّب.
- ضيف الداتا من الـ starter.
- اعرض اسم كل مدير وعدد أبناءه (dependents). المتوقع: Mona = 2 و Omar = 1.
Expected output (press Run to produce your own): name | dependents -----+----------- Mona | 2 Omar | 1 (2 rows)
مشروع نهاية الفصل
ملف المشروع العملي للفصل 4
ملف PDF فيه مشروع صغير بأسئلة عملية تطبّق بيها كل اللي اتعلمته في الفصل، مع حل نموذجي في الآخر.
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.