Adding a new auto increment column to a table in postgres

postgresql postgres sql 2 weeks, 2 days ago
-- New versions of postgres -- adding as primary key ALTER TABLE my_schema.my_table ADD COLUMN id SERIAL PRIMARY KEY; --without primary key aslo you can use ALTER TABLE my_schema.my_table ADD COLUMN id SERIAL -- For old versions ALTER TABLE my_table ADD COLUMN id INTEGER; CREATE SEQUENCE my_id_seq OWNED BY my_table.id; ALTER TABLE my_table ALTER COLUMN id SET DEFAULT nextval('my_id_seq'); UPDATE my_table SET id = nextval('my_id_seq');
40
Posted By