MySQL unsigned int type data use some things

Source: Internet
Author: User
Tags commit

Take signed as an example, you can store -2147483648 to 2147483647 and take Unsigned as an example, you can save 0 to 4294967295, but in the operation of such fields, you must be careful to avoid the deletion of the overdo, but become full of blood fill.

Normally, MySQL can do this when the number of operations is reduced


Pgsql

The code is as follows Copy Code

--Transaction start--
SELECT ' example ' from ' test ' WHERE ID =: ID

--Use the program to calculate the new value of example, and then transfer back

UPDATE ' test ' SET ' example ' = n WHERE id =: ID;
--Commit Transaction
--Transaction Start--select ' example ' from ' test ' WHERE ID =: ID

--Use the program to calculate the new value of example, and then transfer back

UPDATE ' test ' SET ' example ' = n WHERE id =: ID;

--Commit Transaction

But smarter people would probably do that to reduce Query times (assuming a minus 1)


Pgsql

The code is as follows Copy Code

UPDATE ' test ' SET ' example ' = ' example '-1;

UPDATE ' test ' SET ' example ' = ' example '-1;

However, the above line of SQL Statement has a risk. Since there is no boundary check, when example = 0, a further reduction of 1 will become 4294967295.

The following figure is an example:

This is my own screenshot of the page, the figure of the number of Plurk to 4,294,967,294, is more than 4.2 billion pens, suppose I send a pen out a second, in my lifetime will not be stamped (1 * 86400 * 365 * 100 = 3153600000, 3.5 billion). Page links

Therefore, when this field is recorded "Amount" or "point", it will have a great impact on the data (as long as the program Transaction/work flow is not well controlled, the poor can also get rich overnight).

Although the problem is very serious, but the improvement method is also very simple, as long as the WHERE conditions limit, you can immediately avoid this problem.


Pgsql

The code is as follows Copy Code

UPDATE ' test ' SET ' example ' = ' example '-' 1 ' WHERE ' example ' > 0;

UPDATE ' test ' SET ' example ' = ' example '-' 1 ' WHERE ' example ' > 0;

The question has been returned to the authorities, and I wonder when the BUG will be corrected.

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.