SQL Views Chapter 3A. Appendix Objectives Learn basic SQL statements for creating views Learn basic...

18
SQL Views Chapter 3A

Transcript of SQL Views Chapter 3A. Appendix Objectives Learn basic SQL statements for creating views Learn basic...

SQL ViewsChapter 3A

Appendix Objectives

• Learn basic SQL statements for creating views

• Learn basic SQL statements for using views

• Understand the reasons for using views

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

SQL CREATE VIEW Statement

• The SQL CREATE VIEW statement is used to create view structures.

CREATE VIEW ViewName AS

{SQL SELECT statement};

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;

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;

Some Uses for SQL Views

• Hide columns or rows• Display results of computations• Hide complicated SQL syntax• Layer built-in functions

Using SQL Views: Hide columns or rows I

Why do we want to hide columns/rows?1. simplify results2. security reasons

prevent the display of sensitive data

CREATE VIEW BasicDepartmentDataView AS SELECT DepartmentName,

Phone AS DepartmentPhone FROM DEPARTMENT;

SELECT * FROM BasicDepartmentDataViewORDER BY DepartmentName;

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 MarkingDepartmentProjectViewORDER BY ProjectID;

Using SQL Views: Display results of computations – SQL Statement

CREATE VIEW ProjectHoursToDateView ASSELECT PROJECT.ProjectID,

Name AS ProjectName,MaxHours AS ProjectMaxHours,SUM(HoursWorked) AS ProjectHoursWorkedToDate

FROM PROJECT, ASSIGNMENTWHERE PROJECT.ProjectID =

ASSIGNMENT.ProjectIDGROUP BY PROJECT.ProjectID;

Using SQL Views: Display results of computations – Results

SELECT * FROM ProjectHoursToDateViewORDER BY PROJECT.ProjectID;

Using SQL Views: Hide complicated SQL syntax – SQL Statement

CREATE VIEW EmployeeProjectHoursWorkedView ASSELECT Name, FirstName, LastName,

HoursWorkedFROM EMPLOYEE AS E JOIN ASSIGNMENT AS A

ON E.EmployeeNumber =A.EmployeeNumber

JOIN PROJECT AS PON A.ProjectID =

P.ProjectID;

Using SQL Views: Hide complicated SQL syntax – Results

SELECT * FROM EmployeeProjectHoursWorkedView;

Using SQL Views: Layering Computations and Built-in Functions

1st SQL Statement

CREATE VIEW ProjectHoursToDateView ASSELECT PPOJECT.ProjectID,

Name AS ProjectName,MaxHours AS ProjectMaxHours,SUM(HoursWorked) AS

ProjectHoursWorkedToDateFROM PROJECT, ASSIGNMENTWHERE PROJECT.ProjectID =

ASSIGNMENT.ProjectIDGROUP BY PROJECT.ProjectID;

Using SQL Views: Layering Computations and Built-in Functions

2nd SQL Statement

CREATE VIEW ProjectsOverAllotedMaxHoursView AS

SELECT ProjectID, ProjectName, ProjectMaxHours, ProjectHoursWorkedToDate

FROM ProjectHoursToDateView WHERE ProjectHoursWorkedToDate > ProjectMaxHours;

Using SQL Views: Layering Computations and Built-in Functions

Results

SELECT ProjectID, ProjectName, ProjectMaxHours, ProjectHoursWorkedToDate, (ProjectHoursWorkedToDate

- ProjectMaxHours) AS HoursOverMaxAllocated

FROM ProjectsOverAllotedMaxHoursViewORDER BY ProjectID;

SQL ViewsEnd of Presentation on Chapter 3A

DAVID M. KROENKE and DAVID J. AUER

DATABASE CONCEPTS, 5th Edition

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