You are on page 1of 28

Advanced Database(Oracle)

Chapter Five
Lecturer: Mohamed Hussein
Msc in Computer Science
DML Commands in Oracle
● DML stands for Data Manipulation Language. DML commands are basically used

to INSERT, UPDATE, and DELETE data in a database table. That means DML

statements affect the records in a table.

● These are the basic operations that we perform on data such as inserting new

records, deleting unnecessary records, and updating/modifying existing records.

● The most important point that you need to remember is DDL Commands in

Oracle are working on the data of a table, not on the structure of a database

object.
Cont..
● DML provides the following three commands:
 INSERT – To insert new records
 UPDATE – To update/Modify existing records
 DELETE – To delete existing records
● The following two are the Latest DML Commands in the Oracle database.
 INSERT ALL
 MERGE
INSERT DML Command in Oracle
● The INSERT DML command is used to insert a new row into a table. The
syntax to use the INSERT command is given below.
● In the below syntax, we are not specifying the column names, so we need
to supply values for all the columns of the specified table. The value
sequence in the query must be the same as the column sequence in the
table.
● Syntax 1:
INSERT INTO <TableName> VALUES(V1,V2,V3,……….);
● Syntax 2:
INSERT INTO <Table Name> (REQUIRED COLUMN NAMES) VALUES (V1,
V2,……..);
Cont..
Example 1:
● Let us insert a record into the EMPLOYEE table. To do so execute the
following INSERT query. As you can see, we are providing the values for all
the three columns in the same sequence they are defined in the
Employee table i.e. Id, Name, and Salary. The sequence is important in
this case.
INSERT INTO EMPLOYEE VALUES (1, ‘Anurag’, 50000);
Cont..
Example:
● Let us insert another record into the Employee table by executing the
below INSERT statement. Here, as you can see, we are specifying the
column names and in the same column name sequence we are providing
the values i.e. Id, Salary, and Name.
INSERT INTO EMPLOYEE (Id, Name) VALUES (3, ‘Sambit’);
Oracle INSERT INTO SELECT statement
● Sometimes, you want to select data from a table and insert it into another
table.
● To do it, you use the Oracle INSERT INTO SELECT statement as follows:
INSERT INTO target_table (col1, col2, col3)
SELECT col1,
col2,
col3
FROM source_table
WHERE condition;
Cont..
● The Oracle INSERT INTO SELECT statement requires the data type of the
source and target tables to match.
● If you want to copy all rows from the source table to the target table, you
remove the WHERE clause. Otherwise, you can specify which rows from
the source table should be copied to the target table.
Oracle INSERT INTO SELECT examples
A) Insert all sales data example
● Let’s create a table named sales for the demonstration.
CREATE TABLE sales (
customer_id NUMBER,
product_id NUMBER,
order_date DATE NOT NULL,
total NUMBER(9,2) DEFAULT 0 NOT NULL,
PRIMARY KEY(customer_id,
product_id,
order_date)
);
Cont..
● The following statement inserts a sales summary from the orders and
order_items tables into the sales table:
INSERT INTO sales(customer_id, product_id, order_date, total)
SELECT customer_id, product_id, order_date, SUM(quantity *
unit_price) amount
FROM orders
INNER JOIN order_items
USING(order_id)WHERE status = 'Shipped‘
GROUP BY customer_id,product_id,order_date;
UPDATE DML COMMAND in Oracle
➔ The UPDATE DML Statement in Oracle is basically used to
◆ To update all rows data in a table at a time.
◆ To update a specific row data in a table by using the “where”
condition.
➔ The following is the syntax to use the UPDATE DML statement in Oracle.

UPDATE <TABLE NAME> SET <COLUMN NAME1>=<VALUE1>,


<COLUMN NAME2>=<VALUE2>, ……..[ WHERE <CONDITION> ];
Cont..
Example:

➔ Update the Name and Salary of the Employee whose Id is 3. The following Update
SQL Script will update the same in the Employee table.

UPDATE EMPLOYEE SET Name=’Test1′, Salary=55000 WHERE Id=3;

● Update the Salary of the Employee whose Name is Anurag. The following Update
SQL Script will update the same in the Employee table.

UPDATE EMPLOYEE SET Salary=85000 WHERE Name=’Anurag’;

● When you execute the above UPDATE DML statement, you will get the message as
1 row updated as shown in the below image as there is only one employee in the
Employee table whose name is Anurag.
Cont..
● Update the Salary of all the Employees to 500000. The following Update SQL
Script will update the same in the Employee table.

UPDATE EMPLOYEE SET Salary =500000;

● When you execute the above UPDATE statement, you will get the message
as 10 rows updated as shown in the below image as in the Employee table
total of 10 records are there and we are not applying any filter.
DELETE DML COMMAND in Oracle
➔ The DELETE DML Statement in Oracle is basically used to

◆ To delete all rows from a table at a time.

◆ To delete a specific row from a table by using the “where” condition.

➔ The syntax to use the DELETE DML statement in Oracle is given below.

DELETE FROM <TABLE NAME> [ WHERE <CONDITION> ];


