主键一般设置为Integer类型,并且自动增长,起始值为1,增量为1;有两种方法:
法一:在建表时
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201223113609762.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzE5NTMx,size_16,color_FFFFFF,t_70#pic_center)
nextval(‘表名_主键_seq’::regclass)
法二:如果表已经建好
![在这里插入图片描述](https://img-blog.csdnimg.cn/2020122311362933.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzE5NTMx,size_16,color_FFFFFF,t_70#pic_center)
CREATE SEQUENCE 表名_主键_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
alter table 表名 alter column id set default nextval('表名_主键_seq');
//修改主键起始值:
alter sequence if exists 表名_主键_seq restart with 3 cache 1;
PostgreSQL 9中文手册:http://www.postgres.cn/docs/9.3/functions-sequence.html