What is the use of update command in SQL?

Update command is a data manipulation command which is used to edit the records of a table. It may be used to update a single row based on a condition, all rows or set of rows based on the condition given by the user. It is used along with the SET clause,...

Update command is a data manipulation command which is used to edit the records of a table. It may be used to update a single row based on a condition, all rows or set of rows based on the condition given by the user.

It is used along with the SET clause, operationally, a WHERE clause may be used to match conditions −

Example 1

An example is given below for the use of update command −

update table student set name=’sneha’ where branch=’CSE’;

Example 2

Given below is another example of the usage of update command −

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;

Output

You will get the following output −

pinky|CSE
priya|ECE
hari|EEE
sneha|CSE
priya|ECE
hari|EEE

Update the value of a column

Given below is an example to update table employee set 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;

Output

You will get the following output −

ram|projectmanager|40
priya|assistant director|45
hari|developer|46
ram|projectmanager|42
priya|assistant director|47
hari|developer|48

Update multiple columns in one statement

Given below is an example to update table salary set −

Here,

  • bonus=bonus+5000
  • basic=basic+(0.2*bonus)

Example

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;

Output

You will get the following output −

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

评论0

首页 导航 会员 客服