首页 > 编程知识 正文

事务的提交和回滚的含义,事务长时间提交和回滚

时间:2023-05-05 21:48:16 阅读:239972 作者:4532

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 IDCUSTOMER NAMESTATECOUNTRY1AkashDelhiIndia2AmitHyderabadIndia3JasonCaliforniaUSA4JohnTexasUSA 顾客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 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 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 IDCUSTOMER NAMESTATECOUNTRY1AkashDelhiIndia2AmitHyderabadIndia3JasonCaliforniaUSA 顾客ID 顾客姓名 州 国家 1个 阿卡什 新德里 印度 2 阿米特 海得拉巴 印度 3 杰森 加利福尼亚州 美国

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 IDCUSTOMER NAMESTATECOUNTRY1AkashDelhiIndia2AmitHyderabadIndia3JasonCaliforniaUSA4JohnTexasUSA 顾客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 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 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 IDCUSTOMER NAMESTATECOUNTRY1AkashDelhiIndia2AmitHyderabadIndia3JasonCaliforniaUSA4JohnTexasUSA 顾客ID 顾客姓名 州 国家 1个 阿卡什 新德里 印度 2 阿米特 海得拉巴 印度 3 杰森 加利福尼亚州 美国 4 约翰 德州 美国

Table after executing Rollback

执行回滚后的表

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

sql 事务提交和回滚

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。