Database Systems-SEDB-227-Lab Manual
Database Systems-SEDB-227-Lab Manual
DATABASE SYSTEMS
SEDB-227
Tools/ Technologies
• Mysql OR Oracle OR SQL management studio
2
TABLE OF CONTENTS
Preface ....................................................................................................................................................... 2
Tools/ Technologies .................................................................................................................................. 2
LAB 01: Installation of Relational Database Management Systems ................................................... 4
LAB 02: Retrieving Data Using the SQL SELECT Statement ............................................................ 4
LAB 03: Restricting using where clause and Sorting Data using order by clause ........................... 10
LAB 4: Using Single-Row Character Functions to Customize Output ........................................ 19
LAB 05: Using Single-Row Date Functions to Customize Output & Type conversion ................... 26
LAB 06: Constructing ERD using VISIO or draw.io ......................................................................... 30
LAB 07 : Using DDL Statements to Create and Manage Tables ....................................................... 32
LAB 08 : Creating other schema objects like table level and column level constraints .................. 35
LAB 09: Aggregating data using SQL Aggregate functions and group functions ........................... 40
LAB 10: Constructing enhanced ERD using visio or draw.io ........................................................... 47
LAB 11: Use of joins for displaying data from multiple tables .......................................................... 48
LAB 12: Sub Queries ............................................................................................................................. 51
LAB 13: Set Operations in MySQL ...................................................................................................... 55
LAB 14: Normalization.......................................................................................................................... 60
LAB 15: SQL project evaluation lab .................................................................................................... 61
3
LAB 01: Installation of Relational Database Management Systems
Objectives
● To discuss the role of relational database management studio
● To install MySQL or Oracle or Sql server
Theoretical description
1. Step 1: Go to mysql.com
2. Step 2:Go to downloads and select mysql community gpl downloads and select mysqlinstaller for
windows
3. Step 3:It will show you Generally Available (GA) Releases. Where we can see two different
installers, one is a web community installer which comes as a little file and another one is MySQL
installer community. Click the Download button on thesecond one (mysql-installer-community)
4. Step 4 It will ask your MySQL credentials to download the .msi file. If you have your credentials,
you can log in or else if you wish to sign up now you can click on thegreen coloured signup button.If
you are not interested in login or sign up for now, you can directly go and clickon No thanks, just
start my download option. It will download selected MySQL for you on your local machine
5. Step 5:Go to downloads folder where you can see the mysql-installer-community file, right
click on that file and click Install option.
This window configures the installer, in the middle, it may ask you for permissions to change
your computer settings or firewall confirmation, you can accept and then it will take a few
seconds to configure the installer.
4
6. Step 6: Read the license agreement and accept the license terms.
7. Step 7:This window provides you to set up different types of MySQL installations. You can set up
Mysql in 5 different types as provided below. Now I am selecting the server only not I am a
developer so that I need all the products which help my development Theoretical Descriptions.
Click on Next.
5
8. Step 8:Based on your Windows configuration, it may prompt you like “One or moreproduct requirements
have not been satisfied”. You can just click on YES.
10. Step 10:Upon successful execution of all required products, now the MySQL allows usto configure the
server settings. Click on Next to configure the server
6
11. Step 11:Select default setting
7
14. Step 14:Click on reconfigure
16. Step 16
8
17. Step 17:Enter root password i.e password in my case and click check it which shows
green symbol andclick next
20. Step 20:Now go back to mysql.com and select downloads and select mysql
community gpl downloadsand select mysql work bench and click on download button
in below screen
22. Step 22:Work bench is installed and screen shown is click on local instance and enter
your password
2
BS (Software Engineering) 2023
Lab Task
3
BS (Software Engineering) 2023
4
BS (Software Engineering) 2023
• In SQL*Plus, you are required to end each SQL statement with a semicolon (;).
Arithmetic Expressions
Create expressions with number and date data by using arithmetic operators.
5
BS (Software Engineering) 2023
3*2+10=16, 3*(2+10)=36
6
BS (Software Engineering) 2023
• Immediately follows the column name (There can also be the optional AS keyword
between the column name and alias.)
• Requires double quotation marks if it contains spaces or special characters, or if it is case-
sensitive
Using Column Aliases
Concatenation Operator
A concatenation operator:
• Links columns or character strings to other columns
• Is represented by command concat
• Creates a resultant column that is a character expression
Duplicate Rows
The default display of queries is all rows, including duplicate rows.
SELECT department_id
FROM employees;
7
BS (Software Engineering) 2023
8
BS (Software Engineering) 2023
Lab Task
Consider the database named as world. mySQL given to you and perform following
tasks on it
Q1. Write an SQL query to display all records from city table
Q2. Write an SQL Query to display the following records. Give reasoning also that
which table would be used get the following records
• Name
• local name
• government form
• Head of state
• Capital
Q3. Describe the structure of country language table and city table and also explain in your
words about the output result
Q4.
(a) Consider country table and use arithmetic operators within SQL query to calculate 10 %
on population column
(b). Consider any one table of database. Take at least any one appropriate scenario and apply
operator precedence. Also give reasoning
Q5. Consider country language table. Write SQL query to concatenate columns i.e. Country
code and language .Also use column alias to rename the concatenated column as country
details column
9
BS (Software Engineering) 2023
LAB 03: Restricting using where clause and Sorting Data using order by
clause
Objectives
• To limit the rows that are retrieved by a query
• To sort the rows that are retrieved by a query
• To use ampersand substitution to restrict and sort output at run time
Theoretical Description
10
BS (Software Engineering) 2023
11
BS (Software Engineering) 2023
12
BS (Software Engineering) 2023
13
BS (Software Engineering) 2023
14
BS (Software Engineering) 2023
15
BS (Software Engineering) 2023
RULES OF PRECENDENCE
16
BS (Software Engineering) 2023
17
BS (Software Engineering) 2023
Lab Task
Consider the database given to you such as world.sql and perform following tasks
Q1. Write a Single Query to display local name and Gross National Product for Countries who
specifically are in range of 372.00,4834,11705,60,650) Or those Countries who’s Local Name
starts off with B
Q3 Select Name, Capital, Region and Independence Year from Country Table Where Year
Must be Equal to ‘1991’ and The Name Must be Start with ‘A’ and Capital Must be Less Than
500.
Q4 Select Surface Area and Population from Country Table Where Population Must be Equal
to 500000 and Surface Area should Between 1000(lower) And 5000(Higher)
18
BS (Software Engineering) 2023
Theoretical description
SINGLE ROW FUNCTIONS
Single row functions are the one who work on single row and return one output per row.
For example, length and case conversion functions are single row functions. Single row
functions can be character functions, numeric functions, date functions, and
conversion functions. These functions require one or more input arguments and operate on
each row, thereby returning one output value for each row.
Single row functions can be used in SELECT statement, WHERE and ORDER BY clause.
19
BS (Software Engineering) 2023
20
BS (Software Engineering) 2023
21
BS (Software Engineering) 2023
22
BS (Software Engineering) 2023
23
BS (Software Engineering) 2023
24
BS (Software Engineering) 2023
Lab Task
Consider database given to you i.e classic models’ and answer following questions
Q1 write a single sql query to display employee first name, last name and job joined together,
similarly display length of employee email address, and the numeric position of letter A in the
employee last name, for all employees whose employee number is 1002 and job is president
Q2. Write a query to display the last names of all customers who have the letter p in their first
name and the letter A at the 5th position in their last name.
Q3. Write a single query which covers all character manipulation functions such as
substring,instring,concat,upper case,trim,replace and lpad or rpad and should use where clause
as well as comparison operators
Q4.Format the credit limit column to be 15 character long ,left padded with Rs of only those
customers whose last name is king and their credit limit is greater than 10000 .Also label
column as credit balance
Q5. consider order table and round off the price of only those orders whose order number is
10100
25
BS (Software Engineering) 2023
LAB 05: Using Single-Row Date Functions to Customize Output & Type
conversion
Objectives
• To describe various types of functions available in SQL
• To use character, number, and date functions in SELECT statements
Theoretical description
Date format functions
This function allows the developer to format the date anyway that they wish by specifying a
sequence of format strings. A string is composed of the percentage symbol '%' followed by a
letter that signifies how we wish to display the date. These are some of the more common
strings to use:
Syntax:
DATE_FORMAT(date, sequence)
26
BS (Software Engineering) 2023
EXAMPLE
EXAMPLE 2
CHANGING DATE VALUES
28
BS (Software Engineering) 2023
Lab Task
Q1. consider order table and round off the price of only those orders whose
order number is 10100
Q2. consider three major scenarios on classic model database in which
major data format and time functions are covered?
Q3. Explain following functions and also explain how sql queries are
written for them
• Time difference()
• Time stamp()
• Sysdate()
29
BS (Software Engineering) 2023
Objectives
⮚ To learn about Entity Relationship Diagram (ERD).
⮚ To learn about the notations used in tools.
⮚ We will make an ER Diagram using SmartDRAW.
Theoretical description
Entity Relationship Diagram:
An entity–relationship model describes interrelated things of interest in a specific domain of
knowledge. A basic ER model is composed of entity types and specifies relationships that can
exist between entities. ER Diagrams are most often used to design or debug relational databases
in the fields of software engineering, business information systems, education and research.
30
BS (Software Engineering) 2023
5. Relationship
SmartDraw:
SmartDraw is a diagram tool used to make flowcharts, organization charts,
mind maps, project charts, and other business visuals. SmartDraw allows you to draw and print
architectural and engineering diagrams to scale. SmartDraw even provides an AutoCAD-like
annotation layer that automatically resizes to match a diagram.
ERD Diagram:
Lab Task
Q1 create erd for following
An MDXCS computer supply is a wholesale company which buys computers from suppliers
and sells them to customers. MDXCS has a number of suppliers and each product is supplied
by on supplier only. However, suppliers may supply more than one product. MDXCS also
keeps record of suppliers who are not currently supplying any products. The company has
divided their operation into different geographical areas. Each storehouse is responsible for
each geographical area to deliver products to customers. Customers may be located in more
than one place and every storehouse has at least one customer. There are a number of
storehouses which hold stocks of products and these storehouses are assigned to deliver
orders to customers. Suppliers supply product to a particular storehouse and then
MDXCS stock in other depots. When a customer places an order, the company gives them a
unique order reference number. One order can be for different products. Customer can pay by
card or cash and orders are marked as delivered by entering a delivery date.
Note: Specify all the entities, relationships and their cardinalities
31
BS (Software Engineering) 2023
32
BS (Software Engineering) 2023
33
BS (Software Engineering) 2023
Lab Task
Q1 Create tables of your project
Q2. Insert data that is 30 to 40 records in all tables in your project
34
BS (Software Engineering) 2023
LAB 08 : Creating other schema objects like table level and column level
constraints
Objectives
● Understand table level and column level constraints and their working
Theoretical Description
SQL Constraints
• Constraints are used to limit the type of data that can go into a table.
• MySQL constraints are statements that can be applied at the column level or table level to
specify rules for the data that can be entered into a column or data table
35
BS (Software Engineering) 2023
36
BS (Software Engineering) 2023
37
BS (Software Engineering) 2023
38
BS (Software Engineering) 2023
Lab Task
Q2.
39
BS (Software Engineering) 2023
LAB 09: Aggregating data using SQL Aggregate functions and group
functions
Objectives
• Identify aggregate functions
• Identify the available group functions
• Describe the use of group functions
• Group data by using the GROUP BY clause
• Include or exclude grouped rows by using the HAVING clause
Theoretical description
Group functions
⮚ They give the user the ability to answer business questions such as:
▪ What is the average salary of an employee in the company?
▪ What were the total salaries for a particular year?
▪ What are the maximum and minimum salaries in the Computer Department?
⮚ They perform a variety of actions such as counting all the rows in a table, averaging a
column’s data, and summing numeric data.
⮚ They can also search a table to find the highest “MAX” or lowest “MIN” values in a
column.
40
BS (Software Engineering) 2023
Example
41
BS (Software Engineering) 2023
42
BS (Software Engineering) 2023
43
BS (Software Engineering) 2023
44
BS (Software Engineering) 2023
45
BS (Software Engineering) 2023
Lab Task
Q1 .Create following table with all these records and apply all types of
aggregate functions you have learned so far on the appropriate data from
all the columns in below mentioned table and show the SQL query and the
resultant output also
46
BS (Software Engineering) 2023
Q2. Consider Learning management system and list down business rules and create an
enhanced entity relationship diagram for it
Note: Apply all Enhanced ERD concepts
47
BS (Software Engineering) 2023
LAB 11: Use of joins for displaying data from multiple tables
Objectives
• Retrieve data from different tables
Theoretical Description
Create different tables applying referential integrity constraints. Use foreign keys for integrity
constraints and then apply different joins to overserve the retrieved data.
A JOIN clause is used to combine rows from two or more tables, based on a related column
between them.
Syntax:
⮚ SELECT column_name(s) FROM table1 JOIN table2 ON table1.column_name =
table2.column_name;
JOIN and INNER JOIN are same in MySQL:
⮚ SELECT column_name(s) FROM table1 INNER JOIN table2 ON
table1.column_name = table2.column_name;
Tables:
48
BS (Software Engineering) 2023
Example:
1. Now, we create the following SQL statement (that contains an INNER JOIN), that selects
records that have matching values in both tables:
mysql> select STUDENT.sname,COURSE.coursename from STUDENT JOIN
COURSE ON STUDENT.sid=COURSE.sid;
The INNER JOIN keyword selects all rows from both tables as long as there is a match
between the columns. If there are records in the "STUDENT" table that do not have matches
in "COURSE", these will not be shown.
2. Now, we create the following SQL statement (that contains an INNER JOIN), that selects
all records that have matching values in both tables:
mysql> select * from STUDENT JOIN COURSE ON STUDENT.sid=COURSE.sid;
49
BS (Software Engineering) 2023
Lab Task
Q1. Consider your project and apply joins on two tables as well as multiple tables
50
BS (Software Engineering) 2023
● The subquery (inner query) executes once before the main query (outer query)
executes.
● The main query (outer query) use the subquery result.
Using a subquery, list the name of the employees, paid more than 'Alexander' from
emp_details .
51
BS (Software Engineering) 2023
A subquery can be used before or after any of the comparison operators. The subquery can
return at most one value. The value can be the result of an arithmetic expression or a column
function. SQL then compares the value that results from the subquery with the value on the
other side of the comparison operator. You can use the following comparison operators:
Operator Description
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
!= Not equal to
<> Not equal to
<=> NULL-safe equal to operator
For example, suppose you want to find the employee id, first_name, last_name, and salaries
for employees whose average salary is higher than the average salary throughout the
company.
52
BS (Software Engineering) 2023
The following query selects the department with the highest average salary. The subquery
finds the average salary for each department, and then the main query selects the department
with the highest average salary.
Note: Here we have used ALL keyword for this subquery as the department selected by the
query must have an average salary greater than or equal to all the average salaries of the other
departments.
The following query selects any employee who works in the location 1800. The subquery
finds the department id in the 1800 location, and then the main query selects the employees
who work in any of these departments.
A row subquery is a subquery that returns a single row and more than one column value. You
can use = , >, <, >=, <=, <>, !=, <=> comparison operators. See the following examples:
In the following examples, queries shows differentr result according to above conditions :
Lab Task
Q1. Consider your project and write multiple scenarios to apply subqueries
54
BS (Software Engineering) 2023
We are going to use the following EmployeeUK and EmployeeUSA tables to understand the
SET Operators in MySQL.
55
BS (Software Engineering) 2023
Once you execute the above query, you will get the following result set. Please observe, here
we don’t have any duplicate data. Here, in the result set, we got a total of 11 rows out of 14
rows. This is because 3 rows are present in both the result set.
56
BS (Software Engineering) 2023
Here you can see the employees are sorted according to their FirstName column values.
MySQL EXCEPT Operator:
The EXCEPT operator is used to combine two tables or two result sets and will return rows
from the first select statement that are not present in the second select statement. Following is
the syntax of EXCEPT Operator.
57
BS (Software Engineering) 2023
But, the EXCEPT Operator is not supported by MYSQL. We can achieve the EXCEPT
Operator functionality in MySQL using the following ways.
Using NOT IN Operator to achieve EXCEPT functionality:
Here, we are checking the FirstName column value only. Following is the SQL Query using
the NOT IN Operator which returns the employees from the first EmployeeUK table that are
not present in the EmployeeUSA table.
SELECT * FROM EmployeeUK
WHERE FirstName NOT IN (SELECT FirstName FROM EmployeeUSA);
Once you execute the above query, you will get the following result set.
58
BS (Software Engineering) 2023
But the INTERSECT Operator is not supported by MYSQL. We can achieve the INTERSECT
Operator functionality in MySQL using the following ways.
Using IN Operator to achieve INTERSECT functionality:
Here, we are checking the FirstName column value only. Following is the SQL Query using
the IN Operator which returns the common employees i.e. the employees which are present in
both t EmployeeUK and EmployeeUSA tables. Here, we are checking common based on the
First Name column value.
SELECT * FROM EmployeeUK
WHERE FirstName IN (SELECT FirstName FROM EmployeeUSA);
Once you execute the above query, you will get the following result set.
Lab Task
Q1 Practice union, union all, intersect and except on your project
59
BS (Software Engineering) 2023
60
BS (Software Engineering) 2023
61