The Basics Of PostgreSQL UUID Data Type
Summary: in this tutorial, you will learn about the PostgreSQL UUID data type and how to generate UUID values using a supplied module.
Introduction to PostgreSQL UUID type
UUID stands for Universal Unique Identifier defined by RFC 4122 and
other related standards. A UUID value is 128-bit quantity generated by
an algorithm that make it unique in the known universe using the same
algorithm. The following shows some examples of the UUID values:
As you can see, a UUID is a sequence of 32 digits of hexadecimal digits represented in groups separated by hyphens.
Because
of its uniqueness feature, you often found UUID in the distributed
systems because it guarantees a better uniqueness than the
SERIAL
data type which generates only unique values within a single database.
To stores UUID values in the PostgreSQL database, you use the UUID data type.
Generating UUID values
PostgreSQL
allows you store and compare UUID values but it does not include
functions for generating the UUID values in its core.
Instead, it relies on the third-party modules that provide specific algorithms to generate UUIDs. For example the
uuid-ossp
module provides some handy functions that implement standard algorithms for generating UUIDs.
To install the
uuid-ossp
module, you use the CREATE EXTENSION
statement as follows:
The
IF NOT EXISTS
clause allows you to avoid re-installing the module.
To generate the UUID values based on the combination of computer’s MAC address, current timestamp, and a random value, you use the
uuid_generate_v1()
function:
The function generated the following a UUID value:
If you want to generate a UUID value solely based on random numbers, you can use the
uuid_generate_v4()
function. For example:
For more information on the functions for UUID generation, check it out the uuid-ossp module documentation.
Creating a table with UUID column
We will create a table whose primary key is UUID data type. In addition, the values of the primary key column will be generated automatically using the
uuid_generate_v4()
function.
First, create the
contacts
table using the following statement:
In this statement, the data type of the
contact_id
column is UUID
. The contact_id column has a default values provided by the uuid_generate_v4()
function, therefore, whenever you insert new row without specifying the value for the contact_id column
, PostgreSQL will call the uuid_generate_v4()
function to generate the value for it.
Second, insert some data into the
contacts
table:
Third, query all rows in the contacts table using the following
SELECT
statement:
As you can see the
contact_id
column has been populated by the UUID values generated by the uuid_generate_v4()
function.
In this tutorial, you have learned how to use PostgreSQL UUID data type and how to generate UUID values using the
Source Article: http://www.postgresqltutorial.com/postgresql-uuid/
uuid-ossp
module.Source Article: http://www.postgresqltutorial.com/postgresql-uuid/
Comments
Post a Comment