كل المقررات

الرئيسية / مقرر قاعدة البيانات / الفصل 6 الجزء 2

الفصل 6 (الجزء 2): Triggers and Stored Procedures

Stored procedures, functions and transactions

على طول: الـ stored procedure كود متخزن جوه الـ database وبتندهه بالاسم. وهنشوف شكله وبديله في SQLite.

فيديو المحاضرة

فيديو المحاضرة

بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.

رسوم متحركة

تنفيذ جزء كامل مع بعض

أمثلة 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)

افتح الموضوع المقابل في صفحة LiU TDDD12

اختبار قصير

اختبر فهمك

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)

التمرين 2

جرّب تسحب 500 من حساب 1 (الرصيد 100). شوف إيه اللي بيحصل بسبب الـ CHECK.

Try to take 500 from account 1 (balance 100). See what the CHECK does.

(output appears here)

مشروع صغير في آخر المحاضرة

تحويل بنكي آمن بـ 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

الخطوات

  1. الحسابات جاهزة في الـ starter (CHECK على الرصيد).
  2. نفّذ تحويل 40 من 1 لـ 2 داخل transaction. المتوقع: 60 و 90 و 10.
  3. جرّب تحويل 100 من 3 لـ 1 داخل transaction: الخصم هيفشل بسبب الـ CHECK. اعمل ROLLBACK.
  4. اعرض الأرصدة. المتوقع إن الإضافة لحساب 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 فيه مشروع صغير بأسئلة عملية تطبّق بيها كل اللي اتعلمته في الفصل، مع حل نموذجي في الآخر.

افتح ملف المشروع (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.