Menambahkan kolom baru sebagai Primary Key ke sebuah tabel




ALTER TABLE functional ADD COLUMN id serial NOT NULL PRIMARY KEY;

atau

ALTER TABLE "public"."functional"   ADD COLUMN "id" INTEGER;
CREATE SEQUENCE "public"."functional_id_seq";
UPDATE functional SET id = nextval('"public"."functional_id_seq"');
ALTER TABLE "public"."functional"
  ALTER COLUMN "id" SET DEFAULT nextval('"public"."functional_id_seq"');
ALTER TABLE "public"."functional"
  ALTER COLUMN "id" SET NOT NULL;
ALTER TABLE "public"."functional" ADD UNIQUE ("id");
ALTER TABLE "public"."functional" DROP CONSTRAINT "functional_id_key" RESTRICT;
ALTER TABLE "public"."functional" ADD PRIMARY KEY ("id");

Referensi

  1. PostgreSQL - Add primary key to an existing Table, http://blog.enricostahn.com/2010/06/11/postgresql-add-primary-key-to-an-existing-table.html

Comments