![]() In the first session, start a new transaction: BEGIN Code language: SQL (Structured Query Language) ( sql )Īnd subtracting 1000USD from Bob’s account with id 1: UPDATE accounts We will use two sessions for viewing the change of each operation. In this demonstration, we will show you how to transfer 1000USD from Bob’s account to Alice’s account. commit the change (or roll it back later) COMMIT Code language: SQL (Structured Query Language) ( sql ) PostgreSQL COMMIT: Bank account transfer example insert a new row into the accounts table INSERT INTO accounts( name,balance) ![]() The following COMMIT statement inserts Alice’s account to the accounts table: COMMIT Code language: SQL (Structured Query Language) ( sql )įrom other sessions, you can view the change by querying the accounts table: SELECT id,Īccounts Code language: SQL (Structured Query Language) ( sql )Īfter executing the COMMIT statement, PostgreSQL also guarantees that the change will be durable if a crash happens. ![]() ![]() Or simply: COMMIT Code language: SQL (Structured Query Language) ( sql ) Or COMMIT TRANSACTION Code language: SQL (Structured Query Language) ( sql ) To make the change become visible to other sessions (or users) you need to commit the transaction by using the COMMIT WORK statement: COMMIT WORK Code language: SQL (Structured Query Language) ( sql ) SELECT id,Ĭode language: SQL (Structured Query Language) ( sql ) Commit a transaction However, if you start a new session and execute the query above, you will not see the change. Or just: BEGIN Code language: SQL (Structured Query Language) ( sql )įor example, the following statements start a new transaction and insert a new account into the accounts table: BEGIN Ĭode language: SQL (Structured Query Language) ( sql )įrom the current session, you can see the change by querying the accounts table: SELECT id, Or BEGIN WORK Code language: SQL (Structured Query Language) ( sql ) To start a transaction, you use the following statement: BEGIN TRANSACTION Code language: SQL (Structured Query Language) ( sql ) In this case, you do not know when the transaction begins and cannot intercept the modification such as rolling it back. PostgreSQL inserted a new row into the accounts table immediately. VALUES( 'Bob', 10000) Code language: SQL (Structured Query Language) ( sql ) When you execute the following INSERT statement: INSERT INTO accounts( name,balance) ) Code language: SQL (Structured Query Language) ( sql ) Begin a transaction Let’s create a new table named accounts for the demonstration: DROP TABLE IF EXISTS accounts
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |