Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL Views Chapter 3A. Appendix Objectives Learn basic SQL statements for creating views Learn basic SQL statements for using views Understand the reasons.

Similar presentations


Presentation on theme: "SQL Views Chapter 3A. Appendix Objectives Learn basic SQL statements for creating views Learn basic SQL statements for using views Understand the reasons."— Presentation transcript:

1 SQL Views Chapter 3A

2 Appendix Objectives Learn basic SQL statements for creating views Learn basic SQL statements for using views Understand the reasons for using views

3 SQL Views A SQL view is a virtual table that is constructed from other tables or views A view has no data of its own, but uses data stored in tables or other views Views are created using SQL SELECT statements Views are used in other SELECT statements just as if they were a table The SQL statements that create the views may not contain an ORDER BY clause. If the results of a query using a view need to be sorted, the sort order must be provided by the SELECT statement that processes the view However, Oracle allows you to create the views that contain an ORDER BY clause

4 SQL CREATE VIEW Statement The SQL CREATE VIEW statement is used to create view structures. CREATE VIEW ViewName AS {SQL SELECT statement};

5 SQL CREATE VIEW Example Create a view to list the city and product name for each product ordered by its quantity: create view v_products as select city, pname as myname from products order by quantity;

6 Using an SQL SELECT Statement Once the view is created, it can be used in the FROM clause of SELECT statements just like a table. select * from v_products;

7 Some Uses for SQL Views Hide columns or rows Display results of computations Hide complicated SQL syntax Layer built-in functions

8 Using SQL Views: Hide columns or rows I Why do we want to hide columns/rows? 1. simplify results 2. security reasons prevent the display of sensitive data CREATE VIEW BasicDepartmentDataView AS SELECT DepartmentName, Phone AS DepartmentPhone FROM DEPARTMENT; SELECT * FROM BasicDepartmentDataView ORDER BY DepartmentName;

9 Using SQL Views: Hide columns or rows II How do we hide rows? Using WHERE clause CREATE VIEW MarkingDepartmentProjectView AS SELECT ProjectID, Name AS ProjectName, MaxHours, StartDate, EndDate FROM PROJECT WHERE Department = 'Marketing'; SELECT * FROM MarkingDepartmentProjectView ORDER BY ProjectID;

10 Using SQL Views: Display results of computations – SQL Statement CREATE VIEW ProjectHoursToDateView AS SELECT PROJECT.ProjectID, Name AS ProjectName, MaxHours AS ProjectMaxHours, SUM(HoursWorked) AS ProjectHoursWorkedToDate FROM PROJECT, ASSIGNMENT WHERE PROJECT.ProjectID = ASSIGNMENT.ProjectID GROUP BY PROJECT.ProjectID;

11 Using SQL Views: Display results of computations – Results SELECT * FROM ProjectHoursToDateView ORDER BY PROJECT.ProjectID;

12 Using SQL Views: Hide complicated SQL syntax – SQL Statement CREATE VIEW EmployeeProjectHoursWorkedView AS SELECT Name, FirstName, LastName, HoursWorked FROM EMPLOYEE AS E JOIN ASSIGNMENT AS A ON E.EmployeeNumber = A.EmployeeNumber JOIN PROJECT AS P ON A.ProjectID = P.ProjectID;

13 Using SQL Views: Hide complicated SQL syntax – Results SELECT * FROM EmployeeProjectHoursWorkedView;

14 Using SQL Views: Layering Computations and Built-in Functions 1 st SQL Statement CREATE VIEW ProjectHoursToDateView AS SELECT PPOJECT.ProjectID, Name AS ProjectName, MaxHours AS ProjectMaxHours, SUM(HoursWorked) AS ProjectHoursWorkedToDate FROM PROJECT, ASSIGNMENT WHERE PROJECT.ProjectID = ASSIGNMENT.ProjectID GROUP BY PROJECT.ProjectID;

15 Using SQL Views: Layering Computations and Built-in Functions 2 nd SQL Statement CREATE VIEW ProjectsOverAllotedMaxHoursView AS SELECT ProjectID, ProjectName, ProjectMaxHours, ProjectHoursWorkedToDate FROM ProjectHoursToDateView WHERE ProjectHoursWorkedToDate > ProjectMaxHours;

16 Using SQL Views: Layering Computations and Built-in Functions Results SELECT ProjectID, ProjectName, ProjectMaxHours, ProjectHoursWorkedToDate, (ProjectHoursWorkedToDate - ProjectMaxHours) AS HoursOverMaxAllocated FROM ProjectsOverAllotedMaxHoursView ORDER BY ProjectID;

17 SQL Views End of Presentation on Chapter 3A DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition

18 All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. Printed in the United States of America. Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall


Download ppt "SQL Views Chapter 3A. Appendix Objectives Learn basic SQL statements for creating views Learn basic SQL statements for using views Understand the reasons."

Similar presentations


Ads by Google