Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting columns in MS SQL Server

How do I delete a column from an existing table?

like image 662
Roshan Avatar asked Apr 16 '09 10:04

Roshan


People also ask

How do I remove a column from a SQL query?

First, write ALTER TABLE , followed by the name of the table you want to change (in our example, product ). Next add the DROP COLUMN clause, followed by the name of the column you want to remove (in our example, description ). This removes the column from the table, including any data stored in it.

How do I remove two columns in SQL?

Delete Columns Using SSMS Open SSMS and connect to the SQL Server instance. In Object explorer, expand the database and the Tables folder. Locate the table and expand the columns folder to display the column names. Right-click on the column name which you want to delete and click delete.

Which SQL command can be used to delete columns from a table?

We can use Alter table command to remove a column as well. The syntax is simple to use.

How do I remove a column from an existing table?

Syntax. The syntax to drop a column in a table in MySQL (using the ALTER TABLE statement) is: ALTER TABLE table_name DROP COLUMN column_name; table_name.


2 Answers

ALTER TABLE XXX DROP COLUMN YYY;

like image 36
Joakim Backman Avatar answered Sep 20 '22 15:09

Joakim Backman


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.

  • If there are any foreign key references to the column, you'll need to get rid of them first.
  • If there's an index using that column, you'll need to either get rid of it or adjust it to not use that column.

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.

like image 116
paxdiablo Avatar answered Sep 19 '22 15:09

paxdiablo