المعاملات / Transactions

الترتيب / Order
2 من 4 / 2 of 4
آخر تحديث / Updated
8 أكتوبر 2026 / 8 October 2026

أهداف الدرس

  1. أن تجعل عدة تعديلات تنجح معاً أو تفشل معاً.

نقل طالب من شعبة إلى أخرى تعديلان: حذف من الأولى وإضافة إلى الثانية. لو نجح الأول وفشل الثاني لضاع الطالب.

SQL
BEGIN;

DELETE FROM enrolments WHERE student_id = 42 AND section_id = 1;
INSERT INTO enrolments (student_id, section_id) VALUES (42, 2);

COMMIT;   -- or ROLLBACK; to undo both
الخاصيةمعناها
الذرّيةكل شيء أو لا شيء
الاتساقالقواعد تبقى صحيحة
العزلالمعاملات لا تتداخل
الديمومةما حُفظ لا يضيع

تمرين

ماذا يحدث إن انقطع الاتصال قبل COMMIT؟

الإجابة

تُلغى التعديلات كلها، وتبقى البيانات كما كانت.

Lesson goals

  1. Make several changes succeed together or fail together.

Moving a student between sections is two changes: remove from one, add to the other. If the first works and the second fails, the student is lost.

SQL
BEGIN;

DELETE FROM enrolments WHERE student_id = 42 AND section_id = 1;
INSERT INTO enrolments (student_id, section_id) VALUES (42, 2);

COMMIT;   -- or ROLLBACK; to undo both
PropertyMeaning
AtomicityAll or nothing
ConsistencyThe rules stay true
IsolationTransactions do not interfere
DurabilityWhat is committed stays

Exercise

What happens if the connection drops before COMMIT?

Answer

Every change is undone, and the data stays as it was.

التعليقات / Comments

لا تعليقات بعد. كن أول من يسأل. / No comments yet. Be the first to ask.

تعليق الطالب يظهر فوراً، وتعليق الزائر بعد موافقة المدرّس. / A student's comment shows at once; a visitor's once the teacher approves it.

في «قواعد البيانات وSQL» / In “Databases and SQL”

  1. 1الاستعلام والربط / Queries and joinsفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  2. 2المعاملات / Transactionsفيها كود / has codeالتعليقات مفتوحة / comments open
  3. 3تصميم الجداول / Designing tablesفيها كود / has codeالتعليقات مفتوحة / comments open
  4. 4السرعة والفهارس / Speed and indexesفيها كود / has codeالتعليقات مفتوحة / comments open

↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق↑ ↓ to move · Enter to open · Esc to close