Re: Changing a table column datatype

From: Oliver Elphick <olly(at)lfix(dot)co(dot)uk>
To: marshall(at)perilith(dot)com
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Changing a table column datatype
Date: 2003-05-13 04:59:32
Message-ID: 1052801972.18710.33.camel@linda.lfix.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Tue, 2003-05-13 at 01:18, marshall(at)perilith(dot)com wrote:
> Hello,
>
> This has been asked before, but the responses weren't clear enough
> for me to understand.
>
> I have a table `mytable' with an attribute `mycol' of datatype char(4) and
> I'd like to change it to varchar(20). Mycol is populated by two values -
> NULLs and four element chars. What I've tried thusfar:
>
> db=> ALTER TABLE mytable ADD COLUMN mycol_new VARCHAR(20);
> ALTER TABLE
> db=> INSERT INTO mytable (mycol_new) SELECT mycol FROM mytable;
> ERROR: ExecInsert: Fail to add null value in not null attribute fqdn
>
> The `fqdn' attribute is another column in mytable.
>
> So is it the case that INSERT doesn't like inserting NULL values? Is
> there another way to do this?

INSERT inserts new rows and all constraints for the row must be
satisfied; use UPDATE to alter existing rows.

UPDATE mytable SET mycol_new = mycol;

(no WHERE clause, because you want to change every row).
--
Oliver Elphick Oliver(dot)Elphick(at)lfix(dot)co(dot)uk
Isle of Wight, UK http://www.lfix.co.uk/oliver
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839 932A 614D 4C34 3E1D 0C1C
========================================
"Watch ye and pray, lest ye enter into temptation. The
spirit truly is ready, but the flesh is weak."
Mark 14:38

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2003-05-13 05:08:13 Re: Changing a table column datatype
Previous Message Terence Ng 2003-05-13 03:12:41 Re: Multilingual database