Received: from ciderbox.stanaway.net (ciderbox.stanaway.net [170.76.50.98]) by postgresql.org (Postfix) with ESMTP id 0B23B475A2E for ; Wed, 10 Jul 2002 19:39:49 -0400 (EDT) Received: by ciderbox.stanaway.net (Postfix, from userid 1000) id 3D16C939A9; Wed, 10 Jul 2002 18:39:49 -0500 (CDT) Subject: Re: alter table modify column? From: David Stanaway To: Kevin Brannen Cc: pgsql-admin@postgresql.org In-Reply-To: <3D2CC064.7010108@nurseamerica.net> References: <3D2CC064.7010108@nurseamerica.net> Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="=-gBirJWyQ54ZP2hrh15tS" X-Mailer: Ximian Evolution 1.0.7 Date: 10 Jul 2002 18:39:48 -0500 Message-Id: <1026344388.1473.24.camel@ciderbox> Mime-Version: 1.0 X-Archive-Number: 200207/170 X-Sequence-Number: 4856 --=-gBirJWyQ54ZP2hrh15tS Content-Type: text/plain Content-Transfer-Encoding: quoted-printable Try this: 1) Add a new column of type DECIMAL(5,2) 2) update the table setting the new column with the value in the old column. 3) rename the old column to something not used 4) rename the new colum to the name of the old column 5) wait for a later version where you can drop column, or just leave the data there and remove any constraints on it. Maybe update the values to NULL if there is alot of data. scratch=3D# BEGIN; BEGIN scratch=3D# create table foo (decval varchar, id serial); CREATE scratch=3D# insert into foo values('0.4'); INSERT 153524 1 scratch=3D# insert into foo values('1.45'); INSERT 153525 1 scratch=3D# insert into foo values('-4.2'); INSERT 153526 1 scratch=3D# insert into foo values('55'); INSERT 153527 1 ALTER TABLE foo ADD COLUMN decval_new decimal(5,2); ALTER -- Note: You probably want to find a better way to do this cast. -- I couldn't find one quickly that would do the job except this. -- The problem being that the conversion is imprecise. scratch=3D# update foo set decval_new =3D decval::text::float::decimal; UPDATE 4 scratch=3D# SELECT * from foo; decval | id | decval_new=20 --------+----+------------ 0.4 | 1 | 0.40 1.45 | 2 | 1.45 -4.2 | 3 | -4.20 55 | 4 | 55.00 (4 rows) scratch=3D# ALTER TABLE foo RENAME COLUMN decval TO decval_old; ALTER TABLE foo RENAME COLUMN decval_new TO decval; ALTER ALTER scratch=3D# SELECT * from foo; decval_old | id | decval=20 ------------+----+-------- 0.4 | 1 | 0.40 1.45 | 2 | 1.45 -4.2 | 3 | -4.20 55 | 4 | 55.00 (4 rows) scratch=3D# END; COMMIT On Wed, 2002-07-10 at 18:16, Kevin Brannen wrote: > As we transition our database, I'm finding the need to change columns=20 > types. I'm also finding this to be a major PITA. >=20 > I have a column that's a something like: >=20 > create table stuff > ( > ... > rate varchar(7), > ... > ); >=20 > However that doesn't allow math-like functions (e.g. avg), so it's gotta= =20 > be changed. I'd like to do something on the order of: >=20 > alter table stuff modify rate DECIMAL(5,2); >=20 > but that doesn't work. I've scoured the docs and can't seem to find=20 > anything other than: >=20 > 1. create new table with changed columns, > 2. copy data over, > 3. remove old table, > 4. rename new table to old table >=20 > Please tell me there's an easier way! And of course how. :-) >=20 > Also, I can't find a "alter table drop column NAME" to remove a column=20 > either. >=20 > TIA, > Kevin >=20 >=20 > ---------------------------(end of broadcast)--------------------------- > TIP 3: if posting/reading through Usenet, please send an appropriate > subscribe-nomail command to majordomo@postgresql.org so that your > message can get through to the mailing list cleanly >=20 --=-gBirJWyQ54ZP2hrh15tS Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.7 (GNU/Linux) iQEXAwUAPSzFxL6yIDFGeSAyFAIo9QQApCRCFPcyjsCbK/M+4W3MGWxkehFFyu8z c3gJwC9ORPvJCJArglqgY7oYwU/IUDDXmjWkoMIYYwWPEnGDJYbb0BCo/hCtPVve mwAAqKO4YOfmPTxbD1+4TGY7sypoQoGHriqltBa9jUyr6JUp0/9Gr8v0ZUd6XlSz OzUcmu0QXzUD/2Yr9wIfqpmyPt0LMPEZUH1WwVGB4zMP03f4AZmsFGQlyN9q6EjJ TyIi2iL8X/hREIk/kTSK1fv97j4daG3uMsl+HYQh70eGDCn+4TBxDiGfeOP79lZi qV1A6DGfx6k6xbEnenphSWMZf00zXfXpPFRXjZdleCEJP6jyNC0ytdgf =F1Px -----END PGP SIGNATURE----- --=-gBirJWyQ54ZP2hrh15tS--