PostgreSQL 9.3:如何在表中插入大写的UUID [英] PostgreSQL 9.3: How to insert upper case UUID into table

查看:398
本文介绍了PostgreSQL 9.3:如何在表中插入大写的UUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下表,其中只有1列 id ,其类型为 UUID

I have the following table with only 1 column that is id which is of type UUID.

uuidtest

create table uuidtest
(
id uuid
);

插入

我已经通过使用 uuid_generate_v4()生成了uuid,并且还用大写
并将其插入到表中。

I have generated uuid by using uuid_generate_v4() and also done it upper case and inserting it into the table.

Try1:

insert into uuidtest values('{4B36AFC8-5205-49C1-AF16-4DC6F96DB982}');

Try2:

insert into uuidtest values('4B36AFC8-5205-49C1-AF16-4DC6F96DB982');

现在看到结果:

select * from uuidtest;

输出量

id
uuid
-------------------------------------
4b36afc8-5205-49c1-af16-4dc6f96db982
4b36afc8-5205-49c1-af16-4dc6f96db982

我也尝试过进行更新:

update uuidtest
set id = upper(id::text)::uuid

但是:输出保持不变:

id
uuid
-------------------------------------
4b36afc8-5205-49c1-af16-4dc6f96db982
4b36afc8-5205-49c1-af16-4dc6f96db982    


推荐答案

首先,您请注意,在PostgreSQL中,UUID是一个128位数字,并以此形式存储(而不是包含36个字符的字符串!)。我们正在讨论的是这类数据的输入和输出形式。

First of all, you should note that in PostgreSQL a UUID is a 128 bit number, and is stored as such (not as a string with 36 characters!). What we are discussing are the input and output forms of this type of data.

PostgreSQL手册说:


uuid存储的数据类型RFC 4122,ISO / IEC 9834-8:2005和相关标准定义的通用唯一标识符(UUID)。

The data type uuid stores Universally Unique Identifiers (UUID) as defined by RFC 4122, ISO/IEC 9834-8:2005, and related standards.

...

PostgreSQL还接受以下替代形式输入:使用大写数字,用大括号括起来的标准格式,省略一些或所有连字符,在任何四位数组之后添加连字符。

PostgreSQL also accepts the following alternative forms for input: use of upper-case digits, the standard format surrounded by braces, omitting some or all hyphens, adding a hyphen after any group of four digits.

...

输出始终为标准格式。

RFC 4122 说:


每个字段都被视为整数,并且其值打印为
零填充的十六进制数字字符串,其中最高有效
位数首位。十六进制值 a至 f输出为
小写字符,输入时不区分大小写。

Each field is treated as an integer and has its value printed as a zero-filled hexadecimal digit string with the most significant digit first. The hexadecimal values "a" through "f" are output as lower case characters and are case insensitive on input.

换句话说,为了遵循该标准,该值始终以小写形式显示。

In other words, to follow the standard, the value is always printed as lower case.

当然,如果要生成结果用大写字母,您可以使用类似的东西:

Of course, if you want to produce the result with upper case letters, you could use something like:

select upper(id::TEXT) from uuidtest;

这篇关于PostgreSQL 9.3:如何在表中插入大写的UUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