Solve the problem of overflow during the unsigned integer auto-subtraction operation in mysql

Source: Internet
Author: User

When an unsigned integer field is used in mysql for auto-subtraction, if the result is smaller than 0, it will directly overflow and no error is reported.
Normally, a bigint unsigned value is out of range error should be reported. At least my local mysql is like this, but it can be executed normally in online mysql, then the field value is changed to more than 4 billion 4294967295...

It may be caused by mysql configuration problems, but I did not find how to configure it or have no permission to configure it. Therefore, you can only get up and down from the program.
The SQL statement generated by the program is similar to the following:

Update table set field = field-1 where xxx = xxx;

Auto-minus 1. If you want to judge the result, do you have to check the database ??
So I used this method.

Change field = field-1:


Field = convert (field + (-1), signed)

This solves the problem. If the value is negative during subtraction, it will be 0 directly.

 

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.