问题
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?
回答1:
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, andCHANGEclauses in a singleALTER TABLEstatement, separated by commas. This is a MySQL extension to standard SQL, which permits only one of each clause perALTER TABLEstatement.
回答2:
Use ALTER TABLE with DROP COLUMN to drop a column from a table, and CHANGE or MODIFY to change a column.
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;
回答3:
To delete column use this,
ALTER TABLE `tbl_Country` DROP `your_col`
回答4:
You can use
alter table <tblname> drop column <colname>
回答5:
ALTER TABLE `tablename` DROP `columnname`;
Or,
ALTER TABLE `tablename` DROP COLUMN `columnname`;
回答6:
Use ALTER:
ALTER TABLE `tbl_Country` DROP COLUMN `column_name`;
回答7:
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:
ALTER TABLE tbl_Country DROP COLUMN IsDeleted, ALGORITHM=INPLACE, LOCK=NONE;
回答8:
ALTER TABLE tbl_Country DROP columnName;
来源:https://stackoverflow.com/questions/13968494/how-to-delete-a-column-from-a-table-in-mysql