mysql integer (int) digital overflow considerations in program and database design

Source: Internet
Author: User
Tags manual numeric mysql manual

The following tries to make a whole (int):

One: MySQL5

In the MySQL5 version, for example, most administrators might set the column property of the increment number, or other applied numeric field, to type int, which takes 4 bytes, and int is divided into unsigned and signed. For unsigned types, the range is 0 to 4294967295, and the range of the signed type is 2147483648 to 2147483647. Reference to the MySQL manual: 11.2. Numeric type.

When you want to save a value in a numeric column that exceeds the allowable range for that column, the operation of MySQL depends on the SQL mode in effect at this time. If the pattern is not set, MySQL crops the value to the appropriate endpoint of the range and saves the trimmed value. However, if the pattern is set to traditional ("strict mode"), the value out of range will be rejected and prompted for an error, and the insert will fail according to the SQL standard. See MySQL Manual 5.3.2: "SQL Server mode."

If the int column is unsigned, the column range is the same size, but its endpoints change to 0 and 4294967295. If you try to save 9999999999 and 9999999999, the values saved in the non-strict mode to the column are 0 and 4294967296.

If the value assigned in a floating-point or fixed-point column exceeds the specified (or default) precision and scale specification, MySQL holds the value representing the corresponding endpoint in a non strict mode.

When MySQL is not working in strict mode, for Alter TABLE, LOAD DATA INFILE, UPDATE, and multiline INSERT statements, the transformations that occur are reported as warnings. When MySQL is working in strict mode, these statements will fail, and some or all of the values will not be inserted or changed, depending on whether the table is a transaction table and other factors. See the MySQL manual 5.3.2: "SQL Server Mode" for details.
Two: php5:

1: The length of the integer number and platform-related, PHP does not support unsigned integers.
2: If a given number exceeds the range of integer, it will be interpreted as float. Similarly, float is returned if the result of the execution of the operation exceeds the integer range. If you have a comparison of numeric types in your program, you may have problems.
3: You can view the Php_int_size, Php_int_max, to determine the range of integers.

The following children are available for reference:

Under 32-bit server:


The following is a code fragment:

The code is as follows Copy Code
[Shengting@localhost ~]$ Php-r "echo php_int_size;"
4
[Shengting@localhost ~]$ Php-r "echo php_int_max;"
2147483647
[Shengting@localhost ~]$ php-r "Var_dump (2147483647);"
Int (2147483647)
[Shengting@localhost ~]$ php-r "Var_dump (2147483648);"
Float (2147483648)
[Shengting@localhost ~]$ Php-r "Var_dump (-2147483647);"
Int (-2147483647)
[Shengting@localhost ~]$ Php-r "Var_dump (-2147483648);"
Float (-2147483648)
[Shengting@localhost ~]$ php-r "Var_dump (4294967295);"
Float (4294967295)
[Shengting@localhost ~]$ php-r "Var_dump (4294967296);"
Float (4294967296)

Under 64-bit server:

The following is a code fragment:

  code is as follows copy code
[root@login shengting]# php-r" Echo php_int_size;
8
[root@login shengting]# php-r "echo php_int_max;"
9223372036854775807
[root@login shengting]# php-r "Var_dump (2147483647);"
Int (2147483647)
[Root@login shengting]# php-r var_dump (2147483648); "
Int (2147483648)
[Root@login shengting]# php-r var_dump (-2147483647); "
Int ( -2147483647)
[root@login shengting]# php-r "Var_dump (-2147483648);"
Int ( -2147483648)
[root@login shengting]# php-r "Var_dump (4294967295);"
Int (4294967295)
[Root@login shengting]# php-r var_dump (4294967296); "
Int (4294967296)
 


Third: C + +

There is also a problem with both signed and unsigned types for C + +.

For a 32-bit system, an overflow can occur with a signed int, long to define a variable to save a unique number, and this problem occurs.

For 64-bit systems, an overflow may occur if you use int to define a variable to save a unique number, and the problem

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.