9.5 KB · commit 40bb182
| title | T06.4 · Views and CREATE VIEW |
|---|
← 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 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)
View (Virtual Relation): A view is a virtual relation defined by an SQL query. The database does not store view data on disk. Instead, it stores the query definition in the data dictionary and computes rows dynamically when referenced.
View Definition: In SQL, a view is created using CREATE VIEW view_name AS <query_expression>. The query expression can be any valid SELECT query.
Logical Data Independence: Logical data independence is the ability to change the logical schema of base tables without modifying external views or application programs.
CREATE TABLE): Allocates disk storage for tuples and indexes. Updates write directly to disk.CREATE VIEW): Consumes zero disk space for data. It recomputes rows dynamically upon query execution.CREATE TABLE ... AS SELECT): Evaluates a query once and stores the resulting rows on disk. It becomes stale when underlying tables change.WITH): A temporary named result set valid only within the execution scope of a single SQL statement.SELECT * FROM view_name syntax.CREATE VIEW view_name AS
SELECT column1, column2
FROM table_name
WHERE condition;
When views compute calculated or aggregate columns, assign column names explicitly 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 frequently encapsulate multi-table joins:
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 catalog, run:
DROP VIEW view_name;
Dropping a view deletes only the stored query text. It never drops data from underlying base tables.
Section B · CLO2 · Asked in: 2023 (3 marks)
Question (2023 Q.5(a)). What are the benefits of VIEW?
Answer.
A view is a virtual relation defined by an SQL query. The database does not store the view data on disk. Instead, it stores the query definition and executes it when referenced.
Key Benefits of Views:
SELECT queries on the view.Section B · CLO2 · Asked in: 2023 (3 marks)
Question (2023 Q.5(b)). 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.
|
Answer.
Assumption: Schema consists of Employee(EmpID, EmployeeName, Salary, DeptID) and Department(DeptID, DepartmentName).
SQL Query (Standard 2-Table Schema):
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;
Alternative SQL Query (Using Printed Tables E, D, and S): Joining table E, table D, and table S:
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;
View Output:
| EmployeeName | DepartmentName | Salary |
|---|---|---|
| Jane | HR | 60,000 |
| Bob | IT | 70,000 |
(Note: Employees Jhon with 50,000 and Alice with 55,000 are excluded because their salaries do not exceed 55,000).
> 55000. Writing >= 55000 incorrectly includes Alice ($55,000).Department ID. Wrap it 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 →