فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
رسوم متحركة
أمثلة 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)
اختبار قصير
اختبر فهمك
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)
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 - 10 WHERE id = 2;
UPDATE account SET balance = balance + 10 WHERE id = 1;
COMMIT;
SELECT * FROM account ORDER BY id;
''')
Output
id | owner | balance ---+-------+-------- 1 | Mona | 110 2 | Omar | 40 (2 rows)
التمرين 2
ابدأ transaction، امسح كل الحسابات، وبعدين ROLLBACK. اعرض الجدول بعدها.
Start a transaction, delete all accounts, then ROLLBACK. Show the table after.
(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;
DELETE FROM account;
ROLLBACK;
SELECT COUNT(*) AS accounts FROM account;
''')
Output
accounts -------- 2 (1 row)
مشروع صغير في آخر المحاضرة
حجز مقاعد: كله أو ولا حاجة
الداتا: 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
الخطوات
- الجداول seat وـ wallet جاهزة. الحجز = تعليم المقعد محجوز + خصم 50 من المحفظة.
- نفّذ حجز المقعد 1 لـ Mona (رصيدها 120) بـ transaction. المتوقع: المقعد محجوز والرصيد 70.
- جرّب حجز مقعد 2 لـ Omar (رصيده 20): الخصم هيفشل. اعمل ROLLBACK.
- اعرض المقاعد. المتوقع إن المقعد 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.