According to space and time, Universal Unique Identifier, known as UUID, is a 128-bit long value that is unique across the globe. The UUID is specified by the RFC 4122. UUID values are incredibly fascinating because even if the values are generated from the same device, they can never be the same. However, I will not get
into details about the technologies used to implement UUIDs. In this tutorial, we shall focus on the advantages of using UUIDs instead of INT for primary keys, the disadvantages of UUIDs in a database, and how to implement UUIDs in MySQL. Let us get started: To generate a UUID in MySQL, we use the UUID() function. This function returns a utf8 string with a 5-hexadecimal group in the form of: aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee The first three segments are generated as part of timestamp format in the low, middle, and high format. The fourth segment of the UUID value is reserved for ensuring temporary uniqueness, where the timestamp value drops monotonicity. The final segment represents the IEEE 802 node value, signifying uniqueness across space. When to Use UUID in MySQLI know what you are thinking: If UUIDs are unique globally, why don’t we use them as the default primary keys in database tables? The answer is both simple and not simple. To begin, UUIDs are not native data types such as an INT, which you can set as primary key and auto-increment as more data gets added to the database. Second, UUIDs have their drawbacks which might not be applicable in all cases. Allow me to share a few cases where using UUIDs as primary keys might be applicable.
The following are some of the drawbacks of implementing UUIDs in your database.
UUID FunctionsIn MySQL 8.0 and later, you can use various functions to counter some of the drawbacks presented by UUIDs. These functions are:
Basic MySQL UUID Types UsageAs mentioned earlier, to implement UUIDs in MySQL, we use the UUID() function. For example, to generate UUID, we do: mysql> SELECT UUID(); Table with UUIDLet us create a table with UUID values and see how we can implement such functionality. Consider the query below: DROP SCHEMA IF EXISTS uuids; Once all the UUIDs are generated, we can select them and convert them from Binary to string UUID values as shown in the query below: SELECT BIN_TO_UUID(id) id FROM validation; Here is the output: ConclusionThere is not much to cover about UUIDs in MySQL, but if you would like to learn more about them, consider checking out the MySQL source: https://dev.mysql.com/doc/ About the authorMy name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list Can MySQL generate UUID?The MySQL UUID() function is used to return a Universal Unique Identifier (UUID) generated according to RFC 4122, “A Universally Unique IDentifier (UUID) URN Namespace”. A key point to note about UUIDs is that they are designed such that they are globally unique in space and time.
How does MySQL store UUID?In MySQL, you can store UUID values in a compact format ( BINARY ) and display them in human-readable format ( VARCHAR ) with help of the following functions: UUID_TO_BIN. BIN_TO_UUID. IS_UUID.
How a UUID is generated?Version-1 UUIDs are generated from a time and a node ID (usually the MAC address); version-2 UUIDs are generated from an identifier (usually a group or user ID), time, and a node ID; versions 3 and 5 produce deterministic UUIDs generated by hashing a namespace identifier and name; and version-4 UUIDs are generated ...
What is UUID in database?A universally unique identifier (UUID) is a 128-bit number used to identify information in computer systems. You can create a UUID and use it to uniquely identify something.
|