17.8 KB Β· commit 40bb182
β Previous: T06.3: Outer-Join Relational Algebra | π Index | Next: T06.5: View Expansion, Updatable Views, and Materialized Views β
Topic: T06 β Joins & Views | Priority: π’ Foundation | Exam Frequency: 1/7 finals (6 marks) [2023 Q.5(a), 2023 Q.5(b)]
Sources: Silberschatz 7e Ch. 4 (Β§4.2.1, Β§4.2.2) Β· Lecture Deck 04 (Slides 24β28) Β· Raidah Notes (pp. 3, 11, 18) Β· Fahim Notes (pp. 58β59)
Views are a core part of the ANSI/SPARC three-level database architecture because they define external schemas that give different users customized, secure access to data. In RUET exams, views show up both as short theory questions and as practical SQL query problems. For instance, in 2023 Q.5(a) and Q.5(b), examiners tested the benefits of views and asked for a multi-table CREATE VIEW query worth 6 marks total. Once you understand how views support logical data independence and know the CREATE VIEW syntax, these questions are straightforward marks.
View (Virtual Relation): A view is a virtual table defined by a saved SQL query rather than stored as physical rows on disk. It is not part of the physical storage layout, but users can query it just like a regular table. Every time a view is queried, the database computes its rows dynamically from the underlying base tables.
View Definition: You create a view in SQL using the command
CREATE VIEW v AS <query expression>;, wherevis the view name and<query expression>is any validSELECTquery. The database saves only this query text in the system catalog and runs it on demand.
Logical Data Independence: Logical data independence is the ability to change the logical schema of base tables without rewriting external views or application programs. If an administrator splits or restructures base tables, updating the view definition keeps existing user queries working without change.
Think of a view as a saved lens over your tables rather than a new copy of the data. When you create a view, you are simply giving a name to a SELECT query so you can treat its result like a regular table.
In a standard relational database, a view does not store a duplicate copy of rows on disk. Instead, the system catalog stores only the SELECT query that defines the view. Whenever a user or application queries that view, the database engine substitutes the saved query into the user's statement and computes the result on the fly.
CREATE TABLE): Allocates disk space for rows and indexes, and writes every update directly to storage.CREATE VIEW): Uses zero disk space for row data because it is recalculated on demand every time you query it.CREATE TABLE ... AS SELECT): Computes the query once and writes the resulting rows to disk at creation time. Because it is a separate static copy, it goes stale as soon as the original tables change, whereas a virtual view always shows the latest live data.DROP VIEW.WITH is temporary and exists only for the duration of a single SQL statement.Views let administrators control access down to specific columns and rows:
WHERE clause so they only see employees in their own department.Views hide complex SQL logic behind a clean, simple table name. Even if the underlying query joins four tables and computes several aggregates, users can query the view with a simple one-liner:
SELECT * FROM high_performing_departments;
Views shield application code from structural changes in the underlying base tables. For example, if a database administrator splits a large table into two smaller normalized tables, they can create a view with the original table's name that joins those two new tables together. Existing applications can keep querying that view name without any code changes.
When business formulas (such as net pay after tax, profit margins, or student CGPA) are written once inside a view definition, everyone queries the exact same logic. This prevents different departments from accidentally using slightly different formulas in their reports.
Different teams often need different slices of the same underlying data. For instance, academic advisors need to see student course histories, while the accounts office only needs tuition payment records. Views let each group work with a tailored table layout without duplicating the underlying records.
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
When your view's SELECT list includes arithmetic expressions or aggregate functions like SUM() and AVG(), those output columns do not have natural table column names. You can name them cleanly right after the view name in parentheses:
CREATE VIEW dept_salary_summary (dept_name, total_salary, avg_salary) AS
SELECT dept_name, SUM(salary), AVG(salary)
FROM instructor
GROUP BY dept_name;
Views often join several related tables so users do not have to write the join conditions repeatedly:
CREATE VIEW faculty_courses AS
SELECT instructor.ID, instructor.name, course.title, section.semester, section.year
FROM instructor
JOIN teaches ON instructor.ID = teaches.ID
JOIN section ON teaches.course_id = section.course_id
AND teaches.sec_id = section.sec_id
AND teaches.semester = section.semester
AND teaches.year = section.year
JOIN course ON section.course_id = course.course_id;
To remove a view definition from the database catalog, use DROP VIEW:
DROP VIEW view_name;
(Dropping a view deletes only the saved query definition from the catalog. It never deletes any rows from the underlying base tables.)
Suppose a university database has two tables:
Student(Roll, Name, DeptID)Result(Roll, CGPA)We want to create a view named Dean_List_ECE that shows the Roll, Name, and CGPA of all students in 'ECE' whose CGPA is at least 3.75.
CREATE VIEW QueryWe join Student and Result on Roll, filter for DeptID = 'ECE' and CGPA >= 3.75, and wrap the query inside CREATE VIEW:
CREATE VIEW Dean_List_ECE AS
SELECT S.Roll, S.Name, R.CGPA
FROM Student S
JOIN Result R ON S.Roll = R.Roll
WHERE S.DeptID = 'ECE' AND R.CGPA >= 3.75;
If Student and Result contain:
(211001, 'Nadia', 'ECE', 3.82) -> Matches both 'ECE' and 3.82 >= 3.75 (Included)(211002, 'Farhan', 'ECE', 3.60) -> Fails 3.60 >= 3.75 (Excluded)(211003, 'Tariq', 'CSE', 3.90) -> Fails DeptID = 'ECE' (Excluded)Querying SELECT * FROM Dean_List_ECE; returns only Nadia's row, and if Farhan's CGPA is later updated to 3.78 in Result, he will automatically appear the next time Dean_List_ECE is queried.
| Year & Question | Marks | Scenario / Data Given | Key Result / Answer Summary |
|---|---|---|---|
| 2023 Q.5(a) | 3 | Theoretical inquiry into views | State four benefits: access security, query simplification, logical data independence, consistent business metrics. |
| 2023 Q.5(b) | 3 | Three relational tables: E, D, S | Write CREATE VIEW joining tables on IDs with predicate Salary > 55000, yielding Jane and Bob. |
Appeared: 2023 Q.5(a) β (3 marks)
Question:
What are the benefits of VIEW?
Full Answer:
A view is a virtual relation defined by an SQL query. The DBMS stores the query specification rather than physical tuples on disk.
The four primary benefits of using views in database systems are:
SELECT * FROM view_name syntax.Appeared: 2023 Q.5(b) β (3 marks)
Question:
Create a VIEW using SQL query that shows the EmployeeName, DepartmentName and salary for employees who earn a salary of more than 55,000.
E.
| D.
| S.
|
Full Answer:
The provided data relations are E, D, and S. Note that attribute Department ID in table E contains a whitespace and is delimited by backticks:
CREATE VIEW High_Salary_Employees AS
SELECT E.EmployeeName, D.DepartmentName, S.Salary
FROM E
JOIN D ON E.`Department ID` = D.DepartmentID
JOIN S ON E.EmployeeID = S.EmployeeID
WHERE S.Salary > 55000;
If assuming a consolidated schema Employee(EmployeeID, EmployeeName, DeptID, Salary) and Department(DeptID, DepartmentName):
CREATE VIEW High_Salary_Employees AS
SELECT Employee.EmployeeName, Department.DepartmentName, Employee.Salary
FROM Employee
JOIN Department ON Employee.DeptID = Department.DeptID
WHERE Employee.Salary > 55000;
Evaluating the predicate Salary > 55000 on the given records:
Resulting View Relation:
| EmployeeName | DepartmentName | Salary |
|---|---|---|
| Jane | HR | 60,000 |
| Bob | IT | 70,000 |
Source: Silberschatz 7e Β· Chapter 4 (Exercise 4.6)
Exam Relevance: High-Probability Views Implementation Drill
Key Tested Concept: Defining views with aggregation and conditional expressions, and understanding view substitution / query rewriting.
Problem Statement:
Using the university database relations:
- student(IDβ,name,dept_name)
- takes(ID,course_id,sec_id,semester,yearβ,grade)
- course(course_idβ,title,dept_name,credits) Assume letter grades map to grade points:
'A'=4,'B'=3,'C'=2,'D'=1,'F'=0.
- Write an SQL DDL statement to define a view
student_grades(ID, name, GPA)that computes each student's GPA. If a student has taken no courses, GPA should beNULL.- Write a query against this view to find all students whose GPA is greater than 3.5.
- Show how the database engine rewrites the query on the view into an equivalent query on the underlying base tables.
Model Exam-Style Solution:
CREATE VIEW student_grades AS
SELECT
s.ID,
s.name,
ROUND(
SUM(
CASE t.grade
WHEN 'A' THEN 4.0
WHEN 'B' THEN 3.0
WHEN 'C' THEN 2.0
WHEN 'D' THEN 1.0
ELSE 0.0
END * c.credits
) / SUM(c.credits),
2
) AS GPA
FROM student s
LEFT JOIN takes t ON s.ID = t.ID
LEFT JOIN course c ON t.course_id = c.course_id
GROUP BY s.ID, s.name;
SELECT ID, name, GPA
FROM student_grades
WHERE GPA > 3.5;
FROM clause:SELECT ID, name, GPA
FROM (
SELECT
s.ID,
s.name,
ROUND(
SUM(
CASE t.grade
WHEN 'A' THEN 4.0
WHEN 'B' THEN 3.0
WHEN 'C' THEN 2.0
WHEN 'D' THEN 1.0
ELSE 0.0
END * c.credits
) / SUM(c.credits),
2
) AS GPA
FROM student s
LEFT JOIN takes t ON s.ID = t.ID
LEFT JOIN course c ON t.course_id = c.course_id
GROUP BY s.ID, s.name
) AS inline_student_grades
WHERE GPA > 3.5;
HAVING GPA > 3.5 into the aggregated query directly. β π‘ RUET Exam Takeaway: Views are virtual relations; they store only the query definition, not the physical data. When queried, the DBMS substitutes the view definition directly into the execution tree.
> 55000, which excludes Alice ($55,000). Writing >= 55000 loses marks.Department ID. In MySQL, column names containing spaces must be enclosed in backticks (`Department ID`).EmployeeName, DepartmentName and salary. Do not write SELECT *.β Previous: T06.3: Outer-Join Relational Algebra | π Index | Next: T06.5: View Expansion, Updatable Views, and Materialized Views β