×
>
<

Aptitude

SQL DML Commands

DML Commands

DML(Data Manipulation Language): p>

The SQL commands that deals with the manipulation of data present in the database belong to DML or Data Manipulation Language and this includes most of the SQL statements. It is the component of the SQL statement that controls access to data and to the database. Basically, DCL statements are grouped with DML statements.

DML Commands

  • INSERT : It is used to insert data into a table.
  • UPDATE: It is used to update existing data within a table.
  • DELETE : It is used to delete records from a database table.
  • LOCK: Table control concurrency.
  • CALL: Call a PL/SQL or JAVA subprogram.
  • EXPLAIN PLAN: It describes the access path to data.

INSERT Query in DBMS

INSERT is a widely used data manipulation language(DML)command for adding new data to the existing database table

Insert command is used to add one or more rows of data to the database table with specified column values

Syntax

  
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
  

Simple INSERT Query in DBMS

In this type of insert, all the values should be provided to all the columns that exist in the table without specifying the column name

The order of values declared in the values clause should follow the original order of the columns in the table

  
insert into student
 values(66'trishaank','computers','24-07-1998',92);
  

Inserting data into required columns

In this case, the order of columns declared in insert need not be the same as that of the original table order, only the specified column values are added and null values added to the remaining column values in that tuple

  
insert into student(stuid,sname,branch) values (82,'Srinivas','Computers');
  
Dynamic INSERT Query in DBMS (using ampersand &)

In this type of insert, the values are entered by the user at the execution time

  
insert into student values(&stuid,'&sname','&branch','&dob',&score);
  
Action
  
enter value for stuid:73
enter value for sname:Prashanth
enter value for brance:Physics
enter value for dob:17-08-1997
enter value for score:78
  
UPDATE Query in DBMS

In the Real world scenario, facebook, Gmail will give an option to update your name or Profile picture, etc, how does it work, at the backend SQL UPDATE is executed internally

  • The UPDATE statement is used to modify or change the data of the existing table in the database
  • We can update a single column as well as multiple columns as per our requirement

Syntax

  
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
  

The SET is Is used to set new values to the required column and the where Clause is used to filter the rows for which rows of data are needed to be updated

Updating a single column

Any row in the database table can be updated using update statement

Consider a sample table EMP as shown below

EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO
7839KINGPRESIDENT17-NOV-81500010
7698BLAKEMANAGER783901-MAY-81285030
7782CLARKMANAGER783909-JUN-81245010
7566JONESMANAGER783902-APR-81297520
7788SCOTTANALYST756619-APR-87300020
7902FORDANALYST756603-DEC-81300020
7369SMITHCLERK790217-DEC-8080020

Update the commission of all employees to 200/- whose salary is less than 3000/-

  
UPDATE emp SET comm=200 
WHERE sal<3000;
  

Output

  
7 ROWS SELECTED
  
EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO
7839KINGPRESIDENT17-NOV-81500010
7698BLAKEMANAGER783901-MAY-81285020030
7782CLARKMANAGER783909-JUN-81245020010
7566JONESMANAGER783902-APR-81297520020
7788SCOTTANALYST756619-APR-87300020
7902FORDANALYST756603-DEC-81300020
7369SMITHCLERK790217-DEC-8080020020

Initially, in the EMP table, values are NULLfor comm column for all the rows but using update statement we have changed the commission to 200 from NULL for those employees who are having salaries less than 3000.

Updating multiple columns

Using a single update statement with can parallelly update any number of column

Update salary by 500 and change the commission to 1000 for all analysts working in the company

  
update emp set sal=sal+500 ,comm=1000
where job='ANALYST';
  
O/P
  
2 rows selected.
  
EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO
7788SCOTTANALYST756619-APR-873500100020
7902FORDANALYST756603-DEC-813500100020

We have two analysts, hence two rows will be updated i.e salary will be increased from 3000 to 3500 and commission will be changed to 1000 i.e both salary and commission columns get updated.

Updating without where clause

If you don’t specify where clause, it means that no condition is required and no data filtering happens and all the rows present in the table will be updated for that column as specified

Make the salary of all employees working in the company to 500

  
update emp 
set sal=500;
  
O/P
  
7 rows selected
  
EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO
7839KINGPRESIDENT17-NOV-8150010
7698BLAKEMANAGER783901-MAY-8150020030
7782CLARKMANAGER783909-JUN-8150020010
7566JONESMANAGER783902-APR-8150020020
7788SCOTTANALYST756619-APR-8750020
7902FORDANALYST756603-DEC-8150020
7369SMITHCLERK790217-DEC-8050020020

We have seven rows present in the table, the value of a salary in every row becomes 500 because there is no condition for data change, hence all the rows will be updated

DELETE Query in DBMS

In Real world scenario if the user wants to delete his Facebook account the user just click the delete option but at the backend, DELETE command is executed and the record with particular details is been deleted

DELETE statement is used to delete single or multiple records present in the existing database table based on a specific condition

Basic Syntax for the DELETE statement

  
DELETE FROM table_name
WHERE condition;
  

Deleting a single record

Where clause is used to provide a condition for deleting a particular record in the table

Consider the sample table EMP

EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO
7839KINGPRESIDENT17-NOV-81500010
7698BLAKEMANAGER783901-MAY-81285030
7782CLARKMANAGER783909-JUN-81245010
7566JONESMANAGER783902-APR-81297520
7788SCOTTANALYST756619-APR-87300020
7902FORDANALYST756603-DEC-81300020
7369SMITHCLERK790217-DEC-8080020

Delete the record of employee number 7698

  
delete from emp where empno = 7698;
  
O/P
  
1 ROW DELETED
  
EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO
7698BLAKEMANAGER783901-MAY-81285030

  • A single employee present in the company with employee number 7698 will be deleted from the database table
  • The previous table we have 7 rows but here we are having only 6 records i.e one record has been deleted from the database table.

Deleting multiple records

Delete statement can be used to delete multiple rows at a time in a table

Delete the records of all employees whose salary is greater than 2500 expect the president

  
delete from emp 
where sal>2500  and job!=  'PRESIDENT';
  
O/P
  
4 ROWS DELETED
  
EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO
7698BLAKEMANAGER783901-MAY-81285030
7566JONESMANAGER783902-APR-81297520
7788SCOTTANALYST756619-APR-87300020
7902FORDANALYST756603-DEC-81300020

In the previous table, we have 7 records but after the execution of this query we are left with only three records i.e all those employees who are having a salary greater than 1000 except president will be deleted.

Deleting All Records

  • All the rows present in the table will be deleted if there is no condition specified by using where clause
  • In this type of delete, the table will become empty and no records will be present to display

  
delete from emp;
  
O/P
  
7 ROWS DELETED
  

All the seven rows present in the emp table will be deleted and row-count becomes zero i.e nothing to display.

DELETE V/S TRUNCATE

Both these commands delete and truncate make a table empty but there are certain differences where a super learner must be aware of

TRUNCATE command usage :

Example
  
truncate table emp;
  

  • Both these commands delete and truncate  make a table empty but there are certain differences where a super learner must be aware of

    TRUNCATE command usage

    truncate table emp;
  • It removes all rows from a Table and makes a table empty
  • This operation cannot be rolled back (table cannot be restored) and no triggers will be fired
  • You cannot use a where clause and delete particular records you need to delete enter table data
  • Truncate is faster and does not use a match under space as delete