0% found this document useful (0 votes)
3 views9 pages

Lab Manual 05 - DML - Data Manipulation Language

This lab manual from the University of Management and Technology covers Data Manipulation Language (DML) in SQL, detailing its commands: SELECT, INSERT, UPDATE, and DELETE. It provides syntax examples and practical exercises for creating and manipulating database tables. The manual also includes graded exercises for students to practice their SQL skills.

Uploaded by

ehmili884
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Lab Manual 05 - DML - Data Manipulation Language

This lab manual from the University of Management and Technology covers Data Manipulation Language (DML) in SQL, detailing its commands: SELECT, INSERT, UPDATE, and DELETE. It provides syntax examples and practical exercises for creating and manipulating database tables. The manual also includes graded exercises for students to practice their SQL skills.

Uploaded by

ehmili884
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Lab Manual: Database Systems

University of Management and Technology,


Lahore Campus
Lab- 05 Manual

Lab Instructor: Riaz Ahmad


Department of Computer Science
Email: riazahmad@umt.edu.pk

Lab: 05 Data Manipulation Language (DML)

What is Data Manipulation Language (DML)?


Data Manipulation Language, also known as DML, is a set of SQL commands that are
used to manipulate data within database tables or query views. Data analysts, scientists,
engineers, and anyone using SQL rely on DML in order to access, transform, and
analyze data.
What are DML Commands in SQL?
DML commands all data professionals include are SELECT, INSERT, DELETE,
and UPDATE.
These commands allow you to access and change the data that lives in a database.
Comman Description
d
SELECT SELECT is an access command. It allows you to access data within a
database.
INSERT INSERT is a change command. It allows you to input rows of data within
an existing table.
DELETE DELETE is a change command. It allows you to remove rows of data
within an existing table.
UPDATE UPDATE is a change command. It allows you to change data within the
rows of a table.

Department of Computer Science, UMT, Lahore. 1 Riaz Ahmad


Lab Manual: Database Systems

Following are the four main DML commands in SQL:

1. SELECT Command
2. INSERT Command
3. UPDATE Command
4. DELETE Command

1. SELECT
SELECT is the most important data manipulation command in Structured Query
Language. The SELECT command shows the records of the specified table. It also
shows the particular record of a particular column by using the WHERE clause.
Syntax:
SELECT column_Name_1, column_Name_2, ….., column_Name_N FROM Name_of
_table;

Here, column_Name_1, column_Name_2, ….., column_Name_N are the names of


those columns whose data we want to retrieve from the table.

If we want to retrieve the data from all the columns of the table, we have to use the
following SELECT command:

Department of Computer Science, UMT, Lahore. 2 Riaz Ahmad


Lab Manual: Database Systems
SELECT * FROM table_name;

Table creation and Insertion SQL Query of Student Table:

CREATE TABLE Students (


Student_ID VARCHAR(10) PRIMARY KEY,
Student_Name VARCHAR(50),
Student_Marks INT
);
INSERT INTO Students (Student_ID, Student_Name, Student_Marks) VALUES
('BCA1001', 'Riaz Ahmad', 85),
('BCA1002', 'Salman Afzal', 75),
('BCA1003', 'Hira Saleem', 60),
('BCA1004', 'Jawad Hassan', 79),
('BCA1005', 'Sundas Sarfraz', 80);

Table creation and Insertion SQL Query of Employee Table:


CREATE TABLE Employees (
Emp_Id INT PRIMARY KEY,
Emp_Salary INT
);
INSERT INTO Employees (Emp_Id, Emp_Salary) VALUES
(201, 25000),
(202, 45000),
(203, 30000),
(204, 29000),
(205, 40000);

Example 1: This example shows all the values of every column from the table.
SELECT * FROM Student;
This SQL statement displays the following values of the student table:
Student_ID Student_Name Student_Marks
BCA1001 Riaz Ahmad 85
BCA1002 Salman Afzal 75
BCA1003 Hira Saleem 60
BCA1004 Jawad Hassan 79
BCA1005 Sundas Sarfraz 80

Department of Computer Science, UMT, Lahore. 3 Riaz Ahmad


Lab Manual: Database Systems
Example 2: This example shows all the values of a specific column from the table.
SELECT Emp_Id, Emp_Salary FROM Employee;
This SELECT statement displays all the values of Emp_Salary and Emp_Id column
of Employee table:
Emp_Id Emp_Salary
201 25000
202 45000
203 30000
204 29000
205 40000
Example 3: This example describes how to use the WHERE clause with the
SELECT DML command.
Let's take the following Student table:
Student_ID Student_Name Student_Marks
BCA1001 Riaz Ahmad 80
BCA1002 Salman Afzal 75
BCA1003 Hira Saleem 80
BCA1004 Jawad Hassan 79
BCA1005 Sundas Sarfraz 80
If you want to access all the records of those students whose marks is 80 from the above
table, then you have to write the following DML command in SQL:
SELECT * FROM Student WHERE Stu_Marks = 80;

