|
| 1 | +/********************************************/ |
| 2 | +/* Data Types */ |
| 3 | +/********************************************/ |
| 4 | + |
| 5 | +/********************************************/ |
| 6 | +/* String */ |
| 7 | +/********************************************/ |
| 8 | + |
| 9 | +/* |
| 10 | +- be consistent |
| 11 | +VARCHAR (50) : short string |
| 12 | +VARCHAR (255) : medium lenght string |
| 13 | +
|
| 14 | +CHAR(x) : fixed length |
| 15 | +VARCHAR(x) : MAX 65,535 characters (~64 KB) |
| 16 | +MEDIUMTEXT : MAX 16MB |
| 17 | +LONGTEXT : MAX 4GB |
| 18 | +TINYTEXT : MAX 255 bytes |
| 19 | +TEXT : MAX 64 KB |
| 20 | +
|
| 21 | +
|
| 22 | +English : 1B |
| 23 | +European / Middle - eastern: 2B |
| 24 | +Asian : 3B |
| 25 | +
|
| 26 | +*/ |
| 27 | + |
| 28 | + |
| 29 | +/********************************************/ |
| 30 | +/* Integers */ |
| 31 | +/********************************************/ |
| 32 | +/* |
| 33 | +TINYINT 1b [-128,127] |
| 34 | +UNSIGNED TINYINT [0-255] |
| 35 | +
|
| 36 | +SMALLINT 2b [-32K,32K] |
| 37 | +MEDIUMINT 3b [-8M,8M] |
| 38 | +INT 4b [-2B,2B] |
| 39 | +BIGINT 8b [-9Z,9Z] |
| 40 | +
|
| 41 | +*/ |
| 42 | +/********************************************/ |
| 43 | +/* ZERO FILL */ |
| 44 | +/********************************************/ |
| 45 | +/* |
| 46 | +IF using ZEROFILL, |
| 47 | +for INT => 0001 will be displayed. |
| 48 | +`id` INT UNSIGNED ZEROFILL NOT NULL |
| 49 | +
|
| 50 | +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. |
| 51 | +
|
| 52 | +Using ZEROFILL and a display width has no effect on how the data is stored. It affects only how it is displayed. |
| 53 | +
|
| 54 | +Here is some example SQL that demonstrates the use of ZEROFILL: |
| 55 | +
|
| 56 | +CREATE TABLE yourtable (x INT(8) ZEROFILL NOT NULL, y INT(8) NOT NULL); |
| 57 | +INSERT INTO yourtable (x,y) VALUES |
| 58 | +(1, 1), |
| 59 | +(12, 12), |
| 60 | +(123, 123), |
| 61 | +(123456789, 123456789); |
| 62 | +SELECT x, y FROM yourtable; |
| 63 | +Result: |
| 64 | +
|
| 65 | + x y |
| 66 | + 00000001 1 |
| 67 | + 00000012 12 |
| 68 | + 00000123 123 |
| 69 | +123456789 123456789 |
| 70 | +
|
| 71 | +*/ |
| 72 | + |
| 73 | +/********************************************/ |
| 74 | +/* Fixed Point and Floating Point */ |
| 75 | +/********************************************/ |
| 76 | +/* |
| 77 | +
|
| 78 | +DECIMAL (p,s) => precision (maxium number of digits) , scale (number of digits after the decimal point) |
| 79 | +DECIMAL (9,2) => 1234567.89 |
| 80 | +
|
| 81 | +DEC |
| 82 | +NUMERIC |
| 83 | +FIXED |
| 84 | +
|
| 85 | +FLOAT 4b |
| 86 | +DOUBLE 8b |
| 87 | +
|
| 88 | +*/ |
| 89 | + |
| 90 | +/********************************************/ |
| 91 | +/* Boolean */ |
| 92 | +/********************************************/ |
| 93 | +/* |
| 94 | +BOOL |
| 95 | +BOOLEAN |
| 96 | +
|
| 97 | +TRUE/FALSE (or) 1/0 |
| 98 | +
|
| 99 | +*/ |
| 100 | + |
| 101 | +/********************************************/ |
| 102 | +/* ENUM / SET */ |
| 103 | +/********************************************/ |
| 104 | +/* |
| 105 | +if we want to restrict the values |
| 106 | +
|
| 107 | +ENUM('small','medium','large') |
| 108 | +
|
| 109 | +ADD COLUMN size ENUM('small','medium','large') |
| 110 | +
|
| 111 | +*/ |
| 112 | + |
| 113 | + |
| 114 | +/********************************************/ |
| 115 | +/* DATE / TIME */ |
| 116 | +/********************************************/ |
| 117 | +/* |
| 118 | +
|
| 119 | +DATE |
| 120 | +TIME |
| 121 | +DATETIME 8b |
| 122 | +TIMESTAMP 4b (only up to 2038) which is called 2038 probelm |
| 123 | +YEAR |
| 124 | +
|
| 125 | +*/ |
| 126 | + |
| 127 | +/********************************************/ |
| 128 | +/* Blobs */ |
| 129 | +/********************************************/ |
| 130 | +/* |
| 131 | +
|
| 132 | +TINYBLOB 255b |
| 133 | +BLOB 65KB |
| 134 | +MEDIUMBLOB 16MB |
| 135 | +LONGBLOB 4GB |
| 136 | +
|
| 137 | +*/ |
| 138 | + |
| 139 | +/********************************************/ |
| 140 | +/* JSON */ |
| 141 | +/********************************************/ |
| 142 | +/* |
| 143 | +{"key":value} |
| 144 | +*/ |
| 145 | + |
| 146 | +/********************************************/ |
| 147 | +/* Creating JSON */ |
| 148 | +/********************************************/ |
| 149 | +UPDATE products |
| 150 | +SET properties =' |
| 151 | +{ |
| 152 | +"dimensions":[1,2,3], |
| 153 | +"weight":10, |
| 154 | +"manufacture":{"name":"sony"} |
| 155 | +} |
| 156 | +' |
| 157 | +WHERE product_id = 1; |
| 158 | + |
| 159 | + |
| 160 | +UPDATE products |
| 161 | +SET properties = JSON_OBJECT( |
| 162 | + 'dimensions', JSON_ARRAY(1,2,3), |
| 163 | + 'weight',10, |
| 164 | + 'manufacture', JSON_OBJECT('name','sony') |
| 165 | +) |
| 166 | +WHERE product_id = 2; |
| 167 | + |
| 168 | +/********************************************/ |
| 169 | +/* Rerieving JSON */ |
| 170 | +/********************************************/ |
| 171 | + |
| 172 | +SELECT product_id, JSON_EXTRACT(properties, '$.weight') |
| 173 | +FROM products |
| 174 | +WHERE product_id = 1; |
| 175 | + |
| 176 | +SELECT product_id, properties -> '$.di' |
| 177 | +FROM products |
| 178 | +WHERE product_id = 1; |
| 179 | + |
| 180 | +SELECT product_id, properties -> '$.dimensions[0]' |
| 181 | +FROM products |
| 182 | +WHERE product_id = 1; |
| 183 | + |
| 184 | +SELECT product_id, properties -> '$.manufacture.name' |
| 185 | +FROM products |
| 186 | +WHERE product_id = 1; |
| 187 | + |
| 188 | +SELECT product_id, properties ->> '$.manufacture.name' |
| 189 | +FROM products |
| 190 | +WHERE properties ->> '$.manufacture.name' = 'sony'; |
| 191 | + |
| 192 | +/********************************************/ |
| 193 | +/* Set / Update JSON */ |
| 194 | +/********************************************/ |
| 195 | +UPDATE products |
| 196 | +SET properties = JSON_SET( |
| 197 | + properties, |
| 198 | + '$.weight',20, |
| 199 | + '$.age',10 |
| 200 | +) |
| 201 | +WHERE product_id = 2; |
| 202 | + |
| 203 | +/********************************************/ |
| 204 | +/* Remove JSON */ |
| 205 | +/********************************************/ |
| 206 | +UPDATE products |
| 207 | +SET properties = JSON_REMOVE( |
| 208 | + properties, |
| 209 | + '$.age' |
| 210 | +) |
| 211 | +WHERE product_id = 2; |
0 commit comments