Professional Documents
Culture Documents
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
● These are the basic operations that we perform on data such as inserting new
● 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 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 the Salary of the Employee whose Name is Anurag. The following Update
SQL Script will update the same in the Employee table.
● 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.
● 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
➔ The syntax to use the DELETE DML statement in Oracle is given below.
➔ Delete the Employee record from the Employee table whose id is 5. The following DELETE
➔ 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
➔ When you execute the above DELETE DML statement, then you will get the
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
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
INSERT ALL
tables and updates or inserts it into a target table. The MERGE statement
USING source_table
ON search_condition
WHERE <update_condition>
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
rank VARCHAR2(20)
);