M
M
Maybe_V2016-05-19 21:26:53
PostgreSQL
Maybe_V, 2016-05-19 21:26:53

Is it possible to change column type to serial?

There is a table with filled data:

CREATE TABLE "Виплата"
(
  old integer NOT NULL,
  "Код_договору" integer,
  "Дата" timestamp(0) without time zone,
  "Сума_виплат" text,
  "Оплата" boolean,
  "Код" integer,
  CONSTRAINT "Виплата_pkey" PRIMARY KEY (old)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE "Виплата"
  OWNER TO postgres;

Be sure to change the column type "Code" to serial! Ordinary
ALTER: ALTER TABLE "Виплата" ALTER COLUMN "Код" type serial;
I can't do it, because serial is not a type. ERROR: type "serial" does not exist !But it is very necessary to do this.
In what way it is possible to implement it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Aleksey Ratnikov, 2016-05-21
@prokopov-vi

SERIAL in PostreSQL is not a data type , but an alias for creating a SEQUENCE and DEFAULT VALUE for a column from it. Accordingly, you can manually do the described actions:

CREATE SEQUENCE tablename_colname_seq;
alter TABLE "Виплата" alter column "Код" type integer NOT NULL DEFAULT nextval('tablename_colname_seq');
ALTER SEQUENCE tablename_colname_seq OWNED BY "Виплата"."Код";

R
res2001, 2016-05-20
@res2001

Only the following sequence of actions comes to mind: adding a new field with the serial type, changing the values ​​in it from Code, deleting Code, renaming the saerial field to Code.
Why are the field names in Russian? Eye hurts :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question