Technology and General Blog

In accordance to area and time, Common One of a kind Identifier, acknowledged as UUID, is a 128-little bit prolonged value that is exceptional across the globe. The UUID is specified by the RFC 4122.

UUID values are extremely fascinating for the reason that even if the values are generated from the very same unit, they can hardly ever be the identical. However, I will not get into information about the systems used to employ UUIDs.

In this tutorial, we shall target on the advantages of using UUIDs rather of INT for key keys, the cons of UUIDs in a database, and how to carry out UUIDs in MySQL.

Enable us get started out:


To make a UUID in MySQL, we use the UUID() function. This function returns a utf8 string with a 5-hexadecimal team in the form of:


The initial 3 segments are created as component of timestamp format in the reduced, center, and higher format.

The fourth segment of the UUID benefit is reserved for making certain temporary uniqueness, the place the timestamp value drops monotonicity.

The remaining segment represents the IEEE 802 node price, signifying uniqueness across room.

When to Use UUID in MySQL

I know what you are thinking:

If UUIDs are distinctive globally, why really don’t we use them as the default major keys in databases tables? The remedy is both straightforward and not simple.

To begin, UUIDs are not native information sorts these as an INT, which you can established as principal vital and vehicle-increment as more facts gets included to the databases.

2nd, UUIDs have their downsides which may not be applicable in all situations.

Permit me to share a several conditions exactly where making use of UUIDs as key keys may well be applicable.

  1. 1 widespread state of affairs is wherever entire uniqueness is expected. Considering the fact that UUIDs are unique globally, they offer you a ideal solution for merging rows in databases when preserving the uniqueness.
  2. Safety – UUIDs do not expose any information connected to your data and are for that reason helpful when stability is a element. 2nd, they are created offline without having revealing any info about the technique.

The adhering to are some of the downsides of applying UUIDs in your databases.

  1. UUIDs are 6-bytes as opposed to integers which are 4-bytes. That indicates they will occupy additional storage for the very same amount of money of information as opposed to integers.
  2. If UUIDs are indexed, they may possibly bring about significant general performance fees and sluggish down the databases.
  3. Considering that UUIDs are random and unique, they can make the debugging course of action unnecessarily cumbersome.

UUID Capabilities

In MySQL 8. and later on, you can use numerous functions to counter some of the disadvantages introduced by UUIDs.

These capabilities are:

  1. UUID_TO_BIN – Converts UUID from VARCHAR to Binary which is additional productive for storing in databases
  2. BIN_TO_UUID – From Binary to VARCHAR
  3. IS_UUID – Returns Boolean real if arg is valid VARCHAR UUID. The reverse is true.

Primary MySQL UUID Types Usage

As mentioned earlier, to apply UUIDs in MySQL, we use the UUID() function. For instance, to crank out UUID, we do:

mysql> Select UUID()
| UUID()                               |
| f9eb97f2-a94b-11eb-advert80-089798bcc301 |
1 row in set (.01 sec)

Table with UUID

Allow us build a table with UUID values and see how we can put into action this sort of functionality. Look at the query below:

Create SCHEMA uuids
USE uuids
Make Desk validation
    id BINARY(16) Primary Vital
INSERT INTO validation(id)

When all the UUIDs are created, we can select them and convert them from Binary to string UUID values as shown in the question beneath:

Find BIN_TO_UUID(id) id FROM validation

Right here is the output:


There is not much to include about UUIDs in MySQL, but if you would like to find out much more about them, take into account examining out the MySQL resource:

Leave a Reply

Your email address will not be published. Required fields are marked *