天天看點

sql 事務送出和復原_SQL送出和復原

sql 事務送出和復原

The most important aspect of a database is the ability to store data and the ability to manipulate data. COMMIT and ROLLBACK are two such keywords which are used in order store and revert the process of data storage. These keywords are usually used in context with a transaction.

資料庫最重要的方面是存儲資料的能力和處理資料的能力。 COMMIT和ROLLBACK是兩個這樣的關鍵字,它們用于順序存儲和還原資料存儲過程。 這些關鍵字通常在事務中使用。

Let’s try to understand the details about COMMIT and ROLLBACK.

讓我們嘗試了解有關COMMIT和ROLLBACK的詳細資訊。

SQL送出和復原 (SQL Commit and Rollback)

COMMIT and ROLLBACK are performed on transactions. A transaction is the smallest unit of work that is performed against a database. Its a sequence of instructions in a logical order. A transaction can be performed manually by a programmer or it can be triggered using an automated program.

COMMIT和ROLLBACK在事務上執行。 事務是針對資料庫執行的最小工作單元。 它是邏輯順序的指令序列。 交易可以由程式員手動執行,也可以使用自動化程式觸發。

SQL送出 (SQL Commit)

COMMIT is the SQL command that is used for storing changes performed by a transaction. When a COMMIT command is issued it saves all the changes since last COMMIT or ROLLBACK.

COMMIT是用于存儲事務執行的更改SQL指令。 發出COMMIT指令後,它将儲存自上一次COMMIT或ROLLBACK以來的所有更改。

SQL送出的文法 (Syntax for SQL Commit)

COMMIT;
           

The syntax for commit includes just one keyword COMMIT.

commit的文法僅包含一個關鍵字COMMIT 。

SQL送出示例 (SQL Commit Example)

Let us consider the following table for understanding Commit in a better way.

讓我們考慮下表,以更好地了解Commit。

Customer:-

顧客:-

CUSTOMER ID CUSTOMER NAME STATE COUNTRY
1 Akash Delhi India
2 Amit Hyderabad India
3 Jason California USA
4 John Texas USA
顧客ID 顧客姓名 國家
1個 阿卡什 新德裡 印度
2 阿米特 海得拉巴 印度
3 傑森 加利福尼亞州 美國
4 約翰 德州 美國

Now let us delete one row from the above table where State is “Texas”.

現在,讓我們從上表中删除狀态為“ Texas”的一行。

DELETE from Customer where State = 'Texas';
           
sql 事務送出和復原_SQL送出和復原

SQL Delete without Commit

SQL删除沒有送出

Post the DELETE command if we will not publish COMMIT, and if the session is closed then the change that is made due to the DELETE command will be lost.

如果我們不釋出COMMIT,則釋出DELETE指令,并且如果會話關閉,則由于DELETE指令所做的更改将丢失。

Updated Command with COMMIT

使用COMMIT更新指令

DELETE from Customer where State = 'Texas';
COMMIT;
           
sql 事務送出和復原_SQL送出和復原

SQL Commit Execution

SQL送出執行

Using the above-mentioned command sequence will ensure that the change post DELETE command will be saved successfully.

使用上述指令序列将確定change post DELETE指令将成功儲存。

送出後輸出 (Output After Commit)

CUSTOMER ID CUSTOMER NAME STATE COUNTRY
1 Akash Delhi India
2 Amit Hyderabad India
3 Jason California USA
顧客ID 顧客姓名 國家
1個 阿卡什 新德裡 印度
2 阿米特 海得拉巴 印度
3 傑森 加利福尼亞州 美國
sql 事務送出和復原_SQL送出和復原

Table After SQL Commit

SQL送出後的表

SQL復原 (SQL RollBack)

ROLLBACK is the SQL command that is used for reverting changes performed by a transaction. When a ROLLBACK command is issued it reverts all the changes since last COMMIT or ROLLBACK.

ROLLBACK是用于還原事務執行的更改SQL指令。 發出ROLLBACK指令時,它将還原自上次COMMIT或ROLLBACK之後的所有更改。

SQL復原的文法 (Syntax for SQL Rollback)

ROLLBACK;
           

The syntax for rollback includes just one keyword ROLLBACK.

復原的文法僅包含一個關鍵字ROLLBACK 。

SQL復原示例 (SQL Rollback Example)

Let us consider the following table for understanding Rollback in a better way.

讓我們考慮下表,以更好地了解復原。

Customer:-

顧客:-

CUSTOMER ID CUSTOMER NAME STATE COUNTRY
1 Akash Delhi India
2 Amit Hyderabad India
3 Jason California USA
4 John Texas USA
顧客ID 顧客姓名 國家
1個 阿卡什 新德裡 印度
2 阿米特 海得拉巴 印度
3 傑森 加利福尼亞州 美國
4 約翰 德州 美國

Now let us delete one row from the above table where State is “Texas”.

現在,讓我們從上表中删除狀态為“ Texas”的一行。

DELETE from Customer where State = 'Texas';
           
sql 事務送出和復原_SQL送出和復原

SQL Delete without Rollback

SQL删除而無需復原

Post the DELETE command if we publish ROLLBACK it will revert the change that is performed due to the delete command.

如果釋出ROLLBACK,則釋出DELETE指令,它将恢複由于delete指令而執行的更改。

Updated Command with ROLLBACK

使用ROLLBACK更新了指令

DELETE from Customer where State = 'Texas';
ROLLBACK;
           
sql 事務送出和復原_SQL送出和復原

SQL Delete with Rollback

帶復原SQL删除

Using the above-mentioned command sequence will ensure that the change post DELETE command will be reverted successfully.

使用上述指令序列将確定change post DELETE指令将被成功還原。

復原後輸出 (Output After Rollback)

CUSTOMER ID CUSTOMER NAME STATE COUNTRY
1 Akash Delhi India
2 Amit Hyderabad India
3 Jason California USA
4 John Texas USA
顧客ID 顧客姓名 國家
1個 阿卡什 新德裡 印度
2 阿米特 海得拉巴 印度
3 傑森 加利福尼亞州 美國
4 約翰 德州 美國
sql 事務送出和復原_SQL送出和復原

Table after executing Rollback

執行復原後的表

翻譯自: https://www.journaldev.com/23932/sql-commit-sql-rollback

sql 事務送出和復原