فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
رسوم متحركة
أمثلة SQL
أمثلة قصيرة وبسيطة
الكود قصير والنتيجة اتجرّبت فعلاً. عدّل في المحرر واضغط شغّل.
المثال ١: من 1NF
Sample output (press Run to produce your own): student | phone --------+------ Mona | 0101 Mona | 0122 (2 rows)
المثال ٢: 2NF
Sample output (press Run to produce your own): course | course_title -------+------------- DB1 | Databases (1 row) student | course | grade --------+--------+------ Mona | DB1 | A Omar | DB1 | B (2 rows)
المثال ٣: 3NF
Sample output (press Run to produce your own): rows_back --------- 4 (1 row)
اختبار قصير
اختبر فهمك
1. 1NF requires:
2. 2NF is about:
3. emp -> dept_id -> dept_name is a:
4. In BCNF every determinant X must be a:
5. A lossless split means:
تمارين برمجة
جرّب بإيدك
التمرين 1
جدول sale(sale_id, product, product_price) فيه تكرار للسعر. قسّمه لـ product وـ sale_line.
Table sale(sale_id, product, product_price) repeats the price. Split it into product and sale_line.
(output appears here)
sql('''
CREATE TABLE sale (sale_id INTEGER, product TEXT, product_price INTEGER);
INSERT INTO sale VALUES (1,'Pen',10), (2,'Pen',10), (3,'Book',30);
CREATE TABLE product AS SELECT DISTINCT product, product_price FROM sale;
CREATE TABLE sale_line AS SELECT sale_id, product FROM sale;
SELECT * FROM product ORDER BY product;
''')
Output
product | product_price --------+-------------- Book | 30 Pen | 10 (2 rows)
التمرين 2
اتأكد إن التقسيم lossless: اعمل join وعدّ الصفوف.
Check that the split is lossless: join and count the rows.
(output appears here)
sql('''
CREATE TABLE sale (sale_id INTEGER, product TEXT, product_price INTEGER);
INSERT INTO sale VALUES (1,'Pen',10), (2,'Pen',10), (3,'Book',30);
CREATE TABLE product AS SELECT DISTINCT product, product_price FROM sale;
CREATE TABLE sale_line AS SELECT sale_id, product FROM sale;
SELECT COUNT(*) AS rows_back FROM sale_line l JOIN product p ON p.product = l.product;
''')
Output
rows_back --------- 3 (1 row)
مشروع صغير في آخر المحاضرة
نضّف جدول طلبات بعد 3NF
الداتا: Orders sample data: 6 rows with repeated customer and product info. Written for this course (our own data, free to reuse).
License / source: Our own sample data; SQLite is in the public domain. Dataset page
الخطوات
- الجدول flat جاهز. القواعد: customer_id -> customer_name و product_id -> product_title.
- اعمل جدول customer وجدول product وجدول order_line (order_id, customer_id, product_id, qty).
- اتأكد إن الـ join بيرجّع 6 صفوف.
- اعرض اسم كل عميل وعدد الأوردرات. المتوقع: Mona 3 و Omar 2 و Nour 1.
Expected output (press Run to produce your own): rows_back --------- 6 (1 row) customer_name | orders --------------+------- Omar | 2 Nour | 1 Mona | 3 (3 rows)
مشروع نهاية الفصل
ملف المشروع العملي للفصل 7
ملف 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 functional dependencies and normalization.