update 语句

最近更新时间:2024-07-24 17:02:21

我的收藏

单表更新

```sql
update tbase set nickname ='Hello TBase' where id=1;
条件表达方法
update tbase set nickname = 'Good TBase' where nickname is null;
查询结果:
select * from tbase;
id
nickname
2
TBase 好
1
Hello TBase
3
Good TBase
分布键不能更新:
update t2 set f1=1 where f2=1;
错误信息:
ERROR: Distributed column "f1" can't be updated in current version

多表关联更新

update tbase set nickname ='Good TBase' from t_appoint_col where t_appoint_col.id=tbase.id;
查询结果:
select * from tbase;
id
nickname
2
TBase 好

返回更新的数据

update tbase set nickname = nickname where id = (random()*2)::integer returning id, nickname;
返回结果:
id
nickname
2
TBase 好

多列匹配更新

update tbase set ( nickname , age ) = ( 'TBase 好' , (random()*2)::integer );
查询结果:
select * from tbase;
id
nickname
age
1
TBase 好
2
2
TBase 好
0

Shard Key 不允许更新

update tbase set id=8 where id=1;
错误信息:
ERROR: Distribute column or partition column can't be updated in current version