Difference between int unsigned in Mysql 4 and 5

Source: Internet
Author: User

All Integer types can have an optional (non-standard) attribute UNSIGNED. You can use an unsigned value when you want to allow only non-negative numbers in a column and the column requires a large upper limit value range.

Mysql4:
Mysql> create table wubx (a TINYINT unsigned not null default '0 ');
Query OK, 0 rows affected (0.04 sec)
Mysql> select * from wubx;
Empty set (0.00 sec)
Mysql> insert wubx values (0 );
Query OK, 1 row affected (0.00 sec)
Mysql> select * from wubx;
+ ------ +
| A |
+ ------ +
| 0 |
+ ------ +
1 row in set (0.00 sec)
Mysql> update wubx set a = A-1;
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 1
Mysql> select * from wubx;
+ ------ +
| A |
+ ------ +
| 0 |
+ ------ +
1 row in set (0.00 sec)

Mysql5:

(Root @ localhost) [test]> create table wubx (a int (11) unsigned not null default '0 ');
Query OK, 0 rows affected (18.44 sec)
(Root @ localhost) [test]> select * from wubx;
Empty set (0.00 sec)
(Root @ localhost) [test]> insert into wubx values (0 );
Query OK, 1 row affected (0.00 sec)
(Root @ localhost) [test]> update wubx set a = A-1;
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
(Root @ localhost) [test]> select * from wubx;
+ ------------ +
| A |
+ ------------ +
| 1, 4294967295 |
+ ------------ +
1 row in set (0.00 sec)
Mysql> show warnings;
+ --------- + ------ + ----------------------------------------------------- +
| Level | Code | Message |
+ --------- + ------ + ----------------------------------------------------- +
| Warning | 1264 | Out of range value adjusted for column 'A' at row 1 |
+ --------- + ------ + ----------------------------------------------------- +

Check:
When processing values:

MySQL4 will be automatically changed to 0 before the specified value is inserted into the table.
For speed, Mysql5 only stores the number of binary data. In addition and subtraction, it is also a binary operation.

Therefore, when using unsigned, be careful with the 0-1 operation. Try to make a judgment before such operation.

Source:Http://coolriver.cublog.cn, Wubingxi # gmail.com

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.