
作者:小麦苗
在Oracle或MySQL中,当执行到DDL语句时,会隐式的将当前回话的事务进行一次“COMMIT”操作,因此在MySQL或Oracle中执行DDL语句时,应该严格地将DDL和DML完全分开,不能混合在一起执行。
在PG中,DDL语句是可以被回滚的。
Oracle
SYS@LHR11G> SET TRANSACTION NAME 't1';Transaction set.SYS@LHR11G> create table aa(id int);Table created.SYS@LHR11G> select * from aa;no rows selectedSYS@LHR11G> insert into aa values(1);1 row created.SYS@LHR11G> select * from aa; ID---------- 1SYS@LHR11G> rollback;Rollback complete.SYS@LHR11G> select * from aa;no rows selected
在Oracle的一个事务中,DDL语句不会被回滚。
MySQL
MySQL [lhrdb]> begin;Query OK, 0 rows affected (0.05 sec)MySQL [lhrdb]>MySQL [lhrdb]> create table bb(id int);Query OK, 0 rows affected (0.11 sec)MySQL [lhrdb]> rollback;Query OK, 0 rows affected (0.06 sec)MySQL [lhrdb]> select * from bb;Empty set (0.08 sec)MySQL [lhrdb]> begin;Query OK, 0 rows affected (0.05 sec)MySQL [lhrdb]>MySQL [lhrdb]>MySQL [lhrdb]> create table cc(id int);Query OK, 0 rows affected (0.64 sec)MySQL [lhrdb]> insert into cc values(1);Query OK, 1 row affected (0.05 sec)MySQL [lhrdb]> select * from cc;+------+| id |+------+| 1 |+------+1 row in set (0.05 sec)MySQL [lhrdb]> rollback;Query OK, 0 rows affected (0.05 sec)MySQL [lhrdb]> select * from cc;Empty set (0.05 sec)
在MySQL的一个事务中,DDL语句不会被回滚。
PostgreSQL
postgres=# begin;BEGINpostgres=*# create table aa(id int);CREATE TABLEpostgres=*# select * from aa; id----(0 rows)postgres=*# rollback;ROLLBACKpostgres=# select * from aa;ERROR: relation 'aa' does not existLINE 1: select * from aa; ^postgres=# begin;BEGINpostgres=*# create table bb(id int);CREATE TABLEpostgres=*# insert into bb values(1);INSERT 0 1postgres=*# commit;COMMITpostgres=# select * from bb; id---- 1(1 row)postgres=# begin;BEGINpostgres=*# truncate table bb;TRUNCATE TABLEpostgres=*# rollback;ROLLBACKpostgres=# select * from bb; id---- 1(1 row)
可以看到,在PG中,DDL语句可以进行回滚。





新闻|Babelfish使PostgreSQL直接兼容SQL Server应用程序

更多新闻资讯,行业动态,技术热点,请关注中国PostgreSQL分会官方网站
https://www.postgresqlchina.com
中国PostgreSQL分会生态产品
https://www.pgfans.cn
中国PostgreSQL分会资源下载站
https://www.postgreshub.cn


点赞、在看、分享、收藏
文章转载自开源软件联盟PostgreSQL分会,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




