Computer Science

SQL Transaction

A SQL transaction is a sequence of database operations that are treated as a single unit of work. It ensures that all operations are completed successfully or none of them are. Transactions are used to maintain data integrity and consistency in a database.

Written by Perlego with AI-assistance

12 Key excerpts on "SQL Transaction"

  • Book cover image for: Database
    eBook - PDF

    Database

    Principles Programming Performance

    Update Transactions In Section 4.4 we introduced the concept of a transaction. We now wish to review the ideas introduced there, starting with a definition. DEFINITION 9.1 A transaction is a means by which an application pro-grammer can package together a sequence of database operations, so that the database system can provide a number of guarantees, known as the ACID properties of a transaction (explained later in this section). When the operations making up a transaction consist of both reads (that is, Selects) and updates, they represent an attempt by the application pro-grammer at a consistent change of state in the data; when they consist only of reads, they represent an attempt at a consistent view of the data. ■ There is no Begin Transaction statement in standard SQL; a trans-action begins whenever there is no active transaction in progress and an SQL statement is performed that accesses the data (for example, Select, Update, Insert, Delete, etc.). While a transaction is in progress, any updates it makes are not visible to concurrent users, and data read cannot be con-currently updated. Recall that in standard SQL there are two statements bearing on transactional execution. The first is the Commit statement: exec sql commit work; Chapter 9 UPDATE TRANSACTIONS 646 The programmer uses this statement to inform the system that the ongoing transaction has successfully completed; all updates made by the transaction become permanent in the database and visible to concurrent users. The second statement that bears on transactional execution is the Roll-back statement: exec sql rol1 back work; This statement indicates that the ongoing transaction has ended unsuccess-fully; all data updates made by the transaction are reversed, and prior ver-sions of the data are put back in place and become visible again to concurrent users. A rollback action that ends a transaction, whether initi-ated by the program or the system, is often referred to as an abort.
  • Book cover image for: Learning PostgreSQL 11
    No longer available |Learn more

    Learning PostgreSQL 11

    A beginner's guide to building high-performance PostgreSQL database solutions, 3rd Edition

    • Salahaldin Juba, Andrey Volkov(Authors)
    • 2019(Publication Date)
    • Packt Publishing
      (Publisher)
    This is referred to as ACID compliance. A transaction is a logical execution unit and is indivisible, which means all-or-none, also known as atomicity ; this statement holds true regardless of the reason for the failure. For example, a transaction might fail due to an error such as a mathematical error, the misspelling of a relation name, or even an operating system crash. After the transaction is successfully committed, the transaction effect must persist even against hardware crashes; this property is referred to as durability. Furthermore, in a multi-user environment, several users can execute several transactions, and each transaction contains a set of actions or operations. Each transaction should be executed without interference from concurrently running transactions; this property is called isolation. Finally, the consistency property is not a property of a transaction itself, but rather a desirable effect of transaction isolation and atomicity. Database consistency is concerned with business requirements, which are defined via rules, including triggers, constraints, and any combination thereof. If the database state is consistent immediately before executing a transaction, then the database state must also be consistent after executing the transaction. Database consistency is the responsibility of the developer who codes the transaction. ACID properties are not easily met. For example, durability has heavy requirements because there are a lot of factors that might cause data loss, such as an operating system crash, power outage, or hard disk failure. Also, computer architecture is quite complex
  • Book cover image for: Introduction to Database and Knowledge-Base Systems
    • S Krishna(Author)
    • 1992(Publication Date)
    • WSPC
      (Publisher)
    We require that a transaction represent a correct program. That is, if the database state was correct before commencement of the transaction and the transaction executes in isolation and completes its operations, it will leave the system again in a correct state. During intermediate stages of a transaction, this correctness may not be maintained. We may therefore define a transaction as follows: Definition: A transaction is an execution of a series of operations which should be successfully completed in its entirety to preserve correctness of data in a database. For example, consider a transaction in a banking database which trans-fers a certain amount of money from one account, a savings account, to another, a current (or checking) account. The constraint here will be that the total sum in the two accounts should remain the same, unchanged by the transaction. The sequence of operations of the transaction TRANSFER could be: Transaction TRANSFER (Amount) Read Savings_balance Subtract Amount from Savings_balance Write Savings_balance Read Current_balance Add Amount to Current_balance Write Current_balance It can easily be seen that when the operations of the above transaction are carried out in sequence, at intermediate stages, for instance when opera-tions on the savings account are done but those on the current account have not yet been performed, the constraint stated earlier does not hold. We cannot therefore insist that the database should remain in a correct state after each basic operation but can insist on such a condition for a complete transaction. The system, then, should be able to easily recognize the commencement and end of transactions so as to coordinate its own synchronization, 8.2 Integrity Control 199 integrity check and related funcdons.
  • Book cover image for: NoSQL
    eBook - ePub

    NoSQL

    Database for Storage and Retrieval of Data in Cloud

    Transaction processing basically deals with processing information or data that are divided into individual, indivisible rather atomic operations called transactions. It is designed to maintain the integrity database or file systems in a consistent state, ensuring that mutually dependent operations on the system are either all completed successfully or all terminated successfully (Sheetlani Jitendra 2012). A transaction generally represents any change in database with two main purposes: (a) providing reliable units of work that allow correct recovery from failures and keeping a database consistent even in cases of system failure and (b) providing isolation between programs accessing a database concurrently.
    A database transaction, by definition, must be atomic, consistent, isolated, and durable. Database practitioners often refer to these properties of database transactions using the acronym ACID, and ACID properties are considered to be the essential properties of relational DBMS (RDBMS) to ensure successful transaction. (ACID properties for transaction are described in detail in the subsequent sections.)
    RDBMSs are compliant with the ACID, but NoSQL (Non-SQL/Not Only SQL) follows a different approach. It follows the CAP theorem laid down by Eric Brewers. This theorem states that it is mathematically impossible for an NoSQL DBMS to comply with all three (C, A, P) features listed down. Therefore, an NoSQL DBMS may only choose two of the features.
    The various approaches of how transactions access data in a distributed database in transaction processing are as follows: remote SQL (Structured Query Language) statements, distributed SQL statements, shared SQL for remote and distributed statements, remote transactions, and distributed transactions.
    1.2.3.1Distributed Transaction Processing in Relational Database
    A relational database is a digital database whose organization is based on the relational model of data, as proposed by E. F. Codd in 1970. The various software systems used to maintain relational databases are known as a RDBMS. Generally, all RDBMS uses SQL as the language for querying and preserving the database.
  • Book cover image for: SQL: 1999
    eBook - PDF

    SQL: 1999

    Understanding Relational Language Components

    • Jim Melton, Alan R. Simon(Authors)
    • 2001(Publication Date)
    • Morgan Kaufmann
      (Publisher)
    Transaction Management Introduction We first discussed the concept of transactions in Chapter 2, Getting Started with SQL:1999. In that chapter, we discovered what a transaction was and learned about some of the characteristics of transactions, such as the ACID properties: 9 Atomicity: The all-or-nothing illusion that all operations within a trans- action are performed, or none takes place 9 Consistency: The transaction concept that permits programmers to declare consistency points and validate the correctness of incorporated transfor- mations through application-supplied checks 9 Isolation: The regulation that concurrent transactions have no effect on one another (except in some implementations, where some of the concur- rent transactions may be blocked and therefore delayed or even forced to abort and restart) 9 Durability: The condition that all updates of a transactionmthat is, the new states of all objectsmmust be preserved, even in case of hardware or software failures In this chapter, we discuss how transactions are constructed and managed within SQL. We look at applicable statements, as well as at various phenomena 511 512 Chapter 15 Transaction Management and respective SQL: 1999 isolation levels under which your database transactions may be managed. SQL:1999 Transaction Syntax In SQL-92, there was no explicit statement that starts a transaction as there is in SQL: 1999 and in some database management systems. In SQL, you explicitly end a transaction by executing a COMMIT or ROLLBACK statement (SQL-89 required you to write COMMITWORK and ROLLBACKWORK,b u t SQL-92 made the keyword WORK optional). Transactions are started implicitly whenever you execute a statement that needs the context of a transaction and no transaction is active. For example, a SELECTs t a t e m e n t , an UPDATE statement, and a CREATE TABLE statement all require the context of a transaction.
  • Book cover image for: Wiley Pathways Introduction to Database Management
    • Mark L. Gillenson, Paulraj Ponniah, Alex Kriegel, Boris M. Trukhnov, Allen G. Taylor, Gavin Powell, Frank Miller(Authors)
    • 2012(Publication Date)
    • Wiley
      (Publisher)
    This stands for atomicity, consistency, isolation, and durability. Let’s look at each of these separately. ▲ Atomicity: either all of the statements in the transaction are executed, or none of the statements are executed. An atomic transaction represents a complete unit of work. ▲ Consistency: the transaction preserves database consistency. If a transac- tion executes successfully and performs its database updates properly, the database is transformed from one consistent state to another consistent state. ▲ Isolation: a transaction must appear as though it executed by itself with- out interference from any other concurrent transaction. In practice, how- ever, many concurrent database transactions execute simultaneously. ▲ Durability: changes made to the database must be permanent. Even if and when there are subsequent failures, the effects must persist in the database. One way this is ensured is through the transaction log and the recovery process after a failure, as discussed earlier. Factors relating to isolation deserve a closer look. To ensure complete isola- tion, the DBMS would not be able to process any other transactions while the current transaction is running. This could have disastrous effects on performance while transactions build up in a queue, waiting for other transactions to com- plete. For this reason, operations of one transaction are interleaved with opera- tions of other transactions. However, the database system must guarantee the effects of database updates for every pair of concurrent transactions to be as though one transaction executed from beginning to end and then the second started, or vice versa. Transactions must execute independently, and intermedi- ary or partial results of incomplete transactions must be transparent to other transactions. 10.1.4 Understanding Transaction Scope Transaction scope support and scope options are somewhat DBMS-specific.
  • Book cover image for: Understanding Databases
    eBook - PDF

    Understanding Databases

    Concepts and Practice

    • Suzanne W. Dietrich(Author)
    • 2021(Publication Date)
    • Wiley
      (Publisher)
    9 Transaction Management LEARNING OBJECTIVES • To describe the ACID properties of a transaction • To understand how databases recover from failures • To express how databases provide support for concurrent access • To explain how database recovery and concurrency control support the ACID properties of a transaction One of the fundamental features of a database system is the inherent support for the properties of a transaction in the presence of concurrent execution and system failures. A transaction is a unit of code that takes the database state from one consistent state to another. However, during the execution of the transaction, the database may be in a temporarily inconsistent state. What if there is a system failure at that point in time when the database is inconsistent? How does the con- current execution of transactions by the operating system of the computer affect the outcome of the transactions? This chapter describes how the recovery and concurrency control mechanisms of a relational database support transactions in the presence of system failures and concurrent execution so that the database remains in a consistent state. 9.1 ACID Properties of a Transaction The properties of a transaction that guarantee the correctness of the database for concurrent exe- cution and system failures are known as its ACID properties: Atomicity, Consistency, Isolation, Durability. • Atomicity: The individual operations of a transaction must be performed in their entirety or not at all. • Consistency: The concurrent execution of transactions maintains the correctness of the database. • Isolation: The transaction should not reveal its uncommitted results to other transactions. • Durability: The effects of a committed transaction must be preserved. Table 9.1 shows the pseudocode for sample transactions over an online banking scenario, including TransferFunds, ATMWithdrawal, Dashboard, GetBalance, DepositFunds.
  • Book cover image for: Essentials of Microservices Architecture
    eBook - ePub

    Essentials of Microservices Architecture

    Paradigms, Applications, and Techniques

    • Chellammal Surianarayanan, Gopinath Ganapathy, Raj Pethuru(Authors)
    • 2019(Publication Date)
    • Taylor & Francis
      (Publisher)
    Hence, in order to maintain the accuracy and logical correctness and consistency of the database, transactions should occur serially. Database management systems typically use a concurrency control mechanism to implement serialization. Each transaction is assigned a unique timestamp value when it starts. So, the database operations can be ordered according to the timestamp. Isolation —This property emphasizes that each transaction should occur in isolation. This means that when a transaction is in progress, the other transactions cannot see the intermediate results of the ongoing transaction. The locking mechanism is used to implement isolation. When a resource is accessed by a transaction, it is locked. If any other transaction needs to access the resource which is already locked, the transaction manager has to check the type of lock (read lock and write lock) and type of access (read or write) and accordingly it will take the decision to release the resource. Durability —Once a transaction completes successfully, the changes it has made into the database should be made permanent even if there is a system failure. The recovery-management component of database systems ensure the durability of the transaction. Transaction logs (which are flat files) can be used to rectify errors due to hardware failures. That is, a database management system reviews the transaction logs for any uncommitted transactions, and it rolls back uncommitted transactions. Database Transactions in a Single Server When the database is deployed in a single machine, i.e., with centralized databases, implementing a transaction is simple
  • Book cover image for: Beginning SQL
    eBook - PDF
    • Paul Wilton, John Colby(Authors)
    • 2005(Publication Date)
    • Wrox
      (Publisher)
    Transact-SQL Transact-SQL is an extension to the SQL syntax that some DBMSs use to allow a finer degree of control over processing transactions. In essence, whereas the ANSI standard assumes a BEGIN TRANSACTION before the first SQL statement and immediately after a COMMIT or ROLLBACK, Transact-SQL requires a BEGIN TRANSACTION statement to initiate a transaction. BEGIN TRANSACTION The BEGIN TRANSACTION statement tells the DBMS that the SQL statements that follow form a transac- tion group and must be processed as an atomic unit, either all processed or none processed. Unlike the ANSI transaction model, which assumes an automatic BEGIN TRANSACTION and must receive a COMMIT or a ROLLBACK statement at some point, Transact-SQL acts as if a COMMIT is performed after every SQL statement unless a BEGIN TRANSACTION is processed. This is a very different model with very different results from the SQL model. COMMIT TRANSACTION The COMMIT TRANSACTION statement is identical in function to COMMIT from the ANSI model. It causes all SQL statements since the opening BEGIN TRANSACTION to be written to the database as an atomic unit. Unlike the ANSI model, Transact-SQL does not automatically assume a new BEGIN TRANSACTION. Any SQL statements between the COMMIT TRANSACTION and the next BEGIN TRANSACTION are commit- ted individually as they occur. Figure 11-3 shows a series of SQL statements where the first two state- ments are outside of any transaction. These statements are committed immediately with no opportunity to undo or roll them back. Then a BEGIN TRANSACTION is explicitly executed telling the DBMS that everything that follows should be considered part of a transaction until either a ROLLBACK or a COMMIT is encountered. In this figure, a COMMIT is executed, and at that point, the INSERT and UPDATE state- ments are actually saved to the disk. 308 Chapter 11
  • Book cover image for: Professional Microsoft SQL Server 2008 Programming
    • Robert Vieira(Author)
    • 2010(Publication Date)
    • Wrox
      (Publisher)
    11 Transactions and Locks
    What to do…? What to do…? This I pondered when considering this chapter. Since I usually teach this topic even to so-called “beginners” (and I have coverage of it in Beginning SQL Server 2008 Programming ), I seriously debated removing this subject from the Professional title. The problem with that, however, is that, while fundamental in nature, transactions and locks are a fundamental that even lots of fairly advanced users don't quite “get.” You see, while nothing in this chapter is wildly difficult, transactions and locks tend to be two of the most misunderstood areas in the database world.
    This is one of those chapters that, when you go back to work, will make you sound like you've had your Wheaties today. As such, this “beginning” (or at least I think it's a basic) concept is going to make you start to look like a real pro.
    In this chapter, we're going to:
    • Examine transactions
    • Examine how the SQL Server log and “checkpoints” work
    • Unlock your understanding of locks
    Now, lest you think that I've suddenly decided to treat you like a rookie, rest assured, we will look a tad more in depth in several places than I necessarily do for beginning readers. Transactions
    Transactions are all about atomicity . Atomicity is the concept that something should act as a unit. From our database standpoint, it's about the smallest grouping of one or more statements that should be considered to be “all or nothing.”
    Often, when dealing with data, we want to make sure that if one thing happens, another thing happens, or that neither of them do. Indeed, this can be carried out to the degree where 20 things (or more) all have to happen together or nothing happens. Let's look at a classic example.
    Imagine that you are a banker. Sally comes in and wants to transfer $1,000 from checking to savings. You are, of course, happy to oblige, so you process her request.
  • Book cover image for: Transactional Memory, Second Edition
    • Tim Harris, James Larus, Ravi Rajwar(Authors)
    • 2022(Publication Date)
    • Springer
      (Publisher)
    Figure 2.2: Concurrency between transactions. 2.2.1 CORRECTNESS CRITERIA FOR DATABASE TRANSACTIONS Transactions for computing have their roots in database systems, and so it is natural to examine the extent to which the correctness criteria used for database transactions can be used to develop a sequential semantics for TM. To understand this better, let us revisit the three database properties of atomicity, consistency, and isolation: Atomicity (specifically, failure atomicity) requires that a transaction execute to completion or, in case of failure, to appear not to have executed at all. An aborted transaction should have no side effects. Consistency requires that a transaction transform the database from one consistent state to another consistent state. Consistency is a property of a specific data structure, application, or database. It cannot be specified independently of the semantics of a particular system. Enforcing consistency requires a programmer to specify data invariants, typically in the form of predicates. These are typically not specified with TM systems, although some exploration has been made [137]. Isolation requires that execution of a transaction not affect the result of concurrently executing transactions. What about the “D” durability from the full ACID model? One could argue that durability does not apply to TM because volatile memory is inherently not a durable resource, unlike storage on a disk. Conversely, one could argue that the effects of a TM transaction must remain durable so long as the process’s memory state is retained; that is, a correct TM should not allow a transaction’s effects to be discarded until the process terminates. This is a semantic question of exactly what one takes
  • Book cover image for: SQL Clearly Explained
    When a transaction is rolled back, the DBMS starts at the transaction's last record in the log file and replac- es each current value with its old value from the log file. The pro- cess repeats, moving forward in the log file, until the DBMS reaches the log record that indicates the start of the transaction. At this point, the log records can be purged. Starting Transactions The SQL-92 standard and the Core SQL:1999 specification do not specify any command for starting a transaction. Transactions there- fore start automatically whenever there is no current transaction and a user or application program issues a command that requires database interaction. The full SQL:1999 standard, however, does include a START TRANS- ACTION statement: START TRANSACTION mode 214 USERS, SESSIONS, AND TRANSACTION CONTROL The mode of a transaction can include its isolation level and whether it is read only or read/write. Ending Transactions If you are working with an interactive SQL command processor, you probably request the end of a transaction by issuing a Save changes command. If the DBMS can process your updates~if they do not violate any integrity constraints~then the DBMS automati- cally commits your transaction. If there are integrity violations, the the DBMS rolls back your transaction. In an application program, you typically make the decision whether to commit or roll back based on an error code returned by the DBMS. (You will read more about these error codes in Chapter 12.) Once the decision has been made, a program issues COMMIT or COMMIT WORK to commit the transaction. Conformance note: The keyword WORK following COMMIT is re- quired at the SQL-92 entry level. However, it is optional at the full and intermediate levels. The COMMr_T statement (with the optional WORKkeyword) is required for Core SQL:1999 con- formance. To undo everything done by a transaction, you issue either ROLLBACK or ROLLBACK WORK.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.