Site icon IT Tutorial

Oracle SQL Tutorial -6 Update Data In The Oracle Database

Image

Hi ,

In this article , I will explain topics such as updating data , deleting data from table and adding records to table .

ADDING RECORD TO TABLE

The INSERT command is used when adding a new record to the table . To add a single record :

INSERT INTO table [(column [ ,column ...] VALUES (value [ , value ...]);
INSERT INTO ADMIN.ISCI(EMPLOYEE_ID,FIRST_NAME,LAST_NAME) VALUES (6,'Melike','Duran');

ROW COPYING FROM ANOTHER TABLE

INSERT INTO NEW_TABLE SELECT * FROM ISCI WHERE EMPLOYEE_ID=6;

UPDATING DATA IN THE TABLE

INSERT INTO ADMIN.ISCI (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,SALARY) VALUES (12,'Muzaffer','Kazan',1500);

In the query above , we added a new record to our table and entered the salary as 1500 . I’m going to update the salary data now .

UPDATE ADMIN.ISCI SET SALARY=SALARY+500 WHERE EMPLOYEE_ID=12;

And when we run the above query , we just changed the salary value of the person we added to the table from 1500 to 2000 .

Let’s make another example :

UPDATE OGRENCI SET BOLUM_ADI='Bilgisayar Mühendisliği' WHERE BOLUM_ADI='Tarih';

This example changed a single field like our first example and now we will update two fields in a single query .

UPDATE OGRENCI SET AD='Aylin' , BOLUM_ADI='Gastronomi' WHERE OGRENCI_NO=17700;

In our student table , we will change both the name and the section name of the person whose ogrenci_no is 17700 .

DELETING DATA FROM THE TABLE

The DELETE command is used when we want to delete data from the table . With this command , we can delete more than one record at a time .

TRUNCATE : Used to delete all data in the table and is irreversible .

TRUNCATE is a DDL command .

Now let’s look at the values in our table .

Our table looked like this after we just made an update , and now let’s delete Deniz from the table .

DELETE FROM ADMIN.ISCI WHERE FIRST_NAME='deniz';

Now let’s give an example of truncate .

CREATE TABLE ADMIN.YENI_TABLO AS SELECT * FROM ADMIN.ISCI;

First of all , we created a table named new_table by copying the ıscı table . Then we delete this table with the truncate statement .

TRUNCATE TABLE ADMIN.YENI_TABLO;

And now let’s check to see if our table has been deleted .

SELECT * FROM ADMIN.YENI_TABLO;

This way we received a warning , so our table was completely deleted .

DATABASE TRANSACTIONS

The transaction ends .

COMMIT & ROLLBACK

SAVEPOINT

BEFORE COMMIT OR ROLLBACK

AFTER COMMIT

DELETE FROM ADMIN.ISCI WHERE EMPLOYEE_ID=4;

SELECT * FROM ADMIN.ISCI;

We deleted the person with id above 4 , but this process did not happen permanently and now we commit it to make it permanent :

COMMIT;
SELECT * FROM ADMIN.ISCI;

AFTER ROLLBACK

DELETE FROM ADMIN.ISCI;
ROLLBACK;

Now we’re checking to see if it’s taken back :

SELECT * FROM ADMIN.ISCI;

FOR UPDATE

See you in my next post.

Exit mobile version