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

Lab Manual for DBA

The lab manual focuses on understanding database views, including their types, creation, modification, and usage for data access and security. It covers regular, partitioned, materialized, system, and dynamic management views, providing syntax and examples for each. Additionally, it includes lab tasks for practical application of creating various views based on student and course data.

Uploaded by

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

Lab Manual for DBA

The lab manual focuses on understanding database views, including their types, creation, modification, and usage for data access and security. It covers regular, partitioned, materialized, system, and dynamic management views, providing syntax and examples for each. Additionally, it includes lab tasks for practical application of creating various views based on student and course data.

Uploaded by

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

Database Administration & Management Lab

Lab Manual 07

Session: Fall-2024
Instructor: SHEZA SHABIR

Department of Informatics & Systems


School of System & Technology
University of Management & Technology Lahore Pakistan
Objective: (CLO1, CLO2, CLO3)

In today’s lab, we will:

To understand the concept of database views, their types, how to create, modify, and drop views,
and how they can be used to simplify data access and improve security. Additionally, learn to work
with Materialized Views, System Views, and Dynamic Management Views for advanced database
management.

What is a View?

 A view is like a virtual table. It shows data based on an SQL query.


 It doesn’t store the data, but it fetches it from real tables.

Why Use Views?

1. To make queries simpler: You can save complex queries and use them like a table.
2. To secure data: You can hide parts of a table (like private columns).
3. To organize data: Show only the data you need, in the way you need.

How to Create a View?

Syntax:

CREATE VIEW view_name AS


SELECT column1, column2
FROM table_name
WHERE condition;

Types of Views:

1. Regular View
Simplifies complex queries and provides a secure way to access specific data from tables.

When to Use:

 If you need to simplify data retrieval.


 If there are relationships between tables (e.g., joins).

Example: View to show students and their majors:


CREATE VIEW StudentMajor AS
SELECT name, major
FROM Students;
2. Partitioned View

 Purpose: Combines data from multiple tables into one logical table, often used with
distributed databases.

When to Use:

 When there are no relationships between tables.


 When you need to UNION data from tables.

Syntax:

CREATE VIEW view_name AS


SELECT column1, column2
FROM table1
UNION ALL
SELECT column1, column2
FROM table2;

Example:
If we had separate tables for UndergraduateStudents and GraduateStudents:

CREATE VIEW AllStudents AS


SELECT student_id, name, major
FROM UndergraduateStudents
UNION ALL
SELECT student_id, name, major
FROM GraduateStudents;

3. Materialized View

 Purpose: Stores query results physically for faster access but requires refreshing to keep
data updated.

When to Use:

 For large datasets or complex queries that take time to compute.


 When the data doesn’t change frequently.

Syntax:

CREATE MATERIALIZED VIEW view_name AS


SELECT column1, column2
FROM table_name
WHERE condition;
Example:
Materialized view to store the average grade per course:

CREATE MATERIALIZED VIEW AvgGradePerCourse AS


SELECT course_id, AVG(grade) AS average_grade
FROM Enrollments
GROUP BY course_id;

4. System View

 Purpose: Built-in views provided by the database to get system metadata (e.g., schema
details, users).

Syntax:
No custom creation; you query system views directly.

Example:
View all tables in the current database:

SELECT * FROM INFORMATION_SCHEMA.TABLES;

5. Dynamic Management View (DMV)

 Purpose: Used to monitor and manage the database's health, performance, and status.
 Syntax:
No custom creation; you query DMVs directly.
 Example:
View current running queries:

SELECT * FROM sys.dm_exec_requests;

 Modifying a View

 Purpose: To change the definition of an existing view.

Syntax:

CREATE OR REPLACE VIEW view_name AS


SELECT column1, column2
FROM table_name
WHERE condition;
 Dropping a View

 Purpose: To remove an existing view from the database.

Syntax:

DROP VIEW view_name;


Lab Task
1. Write a query to create a view that displays student_name, course_name, and grade by
joining Students, Courses, and Enrollments tables.
2. Write a query to create a view that filters students who have enrolled in courses with more
than 4 credits, displaying student_name and course_name.
3. Create a view that uses a WHERE clause to display students (student_name) who have
received grades higher than 90.
4. Write SQL to create a view combining Courses and Enrollments to show courses with no
students enrolled (course_name).
5. Write a query to create a view that displays the highest grade achieved in each course,
showing course_name and highest_grade.
6. Create a view showing students (student_name) enrolled in more than one course.
7. Write a query to create a view that displays the lowest grade received in each course,
showing course_name and lowest_grade.
8. Create a view that shows all students who are above the age of 20 and have grades above
85.
9. Write SQL to create a view that displays students and their grades in descending order of
grades for each course.
10. Write a query to fetch all the table names in the current database using a system view.
11. Create a materialized view that stores the average grade for each course ( course_name,
average_grade) to improve query performance for frequent aggregate calculations.
12. Use a dynamic management view to fetch all currently running queries

You might also like