bhylu.com

Advertisements

MySQL String Length

Choose a length function from the limit you actually need to enforce.

How it works

A text limit can mean characters for an editing rule or bytes for storage and transport. Those counts need not agree under utf8mb4. This example uses one accented character and a plain ASCII letter so the distinction can be checked without a large dataset. CHAR_LENGTH and CHARACTER_LENGTH count characters, while LENGTH counts bytes. ASCII describes the first byte of its input rather than providing a universal Unicode code-point conversion.

Choose the function from its contract

  • ASCII: Reads the first byte of a known ASCII character.
  • CHAR_LENGTH: Counts one character in this fixture.
  • CHARACTER_LENGTH: Alias of CHAR_LENGTH, not a different grapheme rule.
  • LENGTH: Counts two UTF-8 bytes for this character.

Run in a disposable MySQL lab

Use Oracle MySQL 8.4 with InnoDB, not Bhylu’s SQLite playground. The examples were reviewed on MySQL 8.4.8. XAMPP may provide MariaDB, which has different feature and collation behaviour. Check your actual server before running the script. These lessons do not require a change to Bhylu’s PHP, Apache or normal database service.

  1. On a separate lab instance, create the empty bhylu_mysql_lab schema. Stop if it already exists; do not overwrite an existing database.
  2. Select that schema and import the complete repair-parts seed once. It sets the documented session mode, utf8mb4 collation, UTC and English date-name locale.
  3. Run this lesson in a fresh copy of the seed, not after another lesson’s writes. Use the command-line client or a compatible editor. DELIMITER lines are client instructions for stored-program examples.

Download this complete lesson script. Its setup is the linked seed; review the dataset before changing it. Administrative and destructive examples are restricted to the specifically named disposable objects. Every script repeats the session settings because each new connection has its own session. No MySQL browser runner is currently available.

Example

lesson.sql

SET NAMES utf8mb4 COLLATE utf8mb4_0900_ai_ci;
SET SESSION sql_mode = 'STRICT_TRANS_TABLES,ONLY_FULL_GROUP_BY,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
SET SESSION time_zone = '+00:00';
SET SESSION lc_time_names = 'en_US';
SELECT 'ASCII' AS operation, ASCII('P') AS result;
SELECT 'CHAR_LENGTH' AS operation, CHAR_LENGTH('é') AS result;
SELECT 'CHARACTER_LENGTH' AS operation, CHARACTER_LENGTH('é') AS result;
SELECT 'LENGTH' AS operation, LENGTH('é') AS result;

Expected result

ASCII | 80
CHAR_LENGTH | 1
CHARACTER_LENGTH | 1
LENGTH | 2

Run the seed’s SET NAMES so the client and server agree on encoding. A character count is still not a perfect count of visible graphemes: combining sequences and emoji can contain several code points. Define the product’s limit and validate it consistently at the application boundary. A database length expression is useful evidence, but it does not settle every language-sensitive editing requirement.

Use the official MySQL 8.4 reference for statement contracts and version details.

Advertisements