参考文献:
http://wandejun1012.iteye.com/blog/1902001
数据库:PostgreSQL
CREATE TABLE customers
(
customerid SERIAL primary key ,
companyname character varying,
contactname character varying,
phone character varying,
country character varying
)
在这里 customerid SERIAL 等价于:
customerid integer NOT NULL DEFAULT nextval('customers_customerid_seq'),
当你创建serial的时候,会自动生成一个从1开始,自增值为1的序列,序列的命名规则一般为表名_列名_seq,
当你insert的时候没有指定serial列的值,则默认的从序列中取出值,并且将序列的值自动加1
(链接:https://zhidao.baidu.com/question/257833247.html)
或者
create SEQUENCE product_id_seq
START WITH 1
INCREMENT by 1
NO MINVALUE
MAXVALUE 10000
CACHE 1;
alter table product ALTER COLUMN ID SET DEFAULT
nextval('product_id_seq');
查询表中有那些 sequence 正在找….
数据插入:
INSERT INTO product
VALUES
(
nextval('product_id_seq'),
'apple',
'00015',
15
);