Skip to main content

Class 2.1 Statements in Oracle DML

Statements in Oracle DML

In this session that we are going to see about statements in oracle DML (Data Manipulation Language). So it's better to read the post of Class 2 Statements in Oracle DDL, after read this article.

DML => Data Manipulation Language.

Difference Between statements in Oracle DDL and DML

Data Define Language

Data Manipulation Language

It is used to build the structure for the table

It is used to change or add the data in the form of records 

It is an Auto Commit Statement, so wherever we use DDL it will get committed and save the transaction.

It is a Manual Commit Statement, so manually we should commit the statements else it will rollback to the implicit save point.


As per the Performance wise it has more speed than DML.

Performance wise DML is slow because it makes the transaction to store in undo table space until we giving commit for that particular transaction

If any changes happen it will affect the structure of the table.

If any changes happen it means it will affect only records in the table, won't affect the structure of the table.


Statements in ORACLE Data Manipulation Language

In Data manipulation language is used to manipulated the data by using the following below statements such as:

INSERT

UPDATE

DELETE

MERGE


Shortcuts to Remember Data manipulation Language Statements in Oracle

data manipulation language statements in oracle


INSERT COMMAND


  • It is used to Insert a records in a table

  • Syntax: INSERT INTO table_name column_name VALUES (data in comma separated for each column)


FOR EXAMPLE:


As we already know DDL (Data Define Language) in previous Class. By the continuation recalling the concepts once again in this example


—-- DDL statement

CREATE TABLE orders (id NUMBER

  , customer_name VARCHAR2(50)

  , total_qty NUMBER);

—--- DML statement


As we know the table name and column name


INSERT INTO orders (id, customer_name, total_qty) VALUES (1, ‘Krishna’, 45);


1 row created.


It’s not mandatory to mention the column name during inserting the value, it only mandatory when you are loading data in PL SQL.


INSERT INTO orders VALUES (2, ‘Bala’, 23);


1 row created


Output:


Insert statements in oracle DML


Another method to Insert multiple data: using PLSQL block


BEGIN

    INSERT INTO orders VALUES (1, 'Krishna');

    INSERT INTO orders VALUES (2, 'Bala');

COMMIT;        ------- Mandatory

END;

/                    ------ Mandatory


To know more about this structure, already there is a content for PLSQL basics



UPDATE COMMAND


  • It is used to update a records in a table

  • Syntax: UPDATE table_name

SET column_name = newvalue

WHERE column_name = refvalue    ← (condition)


FOR EXAMPLE


To update orders table by using this command, update the total quantity as 100 for ID = 1 


UPDATE orders

SET total_qty = 100

WHERE id = 1;


update statements in oracle dml

DELETE COMMAND


  • It is used to delete a particular records in a table using where condition or else we can delete all the records in a table

  • Syntax: DELETE FROM table_name

                           WHERE conditions

FOR EXAMPLE
To delete particular records in a orders table.

DELETE FROM orders
WHERE id = 2;

Delete statement in oracle DML


NOTE: "FROM " keyword is optional to mention in the delete syntax of DML statements in oracle.


MERGE COMMAND


  • It is used to merge records in two or more tables using the INSERT and UPDATE command.

  • Syntax: MERGE INTO newtable T2

USING basetable T1

ON (join condition)

WHEN NOT MATCHED THEN

INSERT VALUES (T1.columns, T1.columns)

WHEN MATCHED THEN

UPDATE 

SET column_name = T1.columns;


FOR EXAMPLE


Practical:

CREATE TABLE m1 (id NUMBER, Name VARCHAR2(9));

Table created.

CREATE TABLE m2 (id NUMBER, Name VARCHAR2(9));

Table created.


BEGIN
  INSERT INTO m1 VALUES (1, 'eduflee');
  INSERT INTO m1 VALUES (2, 'Oracle');
  INSERT INTO m1 VALUES (3, 'java');
  INSERT INTO m2 VALUES (1, 'Microsoft');
  INSERT INTO m2 VALUES (4, 'Dell');
  COMMIT;
END;
  /

After creating and Inserting m1 and m2 Table

 SELECT * FROM m1;

    ID NAME
