Re: Is there any way to delete a column

From: Hadley Willan <hadley(dot)willan(at)deeperdesign(dot)co(dot)nz>
To: shreedhar <shreedhar(at)lucidindia(dot)net>
Cc: Postgresql General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Is there any way to delete a column
Date: 2003-03-10 20:22:02
Message-ID: 1047327722.1357.34.camel@atlas.sol.deeper.co.nz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Also I recommend that you explicitly add constraint names for columns
with requirements. That way, you can cleanly drop columns with minimum
fuss.

e.g Normally people just do this

create table foo(
id BIGINT DEFAULT( nextval('sequence_foo_gen') ) NOT NULL,
other_table BIGINT NOT NULL,
some_text TEXT CHECK( some_text <> '' ) NOT NULL,
FOREIGN KEY( other_table ) REFERENCES other_table( id ) ON UPDATE
CASCADE ON DELETE CASCADE,
PRIMARY KEY( id )
);

Problem with this is that I've found it difficult to drop the foreign
key or even the checks. So, if you do the following....

create table foo(
id BIGINT DEFAULT( nextval('sequence_foo_gen') ) NOT NULL,
other_table BIGINT NOT NULL,
some_text TEXT NOT NULL,

CONSTRAINT chk_foo_some_txt CHECK( some_text <> '' ),
CONSTRAINT fk_foo_other_table FOREIGN KEY( other_table ) REFERENCES
other_table( id ) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT pk_foo PRIMARY KEY( id )
);

You now have ultimate control over the tables constraints. Therefore, if
you wanted to drop the column some_text. It's simply a matter of ALTER
TABLE foo DROP CONSTRAINT chk_foo_some_text; THEN ALTER TABLE foo DROP
COLUMN some_text.

Easy.

Hope this helps.

H

On Mon, 2003-03-10 at 22:13, shreedhar wrote:
> To delete a column from table , I am following the method as per
> postgresl documents.
>
> 1) creating emptable with same structure and moving all records.
> 2) creating another table with out column which you have remove.
> 3) Creating all constraints.
> 4) moving all data from temp table to new table and removing temp
> table.
>
> Is there any way to delete a column, other than above method
>
> Regards,
> Sreedhar
>
>
--
Hadley Willan > Systems Development > Deeper Design Limited. +64(7)377-3328
hadley(dot)willan(at)deeperdesign(dot)co(dot)nz > www.deeperdesign.com > +64(21)-28-41-463
Level 1, 4 Tamamutu St, PO Box 90, TAUPO 2730, New Zealand.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2003-03-10 20:27:54 Re: Not Enough Connections
Previous Message Tom Lane 2003-03-10 20:22:01 Re: Problems with pg_dump