简体   繁体   English

如何在 SQL 服务器中使用单个 ALTER TABLE 语句删除多个列?

[英]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.我想编写一个 SQL 命令以在一个ALTER TABLE语句中从单个表中删除多个列。

From MSDN's ALTER TABLE documentation ...来自MSDN 的 ALTER TABLE 文档...

 DROP { [CONSTRAINT] constraint_name | COLUMN column_name }

Specifies that constraint_name or column_name is removed from the table.指定从表中删除 constraint_name 或 column_name。 DROP COLUMN is not allowed if the compatibility level is 65 or earlier.如果兼容级别为 65 或更早,则不允许使用 DROP COLUMN。 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)?我应该如何编写我的 SQL 以在一个语句中删除多个列(如果可能)?

For SQL Server:对于 SQL 服务器:

ALTER TABLE TableName
    DROP COLUMN Column1, Column2;

The syntax is语法是

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

For MySQL:对于 MySQL:

ALTER TABLE TableName
    DROP COLUMN Column1,
    DROP COLUMN Column2;

or like this 1 :或者像这样1

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). 1 COLUMN一词是可选的,可以省略,但RENAME COLUMN除外(以区分列重命名操作和RENAME表重命名操作)。 More info here .更多信息在这里

Summarizing总结

Oracle : Oracle :

ALTER TABLE table_name DROP (column_name1, column_name2);

MS SQL Server : 女士 SQL 服务器

ALTER TABLE table_name DROP COLUMN column_name1, column_name2

MySQL : MySQL :

ALTER TABLE table_name DROP column_name1, DROP column_name2;

PostgreSQL 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. DROP COLUMN不会物理删除某些 DBMS 的数据。 Eg 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.例如,对于 MS SQL。对于固定长度类型( intnumericfloatdatetimeuniqueidentifier等),即使在删除列后添加的记录也会占用空间。 To get rid of the wasted space do ALTER TABLE... REBUILD .要摆脱浪费的空间,请执行ALTER TABLE... REBUILD

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.请注意,DROP COLUMN 不会物理删除数据,对于固定长度类型(int、numeric、float、datetime、uniqueidentifier 等),即使删除列后添加的记录也会占用空间。 To get rid of the wasted space do ALTER TABLE... REBUILD .要摆脱浪费的空间,请执行ALTER TABLE... REBUILD

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.因此,对于每一列,您需要在 Mysql 5.0.45 中指定“删除列”。

The Syntax as specified by Microsoft for the dropping a column part of an ALTER statement is this Microsoft 指定的用于删除ALTER语句的列部分的语法是这样的

 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.请注意,[,...n] 出现在列名之后和整个 drop 子句的末尾。 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.删除列时 SQL 服务器不回收删除的列占用的空间。 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.对于行内存储的数据类型(例如 int),它甚至可能占用 alter 语句后添加的新行的空间。 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.可以在修改表后使用 REBUILD 命令重建索引。 But be warned this can be slow on very big tables.但请注意,这在非常大的桌子上可能会很慢。 For example:例如:

ALTER TABLE Test
    REBUILD;

For MySQL (ver 5.6), you cannot do multiple column drop with one single drop -statement but rather multiple drop -statements:对于 MySQL(5.6 版),您不能使用单个drop语句执行多列删除,而是使用多个drop语句:

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.顺便说一句, drop <col_name>drop column <col_name>的简写,正如您从上面的drop c3中看到的那样。

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对于删除多列,使用DROP COLUMN不起作用,下面的语法有效

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

Generic:通用的:

ALTER TABLE table_name 
DROP COLUMN column1,column2,column3;

Eg:例如:

ALTER TABLE Student 
DROP COLUMN Name, Number, City;
alter table tablename drop (column1, column2, column3......);

for postgis is对于 postgis 是

alter table table01 drop columns col1, drop col2

this query will alter the multiple column test it.此查询将更改多列测试它。

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

alter table test drop(a,B);
ALTER table table_name Drop column column1, Drop column column2,Drop column column3;

for MySQL DB.对于 MySQL 数据库。

Or you can add some column while altering in the same line:或者您可以在同一行中更改时添加一些列:

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

I would like to write a single SQL command to drop multiple columns from a single table in one ALTER TABLE statement.我想编写一个 SQL 命令以在一个ALTER TABLE语句中从单个表中删除多个列。

From MSDN's ALTER TABLE documentation ...MSDN 的 ALTER TABLE 文档...

 DROP { [CONSTRAINT] constraint_name | COLUMN column_name }

Specifies that constraint_name or column_name is removed from the table.指定从表中删除 constraint_name 或 column_name。 DROP COLUMN is not allowed if the compatibility level is 65 or earlier.如果兼容级别为 65 或更早,则不允许使用 DROP COLUMN。 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)?我应该如何编写 SQL 以在一个语句中删除多个列(如果可能)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM