当前位置:网站首页>数据库-玩转数据-Pgsql 使用UUID做主键
数据库-玩转数据-Pgsql 使用UUID做主键
2022-06-29 22:35:00 【人猿宇宙】
一、使用uuid-ossp
1、安装 uuid-ossp 文件:
在postgresql数据库中,是不能直接使用UUID函数的,需要手动安装 uuid 的相关文件,才能应用UUID相关的函数.
2、调用UUID的相应函数:
在安装了 uuid-ossp 文件后,通过 select 语句 调用UUID的相应函数即可获取到随机id;
检查postgresql是否支持uuid自动生成函数
select uuid_generate_v4();
若查询报错,创建扩展
解决方法:
1.安装 uuid-ossp 文件,在sql编辑器中编写并执行该语句:
create extension "uuid-ossp" ;
2.调用UUID的相应函数,:
select uuid_generate_v4() 或 select uuid_generate_v1()` 皆可.
3、创建表,主键添加uuid生成函数
CREATE TABLE "public"."t_uuid" (
"id" varchar(36) COLLATE "pg_catalog"."default" NOT NULL DEFAULT uuid_generate_v4(),
"name" varchar(40) COLLATE "pg_catalog"."default",
CONSTRAINT "t_uuid_pkey" PRIMARY KEY ("id")
);
4、对于已经建好的表,更改主键字段设置
ALTER TABLE "public"."t_uuid" ALTER COLUMN "id" SET DEFAULT uuid_generate_v4();
二、定义序列,再定义主键调用增长函数
1、定义序列
CREATE SEQUENCE "public"."t_student_id_seq"
INCREMENT 1
MINVALUE 1
MAXVALUE 9999999999
START 1
CACHE 1;
2、创建表,主键添加增长函数
CREATE TABLE "public"."t_student" (
"id" int4 NOT NULL DEFAULT nextval('t_student_id_seq'::regclass),
"name" varchar(40) COLLATE "pg_catalog"."default",
CONSTRAINT "t_student_pkey" PRIMARY KEY ("id")
);
3、对于已建好的表,更改主键字段设置
ALTER TABLE "public"."t_student" ALTER COLUMN "id" SET DEFAULT
nextval('t_student_id_seq'::regclass);
方法二:将主键字段定义为serial4类型
CREATE TABLE "public"."t_student_id_seq" (
"id" serial4,
"name" varchar(40),
PRIMARY KEY ("id")
);
查看DDL,会发现方法一和方法二结构是一致的,不同的是方法一定义的序列在表删除时,不会被清除。
边栏推荐
- 地方/园区如何做好产业分析?
- PhpSpreadsheet读写Excel文件
- 还天天熬夜加班做报表?其实你根本不懂如何高效做报表
- R & D test time ratio, bug data analysis
- MySQL lock common knowledge points & summary of interview questions
- An in-depth analysis of the election mechanism in kubernetes
- 深入解析kubernetes controller-runtime
- 从零实现深度学习框架——RNN从理论到实战【实战】
- 合宙AIR32F103CBT6开发板上手报告
- AI scene Storage Optimization: yunzhisheng supercomputing platform storage practice based on juicefs
猜你喜欢

Kr-gcn: an interpretable recommendation system based on knowledge aware reasoning

在线文本数字识别列表求和工具

Number theory - division and blocking

VS2013如何让编写的程序在其它电脑上面也能运行

还天天熬夜加班做报表?其实你根本不懂如何高效做报表
![The server quickly sets up the alist integrated network disk website [pagoda panel one click deployment of alist]](/img/96/3e634c173c96082881286ba402a067.png)
The server quickly sets up the alist integrated network disk website [pagoda panel one click deployment of alist]

Low code, end-to-end, one hour to build IOT sample scenarios, and the sound network released lingfalcon Internet of things cloud platform

What if MySQL fails to store emoticons

Gnawing down the big bone - sorting (I)

从零实现深度学习框架——LSTM从理论到实战【理论】
随机推荐
R & D test time ratio, bug data analysis
How to use filters in jfinal to monitor Druid for SQL execution?
Weekly Postgres world news 2022w25
nrm详解
缓冲流练习
5-1系统漏洞扫描
深入解析kubernetes中的选举机制
What if MySQL fails to store emoticons
Steady! The best posture for thousands of microservices to access Zadig (helm chart)
Detailed description of gaussdb (DWS) complex and diverse resource load management methods
leetcode:91. Decoding method [DFS + memorization]
字节云数据库未来方向的探索与实践
低代码、端到端,一小时构建IoT示例场景,声网发布灵隼物联网云平台
Live broadcast platform development, enter the visual area to execute animation, dynamic effects and add style class names
开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
Does rapid software delivery really need to be at the cost of security?
C language tutorial – -6 loop statement
5-2Web应用程序漏洞扫描
Error: c2665: "qmessagebox:: critical": none of the four overloads can convert all parameter types
js函数相关的复习