在本教程中,我们将学习如何使用sql drop column
子句从现有表中删除一个或多个列。
有时,想要从现有表中删除一个或多个未使用的列。 为此,请使用alter table
,如下所示:
alter table table_name
drop column column_name1,
[drop column column_name2];
在上面语法中,
table_name
是包含要删除列的表名称。column_name1
,column_name2
是将要删除的列。mysql和postgresql支持上述语法。
oracle和sql server的语法略有不同:
alter table table_name
drop column
column_name1,
[column_name2];
以下语句为演示创建一个名为persons
的新表:
create table persons (
person_id int primary key,
first_name varchar(255) not null,
last_name varchar(255) not null,
date_of_birth date not null,
phone varchar(25),
email varchar(255)
);
2.1. 删除一列示例
以下语句从persons
表中删除email
列:
alter table persons
drop column email;
2.2. 删除多列示例
以下语句从persons
表中删除ate_of_birth
和phone
列:
alter table persons
drop column date_of_birth,
drop column phone;
上面语句适用于mysql和postgresql。对于oracle和sql server,需要使用以下语句:
alter table persons
drop column
date_of_birth,
phone;
在本教程中,您学习了如何使用sql drop column
语句从表中删除一个或多个列。