فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
رسوم متحركة
أمثلة SQL
أمثلة قصيرة وبسيطة
الكود قصير والنتيجة اتجرّبت فعلاً. عدّل في المحرر واضغط شغّل.
المثال ١: Select وProject
Sample output (press Run to produce your own): name | year -----+----- Mona | 2 Nour | 3 (2 rows)
المثال ٢: عمليات المجموعات
Sample output (press Run to produce your own): x - 1 2 3 4 (4 rows) x - 2 3 (2 rows) x - 1 (1 row)
المثال ٣: Join
Sample output (press Run to produce your own): name | label -----+------ Mona | IT Omar | Sales Nour | IT (3 rows)
اختبار قصير
اختبر فهمك
1. Which operation keeps only the rows that satisfy a condition?
2. Project (pi) keeps:
3. Which SQL keyword is difference?
4. A join is a Cartesian product followed by:
5. Why can relational algebra operations be chained?
تمارين برمجة
جرّب بإيدك
التمرين 1
من جدول الطلاب اعرض أسماء طلاب الـ Math بس.
From the student table show only the names of Math students.
(output appears here)
sql('''
CREATE TABLE student (name TEXT, program TEXT, year INTEGER);
INSERT INTO student VALUES ('Mona','CS',2), ('Omar','CS',1), ('Nour','Math',3), ('Karim','Math',1);
SELECT name FROM student WHERE program = 'Math';
''')
Output
name ----- Nour Karim (2 rows)
التمرين 2
اعرض الأرقام اللي في a وليست في b باستخدام EXCEPT.
Show the values that are in a but not in b using EXCEPT.
(output appears here)
sql('''
CREATE TABLE a (x INTEGER); CREATE TABLE b (x INTEGER);
INSERT INTO a VALUES (1), (2), (3), (4);
INSERT INTO b VALUES (3), (4), (5);
SELECT x FROM a EXCEPT SELECT x FROM b;
''')
Output
x - 1 2 (2 rows)
مشروع صغير في آخر المحاضرة
أسئلة بالجبر العلاقي على داتا الجامعة
الداتا: University sample data: 5 students, 3 courses and 6 enrollments. Written for this course (our own data, free to reuse).
License / source: Our own sample data; SQLite is in the public domain. Dataset page
الخطوات
- الداتا جاهزة في الـ starter: student و course و enrolled.
- سؤال ١ (select + project): أسماء طلاب السنة الأولى.
- سؤال ٢ (join): اسم الطالب وعنوان المادة لكل تسجيل.
- سؤال ٣ (difference): الطلاب اللي مسجلوش في أي مادة. المتوقع: Karim بس.
Expected output (press Run to produce your own): name ---- Mona Nour (2 rows) name | title ------+------------------ Mona | Databases Mona | Machine Learning Omar | Databases Nour | Operating Systems Salma | Databases Salma | Operating Systems (6 rows) name ----- Karim (1 row)
مشروع نهاية الفصل
ملف المشروع العملي للفصل 3
ملف 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 chapter on the relational model.