Lab 3 SQL DML - Techware Downloads

Lab Manual for Introduction to Database Systems
Lab-03
SQL DML (UPDATE, DELETE, SELECT Advanced ORDER, DISTINCT, LIMIT)
Lab 3:SQL DML (UPDATE, DELETE, SELECT Advanced ORDER, DISTINCT, LIMIT)
Table of Contents
1.
Introduction
49
2.
Activity Time boxing
49
3.
Objective of the experiment
49
4.
Concept Map
49
4.1.
4.2.
4.3.
4.4.
4.5.
4.6.
49
49
50
50
50
51
5.
6.
Modifying Data - UPDATE
Deleting Rows - DELETE FROM
ORDER BY Clause
LIMIT Clause
AS - Alias
DISTINCT
Homework before Lab
51
5.1. Problem Solution Modeling
5.1.2.
Practices from home
5.1.3. Task-1
5.1.4. Task-2
51
51
51
52
Procedure& Tools
52
6.1. Tools
6.2. Setting-up and Setting Up XAMPP (MySQL, Apache)
5mins]52
6.3. Walkthrough Task [Expected time = 30mins]
6.3.1.
Modifying Data - UPDATE
6.3.2.
Deleting Rows - DELETE FROM
6.3.3.
ORDER BY Clause
6.3.4.
LIMIT Clause
6.3.5.
AS - Alias
6.3.6.
DISTINCT
7.
Practice Tasks
7.1.
7.2.
7.3.
Practice Task 1
Out comes
Testing
8.
Evaluation Task (Unseen)
9.
Evaluation criteria
52
[Expected time =
52
54
57
58
59
61
61
63
[Expected time = 15mins]
[Expected time = 60mins for two tasks]
63
64
64
64
64
10.
Further Reading
64
10.1.
Books
64
Text Book
64
10.2.
Slides
64
11.
REFERENCES:
65
Department of Computer Science,
C.U.S.T.
Page 47
Lab 3:SQL DML (UPDATE, DELETE, SELECT Advanced ORDER, DISTINCT, LIMIT)
11.1.
SQL-99 Complete, Really, by Peter Gulutzan & Trudy Pelzer.
Department of Computer Science,
C.U.S.T.
65
Page 48
Lab 3:SQL DML (UPDATE, DELETE, SELECT Advanced ORDER, DISTINCT, LIMIT)
Lab 3: SQL DML (UPDATE,
WITHORDER, DISTINCT, LIMIT)
DELETE,
SELECT
1. Introduction
In this lab you will learn about the UPDATE, DELETE, SELECT,WITH ORDER, DISTINCT,
LIMIT. Update command is used to update a row of a table. Delete command is used to delete
data from a table. Delete command can also be used with condition to delete a particular row.
Throughout this lab you can learn how data is update in the table and how a row is delete from
table.
Relevant Lecture Material
a) Revise Lecture No. 5 and 6
b) Text Book: Java: Text Book: Database Systems, A practical approach to design,
implementation and management by Thomas Connolly, Carolyn Begg, Addison
Wesley , Fifth Edition,
1. Read pages:
2. Read URL:
i. http://www.studytonight.com/dbms/dml-command.php
3. Revise the DML(Update, Delete)
2. Activity Time boxing
Table 1: Activity Time Boxing
Task No. Activity Name
6.2
Setting-up and Setting
XAMPP (MySQL, Apache)
6.3
Walkthrough Tasks
7
Practice tasks
8
Evaluation Task
Up
Activity time
5mins
Total Time
5mins
30mins
20 to 30mins for each task
60mins for all assigned task
60mins
50mins
40mins
3. Objective of the experiment
 To get basic understanding of UPDATE data from table
 To get basic understanding of DELETE a row using conditional statements
 To get basic understanding of SELECT statement with ORDER, DISTINCT, and LIMIT
