如何从 MySQL 中的表中删除列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13968494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:51:48  来源:igfitidea点击:

How to delete a column from a table in MySQL

mysqlddlalter-table

提问by raji

Given the table created using:

鉴于使用创建的表:

CREATE TABLE tbl_Country
(
  CountryId INT NOT NULL AUTO_INCREMENT,
  IsDeleted bit,
  PRIMARY KEY (CountryId) 
)

How can I delete the column IsDeleted?

如何删除列IsDeleted

回答by Cynical

ALTER TABLE tbl_Country DROP COLUMN IsDeleted;

Here'sa working example.

这是一个工作示例。

Note that the COLUMNkeyword is optional, as MySQL will accept just DROP IsDeleted. Also, to drop multiple columns, you have to separate them by commas and include the DROPfor each one.

请注意,COLUMN关键字是可选的,因为 MySQL 只接受DROP IsDeleted. 此外,要删除多列,您必须用逗号分隔它们并DROP为每一列包含 。

ALTER TABLE tbl_Country
  DROP COLUMN IsDeleted,
  DROP COLUMN CountryName;

This allows you to DROP, ADDand ALTERmultiple columns on the same table in the one statement. From the MySQL reference manual:

这允许您DROPADDALTER在一个声明中同一个表的多个列。从MySQL 参考手册

You can issue multiple ADD, ALTER, DROP, and CHANGEclauses in a single ALTER TABLEstatement, separated by commas. This is a MySQL extension to standard SQL, which permits only one of each clause per ALTER TABLEstatement.

您可以在单个语句中发出多个ADDALTERDROPCHANGE子句,并ALTER TABLE用逗号分隔。这是标准 SQL 的 MySQL 扩展,每个ALTER TABLE语句只允许每个子句中的一个。

回答by Saharsh Shah

Use ALTER TABLEwith DROP COLUMNto drop a column from a table, and CHANGEor MODIFYto change a column.

使用ALTER TABLEwithDROP COLUMN从表中删除列,CHANGEMODIFY更改列。

ALTER TABLE tbl_Country DROP COLUMN IsDeleted;
ALTER TABLE tbl_Country MODIFY IsDeleted tinyint(1) NOT NULL;
ALTER TABLE tbl_Country CHANGE IsDeleted IsDeleted tinyint(1) NOT NULL;

回答by echo_Me

To delete column use this,

要删除列使用这个,

ALTER TABLE `tbl_Country` DROP `your_col`

回答by Kapil gopinath

You can use

您可以使用

alter table <tblname> drop column <colname>

回答by Avinash Nair

ALTER TABLE `tablename` DROP `columnname`;

Or,

或者,

ALTER TABLE `tablename` DROP COLUMN `columnname`;

回答by A. Colonna

If you are running MySQL 5.6 onwards, you can make this operation online, allowing other sessions to read and write to your table while the operation is been performed:

如果您运行的是 MySQL 5.6 及更高版本,您可以在线进行此操作,允许其他会话在执行操作时读取和写入您的表:

ALTER TABLE tbl_Country DROP COLUMN IsDeleted, ALGORITHM=INPLACE, LOCK=NONE;

回答by Lo Juego

Use ALTER:

使用ALTER

ALTER TABLE `tbl_Country` DROP COLUMN `column_name`;

回答by Sterling Archer

ALTER TABLE tbl_Country DROP columnName;