ChatGPT解决这个技术问题 Extra ChatGPT

How to DROP multiple columns with a single ALTER TABLE statement in SQL Server?

I would like to write a single SQL command to drop multiple columns from a single table in one ALTER TABLE statement.

From MSDN's ALTER TABLE documentation...

DROP { [CONSTRAINT] constraint_name | COLUMN column_name } Specifies that constraint_name or column_name is removed from the table. DROP COLUMN is not allowed if the compatibility level is 65 or earlier. Multiple columns and constraints can be listed.

It says that mutliple columns can be listed in the the statement but the syntax doesn't show an optional comma or anything that would even hint at the syntax.

How should I write my SQL to drop multiple columns in one statement (if possible)?


i
informatik01

For SQL Server:

ALTER TABLE TableName
    DROP COLUMN Column1, Column2;

The syntax is

DROP { [ CONSTRAINT ] constraint_name | COLUMN column } [ ,...n ] 

For MySQL:

ALTER TABLE TableName
    DROP COLUMN Column1,
    DROP COLUMN Column2;

or like this1:

ALTER TABLE TableName
    DROP Column1,
    DROP Column2;

1 The word COLUMN is optional and can be omitted, except for RENAME COLUMN (to distinguish a column-renaming operation from the RENAME table-renaming operation). More info here.


@Gweebz - I assume that the error that you had was related to column usage. Column could be used in a constraint, index or another calculated column.
Precisely; the second column I had was used in an Index. I dropped that Index and my DROP statement worked perfectly after that.
@jeicam - That would have happened probably because, 1 of the columns you are trying to drop might be a primary key.
i
informatik01

Summarizing

Oracle:

ALTER TABLE table_name DROP (column_name1, column_name2);

MS SQL Server:

ALTER TABLE table_name DROP COLUMN column_name1, column_name2

MySQL:

ALTER TABLE table_name DROP column_name1, DROP column_name2;

PostgreSQL

ALTER TABLE table_name DROP COLUMN column_name1, DROP COLUMN column_name2;

Be aware

DROP COLUMN does not physically remove the data for some DBMS. E.g. for MS SQL. For fixed length types (int, numeric, float, datetime, uniqueidentifier etc) the space is consumed even for records added after the columns were dropped. To get rid of the wasted space do ALTER TABLE ... REBUILD.


For PostgreSQL you can even omit COLUMN keyword as in MySql.
You can also use the same format for MySQL as for MS SQL and Postgre SQL: ALTER TABLE table_name DROP COLUMN column_name1, column_name2;
R
Remus Rusanu
create table test (a int, b int , c int, d int);
alter table test drop column b, d;

Be aware that DROP COLUMN does not physically remove the data, and for fixed length types (int, numeric, float, datetime, uniqueidentifier etc) the space is consumed even for records added after the columns were dropped. To get rid of the wasted space do ALTER TABLE ... REBUILD.


+1 for the note about table sizes and requiring a REBUILD. Can you provide a link to documentation as a reference?
Right there in the ALTER TABLE: msdn.microsoft.com/en-us/library/ms190273.aspx
I talked to my DBA about doing a REBUILD and he said that if the table had a clustered Index (which mine did), I could just run ALTER INDEX PK_IndexName ON TableName REBUILD and it would reclaim the space. This is somewhat described here: msdn.microsoft.com/en-us/library/ms189858.aspx under the section "Rebuilding an Index".
Anyone can shed some light on WHY would this be implemented like so? If I dropped column I expect indexes, data and everything built on that column to be gone. Leaving space wasted without as much as warning seems like bad practice, or am I missing something?
M
MANISH ZOPE

This may be late, but sharing it for the new users visiting this question. To drop multiple columns actual syntax is

alter table tablename drop column col1, drop column col2 , drop column col3 ....

So for every column you need to specify "drop column" in Mysql 5.0.45.


