-
Notifications
You must be signed in to change notification settings - Fork 523
/
12.Data Types.sql
211 lines (164 loc) · 4.61 KB
/
12.Data Types.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/********************************************/
/* Data Types */
/********************************************/
/********************************************/
/* String */
/********************************************/
/*
- be consistent
VARCHAR (50) : short string
VARCHAR (255) : medium lenght string
CHAR(x) : fixed length
VARCHAR(x) : MAX 65,535 characters (~64 KB)
MEDIUMTEXT : MAX 16MB
LONGTEXT : MAX 4GB
TINYTEXT : MAX 255 bytes
TEXT : MAX 64 KB
English : 1B
European / Middle - eastern: 2B
Asian : 3B
*/
/********************************************/
/* Integers */
/********************************************/
/*
TINYINT 1b [-128,127]
UNSIGNED TINYINT [0-255]
SMALLINT 2b [-32K,32K]
MEDIUMINT 3b [-8M,8M]
INT 4b [-2B,2B]
BIGINT 8b [-9Z,9Z]
*/
/********************************************/
/* ZERO FILL */
/********************************************/
/*
IF using ZEROFILL,
for INT => 0001 will be displayed.
`id` INT UNSIGNED ZEROFILL NOT NULL
When you select a column with type ZEROFILL it pads the displayed value of the field with zeros up to the display width specified in the column definition. Values longer than the display width are not truncated. Note that usage of ZEROFILL also implies UNSIGNED.
Using ZEROFILL and a display width has no effect on how the data is stored. It affects only how it is displayed.
Here is some example SQL that demonstrates the use of ZEROFILL:
CREATE TABLE yourtable (x INT(8) ZEROFILL NOT NULL, y INT(8) NOT NULL);
INSERT INTO yourtable (x,y) VALUES
(1, 1),
(12, 12),
(123, 123),
(123456789, 123456789);
SELECT x, y FROM yourtable;
Result:
x y
00000001 1
00000012 12
00000123 123
123456789 123456789
*/
/********************************************/
/* Fixed Point and Floating Point */
/********************************************/
/*
DECIMAL (p,s) => precision (maxium number of digits) , scale (number of digits after the decimal point)
DECIMAL (9,2) => 1234567.89
DEC
NUMERIC
FIXED
FLOAT 4b
DOUBLE 8b
*/
/********************************************/
/* Boolean */
/********************************************/
/*
BOOL
BOOLEAN
TRUE/FALSE (or) 1/0
*/
/********************************************/
/* ENUM / SET */
/********************************************/
/*
if we want to restrict the values
ENUM('small','medium','large')
ADD COLUMN size ENUM('small','medium','large')
*/
/********************************************/
/* DATE / TIME */
/********************************************/
/*
DATE
TIME
DATETIME 8b
TIMESTAMP 4b (only up to 2038) which is called 2038 probelm
YEAR
*/
/********************************************/
/* Blobs */
/********************************************/
/*
TINYBLOB 255b
BLOB 65KB
MEDIUMBLOB 16MB
LONGBLOB 4GB
*/
/********************************************/
/* JSON */
/********************************************/
/*
{"key":value}
*/
/********************************************/
/* Creating JSON */
/********************************************/
UPDATE products
SET properties ='
{
"dimensions":[1,2,3],
"weight":10,
"manufacture":{"name":"sony"}
}
'
WHERE product_id = 1;
UPDATE products
SET properties = JSON_OBJECT(
'dimensions', JSON_ARRAY(1,2,3),
'weight',10,
'manufacture', JSON_OBJECT('name','sony')
)
WHERE product_id = 2;
/********************************************/
/* Rerieving JSON */
/********************************************/
SELECT product_id, JSON_EXTRACT(properties, '$.weight')
FROM products
WHERE product_id = 1;
SELECT product_id, properties -> '$.di'
FROM products
WHERE product_id = 1;
SELECT product_id, properties -> '$.dimensions[0]'
FROM products
WHERE product_id = 1;
SELECT product_id, properties -> '$.manufacture.name'
FROM products
WHERE product_id = 1;
SELECT product_id, properties ->> '$.manufacture.name'
FROM products
WHERE properties ->> '$.manufacture.name' = 'sony';
/********************************************/
/* Set / Update JSON */
/********************************************/
UPDATE products
SET properties = JSON_SET(
properties,
'$.weight',20,
'$.age',10
)
WHERE product_id = 2;
/********************************************/
/* Remove JSON */
/********************************************/
UPDATE products
SET properties = JSON_REMOVE(
properties,
'$.age'
)
WHERE product_id = 2;