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

发布于 2024-11-15 21:31:41 字数 453 浏览 3 评论 0原文

我想编写一个 SQL 命令来在一个 ALTER TABLE 语句中从单个表中删除多个列。

来自 MSDN 的 ALTER TABLE 文档.. 。

DROP { [CONSTRAINT] 约束名称 |列 列名 }

指定从表中删除constraint_name 或column_name。如果兼容级别为 65 或更早,则不允许使用 DROP COLUMN。可以列出多个列和约束。

它表示可以在语句中列出多个列,但语法不显示可选的逗号或任何暗示语法的内容。

我应该如何编写 SQL 以在一个语句中删除多个列(如果可能)?

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)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

木落 2024-11-22 21:31:41

对于 SQL Server:

ALTER TABLE TableName
    DROP COLUMN Column1, Column2;

语法为

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

对于 MySQL:

ALTER TABLE TableName
    DROP COLUMN Column1,
    DROP COLUMN Column2;

或类似1

ALTER TABLE TableName
    DROP Column1,
    DROP Column2;

1 单词 COLUMN可选并且可以省略,但 RENAME COLUMN 除外(以区分列重命名操作和 RENAME 表重命名操作)。更多信息此处

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.

水染的天色ゝ 2024-11-22 21:31:41

总结

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.org/docs/current/static/sql-altertable.html" rel="noreferrer">PostgreSQL

ALTER TABLE table_name DROP COLUMN column_name1, DROP COLUMN column_name2;

请注意

,对于某些 DBMS,例如 MS SQL:

DROP COLUMN 不会物理删除数据,对于固定长度类型(int、numeric、float、datetime、uniqueidentifier 等),即使删除列后添加的记录也会消耗空间。要消除浪费的空间,请执行ALTER TABLE ... REBUILD。 (来源

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

For some DBMS, such as MS SQL:

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. (source)

三五鸿雁 2024-11-22 21:31:41
create table test (a int, b int , c int, d int);
alter table test drop column b, d;

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

决绝 2024-11-22 21:31:41

这可能会晚一些,但请为访问此问题的新用户分享。
要删除多列,实际语法是,

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

因此对于每一列,您需要在 Mysql 5.0.45 中指定“drop column”。

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.

没企图 2024-11-22 21:31:41

Microsoft 指定的用于删除 ALTER 的列部分的语法语句是这样的

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

请注意,[,...n] 出现在列名之后和整个 drop 子句的末尾。这意味着有两种方法可以删除多列。您可以执行以下操作:

ALTER TABLE TableName
    DROP COLUMN Column1, Column2, Column3

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

如果您想将列的删除与约束的删除结合起来,第二种语法很有用:

ALTER TBALE TableName
    DROP
        CONSTRAINT DF_TableName_Column1,
        COLUMN Column1;

删除列时,SQL Sever 不会回收被删除的列占用的空间。对于内联存储在行中的数据类型(例如 int),它甚至可能占用在 alter 语句之后添加的新行上的空间。为了解决这个问题,您需要在表上创建聚集索引,或者重建聚集索引(如果已有聚集索引)。修改表后可以使用 REBUILD 命令重建索引。但请注意,在非常大的表上这可能会很慢。例如:

ALTER TABLE Test
    REBUILD;

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;
层林尽染 2024-11-22 21:31:41

对于 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> 

顺便说一句,drop;drop column 的简写,正如您从上面的 drop c3 中看到的那样。

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.

故人爱我别走 2024-11-22 21:31:41

如果只是单列删除,则以下语法有效

ALTER TABLE tablename DROP COLUMN column1;

对于删除多列,使用DROP COLUMN不起作用,以下语法有效

< code>ALTER TABLE 表名 DROP(column1,column2,column3……);

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......);

走野 2024-11-22 21:31:41

通用:

ALTER TABLE table_name 
DROP COLUMN column1,column2,column3;

例如:

ALTER TABLE Student 
DROP COLUMN Name, Number, City;

Generic:

ALTER TABLE table_name 
DROP COLUMN column1,column2,column3;

E.g:

ALTER TABLE Student 
DROP COLUMN Name, Number, City;
绅士风度i 2024-11-22 21:31:41
alter table tablename drop (column1, column2, column3......);
alter table tablename drop (column1, column2, column3......);
装纯掩盖桑 2024-11-22 21:31:41

对于postgis来说是

alter table table01 drop columns col1, drop col2

for postgis is

alter table table01 drop columns col1, drop col2

浅紫色的梦幻 2024-11-22 21:31:41

这个查询将改变多列测试。

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

alter table test drop(a,B);

this query will alter the multiple column test it.

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

alter table test drop(a,B);
放飞的风筝 2024-11-22 21:31:41
ALTER table table_name Drop column column1, Drop column column2,Drop column column3;

对于 MySQL 数据库。

或者您可以在同一行中更改时添加一些列:

ALTER table table_name Drop column column1, ADD column column2 AFTER column7;
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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文