كل المقررات

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

الفصل 9 (الجزء 1): Introduction to Transactions

What a transaction is and the ACID properties

على طول: الـ transaction هو مجموعة عمليات تتنفذ كلها أو ولا واحدة. وإيه هي خصائص ACID.

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

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

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

رسوم متحركة

حالات الـ transaction

أمثلة SQL

أمثلة قصيرة وبسيطة

الكود قصير والنتيجة اتجرّبت فعلاً. عدّل في المحرر واضغط شغّل.

المثال ١: COMMIT

Sample output (press Run to produce your own):
id | owner | balance
---+-------+--------
1  | Mona  | 70     
2  | Omar  | 80     
(2 rows)

المثال ٢: ROLLBACK (atomicity)

Sample output (press Run to produce your own):
id | owner | balance
---+-------+--------
1  | Mona  | 100    
2  | Omar  | 50     
(2 rows)

المثال ٣: القيد بيحمي الـ consistency

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)

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

اختبار قصير

اختبر فهمك

1. Atomicity means:

2. Durability means a committed change:

3. Isolation is about:

4. After an abort the database is:

5. The log is written:

تمارين برمجة

جرّب بإيدك

التمرين 1

حوّل 10 من حساب 2 لحساب 1 بـ transaction و COMMIT.

Transfer 10 from account 2 to account 1 with a transaction and COMMIT.

(output appears here)

التمرين 2

ابدأ transaction، امسح كل الحسابات، وبعدين ROLLBACK. اعرض الجدول بعدها.

Start a transaction, delete all accounts, then ROLLBACK. Show the table after.

(output appears here)

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

حجز مقاعد: كله أو ولا حاجة

الداتا: Cinema sample data: 4 seats and 2 customers. 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. الجداول seat وـ wallet جاهزة. الحجز = تعليم المقعد محجوز + خصم 50 من المحفظة.
  2. نفّذ حجز المقعد 1 لـ Mona (رصيدها 120) بـ transaction. المتوقع: المقعد محجوز والرصيد 70.
  3. جرّب حجز مقعد 2 لـ Omar (رصيده 20): الخصم هيفشل. اعمل ROLLBACK.
  4. اعرض المقاعد. المتوقع إن المقعد 2 لسه متاح.
Expected output (press Run to produce your own):
Error: CHECK constraint failed: balance >= 0
no | holder
---+-------
1  | Mona  
2  | NULL  
3  | NULL  
4  | NULL  
(4 rows)
owner | balance
------+--------
Mona  | 70     
Omar  | 20     
(2 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 transaction processing.