Programming with Python

  1. Home
  2. Docs
  3. Programming with Python
  4. Database in Python
  5. Transactions

Transactions

To give instructions to database we use SQL or Structured Query language.

The primary purpose is to query and manipulate data stored in a relational database. Statement of SQL is classified into 5 categories:-

  • Data definition language
  • Data retrieval
  • Data manipulation language
  • Transaction control
  • Data control

The purpose of transaction control is managing changes made by DML (Data Manipulating language) statements.

  • Atomicity- Either the transaction completes, or nothing happens. If a transaction contains 4 queries then all these queries must be executed, or none of them must be executed.
  • Consistency- The database must be consistent before the transaction starts and the database must also be consistent after the transaction is completed.
  • Isolation- Intermediate results of a transaction are not visible outside the current transaction.
  • Durability- Once a transaction was committed, the effects are persistent, even after a system failure.

These properties are popularly known as ACID properties.

There are some functions also which helps in transaction control:-

  • commit()- Exactly commits pending transactions to the database.
connection.commit()
  • rollback()- Causes a transaction to be rolled back to the starting.
connection.rollback()
  • close()- Closes the connection to the database permanently.
connection.close()

This is all about transaction control in python.

Was this article helpful to you? Yes No

How can we help?