问题答案 12026年5月26日 22:01
How to format uuid string from binary column in MySQL/MariaDB
In MySQL or MariaDB, UUIDs are typically stored as binary columns to save space and improve efficiency. Typically, UUIDs are stored as a 16-byte binary column (BINARY(16) or VARBINARY(16)) rather than as a 36-character string (including 4 hyphens). This approach conserves space and optimizes index performance. However, when displaying or processing these UUIDs, it is often preferable to format them into the standard 36-character string format.Formatting Binary UUIDsTo convert binary-formatted UUIDs to readable string format, utilize SQL built-in functions based on your database version and configuration. The following are common methods in MySQL or MariaDB:1. UsingMySQL 8.0+ and MariaDB 10.4+ provide the function, which directly converts binary-formatted UUIDs to string format.Example:This converts the binary UUID in to the standard UUID string format.2. Using and string functionsFor older database versions or more complex formatting requirements, use the function to convert binary data to a hexadecimal string, then format it using string functions.Example:This method first converts the binary data into a long hexadecimal string, then uses and functions to split and insert hyphens, constructing the standard UUID format.NotesEnsure you select the appropriate method (such as ) to correctly convert and store UUID data before insertion into the database.Considering performance implications, if you frequently format UUIDs at the application level, it is often more efficient to handle them in application code rather than in database queries.By employing these methods, you can choose the most suitable approach to format UUIDs stored in binary columns based on your specific database version and requirements.