MySQL data out of range processing

Source: Internet
Author: User

MySQL by default, when a data exceeds the range of the data type that defines the column, the data is stored with the maximum value that is allowed for the data type.

Cases:

1. Create a table

CREATE TABLE t1 (' num ' int not null);

2. Inserting data

INSERT INTO T1 (' num ') value (2147483648);

[SQL] INSERT into T1 (num) value (2147483648); affected line: 1 time: 0.003s

3. Enquiry

SELECT * from T1;

+------------+| Num |+------------+| 2147483647 |+------------+1 row in Set (0.00 sec)

Here you can see that the data in the table is 2147483647, not the value we want to see, in which case MySQL inserts data without an error or warning.

In a real-world scenario, if the word Gencun is a very sensitive number, such as bank deposits, money-related data, this is not the result we want to get.

In this case, we want to insert or update the field if the inserted or updated value exceeds the range of the defined field data type, an error is raised, the modification data is forbidden by modifying the global variable Sq_modeSTRICT_ALL_TABLES实现

在my.cnf或my.ini中添加sql_mode=STRICT_ALL_TABLES,重启mysql_server即可。

例:

insert into t1(`num`) value(2147483648);

[SQL] INSERT INTO T1 (' num ') value (2147483648); [ERR] 1054-unknown column ' num ' in ' field list '



This article is from the "lang8027" blog, make sure to keep this source http://lang8027.blog.51cto.com/9606148/1964527

MySQL data out of range processing

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.