Reputation: 717
How do I delete a column from an existing table?
Upvotes: 51
Views: 77802
Reputation: 21
ALTER TABLE [Intake].[MER_SF_Opportunity_History] DROP COLUMN [[IntakeIsDeleted]]]
GO
Upvotes: 1
Reputation: 19406
This can also be done through the SSMS GUI.
I like this method because it warns you if there are any relationships on that column and can also automatically delete those as well. As PaxDiablo states, if there are relationships, they must be deleted first.
At this point, if there are any relationships that would also need to be deleted, it will ask you if you would like to delete those as well.
Upvotes: 5
Reputation: 881613
The command you're looking for is:
alter table tblName drop column columnName
where tblName
is the name of the table and columnName
is the name of the column, but there's a few things you may need to do first.
Keep in mind that the performance of this command may not necessarily be good. One option is to wait for a down-time period when you can be certain no-one will be accessing the database, rename the current table, then use create table
and insert into ... select from
to transfer the columns you don't want deleted.
One of the later releases of Oracle actually has a soft delete which can just marks a column as unused without removing it physically. It has the same effect since you can no longer reference it and there's a command along the lines of alter table ... drop unused columns
which is meant to be run in quiet time, which does the hard work of actually removing it physically.
This has the advantage of "disappearing" the columns immediately without dragging down database performance during busy times.
Upvotes: 105
Reputation: 10552
For large tables this can be very slow. It can often be a lot faster to create a new table, a duplicate of the old one but with the changes, and insert the data. Drop the old table and then rename the new table.
Upvotes: 3