SQL中update命令的作用是什么?
更新命令是一种数据操作命令,用于编辑表的记录。它可用于根据条件更新单行,根据用户给出的条件更新所有行或行集。
它与SET子句一起使用,在操作上,WHERE子句可用于匹配条件-
示例1
下面给出了一个使用更新命令的例子-
update table student set name=’sneha’ where branch=’CSE’;
示例2
下面给出了另一个使用update命令的例子-
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
更新列的值
下面给出了一个更新表员工集age=age+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