Detailed explanation of MySql automatic truncation example

Detailed explanation of MySql automatic truncation example

Detailed explanation of MySql automatic truncation example

A friend asked a question, why can't overlong words be automatically truncated when calling insert or update statements in the project, but can be automatically truncated when directly executed in navicat?

as follows

CREATE TABLE `p_app_station` (
 `WX_APP_ID` ​​varchar(20) NOT NULL,
 `APP_SECRET` varchar(33) DEFAULT NULL,
 `IS_BINDING` int(1) DEFAULT '0',
 `ACCOUNT_ID` int(13) DEFAULT NULL,
 `TOKEN` varchar(40) DEFAULT NULL,
 `BIND_URL` varchar(200) DEFAULT NULL,
 `WX_APP_NAME` varchar(50) DEFAULT NULL,
 `WX_APP_SID` varchar(50) DEFAULT NULL,
 `WX_NO` varchar(50) DEFAULT NULL,
 `CREATE_USER_ID` varchar(13) DEFAULT NULL,
 `UPDATE_DATE` datetime DEFAULT NULL,
 `CREATE_DATE` datetime DEFAULT NULL,
 `UPDATE_USER_ID` varchar(13) DEFAULT NULL,
 `STATION_TYPE` int(1) unsigned zerofill DEFAULT NULL COMMENT 'Mark type (trial version: 0, member version: 1, custom version: 2)',
 `ACTIVE_DATE` datetime DEFAULT NULL COMMENT 'Expiration date',
 `APP_MODULE_ID` varchar(60) DEFAULT NULL COMMENT 'Push template message ID',
 PRIMARY KEY (`WX_APP_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
insert into p_app_station(wx_app_id) values('12121312312312啊啊啊啊啊aassasdasd');
select * from p_app_station where wx_app_id like '12121312312312%';

Obviously varchar(20) is not big enough to hold 12121312312312 ah ah ah aassasdasd

The query results are as follows

It was indeed truncated automatically, but when executing the same SQL in the project, it was found that this was not the case, and an error was reported instead.

Data truncated for column '%s' at row %ld

Considering that it is the same database and there is no difference in schema, the possibility should be on jdbcDriver.

View jdbc source code

private void setupServerForTruncationChecks() throws SQLException {
  if (getJdbcCompliantTruncation()) {
    if (versionMeetsMinimum(5, 0, 2)) {
      String currentSqlMode = this.serverVariables.get("sql_mode");
 
      boolean strictTransTablesIsSet = StringUtils.indexOfIgnoreCase(currentSqlMode, "STRICT_TRANS_TABLES") != -1;
 
      if (currentSqlMode == null || currentSqlMode.length() == 0 || !strictTransTablesIsSet) {
        StringBuilder commandBuf = new StringBuilder("SET sql_mode='");
 
        if (currentSqlMode != null && currentSqlMode.length() > 0) {
          commandBuf.append(currentSqlMode);
          commandBuf.append(",");
        }
 
        commandBuf.append("STRICT_TRANS_TABLES'");
 
        execSQL(null, commandBuf.toString(), -1, null, DEFAULT_RESULT_SET_TYPE, DEFAULT_RESULT_SET_CONCURRENCY, false, this.database, null, false);
 
        setJdbcCompliantTruncation(false); // server's handling this for us now
      } else if (strictTransTablesIsSet) {
        // We didn't set it, but someone did, so we piggy back on it
        setJdbcCompliantTruncation(false); // server's handling this for us now
      }
 
    }
  }
}

View the getJdbcCompliantTruncation method, its default value is

private BooleanConnectionProperty jdbcCompliantTruncation = new BooleanConnectionProperty("jdbcCompliantTruncation", true,
    Messages.getString("ConnectionProperties.jdbcCompliantTruncation"), "3.1.2", MISC_CATEGORY, Integer.MIN_VALUE);

Therefore, from version 3.1.2, if jdbcCompliantTruncation is not set in jdbcurl, the default will be to execute without truncation and report an error.

So is it possible to add parameters?

Make a choice:

If the value is longer than the specified length, there is a risk of loss of precision.

Therefore, it is recommended to check it in the program.

Currently working on the use of hibernate validate.

The above is the explanation about MySQL truncation. If you have any questions, you can leave a message or come to the community of this site to discuss and make progress together. Thank you for reading and I hope it can help everyone. Thank you for your support of this site!

You may also be interested in:
  • mysql5.7.19 winx64 decompressed version installation and configuration tutorial
  • CentOS 6.4 MySQL 5.7.18 installation and configuration method graphic tutorial
  • MySQL 5.7.18 download and installation process detailed instructions
  • How to use regular expression query in MySql
  • Summary of PHP's method of detecting whether a MySQL table exists

<<:  JS implementation of Apple calculator

>>:  Linux tutorial on replacing strings using sed command

Recommend

Detailed explanation of Vue's SSR server-side rendering example

Why use Server-Side Rendering (SSR) Better SEO, s...

Detailed explanation of invisible indexes in MySQL 8.0

Word MySQL 8.0 has been released for four years s...

MySQL merges multiple rows of data based on the group_concat() function

A very useful function group_concat(), the manual...

The process of deploying and running countly-server in docker in win10

I have just come into contact with and become fam...

Solution to data duplication when using limit+order by in MySql paging

Table of contents summary Problem Description Ana...

MySQL replication detailed explanation and simple example

MySQL replication detailed explanation and simple...

How to add a pop-up bottom action button for element-ui's Select and Cascader

As shown in the figure below, it is a common desi...

Centos7 startup process and Nginx startup configuration in Systemd

Centos7 startup process: 1.post(Power-On-Self-Tes...

In-depth understanding of the matching logic of Server and Location in Nginx

Server matching logic When Nginx decides which se...

TypeScript namespace merging explained

Table of contents Merge namespaces with the same ...

Example code of vue icon selector

Source: http://www.ruoyi.vip/ import Vue from ...

How to submit the value of a disabled form field in a form Example code

If a form field in a form is set to disabled, the ...

JS implements simple example code to control video playback speed

introduction I discovered a problem before: somet...

JavaScript web form function communication full of practical information

1. Introduction Earlier we talked about the front...