FUN WITH LINUX

postgresql: using auto-incrementing keys

21 June 2015

Sometimes it’s nice to have an auto-incrementing value in our database so that we don’t have to generate a unique id(for example). In postgresql we can achieve this using sequences:

CREATE SEQUENCE key_seq;
CREATE TABLE sometable (
    key_id INTEGER NOT NULL DEFAULT nextval('key_seq')
);
ALTER SEQUENCE key_seq OWNED BY sometable.key_id;
[ Sysadmin  Database  Postgresql  ]
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 Unported License.

Copyright 2015-present Hoti