ALTER TABLE hr_leave ALTER COLUMN id DEFAULT (select Max(id)+2)
How can I write this query with a prober way to alter primary key default value equal max id +2
ALTER TABLE hr_leave ALTER COLUMN id DEFAULT (select Max(id)+2)
How can I write this query with a prober way to alter primary key default value equal max id +2
First create a SEQUENCE:
CREATE SEQUENCE myseq START WITH 1 INCREMENT BY 2;
In case table hr_leave already contains records and you wish to change the initial value of your new sequence to the max id value of hr_leave do the following:
SELECT setval('myseq', (SELECT max(id) FROM hr_leave));
.. then finally change the column id to use the sequence myseq:
ALTER TABLE hr_leave ALTER COLUMN id SET DEFAULT nextval('myseq');
Demo: db<>fiddle