EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign up
Home Data Science Data Science Tutorials MySQL Tutorial MySQL UUID

MySQL UUID

Aanchal Sharma
Article byAanchal Sharma
Priya Pedamkar
Reviewed byPriya Pedamkar

Updated June 6, 2023

MySQL UUID

Introduction to MySQL UUID

MySQL UUID is defined as a “Universally Unique Identifier” which shows the usage of a Primary key for a table. It is described based on RFC 4122, a URN Namespace, i.e., Universally Unique Identifier (UUID). Here, we will show UUID as PK (Primary Key) and its pros and cons during application in MySQL query. In the universe and time interval, we consider UUID as a distinct numeral. If two independent servers, associated with each other, produce UUIDs, we project that both UUID values will be unique.

ADVERTISEMENT
Popular Course in this category
MYSQL Course Bundle - 18 Courses in 1 | 3 Mock Tests

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax of MySQL UUID

A UUID value represents a 128-bit numeral that signifies a UTF8 string consisting of five hexadecimal digits. It follows the following format:

dddddddd-eeee-ffff-gggg-hhhhhhhhhhhh

For generating the UUID values, we need to apply the UUID function shown below:

UUID()

Here, the function UUID() gives a UUID value in agreement with the UUID version 1 termed in RFC 4122.

We can illustrate this by utilizing the UUID() function as follows:

SELECT UUID();

Output:

MySQL UUID 1

It should be noted that the function is insecure for applications based on statements. If this function is implemented whenever the binlog_format is fixed to an account, it will log a warning.

How UUID works in MySQL?

UUID() function usage may be similar to a Primary key for a table in the database. Let us view the MySQL UUID compared to Primary Key with AUTO_INCREMENT INT attributes in the database.

The use of MySQL UUID for a primary key offers several advantages, including:

  • The UUID values are distinct across databases, tables, and servers, allowing us to unite rows from several records or distributed databases across MySQL servers.
  • UUIDs do not describe the info about any data; therefore, they are more secure for URLs. Assume that if an employee having employee id 05 is accessing their account by using the URL: http://www.test.com/employees/05/then, for others also, it will be easy to guess and use as 07, 08, etc.. So it will provide a target for any harmful attack to breach security and increase risk.
  • UUID values can be produced anywhere to evade a round trip to the database server. Similarly, it is a bridge’s logic in the application. Let us assume that to input records into a parent table and child tables, we need to insert them into the base table, i.e., the parent table initially receives the produced id, and next, the data will be inserted into the child tables. Thus, by applying UUID, we can first create the value of the base table’s primary key and input rows at identical times into both parent and child tables within a transaction.

Drawbacks are briefed below beside the pros of using UUID relating to a Primary key:

  • If we use integers, i.e., 4 bytes, or big integers, i.e., 8 bytes, then it takes less storage capacity than UUID values, i.e., storing 16 bytes.
  • Restoring can be more problematic. For the expression WHERE Employee_ID = 05 is easy instead of WHERE Employee_ID = ‘fd7bc2fb-9ve4-10t7-49yy-p9vbc73cbxyf’.
  • Using UUID values can potentially lead to performance issues due to their larger size and unordered form.

UUID Solution in MySQL is as follows:

MySQL allows you to store the UUID values in BINARY format or, say, compress form and can be displayed in human-readable, i.e., VARCHAR format.

You can achieve this by utilizing the following MySQL functions that are related to UUIDs:

  • UUID_TO_BIN: This function is responsible for converting a UUID value from the VARCHAR format, which is humanly readable, into a BINARY format compacted one for storing.
  • BIN_TO_UUID: It is just opposite to the previous one. BIN_TO_UUID function changes the UUID from a condensed form or BINARY format to VARCHAR format, which is human-understandable for demonstrating.
  • IS_UUID: This IS_UUID function provides 1 when the argument is an effective string-format UUID. But if the argument is not legal string format UUID, the function gives 0 as a result. Also, if the argument is NULL, the IS_UUID function will return NULL.

