Need to Add a column to the existing table
-
Hi, How can I add a new column in middle/(at any position) of the existing table? How can I do that in Postgres? Any help would be appreciated.
Selva
hi plz make use of Sql Help hint: you can achieve this using ALTER TABLE Command ALTER TABLE TABLENAME ADD COLUMNNAME DATATYPE(SIZE) -- modified at 6:11 Tuesday 13th November, 2007
-
hi plz make use of Sql Help hint: you can achieve this using ALTER TABLE Command ALTER TABLE TABLENAME ADD COLUMNNAME DATATYPE(SIZE) -- modified at 6:11 Tuesday 13th November, 2007
-
Thanks for the reply. But it will add the new column at the last. But i want it to get added at the middle. Is there any way to achieve this.
Selva
Column ordering is not important within a SQL server database - why do you need to do this.
-
Thanks for the reply. But it will add the new column at the last. But i want it to get added at the middle. Is there any way to achieve this.
Selva
SelvaKr wrote:
But i want it to get added at the middle. Is there any way to achieve this.
If it was me I would start by questioning the requirement, because it suggests that someone either doesn't know how databases work and/or are using it incorrectly for some other purpose. But excluding that you would have to 1. Create temporary table. 2. Copy all of the data to the temp table. 3. Delete the original table. 4. Re-create with the new column 5. Copy all of the data back to the newly created table. 6. Delete the temp table. Obviously you will need to deal with foreign keys appropriately. And it is likely that that the database can not be active (not processing work) while this is going on.