4. Concept Map
4.1.Modifying Data - UPDATE
To modify existing data, use UPDATE ... SET command, with the following syntax:
UPDATE tableName SET columnName = {value|NULL|DEFAULT}, ...WHERE criteria
4.2.Deleting Rows - DELETE FROM
Use the DELELE FROM command to delete row(s) from a table, with the following syntax:
Department of Computer Science,
C.U.S.T.
Page 49
Lab 3:SQL DML (UPDATE, DELETE, SELECT Advanced ORDER, DISTINCT, LIMIT)
-- Delete all rows from the table. Use with extreme care! Records are NOT recoverable!!!
DELETE FROM tableName
-- Delete only row(s) that meets the criteria
DELETE FROM tableName WHERE criteria
4.3.ORDER BY Clause
You can order the rows selected using ORDER BY clause, with the following syntax:
SELECT ... FROM tableName
WHERE criteria
ORDER BY columnAASC|DESC, columnBASC|DESC, ...
The selected row will be ordered according to the values in columnA, in either ascending (ASC)
(default) or descending (DESC) order. If several rows have the same value in columnA, it will be
ordered according to columnB, and so on. For strings, the ordering could be case-sensitive or
case-insensitive, depending on the so-called character collating sequence used.
4.4.LIMIT Clause
A SELECT query on a large database may produce many rows. You could use the LIMIT clause
to limit the number of rows displayed, e.g.
-- Display the first two rows
SELECT productName,quantityInStock ,buyPrice FROM products ORDER BY buyPrice LIMIT
2;
+----------------------------------------------+--------------------+----------+
| productName | quantityInStock | buyPrice |
+----------------------------------------------+--------------------+----------+
| 1958 Chevy Corvette Limited Edition |
2542 | 15.91 |
| 1982 Lamborghini Diablo
|
7723 | 16.24 |
+----------------------------------------------+--------------------+----------+
To continue to the following records, you could specify the number of rows to be skipped,
followed by the number of rows to be displayed in the LIMIT clause.
4.5.AS - Alias
You could use the keyword AS to define an alias for an identifier (such as column name, table
name). The alias will be used in displaying the name. It can also be used as reference. For
example,
SELECT productName AS Name,quantityInStock AS Quantity , buyPrice AS 'Unit Price' FROM
products ORDER BY buyPriceDesc LIMIT 2;
+---------------------------------------+-----------+------------+
| Name
| Quantity | Unit Price |
+---------------------------------------+-----------+------------+
| 1962 LanciaA Delta 16V
| 6791 | 103.42 |
| 1998 Chrysler Plymouth Prowler | 4724 | 101.51 |
+---------------------------------------+-----------+------------+
Department of Computer Science,
C.U.S.T.
Page 50
Lab 3:SQL DML (UPDATE, DELETE, SELECT Advanced ORDER, DISTINCT, LIMIT)
Take note that the identifier "Unit Price" contains a blank and must be back-quoted.
4.6.DISTINCT
A column may have duplicate values, we could use keyword DISTINCT to select only distinct
values. We can also apply DISTINCT to several columns to select distinct combinations of these
columns. For examples,
-- With DISTINCT
SELECT DISTINCT productVendor from products;
+----------------------------------+
| productVendor|
+----------------------------------+
| Min Lin Diecast|
| Classic Metal Creations |
| Highway 66 Mini Classics |
| Red Start Diecast |
| Motor City Art Classics |
| Second Gear Diecast|
| Autoart Studio Design |
| Welly Diecast Productions |
| Unimax Art Galleries |
| Studio M Art Models
|
| Exoto Designs
|
| Gearbox Collectibles |
| Carousel DieCastLegends |
+---------------------------------+
5. Homework before Lab
You must solve the following problems at home before the lab.
5.1.Problem Solution Modeling
After reading the reference material mentioned in the introduction, now you are ready to perform
homework assigned to you
5.1.1. Problem description:
This task is the next step of the Lab-01 Task 01.
Create two roles like Customer and Employees. Customer can access the outlet and generate an
order, according to the order details pay the amount to the employee. Customer can perform
select operation on productlines and products relation. Customer can INSERT, UPDATE, and
Delete operation on these tables like orderdetails, orders, and payments.
5.1.2. Practices from home
Solve the following subtasks.
5.1.3. Task-1
Update the Employee and customer name and submit the screen shots.
Department of Computer Science,
C.U.S.T.
Page 51
Lab 3:SQL DML (UPDATE, DELETE, SELECT Advanced ORDER, DISTINCT, LIMIT)
5.1.4. Task-2
Describe the difference between where and having clause.
6. Procedure& Tools
6.1.Tools
In this section tools installation and setup is defined.
6.2.Setting-up and Setting Up XAMPP (MySQL, Apache)
[Expected time = 5mins]
Refer to Lab 1 sec 6.2.
6.3.Introduction to “phpMyAdmin”
After setting up XAMPP, open any web browser and write „localhost/phpmyadmin/‟,the
following panel will appear on the browser. (Refer to Figure 1)
Figure 1: phpMyAdmin Panel
phpMyAdmin is a free and open source tool written in PHP intended to handle the
administration of MySQL or MariaDB with the use of a web browser. It can perform various
tasks
such
as
creating,
modifying
or
deleting databases, tables, fields or rows;
executing SQL statements; or managing users and permissions.
The left side of Panel shows the names of all the databases. Select your database from the list. It
will show all the tables present in the database (Refer to figure 2).
Department of Computer Science,
C.U.S.T.
Page 52
Lab 3:SQL DML (UPDATE, DELETE, SELECT Advanced ORDER, DISTINCT, LIMIT)
Figure 2: Tables present in database
Now select a table you want to work on.
Figure 3: Select Productlines table
Figure 4 shows the data present in the selected table.
Department of Computer Science,
C.U.S.T.
Page 53
Lab 3:SQL DML (UPDATE, DELETE, SELECT Advanced ORDER, DISTINCT, LIMIT)
Figure 4: Data present in table
6.4.Walkthrough Task
[Expected time = 30mins]
6.4.1. Modifying Data - UPDATE
Figure 5 shows the values of „Product‟ table before applying Update query.
Department of Computer Science,
C.U.S.T.
Page 54
Lab 3:SQL DML (UPDATE, DELETE, SELECT Advanced ORDER, DISTINCT, LIMIT)
Figure 5: Products table before applying update query
To apply query using phpMyAdmin Panel, select „SQL‟ option from the top Menu and click
„Update‟ button (Refer to figure 6(a))
Figure 6(a):Using phpMyAdmin panel to execute query
Write the desired query and Click „Go‟ (Refer to figure 6(b)).
Department of Computer Science,
C.U.S.T.
Page 55
Lab 3:SQL DML (UPDATE, DELETE, SELECT Advanced ORDER, DISTINCT, LIMIT)
Figure 6(b):Using phpMyAdmin panel to execute query
To modify existing data, use UPDATE ... SET command. Figure 6 shows the data of „Products‟
table after executing the following query:
Query: UPDATE `products` SET `buyPrice`= `buyPrice`*1.1
Figure 7: Products table after applying Update query
Department of Computer Science,
C.U.S.T.
Page 56
Lab 3:SQL DML (UPDATE, DELETE, SELECT Advanced ORDER, DISTINCT, LIMIT)
Query: UPDATE `products` SET `quantityInStock`= `quantityInStock`-100 Where
`productLine` ='MotorCycles'
Figure 8: Update Records using Condition
CAUTION: If the WHERE clause is omitted in the UPDATE command, ALL ROWS will be
updated. Hence, it is a good practice to issue a SELECT query, using the same criteria, to check
the result set before issuing the UPDATE. This also applies to the DELETE statement in the
following section.
6.4.2. Deleting Rows - DELETE FROM
Use the DELELE FROM command to delete row(s) from a table.
For example,
Query: DELETE FROM `products` WHERE `quantityInStock`<=0
Department of Computer Science,
C.U.S.T.
Page 57
Lab 3:SQL DML (UPDATE, DELETE, SELECT Advanced ORDER, DISTINCT, LIMIT)
Figure 9: Delete Rows
6.4.3. ORDER BY Clause
You can order the rows selected using ORDER BY clause.
For example,
Query: SELECT * FROM `products` ORDER BY `quantityInStock` DESC
Figure 10: Order By clause
Department of Computer Science,
C.U.S.T.
Page 58
Lab 3:SQL DML (UPDATE, DELETE, SELECT Advanced ORDER, DISTINCT, LIMIT)
You can randomize the returned records via function RAND(), e.g.,
Query: SELECT * FROM `products` ORDER BY RAND()
Figure 11: Order By with RAND()
6.4.4. LIMIT Clause
A SELECT query on a large database may produce many rows. You could use the LIMIT clause
to limit the number of rows displayed, e.g.
Query: SELECT * FROM `products` ORDER BY `buyPrice` LIMIT 5
Department of Computer Science,
C.U.S.T.
Page 59
Lab 3:SQL DML (UPDATE, DELETE, SELECT Advanced ORDER, DISTINCT, LIMIT)
Figure 12: Limit Clause
To continue to the following records, you could specify the number of rows to be skipped,
followed by the number of rows to be displayed in the LIMIT clause, as follows:
Query: SELECT * FROM `products` ORDER BY `buyPrice` LIMIT 1,5
Figure 13: Limit Clause with specific rows
Department of Computer Science,
C.U.S.T.
Page 60
Lab 3:SQL DML (UPDATE, DELETE, SELECT Advanced ORDER, DISTINCT, LIMIT)
6.4.5. AS - Alias
You could use the keyword AS to define an alias for an identifier (such as column name, table
name). The alias will be used in displaying the name. It can also be used as reference. For
example,
Query: SELECT `productCode` AS ID, `productName` AS Name,`quantityInStock` AS
Quantity, `buyPrice` AS Price FROM `products` ORDER BY ID
Figure 14:UsingAlias
6.4.6. DISTINCT
A column may have duplicate values, we could use keyword DISTINCT to select only distinct
values. We can also apply DISTINCT to several columns to select distinct combinations of these
columns. For example,
Query: SELECT `productLine` AS 'Product category' FROM `products`
Department of Computer Science,
C.U.S.T.
Page 61
Lab 3:SQL DML (UPDATE, DELETE, SELECT Advanced ORDER, DISTINCT, LIMIT)
Figure 15: Without using Distinct
Query: SELECT DISTINCT `productLine` AS 'Product category' FROM `products`
Department of Computer Science,
C.U.S.T.
Page 62
Lab 3:SQL DML (UPDATE, DELETE, SELECT Advanced ORDER, DISTINCT, LIMIT)
Figure 16: Using DISTINCT
7. Practice Tasks
This section will provide more practice exercises which you need to finish during the lab. You
need to finish the tasks in the required time. When you finish them, put these tasks in the
following folder:
\\dataserver\assignments$
7.1.Practice Task 1
[Expected time = 15mins]
Consider the schema given in Lab01 (7.1 Practice Task), write down following SQL queries.
You can use data types of your own choice.
1. Create table “CUSTOMERS” with all required constraints.
2. Alter table “CUSTOMERS” to apply primary key constraint.
3. Alter table “PAYMENTS” to apply the foreign key constraint.
4. Add AUTO-INCREMENT constraint to CUSTOMERS table.
5. INSERT complete record of an order (Hint: Order contains multiple products with different
quantities)
6. Select all ORDERS whose status is “PENDING”.
7. Select all customers where customerName contains a word “MR.” at start.
8. Select all customers where customerName contains a word “MINI”.
9. Select all payments of the year “2015”.
10. Show quantity available in stock of all products.
Department of Computer Science,
C.U.S.T.
Page 63
Lab 3:SQL DML (UPDATE, DELETE, SELECT Advanced ORDER, DISTINCT, LIMIT)
11. Show customer number, order number, status of all orders.
12. Show all customers who were not serviced by a Sales Representative.
13. Show all customers serviced by Sales Representative whose employeenumber is 115.
14. Show all orders those are not shipped yet (Hint: where shipped date is NULL)
15. Show all customers who have not provided addressLine2.
16. Show customer number, order number, status of all orders made by customer whose
customernumber is 108.
17. Update status of all orders to “DELIVERED” where shipped date is between 2015 and 2016.
18. Delete complete record of an order (Hint: multiple queries are required).
7.2.Out comes
After completing this lab, student will be able to understand the UPDATE, DELETE, SELECT
Advanced ORDER, DISTINCT, and LIMIT clauses and it‟s functionality.
7.3.Testing
Test Cases for Practice Task-1
8. Evaluation Task (Unseen)
[Expected time = 60mins for two tasks]
The lab instructor will give you unseen task depending upon the progress of the class.
9. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each
task is assigned the marks percentage which will be evaluated by the instructor in the lab whether
the student has finished the complete/partial task(s).
Table 3: Evaluation of the Lab
Sr. No.
Task No
1
6
7
2
3
8
Description
Procedures and Tools
Practice tasks and Testing
Evaluation Tasks (Unseen)
Marks
05
15
80
10. Further Reading
This section provides the references to further polish your skills.
10.1. Books
Text Book
Database Systems, A practical approach to design, implementation and management by Thomas
Connolly, Carolyn Begg, Addison Wesley , Fifth Edition,
10.2. Slides
Department of Computer Science,
C.U.S.T.
Page 64
Lab 3:SQL DML (UPDATE, DELETE, SELECT Advanced ORDER, DISTINCT, LIMIT)
The slides and reading material can be accessed from the folder of the class instructor available
at \\dataserver\jinnah$\
11. REFERENCES:
11.1. SQL-99 Complete, Really, by Peter Gulutzan& Trudy Pelzer.






More examples for the SELECT command:
http://dev.mysql.com/doc/mysql/en/select.html
MySQL operators:
http://dev.mysql.com/doc/mysql/en/non-typed_operators.html
Built-in functions:
http://dev.mysql.com/doc/mysql/en/functions.html
Joining tables:
http://www.melonfire.com/community/columns/trog/article.php?id=148
Using subqeries:
http://www.melonfire.com/community/columns/trog/article.php?id=204
Using subqeries:
http://www.melonfire.com/community/columns/trog/article.php?id=204
Department of Computer Science,
C.U.S.T.
Page 65