------ ---------
     1 eduflee
     2 Oracle
     3 java

 SELECT * FROM m2;

    ID NAME
------ ---------
     1 Microsoft
     4 Dell

Merging this two tables

MERGE INTO newtable alias name

USING basetable alias name

ON (join condition)

WHEN NOT MATCHED THEN

INSERT VALUES (T1.columns, T1.columns)

WHEN MATCHED THEN

UPDATE 

SET column_name = T1.columns;



MERGE INTO m2 new
USING m1 old
ON (new.id = old.id)
WHEN NOT MATCHED THEN
    INSERT VALUES (old.id, old.name)
WHEN MATCHED THEN
    UPDATE
        SET name = new.name ;

3 rows merged.

SELECT * FROM m2;

        ID NAME
---------- ---------
         1 Microsoft
         4 Dell
         3 java
         2 Oracle

Merge syntax in oracle dml

Conclusion

I hope you got clear idea in understanding this Data Manipulation Concepts, so if you having any doubts regarding the post means kindly reach out us.

Frequently Asked Interview Questions

1, Difference between DDL and DML ?
2. Merge Statement Syntax
3. Truncate vs Delete
4. What happens if we fail to give commit inside the PLSQL block ?
5. Why Truncate is fast compared to Delete ?
6. Is it possible to clear all the records using delete command without giving any conditions ?
7. Update command Syntax
8. Difference between DELETE vs TRUNCATE vs DROP

Comment your answers or mail us to our official mail id.


Comments

Popular posts from this blog

Oracle SQL Interview Questions Series 1 [Basic]

Oracle SQL Interview Questions Series - 1 Oracle SQL Interview Questions covered in Eduflee, so complete all the series to get placed into the IT company. At present Structured Query Language is very important to enter into the Software company. After completing all the Interview series, you can crack many interviews with 100% assurance from my experience. Basic Questions for SQL interview Schema => User Records => Rows Fields => Columns Result set => Output 1, What are the Schema Objects in Oracle SQL ?     Table, View, Materialized view, Sequence, Synonyms, Index are the schema objects in SQL.     Table => Basic Unit of Storage [Rows and Columns]     View => Logical Representation of subject of records, It does not contains any data. So here data is logical.     Materialized View =>   It contains the data, it is also called as Snapshot. It contains data physically.     Sequence => It is a Numeric Value Generator     Synonyms => used to provide alternate nam

Class 3 Functions in Oracle SQL

Functions in Oracle SQL Functions is one of the important features in Oracle SQL, with the help of this functions we can able to get the result as per our requirement. Functions in Oracle SQL contains Single Row Functions, Multiple Row Functions. Mainly those functions are used to returns value based on the input that which we given. And practicing this functions in oracle SQL, one can able to master in query writing. Functions is used to perform some actions like doing any calculations, grouping the data, conversion in type, date conversion etc. Types of Functions in Oracle SQL Functions in oracle is classified on the basis of the input which we are giving and getting the output, and they are: - Single Row Functions - Multiple Row Functions Single Row Functions in Oracle SQL Single Row Functions works on the single rows which will shows the result for that single rows only. If "n" number of inputs to give "n" number of outputs. From the above figure explains about

Learn SQL From Scratch to Advanced topics

Learn SQL from Scratch to Advanced  Nowadays, many IT company moreover depend on SQL  only. So this is one of the basic query language to start for new developers. So track or to be master in SQL, the below topics should be covered. List of Basic Topics :- 1, Should know what is frontend and backend developer  2, What is DBMS, RDBMS ? 3, What is SQL ? 4, What are the database are there ? Beginner Level SQL topics  This are some beginner level SQL topics, if you are new to SQL this are the topics that should be covered first. 1, Oracle Basics  2, List of Objects in SQL   3 , Clause and its usage   4, Functions  5, Sub Query  6, Joins  7, Decode and Case 8, Constraints 9, Important Data Dictionary Table like user_source, all_source, user_tables etc.., Intermediate Level SQL topics (Creating Objects)  When we are entering into objects this below topics should be covered. In  this topics that we are creating database objects like tables, views, sequences, synonyms etc. 1, Data Definitio