当前位置: 首页 > news >正文

PostgreSql 设置自增字段

一、概述

  序列类型是 PostgreSQL 特有的创建一个自增列的方法。包含 smallserial、serial和 bigserial 类型,它们不是真正的类型,只是为了创建唯一标识符列而存在的方便符号。其本质也是调用的序列,序列详情可参考:《PostgreSql 序列》

二、使用示例

postgres=# create table test(id serial primary key,name varchar);
CREATE TABLE
postgres=# \d testTable "public.test"Column |       Type        | Collation | Nullable |             Default
--------+-------------------+-----------+----------+----------------------------------id     | integer           |           | not null | nextval('test_id_seq'::regclass)name   | character varying |           |          |
Indexes:"test_pkey" PRIMARY KEY, btree (id)
postgres=# insert into test(name) values('zhao'),('qian'),('sun');
INSERT 0 3
postgres=# select * from test;id | name
----+------1 | zhao2 | qian3 | sun
(3 rows)--上述等价于
postgres=# create table test(id int primary key,name varchar);
CREATE TABLE
postgres=# create sequence test_id_seq start with 1 increment by 1 no minvalue no maxvalue cache 1;
CREATE SEQUENCE
postgres=# alter table test alter column id set default nextval('test_id_seq');
ALTER TABLE
postgres=# \d testTable "public.test"Column |       Type        | Collation | Nullable |             Default
--------+-------------------+-----------+----------+----------------------------------id     | integer           |           | not null | nextval('test_id_seq'::regclass)name   | character varying |           |          |
Indexes:"test_pkey" PRIMARY KEY, btree (id)postgres=# insert into test(name) values('zhao'),('qian'),('sun');
INSERT 0 3
postgres=# select * from test;id | name
----+------1 | zhao2 | qian3 | sun
(3 rows)
http://www.lryc.cn/news/258781.html

相关文章:

  • 什么是泊松图像混合
  • OpenAI 承认 ChatGPT 最近确实变懒,承诺修复问题
  • 创作活动(四十九)———低代码:美味膳食或垃圾食品?
  • 【DL-TensorFlow遇错】TensorFlow中遇错合集
  • pymysql代替mysqlclient,解决mysqlclient因版本不兼容无法安装成功而无法连接mysql的问题
  • uni-app 设置当前page界面进入直接变为横屏模式
  • Mysql的多表联合查询
  • Linux上使用Python的requests库进行HTTP请求
  • 图像处理领域的应用
  • MySQL笔记-第18章_MySQL8其它新特性
  • C语言—每日选择题—Day46
  • flex布局,换行的元素上下设置间距
  • 【智能家居】八、监控摄像采集、人脸识别比对进行开门功能点
  • golang的文件操作
  • 数据库版本管理框架-Flyway(从入门到精通)
  • 外网访问内网服务器使用教程
  • C# Dictionary 利用 ContainsValue 查询指定值是否已经存在
  • 招不到人?用C语言采集系统批量采集简历
  • HXDSP2441-Demo板
  • 静态路由的原理和配置
  • Ubuntu20.04降低linux版本到5.4.0-26-generic
  • C++ 类型萃取
  • 【JVM从入门到实战】(四)类的生命周期
  • 2023年度美食关键词-葱油花卷
  • 「Verilog学习笔记」简易秒表
  • 《每天一个Linux命令》 -- (12) file命令
  • 如何使用ArcGIS Pro制作类似CAD的尺寸注记
  • Go语言bufio包的使用
  • 计算机网络之IP篇
  • Java中JDK类库常用的6种设计模式