Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 VIEW Structured Query Language (SQL) - Part IV.

Similar presentations


Presentation on theme: "1 VIEW Structured Query Language (SQL) - Part IV."— Presentation transcript:

1 1 VIEW Structured Query Language (SQL) - Part IV

2 2 VIEW l A view is a query on one or more tables that is assigned a name l It is sometimes called a virtual table to differentiate it from a base table l When the SQL engine sees a view name, it executes the query in the view definition and recreates the contents of the view from the base table(s)

3 3 CREATE VIEW CREATE VIEW [( )] AS ; CREATE VIEW OverForty AS SELECT * FROM Employee WHERE salary > 40000; Employee EIDEnameDepartmentSalary 1001JohnFinance40000 1002JacobFinance45000 1003JoeAccounting35000 1004JaneMarketing50000 1005JillAccounting30000 1006JebMarketing35000 1007JudyInfo Tech40000 1008JoshInfo Tech50000 OverForty EIDEnameDepartmentSalary 1002JacobFinance45000 1004JaneMarketing50000 1008JoshInfo Tech50000

4 4 VIEW EXAMPLE l We can query OverForty like a table. SELECT * FROM OverForty; EIDEnameDepartmentSalary 1002JacobFinance45000 1004JaneMarketing50000 1008JoshInfo Tech50000 SELECT ename, department FROM OverForty; EnameDepartment JacobFinance JaneMarketing JoshInfo Tech SELECT avg(salary) FROM OverForty; Avg(Salary) 48333.3333

5 5 VIEW EXAMPLE CREATE VIEW DeptStat (Department, EmpCount, AvgSalary) AS SELECT department, count(*), avg(salary) FROM Employee GROUP BY department; DEPTSTAT DepartmentEmpCountAvgSalary Accounting 2 32500 Finance 2 42500 Info Tech 2 45000 Marketing 2 42500 SELECT department, avgsalary FROM DeptStat; DepartmentAvgSalary Accounting 32500 Finance 42500 Info Tech 45000 Marketing 42500

6 6 VIEW EXAMPLE /* This examples creates a view by Joining two tables */ CREATE VIEW Emp_dependent (emp_fname, emp_lname, dependent_name) AS SELECT fname, lname, dependent_name FROM Employee, Dependent WHERE ssn = essn; View created. SELECT * FROM Emp_dependent; Emp_fnameEmp_lnameDependent_name John Smith Michael John Smith Alice John Smith Elizabeth Franklin Wong Theodore Franklin Wong Alice Franklin Wong Joy Jennifer Wallace Abner

7 7 UPDATABLE VIEW l An updatable view (update or Insert) can be used with insert, update and delete commands. l These updates are passed to the base table on which the view is defined. l An updatable view must satisfy all of the following conditions: u It is defined on a single base table with no group by clause, no having clause, no aggregate function, no calculated columns, no select distinct, no set operators UNION, INTERSECTION etc. u Each of its rows is associated with exactly one row in the base table u Any column excluded from the view must be Null-able or have a default value defined in the base table

8 8 UPDATABLE VIEW l Any updates to the view are passed on to the base table. l OverForty is an updatable view whereas DeptStat is not updatable. /* The following statement will insert a row into OverForty as well as into Employee */ INSERT INTO OverForty VALUES (9999, ‘George’, ‘HR’, 60000); 1 row created

9 9 UPDATABLE VIEW EMPLOYEE EIDEnameDepartmentSalary 1001JohnFinance40000 1002JacobFinance45000 1003JoeAccounting35000 1004JaneMarketing50000 1005JillAccounting30000 1006JebMarketing35000 1007JudyInfo Tech40000 1008JoshInfo Tech50000 9999GeorgeHR60000 SELECT * FROM OverForty; EIDEnameDepartmentSalary 1002JacobFinance45000 1004JaneMarketing50000 1008JoshInfo Tech50000 9999GeorgeHR60000

10 10 UPDATABLE VIEW /* Give a $500 raise to all employees earning more than $40000 */ UPDATE OverForty SET salary = salary + 500; /* This will also modify the salaries of the corresponding employees in the base table */ SELECT ename, salary FROM OverForty; EnameSalary Jacob45500 Jane50500 Josh50500 George60500 SELECT ename, salary FROM Employee; EnameSalary John40000 Jacob45500 Joe35000 Jane50500 Jill30000 Jeb35000 Judy40000 Josh50500 George60500

11 11 DROP VIEW l DROP VIEW will drop a view DROP VIEW OverForty; View dropped SELECT * FROM OverForty; Error: Table or view does not exist


Download ppt "1 VIEW Structured Query Language (SQL) - Part IV."

Similar presentations


Ads by Google