给定使用以下方法创建的表:
CREATE TABLE tbl_Country
(
CountryId INT NOT NULL AUTO_INCREMENT,
IsDeleted bit,
PRIMARY KEY (CountryId)
)
如何删除IsDeleted列?
给定使用以下方法创建的表:
CREATE TABLE tbl_Country
(
CountryId INT NOT NULL AUTO_INCREMENT,
IsDeleted bit,
PRIMARY KEY (CountryId)
)
如何删除IsDeleted列?
当前回答
ALTER TABLE `tablename` DROP `columnname`;
Or,
ALTER TABLE `tablename` DROP COLUMN `columnname`;
其他回答
值得一提的是,MySQL 8.0.23及以上版本支持不可见列
CREATE TABLE tbl_Country(
CountryId INT NOT NULL AUTO_INCREMENT,
IsDeleted bit,
PRIMARY KEY (CountryId)
);
INSERT INTO tbl_Country VALUES (1, 1), (2,0);
ALTER TABLE tbl_Country ALTER COLUMN IsDeleted SET INVISIBLE;
SELECT * FROM tbl_Country;
CountryId
1
2
ALTER TABLE tbl_Country DROP COLUMN IsDeleted;
db < > fiddle演示
当需要暂时“隐藏”一列,然后才能安全地删除它时(如重做相应的应用程序/报告等),它可能很有用。
你可以使用
alter table <tblname> drop column <colname>
删除单个列:
ALTER TABLE `table1` DROP `column1`;
删除多个列。
ALTER TABLE `table1`
DROP `column1`,
DROP `column2`,
DROP `column3`;
使用改变:
ALTER TABLE `tbl_Country` DROP COLUMN `column_name`;
使用ALTER TABLE和DROP COLUMN从表中删除列,使用CHANGE或MODIFY更改列。
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;