Course
UUID
MySQL Tutorial
This SQL tutorial is structured for beginners to guide them from the foundational concepts to advanced data manipulation and querying techniques in SQL. By the end of this tutorial, you will have developed a robust understanding of SQL, equipping you with the knowledge to efficiently manage and analyze data across various database systems. This guide sets the stage for your growth into a skilled data professional, ready to tackle complex data challenges and contribute to the field of data analysis and database management.
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
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
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
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
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
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