pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Herouth Maoz <herouth@oumail.openu.ac.il>
To: pere@hungry.com
To: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] Change attribute type
Date: Sun, 19 Jul 1998 13:24:32 +0300
Message-ID: <l03110705b1d77616b4c8@[147.233.159.109]> (raw)
In-Reply-To: <199807161557.RAA28247@sleeper.games.no>

At 18:57 +0300 on 16/7/98, Petter Reinholdtsen wrote:


> I have a table created with this command:
>
>   create table test(data int4, changed timestamp);
>
> I want to have this table instead:
>
>   create table test(data int4, changed datetime);
>
> I have lots of entries in the table, and want too keep them when I
> change the date type.
>
> The simple method would be to change the type.  Is it possible?
>
> The hard method requires the possibility of removing an attribute.
>
>  1 change name of the attribute
>    'alter table test rename column changed to changedold;'
>  2 create new attribute
>    'alter table test add changed datetime;'
>  3 insert old data in new attribute
>    'update test set changed = changedold;'
>  4 remove the old attribute
>    ?
>
> Any clues?

Well, it seems to me the best way is to copy your table into a new one.

1. Rename the table using ALTER TABLE test RENAME to test_temp;

2. Copy the values over to a new table by:

   SELECT data, datetime( changed ) AS changed
   INTO TABLE test
   FROM test_temp;

3. DROP TABLE test_temp;

4. Create indices as needed on the new table.

Alternatively, if your table includes DEFAULT clauses, NOT NULL clauses
etc., your might want to replace step 2 above with the two steps:

2a. CREATE TABLE test ( ... new definition including DEFAULT etc. );

2b. Copy over the values using:

    INSERT INTO test ( data, changed )
    SELECT data, datetime( changed )
    FROM test_temp;

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma





view thread (2+ messages)

Message-ID: <l03110705b1d77616b4c8@[147.233.159.109]>
Permalink:  ../l03110705b1d77616b4c8@%5B147.233.159.109%5D/
Also on:    postgresql.org/message-id/l03110705b1d77616b4c8@[147.233.159.109]

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: herouth@oumail.openu.ac.il, pere@hungry.com
  Subject: Re: [SQL] Change attribute type
  In-Reply-To: <l03110705b1d77616b4c8@[147.233.159.109]>
  References: <199807161557.RAA28247@sleeper.games.no> <l03110705b1d77616b4c8@[147.233.159.109]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox