-
Notifications
You must be signed in to change notification settings - Fork 0
/
topic_8_datatype.sql
65 lines (51 loc) · 1.3 KB
/
topic_8_datatype.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
## JSON data type:
# for multiple key: value pairs
-- ALTER TABLE `sql_store`.`products`
-- ADD COLUMN `properties` JSON NULL AFTER `unit_price`;
update products
set properties = '
{
"dimension": [1, 2, 3],
"weight": 10,
"manufacturer": {"name": "sony"}
}
'
where product_id = 1;
update products
set properties = json_object(
'weight', 10,
'dimension', json_array(1, 2, 3),
'manufacturer', json_object('name', 'sony')
)
where product_id = 1;
select product_id, json_extract(properties, '$.weight') as weight
from products
where product_id = 1;
select product_id, properties -> '$.weight' as weight
from products
where product_id = 1;
select product_id, properties -> '$.dimension[0]' as weight
from products
where product_id = 1;
select product_id, properties -> '$.manufacturer.name' as weight # this returns: "sony"
from products
where product_id = 1;
select product_id, properties ->> '$.manufacturer.name' as weight # remove " in result. this returns: sony
from products
where product_id = 1;
update products
set properties = json_set(
properties,
'$.weight', 20,
'$.age', 10
)
where product_id = 1;
select product_id, properties
from products
where properties ->> '$.manufacturer.name' = 'sony';
update products
set properties = json_remove(
properties,
'$.age'
)
where product_id = 1;