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 ?


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 . Also, to drop multiple columns, you have to separate them by commas and include the DROP for each one.

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. From the MySQL reference manual:

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


使用带有DROP COLUMN ALTER TABLE从表格中删除一列,然后使用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;

To delete columns from table.

ALTER TABLE tbl_Country DROP COLUMN IsDeleted1, DROP COLUMN IsDeleted2;

Or without word 'COLUMN'

ALTER TABLE tbl_Country DROP IsDeleted1, DROP IsDeleted2;
链接地址: http://www.djcxy.com/p/68632.html

上一篇: 在数据库中存储JSON与为每个密钥创建一个新列

下一篇: 如何从MySQL中的表中删除列