处理错误

PostgreSQL 中的事务与错误处理

Jason Myers

Principal Engineer

难免出错

误把饼干(Biscuits)更新了

UPDATE cookies SET quantity = 13 WHERE name = 'Biscuits';

更正为更新意式脆饼(Biscotti)

UPDATE cookies SET quantity = 1 WHERE name = 'Biscuits';

UPDATE cookies SET quantity = 13 WHERE name = 'Biscotti';
PostgreSQL 中的事务与错误处理

回滚错误

BEGIN TRANSACTION

UPDATE cookies SET quantity = 13 WHERE name = 'Biscuits';
ROLLBACK;
SELECT quantity FROM cookies where name = 'Biscuits';
13
PostgreSQL 中的事务与错误处理

回滚多条语句

BEGIN TRANSACTION;

UPDATE cookies SET deliciousness = 111 where name = 'Cats Tongue';

UPDATE cookies SET deliciousness = 8 where name = 'Gingerbread';

ROLLBACK;
SELECT name, deliciousness FROM cookies where name in ('Cats Tongue', 'Gingerbread');
'Cats Tongue'  10
'Gingerbread'   9
PostgreSQL 中的事务与错误处理

让我们来练习吧!

PostgreSQL 中的事务与错误处理

Preparing Video For Download...