فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
رسوم متحركة
أمثلة SQL
أمثلة قصيرة وبسيطة
الكود قصير والنتيجة اتجرّبت فعلاً. عدّل في المحرر واضغط شغّل.
المثال ١: superclass وsubclass
Sample output (press Run to produce your own): name | gpa -----+---- Mona | 3.6 (1 row)
المثال ٢: Disjoint بقيد CHECK
Sample output (press Run to produce your own):
Error: CHECK constraint failed: kind IN ('student', 'staff')
person_id | name | kind
----------+------+--------
1 | Mona | student
(1 row)
المثال ٣: Weak entity بـ key مركّبة
Sample output (press Run to produce your own): Error: UNIQUE constraint failed: dependent.emp_id, dependent.name emp_id | name | relation -------+------+--------- 1 | Adam | son 1 | Lina | daughter (2 rows)
اختبار قصير
اختبر فهمك
1. Every student is a person. STUDENT is a ... of PERSON:
2. Generalization goes:
3. A subclass inherits from its superclass:
4. A person who can be both student and staff needs:
5. The key of a weak entity is:
تمارين برمجة
جرّب بإيدك
التمرين 1
اعمل جدول staff بيرث من person (نفس الـ person_id كـ primary key و foreign key) وفيه salary، وضيف موظف واعرضه مع اسمه.
Create a staff table that specializes person (same person_id as primary key and foreign key) with a salary column. Insert one staff member and show them with their name.
(output appears here)
sql('''
CREATE TABLE person (person_id INTEGER PRIMARY KEY, name TEXT);
INSERT INTO person VALUES (1, 'Omar');
CREATE TABLE staff (person_id INTEGER PRIMARY KEY REFERENCES person(person_id), salary INTEGER);
INSERT INTO staff VALUES (1, 8000);
SELECT p.name, s.salary FROM person p JOIN staff s ON s.person_id = p.person_id;
''')
Output
name | salary -----+------- Omar | 8000 (1 row)
التمرين 2
اعمل weak entity اسمها room جوه building بـ key مركّبة (building_id, room_no) وجرّب تكرر نفس الغرفة.
Create a weak entity room inside building with composite key (building_id, room_no) and try to insert the same room twice.
(output appears here)
sql('''
CREATE TABLE building (building_id INTEGER PRIMARY KEY, name TEXT);
INSERT INTO building VALUES (1, 'Main');
CREATE TABLE room (building_id INTEGER REFERENCES building(building_id), room_no INTEGER, seats INTEGER, PRIMARY KEY (building_id, room_no));
INSERT INTO room VALUES (1, 101, 30);
INSERT INTO room VALUES (1, 101, 40);
SELECT * FROM room;
''')
Output
Error: UNIQUE constraint failed: room.building_id, room.room_no building_id | room_no | seats ------------+---------+------ 1 | 101 | 30 (1 row)
مشروع صغير في آخر المحاضرة
جامعة بـ specialization وweak entity
الداتا: University sample data: 4 people (2 students, 2 staff) and 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
الخطوات
- اعمل person (superclass) وstudent وstaff كـ subclasses بنفس الـ person_id.
- اعمل dependent كـ weak entity بـ key (staff_id, name).
- ضيف الداتا اللي في الـ starter.
- اعرض اسم كل موظف مع راتبه وعدد الـ dependents بتاعه. المتوقع: Lina 9000 و 1، وOmar 8000 و 2.
Expected output (press Run to produce your own): name | salary | dependents -----+--------+----------- Lina | 9000 | 1 Omar | 8000 | 2 (2 rows)
مشروع نهاية الفصل
ملف المشروع العملي للفصل 2
ملف 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 ER and EER modeling.