sql views appendix e david m. kroenke and david j. auer database concepts, 6 th edition

24
SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition

Upload: nickolas-mosley

Post on 31-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition

SQL ViewsAppendix E

DAVID M. KROENKE and DAVID J. AUER

DATABASE CONCEPTS, 6th Edition

Page 2: SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th 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 © 2013 Pearson Education, Inc.  Publishing as Prentice Hall

Page 3: SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition

Appendix Objectives

• Learn basic SQL statements for creating views

• Learn basic SQL statements for using views

• Understand the reasons for using views

KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall

E-3

Page 4: SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition

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.

KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall

E-4

Page 5: SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition

SQL CREATE VIEW Statement

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

CREATE VIEW ViewName AS {SQL SELECT statement};

KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall

E-5

Page 6: SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition

SQL CREATE VIEW Example

CREATE VIEW EmployeePhoneView AS SELECT FirstName, LastName,

Phone AS EmployeePhone FROM EMPLOYEE;

KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall

E-6

Page 7: SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition

Creating an SQL View:Microsoft SQL Server 2012

KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall

E-7

Figure E-1: Creating a View in the Microsoft SQL Server Management Studio

Page 8: SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition

Creating an SQL View:Oracle Database 11g Release 2

KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall

E-8

Figure E-2: Creating a View in the Oracle SQL Developer

Page 9: SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition

Creating an SQL View in:Oracle MySQL 5.5

KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall

E-9

Figure E-3: Creating a View in the MySQL Workbench

Page 10: SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition

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 EmployeePhoneViewORDER BY LastName;

KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall

E-10

Page 11: SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition

Using the EmployeePhoneView inMicrosoft SQL Server 2012

KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall

E-11

Figure E-4:Using EmployeePhoneView in the Microsoft SQL Server Management Studio

Page 12: SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition

Using the EmployeePhoneView inOracle Database 11g Release 2

KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall

E-12

Figure E-5: Using EmployeePhoneView in the Oracle SQL Developer

Page 13: SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition

Using the EmployeePhoneView in Oracle MySQL 5.5

KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall

E-13

Figure E-6: Using EmployeePhoneView in the MySQL Workbench

Page 14: SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition

Some Uses for SQL Views

KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall

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

E-14

Page 15: SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition

Using SQL Views: Hide columns or rows I

CREATE VIEW BasicDepartmentDataView AS SELECT DepartmentName,

Phone AS DepartmentPhone FROM DEPARTMENT;

SELECT * FROM BasicDepartmentDataViewORDER BY DepartmentName;

KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall

E-15

Page 16: SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition

Using SQL Views: Hide columns or rows II

CREATE VIEW MarkingDepartmentProjectView AS SELECT ProjectID, Name AS ProjectName,

MaxHours, StartDate, EndDate FROM PROJECT WHERE Department = 'Marketing';

SELECT * FROM MarkingDepartmentProjectViewORDER BY ProjectID;

KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall

E-16

Page 17: SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition

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;

KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall

E-17

Page 18: SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition

Using SQL Views: Display results of computations – Results

SELECT * FROM ProjectHoursToDateViewORDER BY PROJECT.ProjectID;

KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall

E-18

Page 19: SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition

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;

KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall

E-19

Page 20: SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition

Using SQL Views: Hide complicated SQL syntax – Results

SELECT * FROM EmployeeProjectHoursWorkedView;

KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall

E-20

Page 21: SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition

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;

KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall

E-21

Page 22: SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition

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;

KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall

E-22

Page 23: SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition

Using SQL Views: Layering Computations and Built-in Functions

Results

SELECT ProjectID, ProjectName, ProjectMaxHours, ProjectHoursWorkedToDate, (ProjectHoursWorkedToDate

- ProjectMaxHours) AS HoursOverMaxAllocated

FROM ProjectsOverAllotedMaxHoursViewORDER BY ProjectID;

KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall

E-23

Page 24: SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition

SQL ViewsEnd of Presentation on Appendix E

DAVID M. KROENKE and DAVID J. AUER

DATABASE CONCEPTS, 6th Edition