uuid-ossp:唯一标识符
uuid-ossp 扩展可以用来生成一个 UUID。
🌐 The uuid-ossp extension can be used to generate a UUID.
概览 #
🌐 Overview
UUID 是一个“通用唯一标识符”,在实际应用中,它是唯一的。这使它们特别适合作为主键。有时它也被称为 GUID,代表“全局唯一标识符”。
🌐 A UUID is a "Universally Unique Identifier" and it is, for practical purposes, unique.
This makes them particularly well suited as Primary Keys. It is occasionally referred to as a GUID, which stands for "Globally Unique Identifier".
启用扩展 #
🌐 Enable the extension
注意:
目前 uuid-ossp 扩展默认已启用,不能关闭。
- 在仪表板中转到数据库页面。
- 点击侧边栏的 扩展。
- 搜索
uuid-ossp并启用这个扩展。
uuid#
🌐 The uuid type
一旦扩展启用,你现在就可以访问 uuid 类型了。
🌐 Once the extension is enabled, you now have access to a uuid type.
uuid_generate_v1()#
根据电脑的MAC地址、当前时间戳和一个随机值生成一个UUID。
🌐 Creates a UUID value based on the combination of computer’s MAC address, current timestamp, and a random value.
UUIDv1 会泄露可识别的细节,这可能让它不适合某些对安全敏感的应用。
🌐 UUIDv1 leaks identifiable details, which might make it unsuitable for certain security-sensitive applications.
uuid_generate_v4()#
仅基于随机数创建 UUID 值。你也可以使用 Postgres 内置的 gen_random_uuid() 函数来生成 UUIDv4。
🌐 Creates UUID values based solely on random numbers. You can also use Postgres's built-in gen_random_uuid() function to generate a UUIDv4.
示例 #
🌐 Examples
在查询中 #
🌐 Within a query
1select uuid_generate_v4();作为主键 #
🌐 As a primary key
在表格中自动创建一个唯一的随机ID:
🌐 Automatically create a unique, random ID in a table:
1create table contacts (2 id uuid default uuid_generate_v4(),3 first_name text,4 last_name text,5 primary key (id)6);资源 #
🌐 Resources