MySQL data types: UNSIGNED Precautions (rpm)

Original Address: https://www.cnblogs.com/blankqdb/archive/2012/11/03/blank_qdb.html

1. UNSIGNED

UNSIGNED attribute is an unsigned type of digital, and C, unsigned same meaning as C ++ programming language of these. For example, INT type range -2 147 483 648 ~ 2 147 483 647, INT UNSIGNED type is a range of 0 to 4,294,967,295.

MYSQL integer in the range:

Type Size range (signed) range (unsigned) use

TINYINT 1 byte (-128,127) (0,255) small integer values

SMALLINT 2 bytes (-32 768,32 767) (535 0,65) large integer

MEDIUMINT 3 bytes (-8388 608,8 388 607) (0,16 777,215) large integer

INT or INTEGER 4 bytes (-2 147 483 147 483 647 648,2) (0,4 294,967,295) large integer

Source document < http://www.cnblogs.com/bukudekong/archive/2011/06/27/2091590.html >

It seemed that this is a good attribute options, especially for the primary key is the type of self-growth, because, in general, users want the primary key non-negative. However, in actual use, UNSIGNED may bring some negative effects, for example:

mysql> CREATE TABLE t ( a INT UNSIGNED, b INT UNSIGNED )

 

ENGINE=INNODB;

 

Query OK, 0 rows affected (0.06 sec)

 

mysql> INSERT INTO t SELECT 1,2;

 

Query OK, 1 row affected (0.00 sec)

 

Records: 1 Duplicates: 0 Warnings: 0

 

mysql> SELECT * FROM t\G;

 

*************************** 1. row ***************************

 

a: 1

 

b: 2

 

1 row in set (0.00 sec)

We created a table t, the storage engine is InnoDB. There are two types of tables INT UNSIGNED on t. Input (1, 2) This row of data, it seems there is no problem, then run the following statement:

SELECT a - b FROM t

At this point what the outcome will be? Will be -1? The answer is uncertain, may be -1, can also be a great value, but also can cause an error. In the Mac operating system in the (windows will also), MySQL database suggest the following errors:

mysql> SELECT a-b FROM t;

 

ERROR 1690 (22003): BIGINT UNSIGNED value is out of range in '(`test`.`t`.`a` - `test`.`t`.`b`)'

This error is very strange at first glance, suggesting that BIGINT UNSIGNED out of range, but the type we are used in INT UNSIGNED ah In another Linux! Operating system , the operating results is:

mysql> SELECT a -b FROM t\G;

 

*************************** 1. row ***************************

 

a - b: 4294967295

 

1 row in set (0.00 sec)

When the above-mentioned problems, there is a developer after I came and said he found a MySQL Bug, MySQL how could such a "stupid" mean? In After hearing his story, I wrote the following code and tell he's not the MySQL Bug, C language will also be so "stupid."

#include

 

int main () {

 

unsigned int a;

 

unsigned int b;

 

a = 1;

 

b = 2;

 

printf(a - b: %d\n,a-b);

 

printf(a - b: %u\n,a-b);

 

return 1;

 

}

Run the code above is:

a - b: -1

 

a - b: 4294967295

It can be seen in the C language ab may also return a very large integer, this value is the maximum value of INT UNSIGNED. Is C language has also undergone a Bug? How could it?

In the actual use of the process, developers of MySQL to the impression that there are many Bug, as long as the result of unexpected circumstances there is or developers can not understand happens, they are often blamed for MySQL Bug. Like other databases, MySQL does have some Bug, in fact, not a MySQL database Bug more, take a look Oracle RAC's Bug, it may be even more, but it is Oracle's flagship product. Therefore, we can not simply think the problem is MySQL's Bug.

For the above problem, as analyzed above, if you understand the number of integer representation in the database, then these very easy to understand, and this is the reason why the previously stressed the need to look at some of the principles of respect books computer. The above C program to make some changes:

#include

 

int main () {

 

unsigned int a;

 

unsigned int b;

 

a = 1;

 

b = 2;

 

printf(a - b: %d,%x\n,a-b,a-b);

 

printf(a - b: %u,%x\n,a-b,a-b);

 

return 1;

 

}

This not only print out the results of ab also print out the results of ab hexadecimal, the result of running the program are as follows:

a - b: -1,ffffffff

 

a - b: 4294967295,ffffffff

Can see the results are 0xFFFFFFFF, 0xFFFFFFFF only represent two possible values: for an unsigned integer value which is the maximum value of an integer, i.e., 4294967295; for a signed integer, a second a representative of the sign bit, if it is 1, indicating a negative number, then it should be inverted to give a negative value plus 1, i.e., -1.

The core of the problem is that in a MySQL database for operand UNSIGNED, its return value is the UNSIGNED. The positive and negative numbers in this issue: a more in-depth analysis "MySQL InnoDB storage engine technology insider", there is interest in further research.

So, how do you get -1 This value is not difficult, as long as the set SQL_MODE this parameter can be, for example?:

mysql>SET sql_mode='NO_UNSIGNED_SUBTRACTION';

 

Query OK, 0 rows affected (0.00 sec)

 

mysql> SELECT a-b FROM t\G;

 

*************************** 1. row ***************************

 

a-b: -1

 

1 row in set (0.00 sec)

SQL_MODE behind will be discussed further, there is not an in-depth discussion. My personal opinion is try not to use UNSIGNED, because it may cause some unexpected results. In addition, INT type may not store data, INT UNSIGNED same may not be stored, instead of this, not as in database design phase will increase INT type BIGINT type.

 

The above text is taken from < http://tech.it168.com/a2012/0808/1382/000001382732.shtml >

I encountered a similar problem: (linux on)

When the (ab) also occurs in the case where the same clauses

The following is the result of using php Mysql query (the first line of each second row is [1] - [2] Results)

86374

                       a                                       b

Array (  [1] => 1351843032  [2] => 1351756658  )

 

-2567

Array ( [1] => 1351843032  [2] => 1351845599  )

 

86374

Array ([1] => 1351843032  [2] => 1351756658 )

 

86374

Array (  [1] => 1351843032  [2] => 1351756658  )

 

-105849

Array (  [1] => 1351650809  [2] => 1351756658 )

 

86374

Array (  [1] => 1351843032 [2] => 1351756658  )

 

86374

Array ( [1] => 1351843032  [2] => 1351756658  )

Mysql following statement in the query select * from table where (ab)> 86374;

The results (normal thinking is concerned, the results should be empty, but Linux has emerged is the result of the following):

Array ( [1] => 1351843032  [2] => 1351845599  )

Array ( [1] => 1351650809  [2] => 1351756658  )

And this is precisely the result [1] - [2] is negative two lines.

Conclusion: If two unsigne and occurs where clause after subtraction is less than 0 ((ab) <0), in the query, Mysql linux on the negative will be converted into unsigned then query ((-2576+ 4294967295 + 1)> 86,374, (4294967295 + -105 849 + 1)> 86,374).

Guess you like

Origin www.cnblogs.com/loveyouyou616/p/11647851.html