Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: Transaction Creation | Section
Practice
Projects
Quizzes & Challenges
Quizze
Challenges
/
SQL-Optimierung und Abfragefunktionen
Abschnitt 1. Kapitel 3
single

single

bookChallenge: Transaction Creation

Swipe um das Menü anzuzeigen

When transferring funds between two accounts in a banking system, you must ensure that both the deduction from the source account and the addition to the destination account happen together as a single, indivisible operation. This is where the concept of a transaction comes in: a transaction groups multiple SQL statements so that they either all succeed or none take effect, maintaining atomicity.

To transfer funds, you will work with the bank_accounts table, which contains columns for account_id, owner_name, and balance. The transaction should:

  • Start with a BEGIN statement to initiate the transaction;
  • Check that the source account has enough funds for the transfer;
  • If there are sufficient funds, deduct the amount from the source account and add it to the destination account;
  • If the source account lacks sufficient funds, the transaction should be rolled back, leaving all balances unchanged;
  • Commit the transaction only if all steps succeed.

This approach ensures that no partial updates occur, and the system remains reliable even in the event of errors.

Aufgabe

Swipe to start coding

Write a SQL transaction that transfers a specified amount from one account to another in the bank_accounts table. The transaction must be atomic and should roll back if the source account does not have enough funds.

  • Retrieve the balance of the source account and ensure it is greater than or equal to the transfer amount.
  • If sufficient, deduct the transfer amount from the source account.
  • Add the transfer amount to the destination account.
  • Record the transfer in the transfers table.
  • If the source account lacks sufficient funds, roll back the transaction and raise an error.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 3
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

some-alt