2022-08-26 19:07:59 +00:00
|
|
|
|
---
|
|
|
|
|
slug: /zh/sql-reference/functions/uuid-functions
|
|
|
|
|
---
|
2020-03-20 18:20:59 +00:00
|
|
|
|
# UUID函数 {#uuidhan-shu}
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
下面列出了所有UUID的相关函数
|
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
## generateuidv4 {#uuid-function-generate}
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
生成一个UUID([版本4](https://tools.ietf.org/html/rfc4122#section-4.4))。
|
|
|
|
|
|
2020-03-20 18:20:59 +00:00
|
|
|
|
``` sql
|
2019-05-05 17:38:05 +00:00
|
|
|
|
generateUUIDv4()
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**返回值**
|
|
|
|
|
|
|
|
|
|
UUID类型的值。
|
|
|
|
|
|
|
|
|
|
**使用示例**
|
|
|
|
|
|
|
|
|
|
此示例演示如何在表中创建UUID类型的列,并对其写入数据。
|
|
|
|
|
|
2023-01-11 13:43:51 +00:00
|
|
|
|
```sql
|
|
|
|
|
CREATE TABLE t_uuid (x UUID) ENGINE=TinyLog
|
|
|
|
|
INSERT INTO t_uuid SELECT generateUUIDv4()
|
|
|
|
|
SELECT * FROM t_uuid
|
|
|
|
|
```
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2023-01-11 14:59:23 +00:00
|
|
|
|
```response
|
2019-05-05 17:38:05 +00:00
|
|
|
|
┌────────────────────────────────────x─┐
|
|
|
|
|
│ f4bf890f-f9dc-4332-ad5c-0c18e73f28e9 │
|
|
|
|
|
└──────────────────────────────────────┘
|
|
|
|
|
```
|
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
## toUUID(x) {#touuid-x}
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
将String类型的值转换为UUID类型的值。
|
|
|
|
|
|
2020-03-20 18:20:59 +00:00
|
|
|
|
``` sql
|
2019-05-05 17:38:05 +00:00
|
|
|
|
toUUID(String)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**返回值**
|
|
|
|
|
|
|
|
|
|
UUID类型的值
|
|
|
|
|
|
|
|
|
|
**使用示例**
|
|
|
|
|
|
2023-01-11 13:43:51 +00:00
|
|
|
|
```sql
|
|
|
|
|
SELECT toUUID('61f0c404-5cb3-11e7-907b-a6006ad3dba0') AS uuid
|
|
|
|
|
```
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2023-01-11 14:59:23 +00:00
|
|
|
|
```response
|
2019-05-05 17:38:05 +00:00
|
|
|
|
┌─────────────────────────────────uuid─┐
|
|
|
|
|
│ 61f0c404-5cb3-11e7-907b-a6006ad3dba0 │
|
|
|
|
|
└──────────────────────────────────────┘
|
|
|
|
|
```
|
|
|
|
|
|
2020-03-20 18:20:59 +00:00
|
|
|
|
## UUIDStringToNum {#uuidstringtonum}
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-04-30 18:19:18 +00:00
|
|
|
|
接受一个String类型的值,其中包含36个字符且格式为`xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`,将其转换为UUID的数值并以[固定字符串(16)](../../sql-reference/functions/uuid-functions.md)将其返回。
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
``` sql
|
|
|
|
|
UUIDStringToNum(String)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**返回值**
|
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
固定字符串(16)
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
**使用示例**
|
|
|
|
|
|
|
|
|
|
``` sql
|
2023-01-11 13:43:51 +00:00
|
|
|
|
SELECT
|
2019-05-05 17:38:05 +00:00
|
|
|
|
'612f3c40-5d3b-217e-707b-6a546a3d7b29' AS uuid,
|
|
|
|
|
UUIDStringToNum(uuid) AS bytes
|
2023-01-11 13:43:51 +00:00
|
|
|
|
```
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2023-01-11 14:59:23 +00:00
|
|
|
|
```response
|
2019-05-05 17:38:05 +00:00
|
|
|
|
┌─uuid─────────────────────────────────┬─bytes────────────┐
|
|
|
|
|
│ 612f3c40-5d3b-217e-707b-6a546a3d7b29 │ a/<@];!~p{jTj={) │
|
|
|
|
|
└──────────────────────────────────────┴──────────────────┘
|
|
|
|
|
```
|
|
|
|
|
|
2020-03-20 18:20:59 +00:00
|
|
|
|
## UUIDNumToString {#uuidnumtostring}
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-04-30 18:19:18 +00:00
|
|
|
|
接受一个[固定字符串(16)](../../sql-reference/functions/uuid-functions.md)类型的值,返回其对应的String表现形式。
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
``` sql
|
|
|
|
|
UUIDNumToString(FixedString(16))
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**返回值**
|
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
字符串。
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
**使用示例**
|
|
|
|
|
|
|
|
|
|
``` sql
|
|
|
|
|
SELECT
|
|
|
|
|
'a/<@];!~p{jTj={)' AS bytes,
|
|
|
|
|
UUIDNumToString(toFixedString(bytes, 16)) AS uuid
|
2023-01-11 14:59:23 +00:00
|
|
|
|
```
|
|
|
|
|
```response
|
2019-05-05 17:38:05 +00:00
|
|
|
|
┌─bytes────────────┬─uuid─────────────────────────────────┐
|
|
|
|
|
│ a/<@];!~p{jTj={) │ 612f3c40-5d3b-217e-707b-6a546a3d7b29 │
|
|
|
|
|
└──────────────────┴──────────────────────────────────────┘
|
|
|
|
|
```
|
|
|
|
|
|
2020-03-20 18:20:59 +00:00
|
|
|
|
## 另请参阅 {#ling-qing-can-yue}
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-04-30 18:19:18 +00:00
|
|
|
|
- [dictgetuid](ext-dict-functions.md)
|