更新命令在SQL中有什么用?
更新命令是一种数据操作命令,用于编辑表的记录。它可用于基于条件更新单个行,基于用户给出的条件更新所有行或一组行。
它与 SET 子句一起使用,在操作上,WHERE 子句可用于匹配条件 -
例 1
下面给出了一个使用 update 命令的示例 -
update table student set name=’sneha’ where branch=’CSE’;
例 2
下面给出的是使用更新命令的另一个示例 -
create table employee(ename varchar(30),department varchar(20)); insert into employee values('pinky','CSE'); insert into employee values('priya','ECE'); insert into employee values('hari','EEE'); select * from employee; update employee set ename='sneha' where department='CSE'; select * from employee;
输出
您将获得以下输出 -
pinky|CSE priya|ECE hari|EEE sneha|CSE priya|ECE hari|EEE
更新列的值
下面给出的是更新表员工设置年龄=年龄+1的示例:
create table employee(ename varchar(30),department varchar(20), age number(30)); insert into employee values('ram','projectmanager',40); insert into employee values('priya','assistant director',45); insert into employee values('hari','developer',46); select * from employee; update employee set age=age+2; select * from employee;
输出
您将获得以下输出 -
ram|projectmanager|40 priya|assistant director|45 hari|developer|46 ram|projectmanager|42 priya|assistant director|47 hari|developer|48
在一个语句中更新多个列
下面给出的是更新表格工资集的示例 -
这里
- 奖金=奖金+5000
- 基本=基本+(0.2*奖金)
例
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; update employee set age=age+2, salary= salary+5000; select * from employee;
输出
您将获得以下输出 -
ram |projectmanager |40|50000 priya|assistant director|45|45000 hari |developer |46|30000 ram |projectmanager |42|55000 priya|assistant director|47|50000 hari |developer |48|35000