Postgres: Add Sequential PKEY
So you forgot to add a primary key? Or you did have one, but now that field isn't going to be unique? Fear not.
alter table sad_table drop constraint if exists sad_table_pkey;
alter table sad_table add column id int;
create sequence sad_table_id_seq;
alter table sad_table alter column id set default nextval('sad_table_id_seq');
update sad_table set id = nextval('sad_table_id_seq');
alter table sad_table add primary key (id);
I won't tell.
Written by Cathy Wise
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Sql
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#