MySQL

The Most Interesting Functions in MySQL – A Guide

intro

Today, we’re walking you through some of the most interesting functions available within MySQL. Have a read!

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT
MySQL logo MySQL
THE MYSQL DATABASE

MySQL is known to every developer – who hasn’t heard of this DBMS? Some use the database management system in college assignments, some use them to build forum software (ever heard of phpBB? MyBB? vBulletin? IPBoard? They’re all MySQL-based), and some even use the relational database management system to build data breach search engines (spoiler alert – the BreachDirectory data breach search engine allows you to check whether your account is at risk of identity theft or not by allowing you to search through a huge trove of data breaches.)

However, what may not be known to everyone who works with MySQL is the fact that MySQL offers a wide range of interesting functions developers can choose to employ – some of those functions are used daily by developers and DBAs who don’t even think twice about using them, but some make you think twice – “wh.. what is this? Is this one really necessary?” This is why today we’re digging further into them!

The Functions Available Within MySQL

Have a look through the functions and operators list provided by MySQL – it’s pretty extensive, isn’t it? MySQL offers a bunch of functions across a wide variety of areas including, but not limited to:

  • Date and time functions
  • Numeric functions
  • Performance schema functions
  • XML and JSON functions
  • String functions
  • Math functions
  • Security-related functions
  • Full-text searching functions

Sadly, in this blog we won’t walk you through all of them – but read this blog until the end and you will have a very good understanding of what the functions within MySQL entail!

Interesting Functions in MySQL

Full-Text Search Functions

Some of the most interesting functions within the  database management system are related to searching for data and performing read-based operations: and read-based operations are nothing without wildcards.

Wildcards in MySQL have two forms – they can take the form of a percentage (“%”) sign, or they can also represent a wildcard (“*”) – the former is used by almost everyone who knows at least something about MySQL, while the latter is used by those who want to play with full-text searching operations.

Those operations look like so:

Copy
        
1 SELECT * FROM [your_table] WHERE MATCH(column) AGAINST (‘Text*’ IN BOOLEAN MODE);

Say, if we would like to find values starting from Lond and ending with anything, we’d need to run a query just like the following:

Image 1 - Running SQL Queries in DbVisualizer
Image 1 - Running SQL Queries in DbVisualizer

Do note that the IN BOOLEAN MODE modifier is necessary for any wildcard-based queries to run (that’s not the case if full-text indexes are not in place.)

MySQL version 5.7 also has a bug where if you have a lot of data with “@” signs (think email addresses and the like), have a full-text index on the column and run a search query on the table just like the one we’ve provided above, chances are you’d crash the entire MySQL server because the query would never complete. Why? Noone knows... (That’s a confirmed bug within MySQL!)

JSON-based Search Functions

Take a look at these queries:

Image 2 - JSON-based Queries in MySQL
Image 2 - JSON-based Queries in MySQL

We’ve set two variables – one of them a JSON array, another – a number. Then we’ve used the JSON_CONTAINS function to figure out whether the number in the second variable exists within the JSON array of the first variable – neat, huh?

And that’s the crux of the JSON_CONTAINS function within MySQL – it allows us to provide a JSON array, then search for something within the array.

Performance Schema Functions

Starting from MySQL 8.0.16, MySQL also includes a couple of functions that can be used to read data from the performance schema – these functions would likely mostly be used by advanced DBAs, but they’re interesting nonetheless. MySQL offers four functions related to the performance schema:

  1. FORMAT_BYTES()
  2. FORMAT_PICO_TIME()
  3. PS_CURRENT_THREAD_ID()
  4. PS_THREAD_ID()

The first one helps us format bytes to a readable value and would likely be used the most frequently, the second one allows us to format time in picoseconds (e.g. nanoseconds) to a more readable time (e.g. minutes), the third one provides the current thread ID that’s assigned to a connection ID, and the last one provides just the thread ID. Here’s what the usage of the FORMAT_PICO_TIME function would look like (do note that DbVisualizer doesn’t recognize the function because we’re not running MySQL 8 or above):

