解释DBMS中delete命令的用法
Delete命令是一种数据操作命令,用于从表中删除记录。所有记录可以一次性删除,或者可以根据条件删除一组记录。
从表中删除特定行
要删除表中的特定行,我们需要指定where条件。根据该条件,该特定行将从表中删除。
语法如下:
delete from tablename where condition
例如:
Delete from employee where name=’sneha’;
示例
下面是一个从表中删除特定行的示例:
create table employee(ename varchar(30),department varchar(20), age number(30), salary number(20)); insert into employee values('ram','projectmanager',40,50000); insert into employee values('priya','assistant director',45,45000); insert into employee values('hari','developer',46,30000); select * from employee;
输出
您将获得以下输出:
ram|projectmanager|40|50000 priya|assistant director|45|45000 hari|developer|46|30000
现在,使用以下程序:
示例
delete from employee where ename='priya'; select * from employee;
输出
您将获得以下输出:
ram|projectmanager|40|50000 hari|developer|46|30000
基于条件删除数据
例如:
delete from employee where age>45;
示例
下面是一个示例,用于删除年龄大于45岁的员工的行,其余员工将保留在数据库中:
create table employee(ename varchar(30),department varchar(20), age number(30), salary number(20)); insert into employee values('ram','projectmanager',40,50000); insert into employee values('priya','assistant director',45,45000); insert into employee values('hari','developer',46,30000); select * from employee;
输出
您将获得以下输出:
ram|projectmanager|40|50000 priya|assistant director|45|45000 hari|developer|46|30000
现在,使用以下程序:
delete from employee where age>45; select * from employee;
输出
您将获得以下输出:
ram|projectmanager|40|50000 priya|assistant director|45|45000
基于子查询的结果删除数据
删除也可以基于子查询的结果进行。
例如:
delete from employee where age in (select age from employee where age>45)
从表中删除所有行
如果我们想删除整个表,可以使用以下命令:
delete from employee; (or) delete * from employee;
要从表中删除所有行,我们还可以使用TRUNCATE命令。
语法如下:
TRUNCATE table tablename;
例如:
TRUNCATE table employee
TRUNCATE 删除表中的所有行,无需备份。
示例
下面是一个从表中删除所有行的示例:
create table employee(ename varchar(30),department varchar(20), age number(30), salary number(20)); insert into employee values('ram','projectmanager',40,50000); insert into employee values('priya','assistant director',45,45000); insert into employee values('hari','developer',46,30000); delete from employee; select * from employee;
输出
您将获得以下输出:
no rows displayed
广告