فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
رسوم متحركة
أمثلة SQL
أمثلة قصيرة وبسيطة
الكود قصير والنتيجة اتجرّبت فعلاً. عدّل في المحرر واضغط شغّل.
المثال ١: الـ anomaly بتحصل فعلاً
Sample output (press Run to produce your own): dept_id | dept_name | rows_n --------+------------------------+------- 10 | IT | 2 10 | Information Technology | 1 20 | Math | 1 (3 rows)
المثال ٢: نفحص dependency على الداتا
Sample output (press Run to produce your own): dept_id | names --------+------ 10 | 1 20 | 1 (2 rows) emp | depts ------+------ Karim | 1 Mona | 1 Nour | 1 Omar | 1 (4 rows)
المثال ٣: نقسم الجدول
Sample output (press Run to produce your own): dept_id | dept_name --------+---------- 10 | IT 20 | Math (2 rows) emp | dept_name ------+---------- Karim | IT Mona | IT Nour | Math Omar | IT (4 rows)
اختبار قصير
اختبر فهمك
1. X -> Y means:
2. An update anomaly happens when:
3. If X -> Y and Y -> Z then:
4. If the closure X+ contains all attributes, X is a:
5. A functional dependency is:
تمارين برمجة
جرّب بإيدك
التمرين 1
في جدول orders(order_id, customer_id, customer_name) افحص هل customer_id -> customer_name متحققة.
In orders(order_id, customer_id, customer_name) check whether customer_id -> customer_name holds.
(output appears here)
sql('''
CREATE TABLE orders (order_id INTEGER, customer_id INTEGER, customer_name TEXT);
INSERT INTO orders VALUES (1,7,'Mona'), (2,7,'Mona'), (3,8,'Omar');
SELECT customer_id, COUNT(DISTINCT customer_name) AS names FROM orders GROUP BY customer_id;
''')
Output
customer_id | names ------------+------ 7 | 1 8 | 1 (2 rows)
التمرين 2
قسّم orders لجدولين: customer وـ orders_small بدون الاسم.
Split orders into customer and a smaller orders table without the name.
(output appears here)
sql('''
CREATE TABLE orders (order_id INTEGER, customer_id INTEGER, customer_name TEXT);
INSERT INTO orders VALUES (1,7,'Mona'), (2,7,'Mona'), (3,8,'Omar');
CREATE TABLE customer AS SELECT DISTINCT customer_id, customer_name FROM orders;
CREATE TABLE orders_small AS SELECT order_id, customer_id FROM orders;
SELECT * FROM customer ORDER BY customer_id;
''')
Output
customer_id | customer_name ------------+-------------- 7 | Mona 8 | Omar (2 rows)
مشروع صغير في آخر المحاضرة
اكتشف الـ dependencies في جدول حقيقي
الداتا: Course registrations sample data: 6 rows. Written for this course (our own data, free to reuse).
License / source: Our own sample data; SQLite is in the public domain. Dataset page
الخطوات
- الجدول reg(student, course, teacher, room) جاهز. القاعدة: كل course ليه teacher واحد.
- افحص: هل course -> teacher متحققة؟ المتوقع: كل course ليه اسم واحد.
- افحص: هل teacher -> room متحققة؟ المتوقع: لأ، Dina ليها غرفتين.
- قسّم الجدول لـ course_teacher وـ registration واعرض الأول. المتوقع: 3 صفوف.
Expected output (press Run to produce your own): course | teachers -------+--------- DB1 | 1 ML1 | 1 OS1 | 1 (3 rows) teacher | rooms --------+------ Dina | 2 Sami | 1 (2 rows) course | teacher -------+-------- DB1 | Dina ML1 | Sami OS1 | Dina (3 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 functional dependencies and normalization.