在只有一个主键字段的表中插入行 [英] Insert row into table with only a primary key field

查看:11
本文介绍了在只有一个主键字段的表中插入行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到处搜索告诉我试试这个:

insert into Plan default values;

但我得到:

null value in column "id" violates not-null constraint

或此:

insert into Plan (id) values (null);

但我得到了同样的东西。

这些例子是针对SQL的:PostgreSQL有什么不同吗?

表结构:

  create table Plan (
    id int4 not null,
    primary key (id)
); 

推荐答案

您应该创建一个sequence并将缺省值设置为nextval,如下所示:

create sequence plan_sequence
start 100
increment 1
;

  create table plan (
    id int4 not null default nextval('plan_sequence'),
    primary key (id)
); 

序列提供自动递增的值。用于主键等

这篇关于在只有一个主键字段的表中插入行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