postgreSQL让主键自增

1.建表时创建

1
2
3
4
5
6
7
8
CREATE TABLE test
(
test_id SERIAL primary key ,
test_name character varying,
contactname character varying,
phone character varying,
country character varying
)

2.在已建表的情况下创建

1
2
3
4
5
6
7
8
CREATE SEQUENCE test_id_seq  
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;

alter table test alter column id set default nextval('test_id_seq');