SQL > Advanced SQL > Commit

SQL COMMIT is a command used in database management systems to permanently save changes made to a transaction. In other words, the COMMIT statement is used to make sure that the changes made to a database are saved and that they are permanent.

Syntax

The syntax for SQL COMMIT is

BEGIN TRANSACTION;
[SQL Statements];
COMMIT;

The COMMIT command is usually used in conjunction with the BEGIN TRANSACTION statement, which initiates a transaction. Once a transaction has been initiated using BEGIN TRANSACTION, all changes made to the database during that transaction are temporary and will not be saved until the COMMIT command is issued. Note that there could be one or more SQL statements between BEGIN TRANSACTION and COMMIT.

Example

Let's say we have the following table,

Table Account_Balance

 Name  Account_ID  Balance 
 John  101 3500
 Jennifer  120 650
 Stella  130 4500

We issue the following SQL:

BEGIN TRANSACTION;

UPDATE Account_Balance SET Balanace = 500 WHERE Account_ID = 101;
UPDATE Account_Balance SET Balanace = 550 WHERE Account_ID = 120;

COMMIT;

After executing the above SQL, the table now becomes,

Table Account_Balance

 Name  Account_ID  Balance 
 John  101 500
 Jennifer  120 550
 Stella  130 4500

Notice that if you do not include the COMMIT; at the end and your connection is closed, the content of you table would not have changed. In other words, John's balance will remain at 3,500 and Jennifer's balance will remain at 650, even though the two UPDATE statements have been run.

Next: SQL AUTO INCREMENT

This page was last updated on October 12, 2024.




Copyright © 2024   1keydata.com   All Rights Reserved     Privacy Policy     About   Contact