在PostgreSQL中生成自动递增主键的可接受方法是什么?

前端之家收集整理的这篇文章主要介绍了在PostgreSQL中生成自动递增主键的可接受方法是什么?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
没有序列和触发器有没有简单的方法呢?我有一般的sql技能,我想用pl / sql(Postgresql)的行业标准方法.我基本上是从 Spring Security转换这个例子:
create table group_members (
    id bigint generated by default as identity(start with 0) primary key,username varchar(50) not null,group_id bigint not null,constraint fk_group_members_group foreign key(group_id) references groups(id));

我到目前为止

CREATE TABLE auth_group_members (
    id NUMBER,username VARCHAR(50) NOT NULL,group_id NUMBER NOT NULL,CONSTRAINT "FK_AuthGroupMembers" FOREIGN KEY(group_id) REFERENCES auth_groups(id)
);
标准的方式是使用 serial or bigserial

The data types serial and bigserial are not true types,but merely a notational convenience for creating unique identifier columns (similar to the AUTO_INCREMENT property supported by some other databases).
[…]
Thus,we have created an integer column and arranged for its default values to be assigned from a sequence generator.

所以你会创建一个这样的表:

CREATE TABLE auth_group_members (
    id bigserial primary key,CONSTRAINT "FK_AuthGroupMembers" FOREIGN KEY(group_id) REFERENCES auth_groups(id)
);

串行和大型类型确实创建了幕后的序列,但是您不必直接使用序列.

原文链接:https://www.f2er.com/postgresql/191833.html

猜你在找的Postgre SQL相关文章