The content of this page has been automatically translated by AI. If you encounter any problems while reading, you can view the corresponding content in Chinese.

update statement

Last updated: 2024-08-22 17:01:27

Single table update

```sql
update tbase set nickname ='Hello TBase' where id=1;
Condition expression method
update tbase set nickname = 'Good TBase' where nickname is null;
Query result:
select * from tbase;
id
nickname
2
TBase is good
1
Hello TBase
3
Good TBase
Distributed keys cannot be updated:
update t2 set f1=1 where f2=1;
Error message:
ERROR: Distributed column "f1" can't be updated in current version

Multi-table join update

update tbase set nickname ='Good TBase' from t_appoint_col where t_appoint_col.id=tbase.id;
Query result:
select * from tbase;
id
nickname
2
TBase is good

Return updated data

update tbase set nickname = nickname where id = (random()*2)::integer returning id, nickname;
Returned result:
id
nickname
2
TBase is good

Multi-column matching update

update tbase set (nickname, age) = ('TBase is good', (random()*2)::integer);
Query result:
select * from tbase;
id
nickname
age
1
TBase is good
2
2
TBase is good
0

Shard Key is not allowed to be updated

update tbase set id=8 where id=1;
Error message:
ERROR: Distribute column or partition column can't be updated in current version