[TOC] # TRANSACTION ## 描述 该语句用于开启事务。 数据库事务(Database Transaction)是指作为单个逻辑工作单元执行的一系列操作。事务处理可以用来维护数据库的完整性,保证成批的SQL操作全部执行或全部不执行。 显示事务是用户自定义或用户指定的事务。通过 BEGIN TRANSACTION,或 BEGIN 和 BEGIN WORK(被作为START TRANSACTION的别名受到支持)语句显示开始,以 COMMIT 或 ROLLBACK 语句显示结束。 ## 格式 ~~~ transaction_stmt: START TRANSACTION [READ ONLY | READ WRITE]; | BEGIN [WORK]; | COMMIT [WORK]; | ROLLBACK [WORK]; | SET TRANSACTION {READ ONLY | READ WRITE}; ~~~ ## 参数解释 <table data-tag="table" id="table-tdq-chl-073" class="table"><colgroup width="265" span="1" data-tag="col" id="col-7ae-pzp-gx8" colwidth="1*" colnum="1" colname="col1" style="width:50%" class="col"></colgroup><colgroup width="430" span="1" data-tag="col" id="col-gie-i7g-h24" colwidth="1*" colnum="2" colname="col2" style="width:50%" class="col"></colgroup><thead id="thead-82d-yz6-3jt" class="thead"><tr id="tr-vpg-l7e-4bb"><th id="td-bp4-t9f-wuj"><p id="p-tgq-at1-h1k"><b>参数</b></p></th><th id="td-mrr-mj5-gg9"><p id="p-9mp-x1b-e7g"><b>描述</b></p></th></tr></thead><tbody data-tag="tbody" id="tbody-60e-k47-uem" class="tbody"><tr data-tag="tr" id="tr-282-y00-7g4" class="tr"><td data-tag="td" id="td-zqt-64v-5bs" class="td"><p data-tag="p" id="p-w74-k8l-xwf" class="p">START TRANSACTION [READ ONLY | READ WRITE]</p></td><td data-tag="td" id="td-nww-773-8je" class="td"><p data-tag="p" id="p-bc6-rdq-3xn" class="p">开启事务语句。一旦开启事务,则随后的 SQL 数据操作语句(即INSERT、UPDATE、DELETE等)直到显式提交时才会生效。</p><p data-tag="p" id="p-fph-x43-wzc" class="p">READ ONLY 子句表示事务是只读方式开启,事务内不允许执行修改操作。</p><p data-tag="p" id="p-4h1-87c-elj" class="p">READ WRITE 子句表示事务是读写方式开启,默认就是此种模式。</p></td></tr><tr data-tag="tr" id="tr-i1d-8te-h9v" class="tr"><td data-tag="td" id="td-9eo-fi8-f0i" class="td"><p data-tag="p" id="p-7rw-b16-9hq" class="p">BEGIN</p></td><td data-tag="td" id="td-d45-rfo-xta" class="td"><p data-tag="p" id="p-uh5-1bc-9s3" class="p">BEGIN 和 BEGIN WORK 被作为 START TRANSACTION 的别名受到支持</p></td></tr><tr data-tag="tr" id="tr-772-zqv-5kt" class="tr"><td data-tag="td" id="td-k1g-k3h-tc5" class="td"><p id="p-ss4-0ez-v5i">COMMIT</p></td><td data-tag="td" id="td-k8b-ttc-vhp" class="td"><p data-tag="p" id="p-cbu-epe-rla" class="p">提交当前事务。</p></td></tr><tr data-tag="tr" id="tr-zcq-3p8-c2w" class="tr"><td data-tag="td" id="td-0py-yup-k6r" class="td"><p id="p-2am-8qe-fjy">ROLLBACK </p></td><td data-tag="td" id="td-hrz-6wd-cus" class="td"><p data-tag="p" id="p-xgf-hkr-s7g" class="p">回滚当前事务。</p></td></tr><tr data-tag="tr" id="tr-m5x-v3j-mfa" class="tr"><td data-tag="td" id="td-hll-y5g-lrk" class="td"><p data-tag="p" id="p-pg4-txd-qy8" class="p">SET TRANSACTION {READ ONLY | READ WRITE}</p></td><td data-tag="td" id="td-3j6-kdu-m9e" class="td"><p data-tag="p" id="p-vgo-a69-cj7" class="p">将当前事务设置成 READ ONLY 或者 READ WRITE 模式。</p></td></tr></tbody></table> ## 示例 假设现有表 a 如下所示。 <table data-tag="table" id="table-vhp-rzq-kjl" class="table"><colgroup width="82" span="1" data-tag="col" id="col-tfd-ozw-imc" colwidth="1*" colnum="1" colname="col1" style="width:25%" class="col"></colgroup><colgroup width="94" span="1" data-tag="col" id="col-6c8-0ra-ygy" colwidth="1*" colnum="2" colname="col2" style="width:25%" class="col"></colgroup><colgroup width="105" span="1" data-tag="col" id="col-1ft-6o9-9lc" colwidth="1*" colnum="3" colname="col3" style="width:25%" class="col"></colgroup><colgroup width="191" span="1" data-tag="col" id="col-898-k8y-u0o" colwidth="1*" colnum="4" colname="col4" style="width:25%" class="col"></colgroup><thead id="thead-nlb-fid-fw6" class="thead"><tr id="tr-oez-3z1-yqy"><th id="td-o5r-hv5-19c"><p id="p-tu6-w2o-qtc">id</p></th><th id="td-fuz-7kr-q73"><p id="p-wr5-2d2-61b">name</p></th><th id="td-a2b-0ha-pb9"><p id="p-ccy-3ec-tz6">num</p></th><th id="td-pl2-b0y-g3c"><p id="p-t8e-uuz-2li">sell_date</p></th></tr></thead><tbody data-tag="tbody" id="tbody-jrz-42q-o7p" class="tbody"><tr data-tag="tr" id="tr-cg1-ehr-a3q" class="tr"><td data-tag="td" id="td-v19-vem-5f5" class="td"><p data-tag="p" id="p-jd1-qab-lj5" class="p">1</p></td><td data-tag="td" id="td-ggk-nax-jz9" class="td"><p data-tag="p" id="p-6u4-t7h-xs6" class="p">a</p></td><td data-tag="td" id="td-y7n-3s6-etc" class="td"><p data-tag="p" id="p-465-owo-v9s" class="p">100</p></td><td data-tag="td" id="td-jfv-emm-mfn" class="td"><p data-tag="p" id="p-6yn-frz-9k6" class="p">2013-06-21 10:06:43</p></td></tr><tr data-tag="tr" id="tr-tjs-9ct-hd4" class="tr"><td data-tag="td" id="td-7kz-iod-i0s" class="td"><p data-tag="p" id="p-acy-hsi-969" class="p">2</p></td><td data-tag="td" id="td-9gm-uic-evf" class="td"><p data-tag="p" id="p-bqz-dmi-z0g" class="p">b</p></td><td data-tag="td" id="td-28t-qmy-xmi" class="td"><p data-tag="p" id="p-ulm-2no-82s" class="p">200</p></td><td data-tag="td" id="td-rwo-hgo-e4g" class="td"><p data-tag="p" id="p-0af-bgj-3oc" class="p">2013-06-21 13:07:21</p></td></tr><tr data-tag="tr" id="tr-x7v-o9m-4vu" class="tr"><td data-tag="td" id="td-udi-b5b-4n0" class="td"><p data-tag="p" id="p-6mh-fsd-ku6" class="p">3</p></td><td data-tag="td" id="td-jjt-j06-0f2" class="td"><p data-tag="p" id="p-x9c-cdr-7wt" class="p">a</p></td><td data-tag="td" id="td-296-jym-wjd" class="td"><p data-tag="p" id="p-tcf-ove-k8b" class="p">50</p></td><td data-tag="td" id="td-wkp-4ay-e0z" class="td"><p data-tag="p" id="p-09l-vcm-qno" class="p">2013-06-21 13:08:15</p></td></tr></tbody></table> 1. 依次执行以下命令开始执行事务,将 id 为 3 的的 name 改为 c,并插入一行当前卖出 a 的记录。 ~~~ OceanBase(admin@test)> START TRANSACTION; Query OK, 0 rows affected (0.00 sec) OceanBase(admin@test)> UPDATE a SET name = 'c' WHERE id = 3; Query OK, 1 rows affected (0.00 sec) OceanBase(admin@test)> INSERT INTO a VALUES (4, 'a', 30, '2013-06-21 16:09:13'); Query OK, 1 rows affected (0.00 sec) OceanBase(admin@test)> COMMIT; Query OK, 0 rows affected (0.00 sec) ~~~ 2. 事务提交后,执行命令查看表 a 信息。 ~~~ SELECT * FROM a; ~~~ 结果如下: <table data-tag="table" id="table-qru-zm3-e6o" class="table"><colgroup width="82" span="1" data-tag="col" id="col-ktm-cig-wie" colwidth="1*" colnum="1" colname="col1" style="width:25%" class="col"></colgroup><colgroup width="94" span="1" data-tag="col" id="col-5hh-m2f-uei" colwidth="1*" colnum="2" colname="col2" style="width:25%" class="col"></colgroup><colgroup width="105" span="1" data-tag="col" id="col-h5q-bt6-6kx" colwidth="1*" colnum="3" colname="col3" style="width:25%" class="col"></colgroup><colgroup width="191" span="1" data-tag="col" id="col-7k5-xhf-33h" colwidth="1*" colnum="4" colname="col4" style="width:25%" class="col"></colgroup><thead id="thead-fvg-2q7-4z4" class="thead"><tr id="tr-5r6-i0o-tby"><th id="td-4oq-kpw-rkq"><p id="p-8p6-9wi-17f">id</p></th><th id="td-hv1-n8f-5up"><p id="p-4jg-2ww-w4v">name</p></th><th id="td-xru-8v8-zdg"><p id="p-bvs-blo-tuc">num</p></th><th id="td-a6c-2pq-j1p"><p id="p-i7i-xfv-fia">sell_date</p></th></tr></thead><tbody data-tag="tbody" id="tbody-bkm-y6w-ikx" class="tbody"><tr data-tag="tr" id="tr-68s-yc0-qyd" class="tr"><td data-tag="td" id="td-1j2-uom-b5x" class="td"><p data-tag="p" id="p-ch7-pqg-br2" class="p">1</p></td><td data-tag="td" id="td-z5h-mdl-aqf" class="td"><p data-tag="p" id="p-d1n-uch-rez" class="p">a</p></td><td data-tag="td" id="td-lbw-p6l-s4w" class="td"><p data-tag="p" id="p-ftu-v3h-wl4" class="p">100</p></td><td data-tag="td" id="td-c4w-zmz-abu" class="td"><p data-tag="p" id="p-bh9-xv2-ej9" class="p">2013-06-21 10:06:43</p></td></tr><tr data-tag="tr" id="tr-dy4-xb9-9j1" class="tr"><td data-tag="td" id="td-mwc-okz-yxk" class="td"><p data-tag="p" id="p-7tu-4pl-37f" class="p">2</p></td><td data-tag="td" id="td-yhd-f79-f1t" class="td"><p data-tag="p" id="p-vwo-tr9-pub" class="p">b</p></td><td data-tag="td" id="td-4zw-h3k-b18" class="td"><p data-tag="p" id="p-c88-bqs-fkk" class="p">200</p></td><td data-tag="td" id="td-agp-dqm-3iw" class="td"><p data-tag="p" id="p-adm-zsx-r2e" class="p">2013-06-21 13:07:21</p></td></tr><tr data-tag="tr" id="tr-5ab-mll-ei4" class="tr"><td data-tag="td" id="td-w5e-ths-0fa" class="td"><p data-tag="p" id="p-nyk-sh9-nuz" class="p">3</p></td><td data-tag="td" id="td-gjk-o04-15q" class="td"><p data-tag="p" id="p-096-vvb-m1z" class="p">c</p></td><td data-tag="td" id="td-h4n-f7f-2tm" class="td"><p data-tag="p" id="p-2gr-8m4-djf" class="p">50</p></td><td data-tag="td" id="td-6ct-y83-g4c" class="td"><p data-tag="p" id="p-8jx-3a5-7os" class="p">2013-06-21 13:08:15</p></td></tr><tr data-tag="tr" id="tr-szq-c06-2k9" class="tr"><td data-tag="td" id="td-o5v-hj2-d01" class="td"><p data-tag="p" id="p-261-etn-mwe" class="p">4</p></td><td data-tag="td" id="td-rhv-ph1-tul" class="td"><p data-tag="p" id="p-cdj-dop-nmt" class="p">a</p></td><td data-tag="td" id="td-qjf-bn8-xs3" class="td"><p data-tag="p" id="p-4sv-76s-fxm" class="p">30</p></td><td data-tag="td" id="td-7nr-are-lfn" class="td"><p id="p-1qm-kx4-5oo">2013-06-21 16:09:13</p></td></tr></tbody></table> **注意** 在事务还没有 COMMIT 之前,您可以查看下本事务中的操作是否已经生效,比如可以在 COMMIT 前,加一句“**SELECT \* FROM a;**"。本事务 session 的访问能读到最新的结果,本事务 session 之外的访问结果肯定是没有生效,在事务还没有 COMMIT 前,你之前做的操作除当前事务连接之外都是不可见的。如果您想回滚该事务,直接用“ROOLBACK”代替“COMMIT”。