如何在将新记录插入postgresql表时调用序列?

时间:2023-01-29 14:00:30

How to invoke postgresql sequence while inserting new row into a table?

如何在将新行插入表中时调用postgresql序列?

I want to do something like this

我想做这样的事情

insert into biz_term(
  biz_term_id, 
  biz_term_name, 
  ) 
values(SELECT nextval(idsequence)',
'temp'


);

How to do this any idea? I want to do it because when I am trying to insert new record into biz_term table then sequence -idsequence is not getting invoked directly..any solution?

怎么做这个想法?我想这样做,因为当我试图在biz_term表中插入新记录时,序列-idsequence不会被直接调用。任何解决方案?

1 个解决方案

#1


67  

You got it almost. You don't need the SELECT in there:

你几乎得到了它。你不需要那里的SELECT:

insert into biz_term(
  biz_term_id, 
  biz_term_name, 
) 
values(
 nextval('idsequence'),
 'temp'
);

Any reasons you did not specify the biz_term_id as serial (or bigserial) which handles that automatically for you?

您没有将biz_term_id指定为串行(或bigserial)的任何原因自动为您处理?

#1


67  

You got it almost. You don't need the SELECT in there:

你几乎得到了它。你不需要那里的SELECT:

insert into biz_term(
  biz_term_id, 
  biz_term_name, 
) 
values(
 nextval('idsequence'),
 'temp'
);

Any reasons you did not specify the biz_term_id as serial (or bigserial) which handles that automatically for you?

您没有将biz_term_id指定为串行(或bigserial)的任何原因自动为您处理?