[TOC] # UPDATE ## 描述 该语句用于修改表中的字段值。 ## 格式 ~~~ UPDATE [IGNORE] table_references SET update_asgn_list [WHERE where_condition] [ORDER BY order_list] [LIMIT row_count]; table_references: tbl_name [PARTITION (partition_name,...)] [, ...] update_asgn_list: column_name = expr [, ...] order_list: column_name [ASC|DESC] [, column_name [ASC|DESC]…] ~~~ ## 参数解释 <table data-tag="table" id="table-af1-lpd-5dp" class="table"><colgroup width="198" span="1" data-tag="col" id="col-du8-tad-5h0" colwidth="1*" colnum="1" colname="col1" style="width:50%" class="col"></colgroup><colgroup width="456" span="1" data-tag="col" id="col-jqe-sp8-279" colwidth="1*" colnum="2" colname="col2" style="width:50%" class="col"></colgroup><thead id="thead-69x-osd-h31" class="thead"><tr id="tr-1nz-zf2-2mk"><th id="td-km4-dyo-e9u"><p id="p-zh3-31d-071"><b>参数</b></p></th><th id="td-bds-jd4-olv"><p id="p-7he-nvz-g19"><b>描述</b></p></th></tr></thead><tbody data-tag="tbody" id="tbody-xxh-7mo-65t" class="tbody"><tr data-tag="tr" id="tr-vse-cy4-7m4" class="tr"><td data-tag="td" id="td-ws7-26q-d8s" class="td"><p data-tag="p" id="p-30b-mki-39w" class="p">IGNORE</p></td><td data-tag="td" id="td-pw9-44m-pmt" class="td"><p data-tag="p" id="p-217-eha-mjl" class="p">在 INSERT 语句执行过程中发生的错误将会被忽略。</p></td></tr><tr data-tag="tr" id="tr-ay5-b94-lmw" class="tr"><td data-tag="td" id="td-d4f-kjm-2s7" class="td"><p data-tag="p" id="p-mev-5mt-53e" class="p">table_references</p></td><td data-tag="td" id="td-zrv-16z-5k6" class="td"><p data-tag="p" id="p-t1b-0lt-zvi" class="p">指定修改表名,多表修改时,表名直接‘,’作为间隔。</p></td></tr><tr data-tag="tr" id="tr-9ho-h03-b3t" class="tr"><td data-tag="td" id="td-1e1-r7p-zls" class="td"><p data-tag="p" id="p-j4l-n4d-hsz" class="p">where_condition</p></td><td data-tag="td" id="td-2pl-yfg-nq0" class="td"><p data-tag="p" id="p-lke-ch1-oq8" class="p">指定过滤条件。</p></td></tr><tr data-tag="tr" id="tr-r05-d53-zpr" class="tr"><td data-tag="td" id="td-t8l-pv5-fs4" class="td"><p data-tag="p" id="p-tg1-zer-rde" class="p">row_count</p></td><td data-tag="td" id="td-zh7-dq3-ngt" class="td"><p data-tag="p" id="p-doy-ejz-zgx" class="p">限制的行数。</p></td></tr><tr data-tag="tr" id="tr-26u-xzt-wbv" class="tr"><td data-tag="td" id="td-0ty-lxm-v8q" class="td"><p data-tag="p" id="p-5v2-qhc-4oj" class="p">tbl_name</p></td><td data-tag="td" id="td-okr-n0x-6pm" class="td"><p data-tag="p" id="p-fe5-6x9-rgy" class="p">插入表名。</p></td></tr><tr data-tag="tr" id="tr-gfu-mfg-wfe" class="tr"><td data-tag="td" id="td-ptj-ypf-rty" class="td"><p data-tag="p" id="p-8b5-vxn-3yq" class="p">partition_name</p></td><td data-tag="td" id="td-ayu-t94-45y" class="td"><p data-tag="p" id="p-nft-qka-p0v" class="p">插入表指定的分区名。</p></td></tr><tr data-tag="tr" id="tr-d51-6bm-t8v" class="tr"><td data-tag="td" id="td-ynr-59k-f3p" class="td"><p data-tag="p" id="p-8a7-0g1-qm3" class="p">column_name</p></td><td data-tag="td" id="td-b2r-o63-df8" class="td"><p data-tag="p" id="p-e7j-oyn-w9a" class="p">列名。</p></td></tr><tr data-tag="tr" id="tr-v24-7xz-oin" class="tr"><td data-tag="td" id="td-yj7-zuo-sbu" class="td"><p data-tag="p" id="p-ibr-mbh-ssa" class="p">column_name ASC</p></td><td data-tag="td" id="td-1ce-5uc-gu4" class="td"><p data-tag="p" id="p-6lw-cm3-f5r" class="p">按列名升序修改。</p></td></tr><tr data-tag="tr" id="tr-x46-whw-g3t" class="tr"><td data-tag="td" id="td-34t-zwh-2ow" class="td"><p data-tag="p" id="p-eef-7ku-2ll" class="p">column_name DESC</p></td><td data-tag="td" id="td-ygd-wi9-etu" class="td"><p data-tag="p" id="p-lum-3pn-0wv" class="p">按列名降序修改。</p></td></tr></tbody></table> ## 注意事项 不管是多表还是单表更新都不支持直接对子查询进行更新值操作,例如:`update (select * from t1) set c1 = 100;` ## 示例 1. 创建示例表 t1 和 t2。 ~~~ OceanBase(admin@test)>create table t1(c1 int primary key, c2 int); Query OK, 0 rows affected (0.16 sec) OceanBase(admin@test)>select * from t1; +----+------+ | c1 | c2 | +----+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | | 4 | 4 | +----+------+ 4 rows in set (0.06 sec) OceanBase(admin@test)>create table t2(c1 int primary key, c2 int) partition by key(c1) partitions 4; Query OK, 0 rows affected (0.19 sec) OceanBase(admin@test)>select * from t2; +----+------+ | c1 | c2 | +----+------+ | 5 | 5 | | 1 | 1 | | 2 | 2 | | 3 | 3 | +----+------+ 4 rows in set (0.02 sec) ~~~ 2. 将表 t1 中 "t1.c1=1" 对应的那一行数据的 c2 列值修改为 100。 ~~~ OceanBase(admin@test)>update t1 set t1.c2 = 100 where t1.c1 = 1; Query OK, 1 row affected (0.02 sec) Rows matched: 1 Changed: 1 Warnings: 0 OceanBase(admin@test)>select * from t1; +----+------+ | c1 | c2 | +----+------+ | 1 | 100 | | 2 | 2 | | 3 | 3 | | 4 | 4 | +----+------+ 4 rows in set (0.01 sec) ~~~ 3. 将表 t1 中按照 c2 列排序的前两行数据的 c2 列值修改为 100。 ~~~ OceanBase(admin@test)>update t1 set t1.c2 = 100 order by c2 limit 2; Query OK, 2 rows affected (0.02 sec) Rows matched: 2 Changed: 2 Warnings: 0 OceanBase(admin@test)>select * from t1; +----+------+ | c1 | c2 | +----+------+ | 1 | 100 | | 2 | 100 | | 3 | 3 | | 4 | 4 | +----+------+ 4 rows in set (0.01 sec) ~~~ 4. 将表 t2 中 p2 分区的数据中 "t2.c1 > 2" 的对应行数据的 c2 列值修改为 100。 ~~~ OceanBase(admin@test)>update t2 partition(p2) set t2.c2 = 100 where t2.c1 > 2; Query OK, 1 row affected (0.02 sec) Rows matched: 1 Changed: 1 Warnings: 0 OceanBase(admin@test)>select * from t2; +----+------+ | c1 | c2 | +----+------+ | 5 | 5 | | 1 | 1 | | 2 | 2 | | 3 | 100 | +----+------+ 4 rows in set (0.06 sec) ~~~ 5. 修改多个表。将 t1 表和 t2 表中满足 "t1.c1 = t2.c1" 对应行的数据 t1 表中的 c2 列值修改为 100,t2 表中的 c2 列值修改为 200。 ~~~ OceanBase(admin@test)>update t1,t2 set t1.c2 = 100, t2.c2 = 200 where t1.c2 = t2.c2; Query OK, 6 rows affected (0.03 sec) Rows matched: 6 Changed: 6 Warnings: 0 OceanBase(admin@test)>select * from t1; +----+------+ | c1 | c2 | +----+------+ | 1 | 100 | | 2 | 100 | | 3 | 100 | | 4 | 4 | +----+------+ 4 rows in set (0.00 sec) OceanBase(admin@test)>select * from t2; +----+------+ | c1 | c2 | +----+------+ | 5 | 5 | | 1 | 200 | | 2 | 200 | | 3 | 200 | +----+------+ 4 rows in set (0.01 sec) ~~~ 6. 修改多个表。修改 t1 表和 t2 表的p2分区中满足 "t1.c1 = t2.c1" 对应行的数据 t1 表中的 c2 列值修改为 100,t2 表中的 c2 列值修改为 200。 ~~~ OceanBase(admin@test)>update t1,t2 partition(p2) set t1.c2 = 100, t2.c2 = 200 where t1.c2 = t2.c2; Query OK, 6 rows affected (0.02 sec) Rows matched: 6 Changed: 6 Warnings: 0 OceanBase(admin@test)>select * from t1; +----+------+ | c1 | c2 | +----+------+ | 1 | 100 | | 2 | 100 | | 3 | 100 | | 4 | 4 | +----+------+ 4 rows in set (0.01 sec) OceanBase(admin@test)>select * from t2; +----+------+ | c1 | c2 | +----+------+ | 5 | 5 | | 1 | 200 | | 2 | 200 | | 3 | 200 | +----+------+ 4 rows in set (0.01 sec) ~~~ 7. 对可更新视图 v 进行更新值。 ~~~ OceanBase(admin@test)>create view v as select * from t1; Query OK, 0 rows affected (0.07 sec) OceanBase(admin@test)>update v set v.c2 = 100 where v.c1 = 1; Query OK, 1 row affected (0.02 sec) Rows matched: 1 Changed: 1 Warnings: 0 OceanBase(admin@test)>select * from v; +----+------+ | c1 | c2 | +----+------+ | 1 | 100 | | 2 | 2 | | 3 | 3 | | 4 | 4 | +----+------+ 4 rows in set (0.01 sec) ~~~