Site icon IT Tutorial

UPDATE Statement in Oracle SQL | Oracle SQL Tutorials -10

Hi,

I will explain UPDATE Statement in Oracle SQL in this post of Oracle SQL Tutorial series.

Read the previous post of this tutorial series before this.

NULL Values And Is Null or Is Not Null in Oracle SQL | Oracle SQL Tutorials -9

The UPDATE statement is popular command of Oracle SQL and You can modify any existing record in a table using Update Statement.

UPDATE Syntax

Update statement syntax is as follows.

 

UPDATE table_name SET column1 = value1 WHERE condition;

UPDATE table_name SET column1 = value1, column2 = value2, … WHERE condition;

UPDATEtable_name SETcolumn1 = value1, column2 = value2, … WHEREcondition;
Update employee’s First name and Last Name column which EMPLOYEE_ID=100 as follows
SQL> update hr.employees set FIRST_NAME='MEHMET SALIH', LAST_NAME='DEVECI' where EMPLOYEE_ID=100;

1 row updated.

SQL> commit;

Commit complete.

SQL> 

SQL> 
SQL> select EMPLOYEE_ID,FIRST_NAME,LAST_NAME from hr.employees where EMPLOYEE_ID=100;

EMPLOYEE_ID FIRST_NAME           LAST_NAME
----------- -------------------- -------------------------
        100 MEHMET SALIH         DEVECI

SQL>
You can update multiple records in a Update statement as follows.
Let’s raise salary of employees that salary is greater than 15000 as follows.
SQL> select EMPLOYEE_ID,FIRST_NAME,LAST_NAME,SALARY from hr.employees where salary>15000;

EMPLOYEE_ID FIRST_NAME           LAST_NAME                     SALARY
----------- -------------------- ------------------------- ----------
        100 MEHMET SALIH         DEVECI                         24000
        101 Neena                Kochhar                        17000
        102 Lex                  De Haan                        17000

SQL> 
SQL> update hr.employees set salary=salary+5000 where salary>15000;

3 rows updated.

SQL> commit;

Commit complete.

SQL> 
SQL> select EMPLOYEE_ID,FIRST_NAME,LAST_NAME,SALARY from hr.employees where salary>15000;

EMPLOYEE_ID FIRST_NAME           LAST_NAME                     SALARY
----------- -------------------- ------------------------- ----------
        100 MEHMET SALIH         DEVECI                         29000
        101 Neena                Kochhar                        22000
        102 Lex                  De Haan                        22000

SQL>
Or update employees’ commission_pct as 0.5 if commission_pct  are null.
SQL> update hr.employees set commission_pct=0.5 where commission_pct is null;

72 rows updated.

SQL> commit;

Commit complete.

SQL>

 

If you don’t use or forget to use WHERE clause, ALL records will be updated! So be carefull during update.

 

 

 

For Example, I don’t use or forget to use Where clause, then All employees’ salary raised by 1000 $ 🙂

SQL> update hr.employees set SALARY=SALARY+1000;

107 rows updated.

SQL> commit;

Commit complete.

SQL>

 

 

 

 

Do you want to learn Oracle Database for Beginners, then read the following articles.

Oracle Tutorial | Oracle Database Tutorials for Beginners ( Junior Oracle DBA )

Exit mobile version