Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Update Command

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Oracle-SQL

“Update” Command:
 Is a DML Command.
 It is a used to update [modify] the records.
 It can be also used to perform calculations and store the result in
tables.
 Using this command we can update single record, a set of records &
all records.
Syntax:

Update <Table_Name>
Set <column-1>=<value-1>[, <column-2>=<value-2>, ......... ]
[Where <Condition>];

Example:
Update emp set sal=4000,comm=500 where empno=7900;

Examples on “Update” Command:


Example-1:
Create Emp Table with following structure, insert the records & perform
update operations on emp table based on different conditions:
Emp
Empno Ename Job MGR HireDate Sal Comm Deptno
7369 RAJU CLERK 7902 17-DEC-80 800 20
7499 VAMSHI SALESMAN 7698 20-FEB-81 1600 300 30
7521 RAVI SALESMAN 7698 22-FEB-81 1250 500 30
7566 SURESH MANAGER 7839 02-APR-81 2975 20
7654 KIWI SALESMAN 7698 28-SEP-81 1250 1400 30
7698 NAYAN MANAGER 7839 01-MAY-81 2850 30
7782 GEETHA MANAGER 7839 09-JUN-81 2450 10
7788 VRINDH ANALYST 7566 09-DEC-82 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 SHASHI SALESMAN 7698 08-SEP-81 1500 30
7876 SWETHA CLERK 7788 12-JAN-83 1100 20
7900 VARUN CLERK 7698 03-DEC-81 950 30
7902 SAI ANALYST 7566 03-DEC-81 3000 20
7934 ANR CLERK 7782 23-JAN-82 1300 10

TechBuzz IT Solutions Shashi Vardhan


Oracle-SQL

Creating Table & Inserting Records:


Above Table Table Creation Query and insert queries are available in
“oracle sample tables.txt” file [ This pdf file available in google drive].

Increasing 10% salary to an employee whose empno is 7900 [Updating


one field value of a record]:
SQL> Update emp Set sal=sal+sal*0.1 where empno=7900;
Output:
1 row updated.

Updating job value as manager and sal as 5000 to the employee whose
empno is 7369 [Updating multiple field values of a record]:
SQL> Update emp set sal=5000, job='MANAGER' where empno=7369;

Output:
1 row updated.

Increasing 10% sal to all managers [Updating Multiple Records]:


SQL> update emp set sal=sal+sal*0.1 where job='MANAGER';

Output:
4 rows updated.

Increasing 15% salary to all employees [Updating all records]:


SQL> update emp set sal=sal+sal*0.15;
Output:
14 rows updated.

Updating records using parameters:


SQL> Update emp set sal=&sal where empno=&empno;
Enter value for sal: 2000
Enter value for empno: 7900
Output:
old 1: Update emp set sal=&sal where empno=&empno
new 1: Update emp set sal=2000 where empno=7900
1 row updated.

TechBuzz IT Solutions Shashi Vardhan


Oracle-SQL

SQL> /
Enter value for sal: 3000
Enter value for empno: 7934

Output:
old 1: Update emp set sal=&sal where empno=&empno
new 1: Update emp set sal=3000 where empno=7934

1 row updated.

Increasing 10% salary to all managers and clerks:


SQL> update emp set sal=sal+sal*0.1 where job='MANAGER' or
job='CLERK';
[OR]
SQL> update emp set sal=sal+sal*0.1 where job IN('MANAGER','CLERK');

Increasing 10% salary to the employees whose salary is between 1000


and 2000:
SQL> update emp set sal=sal+Sal*0.1 where sal>=1000 and sal<=2000;
[OR]
SQL> update emp set sal=sal+Sal*0.1 where sal between 1000 and
2000;

Increasing 10% salary to all employees except managers:


update emp set sal=sal+sal*0.1 where job!='MANAGER';
[OR]
update emp set sal=sal+Sal*0.1 where job not in('MANAGER');

Increasing 10% sal to the employees whose comm is null:


Update emp set sal=sal+sal*0.1 where comm is null;

Increasing 10% sal to the employees whose comm is not null:


Update emp set sal=sal+sal*0.1 where comm is not null;

Updating comm as null to the employees whose comm is not null:


Update emp set comm=null where comm is not null;

TechBuzz IT Solutions Shashi Vardhan


Oracle-SQL

Updating comm as 500 to all employees:


SQL> Update emp set comm=500;

Updating comm as 1000 for all employees of deptno 20:


