據Postgres的文件,添加一個關鍵的hstore列如下:添加一鍵空hstore列
UPDATE tab SET h = h || ('c' => '3');
但似乎如果hstore字段不爲空,只工作。例如:
postgres=# create table htest (t text, h hstore);
CREATE TABLE
postgres=# insert into htest (t) VALUES ('key');
INSERT 0 1
postgres=# update htest set h = h || ('foo'=>'bar') where t='key';
UPDATE 1
postgres=# select * from htest;
t | h
-----+---
key |
(1 row)
更新成功,但沒有更新的hstore。但是:
postgres=# update htest set h = ('foo'=>'bar') where t='key';
UPDATE 1
postgres=# select * from htest;
t | h
-----+--------------
key | "foo"=>"bar"
(1 row)
postgres=# update htest set h = h || ('bar'=>'foo') where t='key';
UPDATE 1
postgres=# select * from htest;
t | h
-----+----------------------------
key | "bar"=>"foo", "foo"=>"bar"
(1 row)
有沒有一種方法以原子一鍵添加到hstore沒有先檢查是否hstore是空的?
謝謝!我意識到我的hstore是空的,但我不知道這與空hstore有什麼不同。 – janr 2012-02-16 20:42:49
好東西,謝謝Daniel! – 2012-12-06 21:52:18
未來可能對某人有幫助。我使用8.4,我創建了hstore('')而不是hstore(array [] :: varchar [])的空hstore – trex005 2013-03-07 20:11:41