This is incorrect syntax. Use: ALTER TABLE Test DROP COLUMN c1, c2;
@Graeme the syntax is correct, i have tested it and it works, if i use: ALTER TABLE Test DROP COLUMN c1, c2; Mysql throws an error. ...near c2
@TomBikiNurse The question has an MSDN reference hence it was assumed that the SQL in question is related to MS SQL Server and not MySQL
@SuryaPratap The question does mention 'my SQL' in the last paragraph.
@Eternal21 the reference you mention is How should I write my SQL to drop multiple columns in one statement (if possible)? the context of the sentence suggests that the "my" referred to the author and "SQL" referred the language.
M
Martin Brown

The Syntax as specified by Microsoft for the dropping a column part of an ALTER statement is this

 DROP 
 {
     [ CONSTRAINT ] 
     { 
          constraint_name 
          [ WITH 
           ( <drop_clustered_constraint_option> [ ,...n ] ) 
          ] 
      } [ ,...n ]
      | COLUMN 
      {
          column_name 
      } [ ,...n ]
 } [ ,...n ]

Notice that the [,...n] appears after both the column name and at the end of the whole drop clause. What this means is that there are two ways to delete multiple columns. You can either do this:

ALTER TABLE TableName
    DROP COLUMN Column1, Column2, Column3

or this

ALTER TABLE TableName
    DROP 
        COLUMN Column1,
        COLUMN Column2,
        COLUMN Column3

This second syntax is useful if you want to combine the drop of a column with dropping a constraint:

ALTER TBALE TableName
    DROP
        CONSTRAINT DF_TableName_Column1,
        COLUMN Column1;

When dropping columns SQL Sever does not reclaim the space taken up by the columns dropped. For data types that are stored inline in the rows (int for example) it may even take up space on the new rows added after the alter statement. To get around this you need to create a clustered index on the table or rebuild the clustered index if it already has one. Rebuilding the index can be done with a REBUILD command after modifying the table. But be warned this can be slow on very big tables. For example:

ALTER TABLE Test
    REBUILD;

P
Patrick Kostjens

For MySQL (ver 5.6), you cannot do multiple column drop with one single drop-statement but rather multiple drop-statements:

mysql> alter table test2 drop column (c1,c2,c3);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(c1,c2,c3)' at line 1
mysql> alter table test2 drop column c1,c2,c3;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'c2,c3' at line 1
mysql> alter table test2 drop column c1, drop column c2, drop c3;
Query OK, 0 rows affected (0.64 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> 

BTW, drop <col_name> is shorthanded for drop column <col_name> as you can see from drop c3 above.


B
Bhavesh G

If it is just single column to delete the below syntax works

ALTER TABLE tablename DROP COLUMN column1;

For deleting multiple columns, using the DROP COLUMN doesnot work, the below syntax works

ALTER TABLE tablename DROP (column1, column2, column3......);


C
Chetter Hummin
alter table tablename drop (column1, column2, column3......);

This is incorrect syntax. Use: ALTER TABLE Test DROP COLUMN c1, c2;
C
Chiragkumar Thakar

Generic:

ALTER TABLE table_name 
DROP COLUMN column1,column2,column3;

E.g:

ALTER TABLE Student 
DROP COLUMN Name, Number, City;

J
Joselin Ceron

for postgis is

alter table table01 drop columns col1, drop col2


tried with sql, but it does not work. Got this in log: 12:58:04 ALTER TABLE ORDERS DROP COLUMNS user_name, user_contact_number, user_address Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'user_name, user_contact_number, user_address' at line 2 0.0060 sec
A
Alexis Pigeon

this query will alter the multiple column test it.

create table test(a int,B int,C int);

alter table test drop(a,B);

This is incorrect syntax. Use: ALTER TABLE Test DROP COLUMN c1, c2;
f
fredmaggiowski
ALTER table table_name Drop column column1, Drop column column2,Drop column column3;

for MySQL DB.

Or you can add some column while altering in the same line:

ALTER table table_name Drop column column1, ADD column column2 AFTER column7;