The above SQL query shows the following table in result:


Student_ID Student_Name Student_Marks
BCA1001 Riaz Ahmad 80
BCA1003 Hira Saleem 80
BCA1005 Sundas Sarfraz 80

2. INSERT
INSERT is another most important data manipulation command in Structured Query
Language, which allows users to insert data in database tables.
Syntax:

INSERT INTO TABLE_NAME ( column_Name1 , column_Name2 , column_Name3 ,


.. column_NameN ) VALUES (value_1, value_2, value_3, ..value_N ) ;

Example 1: This example describes how to insert the record in the database table.

Department of Computer Science, UMT, Lahore. 4 Riaz Ahmad


Lab Manual: Database Systems
Let's take the following student table, which consists of only 2 records of the student.
Stu_Id Stu_Name Stu_Marks Stu_Age
101 Ramsha Noor 92 20
201 Umar Farooq 83 19

CREATE TABLE Student_CS (


Stu_Id INT PRIMARY KEY,
Stu_Name VARCHAR(50),
Stu_Marks INT,
Stu_Age INT
);

INSERT INTO Student_CS (Stu_Id, Stu_Name, Stu_Marks, Stu_Age) VALUES


(101, 'Ramsha Noor', 92, 20),
(201, 'Umar Farooq', 83, 19);

INSERT INTO Student_CS (Stu_id, Stu_Name, Stu_Marks, Stu_Age) VALUES (104,


“Riaz Ahmad”, 89, 19);

3. UPDATE
UPDATE is another most important data manipulation command in Structured Query
Language, which allows users to update or modify the existing data in database tables.

Syntax:

UPDATE Table_name SET [column_name1= value_1, ….., column_nameN = value_N


] WHERE CONDITION;

Here, 'UPDATE', 'SET', and 'WHERE' are the SQL keywords, and 'Table_name' is the
name of the table whose values you want to update.

Example 1: This example describes how to update the value of a single field.
Let's take a Product table consisting of the following records:
Product_Id Product_Name Product_Price Product_Quantity
P101 Chips 20 20
P102 Chocolates 60 40
P103 Maggi 75 5
P201 Biscuits 80 20
P203 Namkeen 40 50

Department of Computer Science, UMT, Lahore. 5 Riaz Ahmad


Lab Manual: Database Systems

CREATE TABLE Product (


Product_Id VARCHAR(10) PRIMARY KEY,
Product_Name VARCHAR(50),
Product_Price INT,
Product_Quantity INT
);

INSERT INTO Product (Product_Id, Product_Name, Product_Price, Product_Quantity)


VALUES
('P101', 'Chips', 20, 20),
('P102', 'Chocolates', 60, 40),
('P103', 'Maggi', 75, 5),
('P201', 'Biscuits', 80, 20),
('P203', 'Namkeen', 40, 50);

Suppose, you want to update the Product_Price of the product whose Product_Id is
P102. To do this, you have to write the following DML UPDATE command:

UPDATE Product SET Product_Price = 80 WHERE Product_Id = 'P102' ;

Example 2: This example describes how to update the value of multiple fields of the
database table.
Let's take a Student table consisting of the following records:
Stu_Id Stu_Name Stu_Marks Stu_Age
101 Riaz Ahmad 92 20
201 Salman Afzal 83 19
202 Hira Saleem 85 19
203 Jawad Hassan 95 21
102 Sundas Sarfraz 65 21
103 Mahnoor 78 19
104 Fiza Ali 98 20

Delete the previous student table using drop query then create this table:
CREATE TABLE Student (
Stu_Id INT PRIMARY KEY,
Stu_Name VARCHAR(50),
Stu_Marks INT,

Department of Computer Science, UMT, Lahore. 6 Riaz Ahmad


Lab Manual: Database Systems
Stu_Age INT
);

INSERT INTO Student (Stu_Id, Stu_Name, Stu_Marks, Stu_Age) VALUES


(101, 'Riaz Ahmad', 92, 20),
(201, 'Salman Afzal', 83, 19),
(202, 'Hira Saleem', 85, 19),
(203, 'Jawad Hassan', 95, 21),
(102, 'Sundas Sarfraz', 65, 21),
(103, 'Mahnoor', 78, 19),
(104, 'Fiza Ali', 98, 20);

Suppose, you want to update Stu_Marks and Stu_Age of that student whose Stu_Id is
103 and 202. To do this, you have to write the following DML Update command:

UPDATE Student SET Stu_Marks = 80, Stu_Age = 21 WHERE Stu_Id = 103 AND St
u_Id = 202;

