Lab Manual 05 - DML - Data Manipulation Language
Lab Manual 05 - DML - Data Manipulation Language
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;
If we want to retrieve the data from all the columns of the table, we have to use the
following SELECT command:
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
2. INSERT
INSERT is another most important data manipulation command in Structured Query
Language, which allows users to insert data in database tables.
Syntax:
Example 1: This example describes how to insert the record in the database table.
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:
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
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:
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,
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:
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:
Graded Exercises
Employees Table: