0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PostgreSQLでのsequenceの使い方

Last updated at Posted at 2022-06-17
../

PostgreSQLで、insert 時に毎回nextval()を使うのではなく、create table時にdefaultでnextval()を定義しておく方が効率的である。

insert 時に明示的にnextval()を使う:

create sequence auto_num;
create table dummy (seq_no integer primary key, name varchar(20));
insert into dummy (seq_no,name) values (nextval('auto_num'),'abc');
insert into dummy (seq_no,name) values (nextval('auto_num'),'123');

create table時にdefaultでnextval()を定義:

create sequence auto_num;
create table dummy (
  seq_no integer default nextval('auto_num') primary key, 
  name varchar(20)
);
insert into dummy (name) values ('abc');
insert into dummy (name) values ('123');
../
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?