如何在 SQL Server 中使用单个 ALTER TABLE 语句删除多个列?
我想编写一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
对于 SQL Server:
语法为
对于 MySQL:
或类似1:
1 单词
COLUMN
是可选并且可以省略,但RENAME COLUMN
除外(以区分列重命名操作和RENAME
表重命名操作)。更多信息此处。For SQL Server:
The syntax is
For MySQL:
or like this1:
1 The word
COLUMN
is optional and can be omitted, except forRENAME COLUMN
(to distinguish a column-renaming operation from theRENAME
table-renaming operation). More info here.总结
Oracle:
MS SQL Server:
MySQL
: postgresql.org/docs/current/static/sql-altertable.html" rel="noreferrer">PostgreSQL
请注意
,对于某些 DBMS,例如 MS SQL:
Summarizing
Oracle:
MS SQL Server:
MySQL:
PostgreSQL
Be aware
For some DBMS, such as MS SQL:
请注意,DROP COLUMN 不会物理删除数据,对于固定长度类型(int、numeric、float、datetime、uniqueidentifier 等),即使删除列后添加的记录也会消耗空间。要消除浪费的空间,请执行
ALTER TABLE ... REBUILD
。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
.这可能会晚一些,但请为访问此问题的新用户分享。
要删除多列,实际语法是,
因此对于每一列,您需要在 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
So for every column you need to specify "drop column" in Mysql 5.0.45.
Microsoft 指定的用于删除 ALTER 的列部分的语法语句是这样的
请注意,[,...n] 出现在列名之后和整个 drop 子句的末尾。这意味着有两种方法可以删除多列。您可以执行以下操作:
或
如果您想将列的删除与约束的删除结合起来,第二种语法很有用:
删除列时,SQL Sever 不会回收被删除的列占用的空间。对于内联存储在行中的数据类型(例如 int),它甚至可能占用在 alter 语句之后添加的新行上的空间。为了解决这个问题,您需要在表上创建聚集索引,或者重建聚集索引(如果已有聚集索引)。修改表后可以使用 REBUILD 命令重建索引。但请注意,在非常大的表上这可能会很慢。例如:
The Syntax as specified by Microsoft for the dropping a column part of an ALTER statement is this
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:
or this
This second syntax is useful if you want to combine the drop of a column with dropping a constraint:
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:
对于 MySQL(版本 5.6),您不能使用单个
drop
语句执行多列删除,而是使用多个drop
语句:顺便说一句,
drop;
是drop column
的简写,正如您从上面的drop c3
中看到的那样。For MySQL (ver 5.6), you cannot do multiple column drop with one single
drop
-statement but rather multipledrop
-statements:BTW,
drop <col_name>
is shorthanded fordrop column <col_name>
as you can see fromdrop c3
above.如果只是单列删除,则以下语法有效
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 worksALTER TABLE tablename DROP (column1, column2, column3......);
通用:
例如:
Generic:
E.g:
对于postgis来说是
alter table table01 drop columns col1, drop col2
for postgis is
alter table table01 drop columns col1, drop col2
这个查询将改变多列测试。
this query will alter the multiple column test it.
对于 MySQL 数据库。
或者您可以在同一行中更改时添加一些列:
for MySQL DB.
Or you can add some column while altering in the same line: