Dbms Lab#4 Updated Final Faiza
Dbms Lab#4 Updated Final Faiza
LAB#4
MULTIPLE ROWS, VIEWS AND STORED
PROCEDURES
OBJECTIVE:
To learn and implement group functions
To learn and implement ‘GROUP BY’ and ‘HAVING’ clause.
To learn and implement views
To learn and implement predefined and user-defined stored procedures with input
parameters.
THEORY:
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.
MULTIPLE ROW FUNCTIONS
Multiple row functions work upon group of rows and return one result for the complete set of
rows. They are also known as Group Functions.
WHAT ARE GROUP FUNCTIONS?
Group functions operate on sets of rows to give one result per group.
CE-301L: Database Management Systems
EXAMPLE:
You can divide rows in a table into smaller groups by using the GROUP BY clause. All columns
in the SELECT list that are not in group functions must be in the GROUP BY clause.
VIEWS IN SQL:
A view is, at its core, nothing more than a stored query. What’s great is that you can mix and
match your data from base tables (or other views) to create what will, in most respects, function
just like an ordinary base table. You can create a simple query that selects from only one table
and leaves some rows or columns out, or you can create a complex query that joins several tables
and makes them appear as one.
CREATING A VIEW
Syntax:
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
CE-301L: Database Management Systems
Example#1:
In the following example, we have created a view which displays the employees’ names and
their departments by applying join operations on two tables.
Output:
Example#2
In the following query, we have created a view by using the previous view. Also, we have added
a condition for department location by using where clause.
Output:
UPDATING A VIEW
A view can be updated with the ALTER VIEW statement.
CE-301L: Database Management Systems
Syntax:
ALTER VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
Output:
STORED PROCEDURES
A Stored Procedure is precompiled collection of T-SQL statements. Stored procedures are
named and processed as a single unit.
SYSTEM STORED PROCEDURES
SQL Server provides some readily available stored procedures form managing SQL Server and
for users and database information retrieval. These can only be executed. The following table list
some system stored procedures.
CE-301L: Database Management Systems
Output:
CE-301L: Database Management Systems
Example#2
In the following example, we have created a procedure for the JOIN statements. To execute these
kind of statements, use BEGIN and END keyword.
Output:
EXECUTION
CE-301L: Database Management Systems
OUTPUT
LAB TASKS:
1. Create a table named Students’ GPAs with the following schema. (Student id, batch,
semester #, GPA)
Now Display the average GPA for each batch. List in order of decreasing batches.
2. Create a view which displays those student ids whose average GPAs are greater than or
equal to 3.
3. Create a procedure to display the maximum, minimum and average GPA of each student.
4. Consider the following schema:
Sailors (sid(PK), sname, rating, age)
Boats (bid(PK), bname, color)
Reserves (sid (FK), bid(FK), day(date)). Create a view that calculates the average age of
sailors for each rating level.
5. Create a view that shows the sailor names and the days they made reservations for
sailboats (boats with the color 'Blue').
6. Create a procedure for task#6 where the boat color will be passed as a parameter.
7. Create a table Projects with the following schema. (P_id, Pro_name, Dep_id).
Now write a query to show which projects have been assigned to the ‘HR’ department.
Wrap the above query in the procedure. Also pass the department name as parameter.
8. Answer the following questions:
What have you learned from the lab task?
What was the most challenging task and how did you overcome that challenge?