فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
رسوم متحركة
أمثلة SQL
أمثلة قصيرة وبسيطة
الكود قصير والنتيجة اتجرّبت فعلاً. عدّل في المحرر واضغط شغّل.
المثال ١: التحويل كـ procedure (MySQL) ونفس المنطق
Sample output (press Run to produce your own): id | owner | balance ---+-------+-------- 1 | Mona | 70 2 | Omar | 80 (2 rows)
المثال ٢: ROLLBACK لما حاجة تفشل
Sample output (press Run to produce your own): Error: CHECK constraint failed: balance >= 0 id | owner | balance ---+-------+-------- 1 | Mona | 100 2 | Omar | 50 (2 rows)
المثال ٣: Functions جاهزة
Sample output (press Run to produce your own): owner | balance | with_interest | letters ------+---------+---------------+-------- MONA | 100 | 105.0 | 4 OMAR | 50 | 52.5 | 4 (2 rows)
اختبار قصير
اختبر فهمك
1. A stored procedure is:
2. Does SQLite support stored procedures?
3. ROLLBACK does what?
4. What is the difference between a function and a procedure?
5. Parameters in queries protect against:
تمارين برمجة
جرّب بإيدك
التمرين 1
حوّل 20 من حساب 1 لحساب 2 داخل transaction وبعدين COMMIT.
Transfer 20 from account 1 to account 2 inside a transaction and COMMIT.
(output appears here)
sql('''
CREATE TABLE account (id INTEGER PRIMARY KEY, owner TEXT, balance INTEGER CHECK (balance >= 0));
INSERT INTO account VALUES (1,'Mona',100), (2,'Omar',50);
BEGIN;
UPDATE account SET balance = balance - 20 WHERE id = 1;
UPDATE account SET balance = balance + 20 WHERE id = 2;
COMMIT;
SELECT * FROM account ORDER BY id;
''')
Output
id | owner | balance ---+-------+-------- 1 | Mona | 80 2 | Omar | 70 (2 rows)
التمرين 2
جرّب تسحب 500 من حساب 1 (الرصيد 100). شوف إيه اللي بيحصل بسبب الـ CHECK.
Try to take 500 from account 1 (balance 100). See what the CHECK does.
(output appears here)
sql('''
CREATE TABLE account (id INTEGER PRIMARY KEY, owner TEXT, balance INTEGER CHECK (balance >= 0));
INSERT INTO account VALUES (1,'Mona',100), (2,'Omar',50);
UPDATE account SET balance = balance - 500 WHERE id = 1;
SELECT * FROM account ORDER BY id;
''')
Output
Error: CHECK constraint failed: balance >= 0 id | owner | balance ---+-------+-------- 1 | Mona | 100 2 | Omar | 50 (2 rows)
مشروع صغير في آخر المحاضرة
تحويل بنكي آمن بـ transaction
الداتا: Bank sample data: 3 accounts. 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 (CHECK على الرصيد).
- نفّذ تحويل 40 من 1 لـ 2 داخل transaction. المتوقع: 60 و 90 و 10.
- جرّب تحويل 100 من 3 لـ 1 داخل transaction: الخصم هيفشل بسبب الـ CHECK. اعمل ROLLBACK.
- اعرض الأرصدة. المتوقع إن الإضافة لحساب 1 اتلغت: 60 و 90 و 10.
Expected output (press Run to produce your own): Error: CHECK constraint failed: balance >= 0 id | balance ---+-------- 1 | 60 2 | 90 3 | 10 (3 rows)
مشروع نهاية الفصل
ملف المشروع العملي للفصل 6
ملف 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 sections on triggers and stored procedures.