The use and subtraction of mysql unsigned have a complementary overflow solution
- 2020-05-13 03:36:25
- OfStack
For example, if tinyint has a maximum of 127, then tinyint unsigned has a maximum of 127 times 2
The unsigned property is for integers only, while the binary property is for char and varchar only.
type
instructions
tinyint
Very small integers
smallint
The smaller integer
mediumint
Medium integer
int
Standard integer
bigint
Large integer
float
Single precision floating point number
double
A double precision floating point number
decimal
A string of floating point Numbers
The names and ranges of each value type are shown in table 2.
Type specification
Value range
tinyint[(m)]
Signed values: -128 to 127 (-27 to 27-1)
Unsigned values: 0 to 255 (0 to 28-1) 1 byte
smallint[(m)]
Signed values: -32768 to 32767 (-215 to 215-1)
Unsigned values: 0 to 65535 (0 to 21 6-1) 2 bytes
mediumint[(m)]
Signed values: -8388608 to 8388607 (-223 to 223-1)
Unsigned values: 0 to 16777215 (0 to 22 4-1) 3 bytes
int[(m)]
Signed value: -2147683648 to 2147683647 (-231 to 231-1)
Unsigned values: 0 to 4294967295 (0 to 232-1) 4 bytes
bigint[(m)]
Signed value: -9223372036854775808 to 9223373036854775807 (-263 to 263-1)
Unsigned values: 0 to 18446744073709551615 (0 to 264 bits 1) 8 bytes
float[(m, d)]
Minimum non-zero value: ±1.175494351 e-38
double[(m,d)]
Minimum non-zero value: + / - 2.2250738585072014 e - 308
decimal (m, d)
The variable; The range of values depends on m and d
Table 2: value ranges for numeric column types
The amount of storage required for each type of value is shown in table 3.
Type specification
Storage requirements
tinyint[(m)]
1 byte
smallint[(m)]
2 -
mediumint[(m)]
3 bytes
int[(m)]
4 bytes
bigint[(m)]
8 bytes
float[(m, d)]
4 bytes
double[(m, d)]
8 bytes
decimal (m, d)
m byte (mysql) < 3.23), m+2 bytes (mysql) > 3.23)
Table 3: storage requirements for numeric column types
mysql offers five integers: tinyint, smallint, mediumint, int, and bigint. int is short for integer. These types differ in the range of representable values. Integer columns can be defined as unsigned to disable negative values; This makes the value of the column range above 0. The demand for different types of storage also varies. Types with a wide range of values require more storage.
mysql provides three floating point types: float, double, and decimal. Unlike integer types, floating point types cannot be unsigned and have a different range of values than integer types, not only because these types have a maximum value, but also a minimum non-zero value. Minima provide a measure of the accuracy of the corresponding type, which is important for recording scientific data
mysql5 unsigned substraction presents a complement overflow solution
For speed, Mysql5 only stores data in base 2, and in addition and subtraction, it is also a base 2 operation.
So be careful with 0-1 when using unsigned. Try to make a judgment before doing this.
If you make a decision directly in the program, it is inevitable that there will be data errors, because there is a little bit of time to put the data in memory, but not in the library, the best way is to directly send out sql statement to modify, here mysql5 has a solution CONVERT(field + (the number to be added and subdivided) AS SIGNED) can be.
For example: UPDATE 'user' SET 'tk' = CONVERT(tk + (-8),SIGNED) WHERE 'id' = '1330'