如何从MySQL中的表中删除列

本文翻译自:How to delete a column from a table in MySQL

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


#1楼

参考:https://stackoom.com/question/wbqI/如何从MySQL中的表中删除列


#2楼

ALTER TABLE tbl_Country DROP COLUMN IsDeleted;

Here's a working example. 这是一个有效的例子。

Note that the COLUMN keyword is optional, as MySQL will accept just DROP IsDeleted . 请注意, COLUMN关键字是可选的,因为MySQL只接受DROP IsDeleted Also, to drop multiple columns, you have to separate them by commas and include the DROP for each one. 另外,要删除多个列,必须用逗号分隔它们,并为每个列包含DROP

ALTER TABLE tbl_Country
  DROP COLUMN IsDeleted,
  DROP COLUMN CountryName;

This allows you to DROP , ADD and ALTER multiple columns on the same table in the one statement. 这允许您在一个语句中的同一个表上DROPADDALTER多个列。 From the MySQL reference manual : MySQL参考手册

You can issue multiple ADD , ALTER , DROP , and CHANGE clauses in a single ALTER TABLE statement, separated by commas. 您可以在单个ALTER TABLE语句中发出多个ADDALTERDROPCHANGE子句,以逗号分隔。 This is a MySQL extension to standard SQL, which permits only one of each clause per ALTER TABLE statement. 这是标准SQL的MySQL扩展,它只允许每个ALTER TABLE语句中的一个子句。


#3楼

要删除列,请使用此项,

ALTER TABLE `tbl_Country` DROP `your_col`

#4楼

ALTER TABLE tbl_Country DROP columnName;

#5楼

使用ALTER

ALTER TABLE `tbl_Country` DROP COLUMN `column_name`;

#6楼

ALTER TABLE `tablename` DROP `columnname`;

要么,

ALTER TABLE `tablename` DROP COLUMN `columnname`;