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

后端 未结 13 934
星月不相逢
星月不相逢 2020-12-22 18:17

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

相关标签:
13条回答
  • 2020-12-22 18:52

    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

    0 讨论(0)
  • 2020-12-22 18:58

    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.

    0 讨论(0)
  • 2020-12-22 18:58

    for postgis is

    alter table table01 drop columns col1, drop col2

    0 讨论(0)
  • 2020-12-22 19:00

    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.

    0 讨论(0)
  • 2020-12-22 19:03
    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;
    
    0 讨论(0)
  • 2020-12-22 19:04

    this query will alter the multiple column test it.

    create table test(a int,B int,C int);
    
    alter table test drop(a,B);
    
    0 讨论(0)
提交回复
热议问题