postgresql pgAdmin 中的回滚 DML 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9917621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-20 23:47:02  来源:igfitidea点击:

Rollback DML statement in pgAdmin

postgresqlpgadmin

提问by chrismarx

In pgAdmin, if I execute an insert query, I don't see any way to either commit or rollback the statement I just ran (I know it auto commits). I'm used to Oracle and SQL developer, where I could run a statement, and then rollback the last statement I ran with a press of a button. How would I achieve the same thing here?

在 pgAdmin 中,如果我执行插入查询,我看不到任何提交或回滚我刚刚运行的语句的方法(我知道它会自动提交)。我已经习惯了 Oracle 和 SQL 开发人员,我可以在其中运行一条语句,然后按一下按钮回滚我运行的最后一条语句。我将如何在这里实现同样的目标?

回答by Frank Heikens

Use transaction in the SQL window:

在 SQL 窗口中使用事务:

BEGIN;
DROP TABLE foo;
ROLLBACK; -- or COMMIT;

-- edit -- Another example:

-- 编辑 -- 另一个例子:

BEGIN;
INSERT INTO foo(bar) VALUES ('baz') RETURNING bar; -- the results will be returned
SELECT * FROM other_table; -- some more result
UPDATE other_table SET var = 'bla' WHERE id = 1 RETURNING *; -- the results will be returned

-- and when you're done with all statements and have seen the results:
ROLLBACK; -- or COMMIT 

回答by Rop

I also DEARLYprefer the Oracle way of putting everything in a transaction automatically, to help avoid catastrophic manual mistakes.

我也非常喜欢 Oracle 自动将所有内容放入事务中的方式,以帮助避免灾难性的手动错误。

Having auto-commit enabled by default in an Enterpriseproduct, IMO, is beyond vicious, and nothing but a COMPLETELY, UTTERLY INSANEdesign-choice :(

企业产品 IMO 中默认启用自动提交是非常邪恶的,只不过是一个完全、非常疯狂的设计选择:(

Anyways --- working with Postgres, one always needs to remember

无论如何 --- 与 Postgres 一起工作,人们总是需要记住

BEGIN;

开始;

at the start of manual work or sql-scripts.

在手动工作或 sql 脚本开始时。

As a practical habit: then, when you would say: COMMIT;in Oracle, I use the line

作为一个实用的习惯:那么,当你说:COMMIT; 在 Oracle 中,我使用该行

END; BEGIN;

结尾; 开始;

in Postgres which does the same thing, i.e commits the current transaction and immediately starts a new one.

在 Postgres 中,它做同样的事情,即提交当前事务并立即开始一个新事务。



When using JDBC or similar, to create a connection, always use some method, e.g. getPGConnection(), that includes:

使用 JDBC 或类似方法时,要创建连接,请始终使用某种方法,例如 getPGConnection(),其中包括:

...
Connection dbConn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
dbConn.setAutoCommit(false);
...

to make sure every connection has auto-commit disabled.

确保每个连接都禁用了自动提交。

回答by Stefan Spasojcevic

If you are using pgAdmin4, you can turn the auto commit and/or auto rollback on and off.

如果您使用 pgAdmin4,您可以打开和关闭自动提交和/或自动回滚。

Go to the File drop down menu and select Preferences option. In the SQL editor tab -> Options you can see the options to turn auto commit/rollback on and off.

转到文件下拉菜单并选择首选项选项。在 SQL 编辑器选项卡 -> 选项中,您可以看到打开和关闭自动提交/回滚的选项。

Auto commit/rollback option

自动提交/回滚选项