- Mastering PostgreSQL 10
- Hans Jürgen Sch?nig
- 161字
- 2021-06-30 19:03:49
Handling errors inside a transaction
It is not always the case that transactions are correct from beginning to end. However, in PostgreSQL, only error-free transactions can be committed. Here is what happens:
test=# BEGIN; BEGIN
test=# SELECT 1; ?column?
----------
1
(1 row)
test=# SELECT 1 / 0;
ERROR: division by zero
test=# SELECT 1; ERROR: current transaction is aborted, commands ignored until end of transaction block
test=# COMMIT; ROLLBACK
Note that division by zero did not work out.
In any proper database, an instruction similar to this will instantly error-out and make the statement fail.
It is important to point out that PostgreSQL will error-out, unlike MySQL, which is far less strict. After an error has occurred, no more instructions will be accepted even if those instructions are semantically and syntactically correct. It is still possible to issue a COMMIT. However, PostgreSQL will roll back the transaction because it is the only correct thing to be done at this point.
推薦閱讀
- 腦動(dòng)力:C語(yǔ)言函數(shù)速查效率手冊(cè)
- Mastercam 2017數(shù)控加工自動(dòng)編程經(jīng)典實(shí)例(第4版)
- 協(xié)作機(jī)器人技術(shù)及應(yīng)用
- 手把手教你玩轉(zhuǎn)RPA:基于UiPath和Blue Prism
- 控制系統(tǒng)計(jì)算機(jī)仿真
- 我也能做CTO之程序員職業(yè)規(guī)劃
- 工業(yè)機(jī)器人安裝與調(diào)試
- 從零開始學(xué)C++
- MATLAB-Simulink系統(tǒng)仿真超級(jí)學(xué)習(xí)手冊(cè)
- 計(jì)算機(jī)硬件技術(shù)基礎(chǔ)(第2版)
- 玩轉(zhuǎn)PowerPoint
- 51單片機(jī)應(yīng)用程序開發(fā)與實(shí)踐
- Practical Network Automation
- 從機(jī)器學(xué)習(xí)到無(wú)人駕駛
- AVR單片機(jī)C語(yǔ)言程序設(shè)計(jì)實(shí)例精粹