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 documenta
Select column to drop:
ALTER TABLE my_table
DROP column_to_be_deleted;
However, some databases (including SQLite) have limited support, and you may have to create a new table and migrate the data over:
https://www.sqlite.org/faq.html#q11
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.
for postgis is
alter table table01 drop columns col1, drop col2
ALTER TABLE TableName
DROP COLUMN Column1, Column2;
The syntax is
DROP { [ CONSTRAINT ] constraint_name | COLUMN column } [ ,...n ]
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.
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;
this query will alter the multiple column test it.
create table test(a int,B int,C int);
alter table test drop(a,B);