4. DELETE
DELETE is a DML command which allows SQL users to remove single or multiple
existing records from the database tables.
This command of Data Manipulation Language does not delete the stored data
permanently from the database. We use the WHERE clause with the DELETE command
to select specific rows from the table.
Syntax:

DELETE FROM Table_Name WHERE condition;


Example 1: This example describes how to delete a single record from the table.
Let's take a Product table consisting of the following records:
Product_Id Product_Name Product_Price Product_Quantity
P101 Chips 20 20
P102 Chocolates 60 40
P103 Maggi 75 5
P201 Biscuits 80 20
P203 Namkeen 40 50
Suppose, you want to delete that product from the Product table whose Product_Id is
P203. To do this, you have to write the following DML DELETE command:

DELETE FROM Product WHERE Product_Id = 'P202' ;

Department of Computer Science, UMT, Lahore. 7 Riaz Ahmad


Lab Manual: Database Systems

Example 2: This example describes how to delete the multiple records or rows from
the database table.
Let's take a Student table consisting of the following records:
Stu_Id Stu_Name Stu_Marks Stu_Age
101 Riaz Ahmad 92 20
201 Salman Afzal 83 19
202 Hira Saleem 85 19
203 Jawad Hassan 95 21
102 Sundas Sarfraz 65 21
103 Mahnoor 78 19
104 Fiza Ali 98 20
Suppose, you want to delete the record of those students whose Marks is greater than 70.
To do this, you have to write the following DML Update command:

DELETE FROM Student WHERE Stu_Marks > 70 ;

DML Commands in Comparison to Other Command Types


SQL DML instructions are created specifically to be applied to data that is stored in a
database. These procedures can also be used to update, delete or even insert data in the
already created table.

Graded Exercises
Employees Table:

create table Emplyees


(
EmpNum int primary key,
EmpName varchar(25),
Job varchar(25),
Mgr int,
HireDate DATE,
Salary int,
commission int,
DepartmentNo int
);

insert into Employees()


values(7369, 'Salman', 'Clerk', 7902, '2000/12/17', 8000,Null,20),
(7499, 'Alia', 'Salesman', 7698, '2001/02/20', 16000,3000,30),
(7521, 'Waheed', 'Salesman', 7698, '2001/02/22', 12500,5000,30),

Department of Computer Science, UMT, Lahore. 8 Riaz Ahmad


Lab Manual: Database Systems
(7566, 'Jamil', 'Manager', 7839, '2001/04/02', 29750,Null,20),
(7654, 'Mehwish', 'Salesman', 7698, '2001/09/28', 12000,12500,30),
(7698, 'Bilal', 'Manager', 7839, '2001/05/01', 28500,Null,30),
(7782,'Qasim','Manager',7839,'2001/06/9',24500,Null,10),
(7788,'Shams','Analyst',7566,'2002/12/09',30000,Null,20),
(7839,'Kamran','President',Null,'2001/11/17',50000,Null,Null),
(7844,'Tanveer','Salesman',7698,'2001/09/08',15000,0,30),
(7876,'Aftab','Clerk',7788,'2003/01/12',11000,Null,20),
(7900,'Javeria','Clerk',7698,'2001/10/03',9500,Null,30),
(7902,'Fozia','Analyst',7566,'2001/12/03',30000,Null,20),
(7934,'Murad','Clerk',7782,'2002/01/23',13000,Null,10);

Graded Exercise Questions:

1. Insert a New Employee:


Add a new employee named "Hassan" as a Salesman with EmpNum = 8000, working under Mgr
= 7698, hired on '2003-05-15', with a salary of 14000, commission 2000, and assigned to
DepartmentNo = 30.
2. Update Salary:
Increase the salary of all employees in DepartmentNo = 30 by 10%.
3. Delete an Employee:
Remove the employee named "Murad" from the Employees table.
4. Change Job Title:
Update the job title of employee "Waheed" from 'Salesman' to 'Senior Salesman'.
5. Set Null Values:
Set the commission of employees in DepartmentNo = 20 to Null if it is currently 0.
6. Increase Commission:
Increase the commission of all Salesmen (Job = 'Salesman') by 2000 where it is not null.
7. Transfer Employee:
Change the DepartmentNo of "Tanveer" from 30 to 20.
8. Promote an Employee:
Update the job title of "Aftab" to 'Senior Clerk' and increase his salary by 5000.
9. Delete Employees with Low Salary:
Remove all employees who have a salary less than 10000.
10. Reset Commission to Zero:
Set the commission of all employees to 0 where it is currently Null.

Happy Learning! May Allah Enhance your knowledge😊

Department of Computer Science, UMT, Lahore. 9 Riaz Ahmad

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy