当前位置:网站首页>数据库-玩转数据-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,会发现方法一和方法二结构是一致的,不同的是方法一定义的序列在表删除时,不会被清除。
边栏推荐
- 一键式文件共享软件Jirafeau
- 还天天熬夜加班做报表?其实你根本不懂如何高效做报表
- error: C2665: “QMessageBox::critical”: 4 个重载中没有一个可以转换所有参数类型
- 在线文本数字识别列表求和工具
- Mysql database: the difference between drop, truncate and delete
- One click file sharing software jirafeau
- leetcode:91. Decoding method [DFS + memorization]
- Phpspreadsheet reading and writing Excel files
- leetcode 416. Partition equal subset sum partition equal subset sum (medium)
- 合宙AIR32F103CBT6开发板上手报告
猜你喜欢

微博系统中”微博评论“的高性能高可用计算架构

5 - 1 Analyse de vulnérabilité du système

写论文工具:LaTex在线网站

Basic use of Nacos configuration center

Can you be a coder if you don't learn English well? Stop worrying and learn it first

Digital tracking analysis of insurance services in the first quarter of 2022

服务器快速搭建AList集成网盘网站【宝塔面板一键部署AList】

#第三天

Ansible automatic operation and maintenance

Steady! The best posture for thousands of microservices to access Zadig (helm chart)
随机推荐
中国数据库崛起,阿里云李飞飞:中国云数据库多种主流技术创新已领先国外
The details of industry are all made by money and time
微博系统中”微博评论“的高性能高可用计算架构
MySQL lock common knowledge points & summary of interview questions
uniapp复制内容到剪贴板
Mysql database: read write separation
Qdomdocument and qdomnode are used in QT to read XML
2022年第一季度保险服务数字化跟踪分析
直播平台开发,进入可视区域执行动画、动效、添加样式类名
Cloud native enthusiast weekly: cool collection of grafana monitoring panels
Nacos-配置中心基本使用
nrm详解
深入解析kubernetes controller-runtime
Phpspreadsheet reading and writing Excel files
Free PDF to word software sharing, these software must know!
《天天数学》连载54:二月二十三日
论文阅读《Large-Scale Direct SLAM with Stereo Cameras》
Optional类的高级使用
[从零开始学习FPGA编程-51]:高阶篇 - 基于IP核的FPGA开发- 什么是FPGA IP核(软核、固核、硬核)与学习方法
把数组排成最小的数_数组中的逆序对(归并统计法)_数字在升序数组中出现的次数_丑数(剑指offer)