Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Challenge: Transaction Creation | Section
Optimización de SQL y Características de Consulta
Sección 1. Capítulo 3
single

single

bookChallenge: Transaction Creation

Desliza para mostrar el menú

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.

Tarea

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.

Solución

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 3
single

single

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

some-alt