postgresql 创建主键自增的三种方法

postgresql 有以下三种方法设置主键递增:

1.方法一

highgo=# create table test_a 
highgo-# ( 
highgo(#   id serial, 
highgo(#   name character varying(128), 
highgo(#   constraint pk_test_a_id primary key( id) 
highgo(# ); 
CREATE TABLE
highgo=# \d test_a
                                    Table "public.test_a"
 Column |          Type          | Collation | Nullable |              Default               
--------+------------------------+-----------+----------+------------------------------------
 id     | integer                |           | not null | nextval('test_a_id_seq'::regclass)
 name   | character varying(128) |           |          | 
Indexes:
    "pk_test_a_id" PRIMARY KEY, btree (id)

highgo=# insert into test_a(name) values('a');
INSERT 0 1
highgo=# insert into test_a(name) values('b');
INSERT 0 1
highgo=# select * from test_a;
 id | name 
----+------
  1 | a
  2 | b
(2 rows)

highgo=# insert into test_a(id,name) values(1,'c');
2022-11-15 17:54:01.015 CST [11510] ERROR:  duplicate key value violates unique constraint "pk_test_a_id"
2022-11-15 17:54:01.015 CST [11510] DETAIL:  Key (id)=(1) already exists.
2022-11-15 17:54:01.015 CST [11510] STATEMENT:  insert into test_a(id,name) values(1,'c');
ERROR:  duplicate key value violates unique constraint "pk_test_a_id"
DETAIL:  Key (id)=(1) already exists.
highgo=# 

2.方法二

highgo=# create table test_b 
highgo-# ( 
highgo(#   id serial PRIMARY KEY, 
highgo(#   name character varying(128) 
highgo(# ); 
CREATE TABLE
highgo=# \d test_b
                                    Table "public.test_b"
 Column |          Type          | Collation | Nullable |              Default               
--------+------------------------+-----------+----------+------------------------------------
 id     | integer                |           | not null | nextval('test_b_id_seq'::regclass)
 name   | character varying(128) |           |          | 
Indexes:
    "test_b_pkey" PRIMARY KEY, btree (id)
    
highgo=# insert into test_b(name) values('a');
INSERT 0 1
highgo=# insert into test_b(name) values('b');
INSERT 0 1
highgo=# select * from test_b;
 id | name 
----+------
  1 | a
  2 | b
(2 rows)

highgo=# insert into test_b(id,name) values(1,'c');
2022-11-15 17:56:25.780 CST [11510] ERROR:  duplicate key value violates unique constraint "test_b_pkey"
2022-11-15 17:56:25.780 CST [11510] DETAIL:  Key (id)=(1) already exists.
2022-11-15 17:56:25.780 CST [11510] STATEMENT:  insert into test_b(id,name) values(1,'c');
ERROR:  duplicate key value violates unique constraint "test_b_pkey"
DETAIL:  Key (id)=(1) already exists.
highgo=# 

3.方法三

highgo=# create table test_c 
highgo-# ( 
highgo(#   id integer PRIMARY KEY, 
highgo(#   name character varying(128) 
highgo(# );  
CREATE TABLE
highgo=# CREATE SEQUENCE test_c_id_seq 
highgo-#     START WITH 1 
highgo-#     INCREMENT BY 1 
highgo-#     NO MINVALUE 
highgo-#     NO MAXVALUE 
highgo-#     CACHE 1
highgo-#     OWNED BY test_c.id; 
CREATE SEQUENCE
highgo=# insert into test_c(id,name) values(nextval('test_c_id_seq'),'a');
INSERT 0 1
highgo=# insert into test_c(id,name) values(nextval('test_c_id_seq'),'b');
INSERT 0 1
highgo=#  select * from test_c;
 id | name 
----+------
  1 | a
  2 | b
(2 rows)

highgo=# insert into test_c(id,name) values('1','c');
2022-11-15 18:02:57.230 CST [11510] ERROR:  duplicate key value violates unique constraint "test_c_pkey"
2022-11-15 18:02:57.230 CST [11510] DETAIL:  Key (id)=(1) already exists.
2022-11-15 18:02:57.230 CST [11510] STATEMENT:  insert into test_c(id,name) values('1','c');
ERROR:  duplicate key value violates unique constraint "test_c_pkey"
DETAIL:  Key (id)=(1) already exists.
highgo=# \d test_c
                      Table "public.test_c"
 Column |          Type          | Collation | Nullable | Default 
--------+------------------------+-----------+----------+---------
 id     | integer                |           | not null | 
 name   | character varying(128) |           |          | 
Indexes:
    "test_c_pkey" PRIMARY KEY, btree (id)

highgo=# 

方法一和方法二只是写法不同,实质上主键都通过使用 serial 类型来实现的, 使用serial类型,PG会自动创建一个序列给主键用,当插入表数据时如果不指定ID,则ID会默认使用序列的NEXT值。

方法三是先创建一张表,再创建一个序列,并关联这个表的id列。这种写法似乎更符合人们的思维习惯,也便于管理,如果系统遇到sequence 性能问题时,便于调整 sequence 属性。

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值