Cont..
Example:

➔ Delete the Employee record from the Employee table whose id is 5. The following DELETE

SQL Script will update the same in the Employee table.

DELETE FROM EMPLOYEE WHERE Id=5;

➔ When you execute the above DELETE query, you will get the message as 1 row deleted as

shown in the below image as there is only one employee in the Employee table whose id is

5.
Cont..
➔ If you want to DELETE all the employees from the Employee table, then you

need to use the DELETE statement as follows without any conditions.

DELETE FROM EMPLOYEE;

➔ When you execute the above DELETE DML statement, then you will get the

message as 9 rows deleted as shown in the below image as in the Employee

table total 9 records are there and we are not applying any filter.
DELETE VS TRUNCATE in Oracle
Delete

1. It is a DML command
2. It can delete a specific row from a table.
3. It supports the “WHERE” clause condition.
4. It is a temporary data deletion.
5. We can restore deleted data by using the “ROLLBACK” command.
6. Execution speed is slow as deleting operation is performed row by row / one
by one manner.
Cont..
Truncate

1. It is a DDL command

2. It is not possible to delete a specific row from a table.

3. It does not support the “WHERE” clause condition.

4. It is a permanent data deletion.

5. We cannot restore deleted data by using the “ROLLBACK” command.

6. Execution speed is fast as deleting a group of rows at a time.


INSERT ALL COMMAND IN ORACLE
➔ The INSERT ALL statement in Oracle is basically used to add multiple rows
with a single INSERT statement. The important point that you need to
remember is, the rows can be inserted into one table or multiple tables
using only one SQL command. The syntax for the INSERT ALL statement
in Oracle is shown in the below image:
Cont..
Parameters or Arguments

1. table_name: It specifies the name of the table in which you want to insert
records.
2. column1, column2, column_n: This specifies the name of the columns in the
table to insert values.
3. expr1, expr2, expr_n: This specifies the values to assign to the columns in the
table.
Cont..
➔ Example: Insert into One Table
◆ The following example specifies how to insert multiple records into one table.
Here we are inserting three records into the EMPLOYEE table.

INSERT ALL

INTO EMPLOYEE (Id, Salary, Name) VALUES (1, 35000, 'Pranaya')

INTO EMPLOYEE (Id, Salary, Name) VALUES (2, 45000, 'Kumar')

INTO EMPLOYEE (Id, Salary, Name) VALUES (3, 55000, 'Rout')


Cont..
➔ Example: Insert into Multiple Table
◆ It is also possible in Oracle to use the INSERT ALL statement to insert multiple
rows into multiple tables in one command. For example, let us insert records into
both the EMPLOYEE and ADDRESS table by executing the following SQL
statement.

INSERT ALL

INTO EMPLOYEE (Id, Salary, Name) VALUES (4, 35000, 'Pranaya')

INTO EMPLOYEE (Id, Salary, Name) VALUES (5, 45000, 'Kumar')

INTO ADDRESS (Id, EmpId, Addrees) VALUES (1, 4, 'BBSR')

INTO ADDRESS (Id, EmpId, Addrees) VALUES (2, 5, 'CTC')


MERGE DML COMMAND in Oracle
● The Oracle MERGE statement selects data from one or more source

tables and updates or inserts it into a target table. The MERGE statement

allows you to specify a condition to determine whether to update data

from or insert data into the target table.

● The following illustrates the syntax of the Oracle MERGE statement:


Cont..
MERGE INTO target_table

USING source_table

ON search_condition

WHEN MATCHED THEN

UPDATE SET col1 = value1, col2 = value2,...

WHERE <update_condition>

[DELETE WHERE <delete_condition>]

WHEN NOT MATCHED THEN

INSERT (col1,col2,...)

values(value1,value2,...)

WHERE <insert_condition>;
Cont..
In this the MERGE statement:
● First, specify the target table (target_table) in the INTO clause, which you want to update
or insert.
● Second, specify the source of data (source_table) to be updated or inserted in the USING
clause.
● Third, specify the search condition upon which the merge operation either updates or
inserts in the ON clause.
For each row in the target table, Oracle evaluates the search condition:
● If the result is true, then Oracle updates the row with the corresponding data from the
source table.
● In case the result is false for any rows, then Oracle inserts the corresponding row from
the source table into the target table.
Cont..
➔ The MERGE statement becomes convenient when you want to combine
multiple INSERT, UPDATE, and DELETE statements in a single operation.
➔ Because the MERGE is a deterministic statement, you cannot update the
same row of the target table multiple times in the same MERGE statement.
➔ You can add an optional DELETE WHERE clause to the MATCHED clause to
clean up after a merge operation. The DELETE clause deletes only the rows
in the target table that match both ON and DELETE WHERE clauses.
Cont..
➔ Oracle MERGE example

CREATE TABLE members (

member_id NUMBER PRIMARY KEY,

first_name VARCHAR2(50) NOT NULL,

last_name VARCHAR2(50) NOT NULL,

rank VARCHAR2(20)

);

CREATE TABLE member_staging AS

SELECT * FROM members;


END
ANY QUESTION

You might also like