Image 3 - DbVisualizer Doesnt Recognize FORMAT_PICO_TIME
Image 3 - DbVisualizer Doesn't Recognize FORMAT_PICO_TIME

Window Functions

MySQL also has these so-called “window” functions that perform operations on rows. Such operations include GROUP BY and other operations according to two key concepts:

  1. MySQL must be provided a row and a call a function on it.
  2. MySQL must go through a couple of rows to evaluate the called function.

Queries using window functions look like so:

Copy
        
1 SELECT SUM(customers) AS total_customers FROM users; 2 3 SELECT * FROM sales ORDER BY product, user_country;

You get the point – these are not very complex. A window function is any function that matches both aforementioned concepts. That’s it!

Invisible Functions

Contrary to a popular belief, MySQL also has a couple of functions that are intended to be used only by the server and not the client (the user.) In other words, certain functions do exist, but attempts to invoke them will provoke an error by MySQL:

Image 4 - the INTERNAL_TABLE_ROWS Function within MySQL
Image 4 - the INTERNAL_TABLE_ROWS Function within MySQL

Why does MySQL even have these functions? The answer is plain and simple – to make the server’s work possible. Hey, MySQL has to use some help, right?

Such functions include, but are not limited to:

  • CAN_ACCESS_[COLUMN|DATABASE|TABLE|USER|VIEW] – used when checking for permissions of a specific user. Think of it like so – can the user access a...[column|database|table|user|view?]
  • INTERNAL_AUTO_INCREMENT() increments values once the AUTO_INCREMENT keyword is used.
  • INTERNAL_GET_[HOSTNAME|USERNAME] helps MySQL get the hostname of the server or the username of the user that’s currently using MySQL.

MySQL does offer other functions and the full list of internal functions inside of MySQL can be seen here.

Summary

In this blog, we’ve walked you through some of the most interesting functions within MySQL as well as its brothers and sisters Percona Server and MariaDB. Some of these functions include functions that are a necessity for everyone who searches for data, some of them help with performance optimization, while some are only made to work internally.

Take a look through our blog to learn more about database behavior, and until next time!

Dbvis download link img
About the author
LukasVileikisPhoto
Lukas Vileikis
Lukas Vileikis is an ethical hacker and a frequent conference speaker. He runs one of the biggest & fastest data breach search engines in the world - BreachDirectory.com, frequently speaks at conferences and blogs in multiple places including his blog over at lukasvileikis.com.
The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

How to Display All Duplicate Records in a MySQL Table

author Antonello Zanini tags MySQL 6 min 2025-01-08
title

MySQL Primary Key: What It Is, How It Works, and When to Use It

author Lukas Vileikis tags Data Visualization Tools DbVisualizer MySQL 8 min 2024-12-23
title

SQL CHECK Constraint: Definitive Guide With Examples

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2024-12-16
title

A Guide To the SQL DECLARE Statement

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SERVER 7 min 2024-12-12
title

SQL Upsert: Inserting a Record If It Does Not Exist

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2024-12-10
title

A Complete Guide to the MySQL COUNT Function

author Antonello Zanini tags MySQL 9 min 2024-12-09
title

MySQL Nullable Columns: Everything You Need to Know

author Antonello Zanini tags MySQL 6 min 2024-12-03
title

A Complete Guide to the MySQL GROUP_CONCAT Function

author Antonello Zanini tags Data aggregation MySQL 8 min 2024-12-02
title

How and Why to Add an Index in SQL

author Lukas Vileikis tags MySQL POSTGRESQL Search SQL SQL SERVER 8 min 2024-11-28
title

MySQL 9.0 and the New EXPLAIN Features

author Lukas Vileikis tags MySQL SQL 9 min 2024-11-27

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.