-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_types.sql
More file actions
69 lines (52 loc) · 1.66 KB
/
data_types.sql
File metadata and controls
69 lines (52 loc) · 1.66 KB
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
-- Strings Types
-- CHAR(X), VARCHAR(X) {64KB}, MEDIUMTEXT {16MB}, LONGTEXT {4GB}, TINYTEXT {255B}, TEXT {64KB}
-- Integer Types
-- TINYINT {1B} [-128, 127], UNSIGNED TINYINT[0, 255], SMALLINT {2B} [-32K, 32K], MEDIUMINT {3B} [-8M, 8M],
-- INT {4B} [-2B, 2B], BIGINT {8B} [-9Z, 9Z]
-- Floating Point Types
-- DECIMAL(p, s), DEC, NUMERIC, FIXED, FLOAT, DOUBLE
-- Boolean Types
-- BOOL, BOOLEAN (TRUE AND FALSE).
-- Enum and Set Types
-- ENUM('value1', 'value2'), SET(...)
-- Date and Time Types
-- DATE, TIME, DATETIME {8B}, TIMESTAMP {4B}, YEAR
-- Blob Types (for storing images, videos, binary data)
-- TINYBLOB {255B}, BLOB {64KB}, MEDIUMBLOB {16MB}, LONGBLOB {4GB}
-- JSON Types
UPDATE products
SET properties = JSON_OBJECT(
'weight', '10',
'dimensions', JSON_ARRAY(1,2,3),
'manufacture', JSON_OBJECT('name', 'roku'))
WHERE product_id = 1;
SELECT * FROM sql_store.products;
SELECT
product_id,
JSON_EXTRACT(properties, '$.weight') as product_weight,
properties -> '$.dimensions' as product_dimensions,
properties -> '$.dimensions[1]' as product_y_dimension, -- (-> is column path operator, $. is accessing path)
properties ->> '$.manufacture.name' as manufacturer -- (to extract roku without quotes)
FROM products
WHERE product_id = 1;
-- Update JSON properties
UPDATE products
SET properties = JSON_SET(
properties,
'$.weight', 20,
'$.screen', 'UHD'
)
WHERE product_id = 1;
SELECT
product_id,
JSON_EXTRACT(properties, '$.weight') as product_weight,
properties ->> '$.screen' as screen
FROM products
WHERE product_id = 1;
-- Remove JSON property
UPDATE products
SET properties = JSON_REMOVE(
properties,
'$.screen'
)
WHERE product_id = 1;