NAME
BEGIN - 开始一个事务块
SYNOPSIS
BEGIN [ WORK | TRANSACTION ]
DESCRIPTION 描述
BEGIN 初始化一个事务块, 也就是说所有 BEGIN 命令后的用户语句都将在一个事务里面执行直到给出一个明确的 COMMIT [commit(7)] 或者 ROLLBACK [rollback(l)] 。缺省时,(没有 BEGIN),PostgreSQL 以 "autocommit" 模式执行事务,也就是说,每个语句在其自身的事务中执行, 并且在语句结束的时候隐含地执行一个提交(如果执行成功,否则执行回滚)。
在事务块里执行的语句很明显地快得多, 因为事务开始/提交(start/commit)需要大量的CPU和磁盘活动。 在一个 事务内部执行多条语句对于修改若干个相关的表的时候也是很有用的: 在所有相关的更新完成之前,其它会话看不到中间的状态。
PARAMETERS 参数
- WORK
- TRANSACTION
可选关键字。没什么作用。
NOTES 注意
START TRANSACTION [start_transaction(7)] 有着和 BEGIN 一样的功能。
使用 COMMIT [commit(7)] 或 ROLLBACK [rollback(7)] 结束一个事务。
在一个现有事务块内部发出一个 BEGIN 将产生一个警告信息。 事务的状态将不会被影响。
EXAMPLES 例子
开始一个用户事务:
BEGIN;
COMPATIBILITY 兼容性
BEGIN 是 PostgreSQL 语言的扩展。 在 SQL 标准中没有明确的 BEGIN 的定义;事务初始化总是隐含的而且使用一个 COMMIT 或者 ROLLBACK 语句终止。
许多关系型数据库为了方便提供一个自动提交(autocommit)特性。
顺便说一句,BEGIN 关键字在嵌入 SQL 里用于不同的目的。 我们建议你在移植数据库应用时仔细检查事务的语义。
SEE ALSO 参见
COMMIT [commit(7)], ROLLBACK [rollback(l)]
#p#
NAME
BEGIN - start a transaction block
SYNOPSIS
BEGIN [ WORK | TRANSACTION ]
DESCRIPTION
BEGIN initiates a transaction block, that is, all statements after BEGIN command will be executed in a single transaction until an explicit COMMIT [commit(7)] or ROLLBACK [rollback(l)] is given. By default (without BEGIN), PostgreSQL executes transactions in ``autocommit'' mode, that is, each statement is executed in its own transaction and a commit is implicitly performed at the end of the statement (if execution was successful, otherwise a rollback is done).
Statements are executed more quickly in a transaction block, because transaction start/commit requires significant CPU and disk activity. Execution of multiple statements inside a transaction is also useful to ensure consistency when making several related changes: other sessions will be unable to see the intermediate states wherein not all the related updates have been done.
PARAMETERS
- WORK
- TRANSACTION
- Optional key words. They have no effect.
NOTES
START TRANSACTION [start_transaction(7)] has the same functionality as BEGIN.
Use COMMIT [commit(7)] or ROLLBACK [rollback(7)] to terminate a transaction block.
Issuing BEGIN when already inside a transaction block will provoke a warning message. The state of the transaction is not affected.
EXAMPLES
To begin a transaction block:
BEGIN;
COMPATIBILITY
BEGIN is a PostgreSQL language extension. There is no explicit BEGIN command in the SQL standard; transaction initiation is always implicit and it terminates either with a COMMIT or ROLLBACK statement.
Other relational database systems may offer an autocommit feature as a convenience.
Incidentally, the BEGIN key word is used for a different purpose in embedded SQL. You are advised to be careful about the transaction semantics when porting database applications.
SEE ALSO
COMMIT [commit(7)], ROLLBACK [rollback(l)]