PostgreSQL数组怎么添加元素
1、首先是插入数组数据,有两种方式
推荐:PostgreSQL教程
# 方式1 postgres=# insert into t_kenyon(items) values('{1,2}'); INSERT 0 1 postgres=# insert into t_kenyon(items) values('{3,4,5}'); INSERT 0 1 # 方式2 postgres=# insert into t_kenyon(items) values(array[6,7,8,9]); INSERT 0 1 postgres=# select * from t_kenyon;id | items ----+----------- 1 | {1,2} 2 | {3,4,5} 3 | {6,7,8,9} (3 rows)
2、数据删除
postgres=# delete from t_kenyon where id = 3; DELETE 1 postgres=# delete from t_kenyon where items[1] = 4; DELETE 0 postgres=# delete from t_kenyon where items[1] = 3; DELETE 1
3、数据更新(往数组内添加元素)
# 往后追加 postgres=# update t_kenyon set items = items||7; UPDATE 1 postgres=# select * from t_kenyon;id | items ----+--------- 1 | {1,2,7} (1 row) postgres=# update t_kenyon set items = items||'{99,66}'; UPDATE 1 postgres=# select * from t_kenyon; id | items ----+------------------ 1 | {1,2,7,55,99,66} (1 row) # 往前插 postgres=# update t_kenyon set items = array_prepend(55,items) ; UPDATE 1 postgres=# select * from t_kenyon; id | items ----+--------------------- 1 | {55,1,2,7,55,99,66} (1 row)
推荐学习《Python教程》。