Update emp set comm=1000 where deptno=20;

Increasing 10% sal to the employees whose name is started with 's':
Update emp set sal=sal+sal*0.1 where ename like 'S%';

Increasing 10% sal to the employees whose name's second character


is 'A':
update emp set sal=sal+sal*0.1 where ename like '_A%';

Increasing 10% sal to the employees who joined in 1981:


Update emp set sal=sal+sal*0.1 where hiredate like '%81';

Increasing 10% salary to the employees of deptno 20 who joined in


1981:
Update emp set sal=sal+sal*0.1 where hiredate like '%81' and
deptno=20;

Increasing 10% sal to the employees who joined in 82 and 83:


SQL> Update emp set sal=sal+sal*0.1 where hiredate like '%82' or
hiredate like '%83';
[OR]
SQL> Update emp set sal=sal+sal*0.1 where hiredate between '1-JAN-
1982' and '31-DEC-1983';
[OR]
Update emp set sal=sal+sal*0.1 where hiredate>='1-JAN-1982' and
hiredate<='31-DEC-1983';

Increasing 10% sal to the employees whose name has 4 characters:


Update emp set sal=sal+sal*0.1 where ename like ' ';

Increasing 10% sal to the employees who have greater than 40 years
experience:
Update emp set sal=sal+sal*0.1 where (sysdate-hiredate)/365>40;

TechBuzz IT Solutions Shashi Vardhan


Oracle-SQL

Example-2:
Create a Table with following Structure & Insert the records. Then
calculate total and average marks:
Student
StdID SName M1 M2 M3 Total Avrg
1001 Ravi 40 80 60
1002 Sravan 66 44 77

Creating “Student” Table:


SQL> Create Table Student(
StdId Number(4),
SName Varchar2(20),
M1 Number(3),
M2 Number(3),
M3 Number(3),
Total Number(3),
Avrg Number(5,2));

Output:
Table created.

Inserting Records into “Student” Table:


SQL> Insert into Student(StdId,Sname,M1,M2,M3)
values(1001,'Ravi',40,80,60);

Output:
1 row created.

SQL> Insert into Student(StdId,Sname,M1,M2,M3)


values(1002,'Sravan',66,44,77);
Output:
1 row created.

Calculating Total:
SQL> Update Student Set Total=M1+M2+M3;
Output:
2 rows updated.

TechBuzz IT Solutions Shashi Vardhan


Oracle-SQL

Calculating Average Marks:


SQL> Update Student Set Avrg=Total/3;

Output:
2 rows updated.

Retrieving All Records:


SQL> Select * from Student;

STDID SNAME M1 M2 M3 TOTAL AVRG

1001 Ravi 40 80 60 180 60


1002 Sravan 66 44 77 187 62.33
Example-3:
Create a Table with following structure & insert the records. Cslculate
TA, DA, HRA and Gross Salary for all employees.
Employee
Empno Ename Job BasicSal TA DA HRA GROSS

TA: 10% on Basic Salary


DA: 20% on Basic Salary
HRA: 15% Basic Salary
GROSS: BasicSal+TA+DA+HRA

Creating “Employee” Table:


SQL> Create Table Employee(
Empno Number(4),
Ename Varchar2(20),
Job Varchar2(10),
BasicSal Number(7,2),
TA Number(7,2),
DA Number(7,2),
HRA NUmber(7,2),
GROSS Number(7,2));

Output:
Table created.

TechBuzz IT Solutions Shashi Vardhan


Oracle-SQL

Inserting Records:
SQL> Insert into Employee(Empno,Ename,Job,BasicSal)
values(1001,'Sai','CLERK',4000);

Output:
1 row created.

SQL> Insert into Employee(Empno,Ename,Job,BasicSal)


values(1002,'Krishna','MANAGER',8000);

Output:
1 row created.

Calculating TA, DA, HRA:


SQL> Update Employee
Set TA=BasicSal*0.1, DA=BasicSal*0.2, HRA=BasicSal*0.15;
Output:
2 rows updated.

Calculating Gross Salary:


SQL> Update Employee Set Gross=BasicSal+TA+DA+HRA;

Output:
2 rows updated.

Retrieving all Records:


SQL> Select * from Employee;

Output:
EMPNO ENAME JOB BASICSAL TA DA HRA GROSS

1001 Sai CLERK 4000 400 800 600 5800


1002 Krishna MANAGER 8000 800 1600 1200 11600

TechBuzz IT Solutions Shashi Vardhan

You might also like