-
Notifications
You must be signed in to change notification settings - Fork 1
/
update_and_delete.sql
158 lines (125 loc) · 6.48 KB
/
update_and_delete.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
UPDATE books SET payload ->> 'publisher' = 'sara'
WHERE title = 'book1';
-- ERROR: syntax error at or near "->>"
-- LINE 1: UPDATE books SET payload ->> 'publisher' = 'sara'
-- update value
SELECT jsonb_set('[{"f1":1,"f2":null},2]', '{0,f3}','[2,3,4]');
-- jsonb_set
-- ---------------------------------------------
-- [{"f1": 1, "f2": null, "f3": [2, 3, 4]}, 2]
-- (1 row)
SELECT jsonb_set('[{"f1":1,"f2":null},2]', '{0,f3}','[2,3,4]',false);
-- jsonb_set
-- ----------------------------
-- [{"f1": 1, "f2": null}, 2]
-- (1 row)
SELECT jsonb_set('[1,2,{"f1":1,"f2":null}]', '{-1,f2}','[2,3,4]',false);
-- jsonb_set
-- ------------------------------------
-- [1, 2, {"f1": 1, "f2": [2, 3, 4]}]
-- (1 row)
SELECT jsonb_set('[{"f1":1,"f2":null},2]', '{0}','[2,3,4]',false);
-- jsonb_set
-- ----------------
-- [[2, 3, 4], 2]
-- (1 row)
SELECT jsonb_set(payload, '{publisher}', '"new publisher"') from books WHERE title = 'book1';
-- {"price": 100, "authors": [{"id": 1, "name": "author1"}, {"id": 2, "name": "author2"}], "publisher": "new publisher", "published_date": "2017-04-07"}
UPDATE books SET payload = jsonb_set(payload, '{publisher}', '"new publisher"') WHERE title = 'book1';
SELECT payload from books WHERE title = 'book1';
-- Replace the tags (as oppose to adding or removing tags) add tags key:
UPDATE books SET payload = jsonb_set(payload, '{tags}', '["tag3", "tag4"]') WHERE title = 'book1';
-- {"tags": ["tag3", "tag4"], "price": 100, "authors": [{"id": 1, "name": "author1"}, {"id": 2, "name": "author2"}], "publisher": "publisher1", "published_date": "2017-04-07"}
SELECT jsonb_set(payload, '{tags}', '["tag3"]') FROM books WHERE title = 'book1';
-- {"tags": ["tag3"], "price": 100, "authors": [{"id": 1, "name": "author1"}, {"id": 2, "name": "author2"}], "publisher": "publisher1", "published_date": "2017-04-07"}
-- Replacing the second tag (0-indexed):
UPDATE books SET payload = jsonb_set(payload, '{tags,1}', '"tag5"') WHERE title = 'book1';
-- {"tags": ["tag3", "tag5"], "price": 100, "authors": [{"id": 1, "name": "author1"}, {"id": 2, "name": "author2"}], "publisher": "publisher1", "published_date": "2017-04-07"}
------------------------------------------------------
SELECT '["a", "b"]'::jsonb || '["c", "d"]'::jsonb;
-- ?column?
-- ----------------------
-- ["a", "b", "c", "d"]
-- (1 row)
SELECT '["a", "b"]'::jsonb || '["a", "d"]'::jsonb;
-- ?column?
-- ----------------------
-- ["a", "b", "a", "d"]
-- (1 row)
SELECT '{"a":1, "b":2}'::jsonb || '{"c":3, "d":4}'::jsonb;
-- ?column?
-- ----------------------------------
-- {"a": 1, "b": 2, "c": 3, "d": 4}
-- (1 row)
SELECT '{"a":1, "b":2}'::jsonb || '{"a":3, "d":4}'::jsonb;
-- ?column?
-- --------------------------
-- {"a": 3, "b": 2, "d": 4}
-- (1 row)
SELECT '{"a": "b"}'::jsonb - 'a';
-- ?column?
-- ----------
-- {}
-- (1 row)
SELECT '{"a": 1,"b":2}'::jsonb - 'c';
-- ?column?
-- ------------------
-- {"a": 1, "b": 2}
-- (1 row)
SELECT '["a", "b"]'::jsonb - 1;
-- ?column?
-- ----------
-- ["a"]
-- (1 row)
SELECT '["a", "b"]'::jsonb - -1;
-- ?column?
-- ----------
-- ["a"]
-- (1 row)
SELECT '["a", "b"]'::jsonb - 'a';
-- ?column?
-- ----------
-- ["b"]
-- (1 row)
SELECT '{"a":1, "b":2}'::jsonb - -1;
-- ERROR: cannot delete from object using integer index
SELECT '["a", {"b":1}]'::jsonb #- '{1,b}';
-- ?column?
-- -----------
-- ["a", {}]
-- (1 row)
SELECT '["a", {"b":1}]'::jsonb #- '{0,b}';
-- ?column?
-- -----------------
-- ["a", {"b": 1}]
-- (1 row)
SELECT '{"1": {"b":1,"c":2}}'::jsonb #- '{1,b}';
-- ?column?
-- -----------------
-- {"1": {"c": 2}}
-- (1 row)
-- Remove the last tag:
-- {"tags": ["tag3", "tag5"], "price": 100, "authors": [{"id": 1, "name": "author1"}, {"id": 2, "name": "author2"}], "publisher": "publisher1", "published_date": "2017-04-07"}
UPDATE books SET payload = payload #- '{tags,-1}';
-- {"tags": ["tag3"], "price": 100, "authors": [{"id": 1, "name": "author1"}, {"id": 2, "name": "author2"}], "publisher": "publisher1", "published_date": "2017-04-07"}
-- Complex update (delete the last tag, insert a new tag, and change the name or insert it if it is not there):
UPDATE books SET payload = jsonb_set(jsonb_set(payload #- '{tags,-1}', '{tags,0}', '"tag10"', true), '{name}', '"my-other-name"');
-- {"name": "my-other-name", "tags": ["tag10"], "price": 100, "authors": [{"id": 1, "name": "author1"}, {"id": 2, "name": "author2"}], "publisher": "publisher1", "published_date": "2017-04-07"}
UPDATE books SET payload = jsonb_set(payload, '{tags,1}', '"tag5"') WHERE title = 'book1';
-- {"name": "my-other-name", "tags": ["tag10", "tag5"], "price": 100, "authors": [{"id": 1, "name": "author1"}, {"id": 2, "name": "author2"}], "publisher": "publisher1", "published_date": "2017-04-07"}
UPDATE books SET payload = payload || '{"a": "apple"}' WHERE title = 'book1';
-- {"a": "apple", "name": "my-other-name", "tags": ["tag10", "tag5"], "price": 100, "authors": [{"id": 1, "name": "author1"}, {"id": 2, "name": "author2"}], "publisher": "new publisher", "published_date": "2017-04-07"}
SELECT payload - 'a' from books WHERE title = 'book1';
-- {"name": "my-other-name", "tags": ["tag10", "tag5"], "price": 100, "authors": [{"id": 1, "name": "author1"}, {"id": 2, "name": "author2"}], "publisher": "new publisher", "published_date": "2017-04-07"}
SELECT (payload -> 'tags') - 'tag10' from books WHERE title = 'book1';
-- ["tag5"]
-- tags are "tags": ["tag10", "tag5"] the proives query is select not update
SELECT (payload -> 'tags') - 0 from books WHERE title = 'book1';
-- ["tag5"]
SELECT payload #- '{"tags",0}' from books WHERE title = 'book1';
-- {"a": "apple", "name": "my-other-name", "tags": ["tag5"], "price": 100, "authors": [{"id": 1, "name": "author1"}, {"id": 2, "name": "author2"}], "publisher": "new publisher", "published_date": "2017-04-07"}
SELECT payload #- '{"authors",0,"name"}' from books WHERE title = 'book1';
-- {"a": "apple", "name": "my-other-name", "tags": ["tag10", "tag5"], "price": 100, "authors": [{"id": 1}, {"id": 2, "name": "author2"}], "publisher": "new publisher", "published_date": "2017-04-07"}
-- no error if key is not found
SELECT payload #- '{"authors",0,"age"}' from books WHERE title = 'book1';
-- {"a": "apple", "name": "my-other-name", "tags": ["tag10", "tag5"], "price": 100, "authors": [{"id": 1, "name": "author1"}, {"id": 2, "name": "author2"}], "publisher": "new publisher", "published_date": "2017-04-07"}