Second, insert some data into the contacts table: INSERT INTO contacts language: SQL (Structured Query Language) ( sql ) 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. In this statement, the data type of the contact_id column is UUID. In addition, the values of the primary key column will be generated automatically using the uuid_generate_v4() function.įirst, create the contacts table using the following statement: CREATE TABLE contacts (Ĭontact_id uuid DEFAULT uuid_generate_v4 (), We will create a table whose primary key is UUID data type. For example: SELECT uuid_generate_v4() įor more information on the functions for UUID generation, check it out the uuid-ossp module documentation. If you want to generate a UUID value solely based on random numbers, you can use the uuid_generate_v4() function. The function generated the following a UUID value: uuid_generate_v1 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: SELECT uuid_generate_v1() The IF NOT EXISTS clause allows you to avoid re-installing the module. To install the uuid-ossp module, you use the CREATE EXTENSION statement as follows: CREATE EXTENSION IF NOT EXISTS "uuid-ossp" For example the uuid-ossp module provides some handy functions that implement standard algorithms for generating UUIDs. Instead, it relies on the third-party modules that provide specific algorithms to generate UUIDs. PostgreSQL allows you store and compare UUID values but it does not include functions for generating the UUID values in its core. To stores UUID values in the PostgreSQL database, you use the UUID data type. The following shows some examples of the UUID values: 40e6215d-b5c6-4896-987c-f30f3678f608Ĭode language: SQL (Structured Query Language) ( sql )Īs you can see, a UUID is a sequence of 32 digits of hexadecimal digits represented in groups separated by hyphens.īecause 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. A UUID value is 128-bit quantity generated by an algorithm that make it unique in the known universe using the same algorithm. UUID stands for Universal Unique Identifier defined by RFC 4122 and other related standards. Summary: in this tutorial, you will learn about the PostgreSQL UUID data type and how to generate UUID values using a supplied module.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |