ad

DML (Data Manipulation Language)

      DML means Data Manipulation Language. These statements will be used to modify the data present in the tables. These statements will work on the data. There is no relation for these statements with the structure of the tables. Data manipulation involves Inserting data into tables, modifying the data present in the tables and removing the data from the tables. DML statements require Transaction control.

     Any change which is made to the database by a DML statement will be called as a transaction. So, any change made by DML statement needs to be controlled by TCL statements (Transaction Control). I will discuss about these TCL statements in my future posts. In order to handle the changes done to the database by DML statements, we need transaction control.

INSERT:
            Insert will be used to insert new data into the tables. In other words we can say Insert will be used to create new rows in the tables. We cannot affect the existing rows from the table using Insert statement.

We have two different syntaxes for writing the Insert statement. They are,
  • INSERT INTO tab_name values (val1, val2, … , val n);
  • INSERT INTO tab_name (col1, col2, col3, .....,coln) VALUES (val1, val2, ..... , valn);
            First syntax is the general syntax of insert statement. In this type of syntax we must pass values to all the columns of the table, and that also in the order of presence of these columns in the table. Otherwise, we will get error.

           
For Example, Let us say we have a table called Employee in the database. The columns present in this table are Empno, Ename, hiredate, Sal and Deptno. Now If I want to insert new row into this table using first syntax then,
  • INSERT INTO Employee VALUES ( 1111, 'RAM', ’12-jan-2010’,5000, 20);
            Now, I will discuss about the 2nd syntax. In this second syntax we can enter values into the columns of the table while creating new rows in our own desired order irrespective of the order of presence of these columns in the table. This we can achieve by mentioning the desired order between tab_name and VALUES as shown in the syntax. In this syntax we can also give values to specific columns of the table only. This means, if we have 5 columns in the table and while creating the new row if you want to give values only to 3 columns we can do that by using second syntax.

Ex:
  • INSERT INTO Emp ( Empno, Ename, Sal) Values (2222, 'RAJ', 2000);
  • INSERT INTO Emp (Empno, Sal, Ename) Values (3333, 1500, 'KRISH');
We have one more syntax for creating new rows in the table using Insert statement.  That is shown below.
  • INSERT INTO Emp VALUES ( &val1, &val2, ..... &valn).
     In this syntax, if we run the above statement then system will ask us to enter the values one by one. After entering the entire values row will be created in the table. After that if we hit '/' at the command prompt then system will ask us to enter the values again to create another row. Like this we can create n number of rows.

DELETE: 
           Delete will be used to remove the data from the tables. Using Delete we can remove single row or multiple rows from the table. We have two syntaxes for Delete statement.
  • DELETE FROM tab_name;
  • DELETE FROM tab_name WHERE condition;
If we use first syntax then all the rows from the table will be removed. If we want to remove specific rows only then we have to use the second syntax.
Ex:   
If we execute this statement then all the rows from the EMP table will be removed.
  • DELETE FROM Employee;
If we execute this statement then the row belongs to employee with Empno 1111 will be removed. So, using this syntax we can remove specific rows from the table by specifying the Where clause.
  •  DELETE FROM Employee WHERE empno = 1111;
Using Delete we can remove entire row from the table only. We cannot remove values from specific columns of the table using Delete statement.

UPDATE:
         Update will be used to modify the data present in the tables. Using single update statement we can modify the data present in the single column or multiple columns of the table. Similarly, using single update we can modify data present in single row or multiple rows.

Different syntax's for Update statement are,
  • UPDATE tab_name SET col_name = val ;
  • UPDATE tab_name SET col_name = val WHERE condition;
  • UPDATE tab_name SET col1 = val1, col2 = val2 , .... ;
  • UPDATE tab_name SET col1 = val1, col2 = val2,... WHERE condition;
            If we use the first syntax, then in all the rows of the table the column specified (col_name) will be updated to the provided value (val).

            If we use second syntax, then for the rows which will satisfy the condition specified in the WHERE clause column specified (col_name) will be updated to the provided value (val).Third and Fourth syntax's are similar to first and second syntax's only , only difference here is instead of updating single column we are updating multiple columns of the table.

Ex: 
  • UPDATE Employee SET deptno = 20; (This will update deptno to 20 in all the rows of Emp table).
  • UPDATE Employee SET sal = 5500 WHERE ename = 'RAM'; (This will update sal of Ram to 5500).
  • UPDATE Employee SET sal = 6000, deptno = 30 ;
  • UPDATE Employee SET sal = 7000, deptno = 40 WHERE ename = 'RAM';
            As I discussed before, if we want to insert value to a particular column of existing row we cannot use Insert statement. We have to use Update statement to update the column to that particular value. Similarly if we want to remove values from specific column then we cannot do it using Delete statement. We have to update that column or columns to null.
Ex:
  • UPDATE Employee SET deptno = null WHERE empno = 3333;
MERGE: 
          Merge statement will be used to combine data from two similar tables into single table. In order to do merge operation two tables containing similar columns are required. Generally we will use merge statement to insert data from one table into other similar table. While doing this if same row is present in both the tables then we cannot perform insert operation due to the constraints of the table. This is where the merge operation is very useful.

            Merge is the combination of both Insert and Update operations. So, while merging data from one table into another table, if we have same row in both the tables then we will perform UPDATE operation. If different row is present, then we will perform INSERT operation. This is the concept involved with MERGE statement.
Syntax for writing the merge statement is,      
     MERGE INTO tab1 USING tab2 ON condition 
       WHEN MATCHED THEN 
     UPDATE SET tab1.Col = tab2.Col
       WHEN NOT MATCHED THEN
     INSERT VALUES (tab2.col1, tab2.col2,  ........ );
       Let me explain this merge statement with an example. Let us say we have two similar tables Emp and Emp1. I want to merge data from Emp1 to Emp table. Then statement used for this is,

    MERGE INTO Emp USING Emp1 ON emp.empno = emp1.empno
      WHEN MATCHED THEN
    UPDATE SET emp.sal = emp1.sal
      WHEN NOT MATCHED THEN
    INSERT VALUES ( emp1.empno, emp1.ename, emp1.sal, emp1.deptno)
            So, here I am merging data from Emp1 table into Emp table. ON will be used to check the common thing between the two tables to decide when to do update and when to do insert. Here using Empno column I am deciding this. Generally we will use primary key columns where unique values will be present to compare in ON condition.

            Now if the condition is satisfied then, WHEN MATCHED condition will be satisfied and the UPDATE statement will be performed. Here we are taking sal from emp1 table and updating it to emp table. If condition is not satisfied then WHEN NOT MATCHED condition will be satisfied and new row will be inserted into Emp table from Emp1 table.

            So, this is how Merge statement will be worked.  So, in case of Merge operation if similar rows are there we will perform Update operation and if different row is there then we will perform Insert operation.