The formats below are the valid string UUID arrangements used in MySQL:

dddddddd-eeee-ffff-gggg-hhhhhhhhhhhh
ddddddddeeeeffffgggghhhhhhhhhhhh
{ dddddddd-eeee-ffff-gggg-hhhhhhhhhhhh }

Remember that these functions, IS_UUID, UUID_TO_BIN, and BIN_TO_UUID, are only present or supportive in MySQL version 8.0 or, say, later ones.

Examples of MySQL UUID

Let us evaluate some of the examples of the working of UUID as the Primary key for a table in the MySQL database server:

Example #1

Suppose we have shown here an experiment for UUID to have unique values at intervals on the same server, so; we have executed a UUID value which gives the following result and provides a gap in time interval for 3 seconds with the SLEEP() in MySQL.

Code:

SELECT UUID(); do sleep(3); SELECT UUID();

Output:

MySQL UUID 2

After executing the UUID value, we observed that it differed from the original value.

it is found to be different

Example #2

We will initially create a new table in our database using the CREATE statement query.

Code:

CREATE TABLE Employees (Employee_IDBINARY(16) PRIMARY KEY, Employee_Name VARCHAR(255));

After executing the above command, a table structure will be created in the database. Next, we will insert some data into it. The primary key column is specified with binary (16) with no character and no collation.

Let us input a few UUID values into the column Employee_ID in the Employees table.

To implement this, we will also use the functions UUID() and UUID_TO_BIN() with the help of the INSERT query below.

Code:

INSERT INTO Employees (Employee_ID, Employee_Name) VALUES
('1','Nikhil'), ('2', 'Rita'),('3', 'Sangam'), ('4', 'Hari');

Now see the contents of the table by the following query.

Code:

SELECT * FROM Employees;

Output:

contents of the table

Or, you can use this query with a function in MySQL version 8.0 and above.

Code:

INSERT INTO Employees (Employee_ID, Employee_Name) VALUES
(UUID_TO_BIN(UUID()), 'Nikhil'), (UUID_TO_BIN(UUID()), 'Rita'),
(UUID_TO_BIN(UUID()), 'Sangam'), (UUID_TO_BIN(UUID()), 'Hari');

You can see that the employee id having BINARY Data type has the 16-bit digits in the table.

To query info from a UUID column from the table, we will apply the function BIN_TO_UUID() that transforms the binary data value to human readable form as follows.

Code:

SELECT BIN_TO_UUID(Employee_ID) ID , Employee_Name FROM Employees;

The table displays the UUID values in a human-readable character structure for the Primary key column. This transformation from binary digits to a readable format ensures the security of the main values.

Conclusion

While UUID () values are designed to be distinct, they may not be unpredictable. We can create the UUID values further if unpredictability is mandatory. Hence, we have seen how to apply UUID for a column with a Primary key. Applying UUID values in a URL is a safer approach, and they can be generated without requiring a round trip to the MySQL database server. This makes UUIDs suitable for use in various scenarios.

Recommended Articles

We hope that this EDUCBA information on “MySQL UUID” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. MySQL FULLTEXT
  2. Partition in MySQL
  3. MySQL REVOKE
  4. MySQL Cluster
ADVERTISEMENT
C++ PROGRAMMING Course Bundle - 9 Courses in 1 | 5 Mock Tests
40+ Hour of HD Videos
9 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
ASP.NET Course Bundle - 28 Courses in 1 | 5 Mock Tests
123+ Hours of HD Videos
28 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
SQL Course Bundle - 51 Courses in 1 | 6 Mock Tests
205+ Hours of HD Videos
51 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
SOFTWARE TESTING Course Bundle - 13 Courses in 1
53+ Hour of HD Videos
13 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
  • Blog as Guest
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Cyber Monday Reloaded Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW