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

How to reset the root password in mysql8.0.12

After installing the database, if you accidentall...

Example code of CSS responsive layout system

Responsive layout systems are already very common...

VMware Workstation Installation (Linux Kernel) Kylin Graphic Tutorial

This article shares with you how to install Kylin...

Several commonly used methods for centering CSS boxes (summary)

The first one: Using the CSS position property &l...

Detailed explanation of two points to note in vue3: setup

Table of contents In vue2 In vue3 Notes on setup ...

Solution to Vue's inability to watch array changes

Table of contents 1. Vue listener array 2. Situat...

Use of Linux telnet command

1. Introduction The telnet command is used to log...

Solve the problem of docker pull being reset

This article introduces how to solve the problem ...

Detailed explanation of MySQL information_schema database

1. Overview The information_schema database is th...

Docker installation and deployment example on Linux

After reading the following article, you can depl...

Detailed tutorial on installing SonarQube using Docker

Table of contents 1. Pull the image 1.1 Pull the ...

Implementation of Docker cross-host network (manual)

1. Introduction to Macvlan Before the emergence o...

Pure CSS to achieve a single div regular polygon transformation

In the previous article, we introduced how to use...