加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-13 16:12:46 所属栏目:百科 来源:网络整理
导读:没有序列和触发器有没有简单的方法呢?我有一般的SQL技能,我想用pl / sql(PostgreSQL)的行业标准方法.我基本上是从 Spring Security转换这个例子: create table group_members ( id bigint generated by default as identity(start with 0) primary key,use
没有序列和触发器有没有简单的方法呢?我有一般的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)
);

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读