Binary Type Operations in MySQL

Binary Type Operations in MySQL

This article mainly introduces the binary type operations of MySQL database, and presents it to you through specific content. I hope it will be helpful for you to learn MySQL database.

Example data table:

CREATE TABLE test_bin (
  bin_id BINARY(16) NOT NULL
) Engine=InnoDB; 
 

Insert data (the content is a 32-bit UUID string value):

INSERT INTO test_bin(bin_id) VALUES(UNHEX('FA34E10293CB42848573A4E39937F479'));

INSERT INTO test_bin(bin_id) VALUES(UNHEX(?));

or

INSERT INTO test_bin(bin_id) VALUES(x'FA34E10293CB42848573A4E39937F479');

Query data:

SELECT HEX(bin_id) AS bin_id FROM test_bin;
 
SELECT HEX(bin_id) AS bin_id FROM test_bin WHERE bin_id = UNHEX('FA34E10293CB42848573A4E39937F479');
SELECT HEX(bin_id) AS bin_id FROM test_bin WHERE bin_id = UNHEX(?);
 
SELECT HEX(bin_id) AS bin_id FROM test_bin WHERE bin_id = x'FA34E10293CB42848573A4E39937F479';

Query results:

bin_id

--------------------------

FA34E10293CB42848573A4E39937F479

Note: Use the MySQL built-in UUID() to create a function that returns a UUID value of type BINARY(16).

CREATE FUNCTION uu_id() RETURNS binary(16) RETURN UNHEX(REPLACE(UUID(),'-',''));

or

CREATE FUNCTION uu_id() RETURNS binary(16) RETURN UNHEX(REVERSE(REPLACE(UUID(),'-','')));

use:

INSERT INTO test_bin(bin_id) VALUES(uu_id());

Example 1:

Connection conn = null;
 Statement stat = null;
 ResultSet rs = null;
 try {
  conn = JDBCUtils.getConnection(map);
  String sql = "select HEX(RECID) AS recid,STDNAME AS stdname ,HEX(RESID) AS resid from jyyt";
  stat = conn.createStatement();
  rs = stat.executeQuery(sql);
  while (rs.next()) {
  String recid = rs.getString("recid");
  String staname = rs.getString("stdname");
  String resid = rs.getString("resid");
  System.out.println(recid + "---" + staname + "---" + resid);
  }
 } catch (SQLException e) {
  e.printStackTrace();
 finally
  JDBCUtils.closeConnection(conn, stat, rs);
 }

Example 2:

SELECT
 y.UNITID AS unitid,
 y.UNITNAME AS unitName,
 y.WARNINGTYPE AS warningType 
FROM
 GXJT_YJ AS y
 LEFT JOIN md_org AS m ON m.RECID = y.UNITID
 LEFT JOIN PMS_COMPANY_INFO AS p ON m.RECID = p.UNITID 
WHERE
 HEX(m.parents) LIKE '%66F7B47C80000101D5E8ABF15CD9DA73%' 
AND y.WARNINGTYPE = 'REGISTRATION_DIFFERENT'

HEX() function is not used:

Use the HEX() function:

Supplementary knowledge: [MySQL] How to use Navicat to view the contents of varbinary variables in the MySQL database?

environment

Navicat software version: Navicat premium 11.1.13 (64-bit)

MySQL database version: 5.7

Problem

Just as the title says.

step

The solution is simple, memo.

1. The design of the database table is as follows. The photo variable type in the table is varbinary.

2. Navicat software displays garbled characters, as shown below.

3. Right-click "Save Data As" and save it as 1.txt. The file name is arbitrary.

4. Open it with UE, and the display is as follows.

The above article on the use of binary type in MySQL is all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of MYSQL configuration parameter optimization
  • Several ways to backup MySql database
  • Mysql query statement optimization skills

<<:  Common symbols in Unicode

>>:  Detailed explanation of dynamically generated tables using javascript

Recommend

Vue implements top left and right sliding navigation

Navigation and other things are often used in dai...

Complete steps to solve 403 forbidden in Nginx

The webpage displays 403 Forbidden Nginx (yum ins...

Vue implements the countdown component for second kills

This article shares the specific code of Vue to i...

Design a simple HTML login interface using CSS style

login.html part: <!DOCTYPE html> <html l...

How to display the border when td is empty

Previously, I summarized how to use CSS to achieve...

What is jQuery used for? jQuery is actually a js framework

Introduction to jQuery The jQuery library can be ...

How to set static IP in CentOS7 on VirtualBox6 and what to note

Install CentOS 7 after installing VirtualBox. I w...

The implementation of event binding this in React points to three methods

1. Arrow Function 1. Take advantage of the fact t...

Detailed tutorial on running Tomcat in debug mode in IDEA Maven project

1. Add the following dependencies in pom.xml <...

Tips for optimizing MySQL SQL statements

When faced with a SQL statement that is not optim...

How to configure two-way certificate verification on nginx proxy server

Generate a certificate chain Use the script to ge...

Explain TypeScript enumeration types in detail

Table of contents 1. Digital Enumeration 2. Strin...

Use javascript to create dynamic QQ registration page

Table of contents 1. Introduction 1. Basic layout...

Theory Popularization——User Experience

1. Concept Analysis 1: UE User Experience <br ...

Node.js file copying, folder creation and other related operations

NodeJS copies the files: Generally, the copy oper...