
- MySQL - Home
- MySQL - Introduction
- MySQL - Features
- MySQL - Versions
- MySQL - Variables
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Node.js Syntax
- MySQL - Java Syntax
- MySQL - Python Syntax
- MySQL - Connection
- MySQL - Workbench
- MySQL Databases
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Show Database
- MySQL - Copy Database
- MySQL - Database Export
- MySQL - Database Import
- MySQL - Database Info
- MySQL Users
- MySQL - Create Users
- MySQL - Drop Users
- MySQL - Show Users
- MySQL - Change Password
- MySQL - Grant Privileges
- MySQL - Show Privileges
- MySQL - Revoke Privileges
- MySQL - Lock User Account
- MySQL - Unlock User Account
- MySQL Tables
- MySQL - Create Tables
- MySQL - Show Tables
- MySQL - Alter Tables
- MySQL - Rename Tables
- MySQL - Clone Tables
- MySQL - Truncate Tables
- MySQL - Temporary Tables
- MySQL - Repair Tables
- MySQL - Describe Tables
- MySQL - Add/Delete Columns
- MySQL - Show Columns
- MySQL - Rename Columns
- MySQL - Table Locking
- MySQL - Drop Tables
- MySQL - Derived Tables
- MySQL Queries
- MySQL - Queries
- MySQL - Constraints
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Replace Query
- MySQL - Insert Ignore
- MySQL - Insert on Duplicate Key Update
- MySQL - Insert Into Select
- MySQL Indexes
- MySQL - Indexes
- MySQL - Create Index
- MySQL - Drop Index
- MySQL - Show Indexes
- MySQL - Unique Index
- MySQL - Clustered Index
- MySQL - Non-Clustered Index
- MySQL Operators and Clauses
- MySQL - Where Clause
- MySQL - Limit Clause
- MySQL - Distinct Clause
- MySQL - Order By Clause
- MySQL - Group By Clause
- MySQL - Having Clause
- MySQL - AND Operator
- MySQL - OR Operator
- MySQL - Like Operator
- MySQL - IN Operator
- MySQL - ANY Operator
- MySQL - EXISTS Operator
- MySQL - NOT Operator
- MySQL - NOT EQUAL Operator
- MySQL - IS NULL Operator
- MySQL - IS NOT NULL Operator
- MySQL - Between Operator
- MySQL - UNION Operator
- MySQL - UNION vs UNION ALL
- MySQL - MINUS Operator
- MySQL - INTERSECT Operator
- MySQL - INTERVAL Operator
- MySQL Joins
- MySQL - Using Joins
- MySQL - Inner Join
- MySQL - Left Join
- MySQL - Right Join
- MySQL - Cross Join
- MySQL - Full Join
- MySQL - Self Join
- MySQL - Delete Join
- MySQL - Update Join
- MySQL - Union vs Join
- MySQL Keys
- MySQL - Unique Key
- MySQL - Primary Key
- MySQL - Foreign Key
- MySQL - Composite Key
- MySQL - Alternate Key
- MySQL Triggers
- MySQL - Triggers
- MySQL - Create Trigger
- MySQL - Show Trigger
- MySQL - Drop Trigger
- MySQL - Before Insert Trigger
- MySQL - After Insert Trigger
- MySQL - Before Update Trigger
- MySQL - After Update Trigger
- MySQL - Before Delete Trigger
- MySQL - After Delete Trigger
- MySQL Data Types
- MySQL - Data Types
- MySQL - VARCHAR
- MySQL - BOOLEAN
- MySQL - ENUM
- MySQL - DECIMAL
- MySQL - INT
- MySQL - FLOAT
- MySQL - BIT
- MySQL - TINYINT
- MySQL - BLOB
- MySQL - SET
- MySQL Regular Expressions
- MySQL - Regular Expressions
- MySQL - RLIKE Operator
- MySQL - NOT LIKE Operator
- MySQL - NOT REGEXP Operator
- MySQL - regexp_instr() Function
- MySQL - regexp_like() Function
- MySQL - regexp_replace() Function
- MySQL - regexp_substr() Function
- MySQL Fulltext Search
- MySQL - Fulltext Search
- MySQL - Natural Language Fulltext Search
- MySQL - Boolean Fulltext Search
- MySQL - Query Expansion Fulltext Search
- MySQL - ngram Fulltext Parser
- MySQL Functions & Operators
- MySQL - Date and Time Functions
- MySQL - Arithmetic Operators
- MySQL - Numeric Functions
- MySQL - String Functions
- MySQL - Aggregate Functions
- MySQL Misc Concepts
- MySQL - NULL Values
- MySQL - Transactions
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - SubQuery
- MySQL - Comments
- MySQL - Check Constraints
- MySQL - Storage Engines
- MySQL - Export Table into CSV File
- MySQL - Import CSV File into Database
- MySQL - UUID
- MySQL - Common Table Expressions
- MySQL - On Delete Cascade
- MySQL - Upsert
- MySQL - Horizontal Partitioning
- MySQL - Vertical Partitioning
- MySQL - Cursor
- MySQL - Stored Functions
- MySQL - Signal
- MySQL - Resignal
- MySQL - Character Set
- MySQL - Collation
- MySQL - Wildcards
- MySQL - Alias
- MySQL - ROLLUP
- MySQL - Today Date
- MySQL - Literals
- MySQL - Stored Procedure
- MySQL - Explain
- MySQL - JSON
- MySQL - Standard Deviation
- MySQL - Find Duplicate Records
- MySQL - Delete Duplicate Records
- MySQL - Select Random Records
- MySQL - Show Processlist
- MySQL - Change Column Type
- MySQL - Reset Auto-Increment
- MySQL - Coalesce() Function
MySQL - UUID
The MySQL UUID function
The MySQL UUID() function is used to generate "Universal Unique Identifiers" (UUIDs) in accordance with RFC 4122. UUIDs are designed to be universally unique, even when generated on different servers. The UUID is generated using a combination of the current timestamp, the unique identifier of the server, and a random number.
UUID Format
The UUID value is represented as a UTF-8 string and is a 128-bit number. The format of the UUID value is in hexadecimal number, and it consists of five segments which are separated by hyphens.
The general format of the UUID value is: aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee, where each segment represents a hexadecimal value.
Generating a UUID
Following is the basic example to generate a UUID using the UUID() function in MySQL −
SELECT UUID();
Output
It will display a universal unique identifier as shown below −
UUID() |
---|
55f7685d-e99c-11ed-adfc-88a4c2bbd1f9 |
Generating Multiple UUIDs
You can generate multiple UUIDs in a single query, and each UUID will be different as shown below −
SELECT UUID() AS ID1, UUID() AS ID2;
Output
The output will show two different UUIDs, with differences generally in the first segment −
ID1 | ID2 |
---|---|
78c3fb43-e99c-11ed-adfc-88a4c2bbd1f9 | 78c3fb4f-e99c-11ed-adfc-88a4c2bbd1f9 |
UUIDs in a Database Table
You can use UUIDs as unique identifiers in a database table. Following is an example of how to create a table with a UUID column and insert data −
Here, we are first creating a table with the name "ORDERS", with an ORDER_ID column of type VARCHAR using the following query −
CREATE TABLE ORDERS( ID int auto_increment primary key, NAME varchar(40), PRODUCT varchar(40), ORDER_ID varchar(100) );
Now, we are inserting data into the ORDERS table, using the UUID() function to generate unique values for the ORDER_ID column −
INSERT INTO ORDERS (NAME, PRODUCT, ORDER_ID) VALUES ("Varun", "Headphones", UUID()); INSERT INTO ORDERS (NAME, PRODUCT, ORDER_ID) VALUES ("Priya", "Mouse", UUID()); INSERT INTO ORDERS (NAME, PRODUCT, ORDER_ID) VALUES ("Nikhil", "Monitor", UUID()); INSERT INTO ORDERS (NAME, PRODUCT, ORDER_ID) VALUES ("Sarah", "Keyboard", UUID()); INSERT INTO ORDERS (NAME, PRODUCT, ORDER_ID) VALUES ("Vaidhya", "Printer", UUID());
Following is the ORDERS table obtained −
ID | NAME | PRODUCT | ORDER_ID |
---|---|---|---|
1 | Varun | Headphones | a45a9632-e99d-11ed-adfc-88a4c2bbd1f9 |
2 | Priya | Mouse | a45b03a3-e99d-11ed-adfc-88a4c2bbd1f9 |
3 | Nikhil | Monitor | a45b49cc-e99d-11ed-adfc-88a4c2bbd1f9 |
4 | Sarah | Keyboard | a45b8d3f-e99d-11ed-adfc-88a4c2bbd1f9 |
5 | Vaidhya | Printer | a4b003d0-e99d-11ed-adfc-88a4c2bbd1f9 |
Modifying UUIDs
You can modify UUIDs without losing their uniqueness. For example, you can remove hyphens or convert them to base64 notation using functions like REPLACE() and TO_BASE64().
Example
Here, we are updating the UUID value for the record where ID = 1 using the following query −
UPDATE ORDERS SET ORDER_ID = UUID() WHERE ID=1;
Output
Following is the output of the above code −
Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
Verification
To verify the modified UUID values, we can use the following SELECT query −
SELECT * FROM ORDERS;
As we can see in the output below, every time we execute the UUID() function, we get a different UUID value −
ID | NAME | PRODUCT | ORDER_ID |
---|---|---|---|
1 | Varun | Headphones | 38f4d94a-e99d-11ed-adfc-88a4c2bbd1f9 |
2 | Priya | Mouse | a45b03a3-e99d-11ed-adfc-88a4c2bbd1f9 |
3 | Nikhil | Monitor | a45b49cc-e99d-11ed-adfc-88a4c2bbd1f9 |
4 | Sarah | Keyboard | a45b8d3f-e99d-11ed-adfc-88a4c2bbd1f9 |
5 | Vaidhya | Printer | a4b003d0-e99d-11ed-adfc-88a4c2bbd1f9 |
Example
Assume the previously created table and let us remove hyphens from the UUID of the row with ID = 2 using the REPLACE() function as shown below −
UPDATE ORDERS SET ORDER_ID = REPLACE(UUID(), '-', '') WHERE ID = 2;
Output
Output of the above code is as follows −
Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
Verification
To verify the modified UUID value, we can use the following SELECT query −
SELECT * FROM ORDERS;
As we can see in the output below, the UUID of row = 2 is modified without disturbing the "unique" part of it −
ID | NAME | PRODUCT | ORDER_ID |
---|---|---|---|
1 | Varun | Headphones | a45a9632-e99d-11ed-adfc-88a4c2bbd1f9 |
2 | Priya | Mouse | 069b0ca-7e99e11ed-adfc-88a4c2bbd1f9 |
3 | Nikhil | Monitor | a45b49cc-e99d-11ed-adfc-88a4c2bbd1f9 |
4 | Sarah | Keyboard | a45b8d3f-e99d-11ed-adfc-88a4c2bbd1f9 |
5 | Vaidhya | Printer | a4b003d0-e99d-11ed-adfc-88a4c2bbd1f9 |
Example
In the following query, we are converting the UUID of ID = 4 to base64 notation using the TO_BASE64() function −
UPDATE ORDERS SET ORDER_ID = TO_BASE64(UNHEX(REPLACE(UUID(),'-',''))) WHERE ID=4;
Output
The result produced is as follows −
Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
Verification
Let us verify the modified UUID value using the following SELECT query −
SELECT * FROM ORDERS;
The output produced is as given below −
ID | NAME | PRODUCT | ORDER_ID |
---|---|---|---|
1 | Varun | Headphones | a45a9632-e99d-11ed-adfc-88a4c2bbd1f9 |
2 | Priya | Mouse | 069b0ca7-e99e11ed-adfc-88a4c2bbd1f9 |
3 | Nikhil | Monitor | a45b49cc-e99d-11ed-adfc-88a4c2bbd1f9 |
4 | Sarah | Keyboard | ObRYA+mfEe2t/IikwrvR+Q== |
5 | Vaidhya | Printer | a4b003d0-e99d-11ed-adfc-88a4c2bbd1f9 |