SQL Views Chapter 3A DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition.
-
Upload
benjamin-hicks -
Category
Documents
-
view
259 -
download
3
Transcript of SQL Views Chapter 3A DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition.
SQL ViewsChapter 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
Appendix Objectives
• Learn basic SQL statements for creating views
• Learn basic SQL statements for using views
• Understand the reasons for using views
3A-3KROENKE and AUER - DATABASE CONCEPTS (5th Edition) Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall
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
3A-4KROENKE and AUER - DATABASE CONCEPTS (5th Edition) Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall
SQL CREATE VIEW Statement
• The SQL CREATE VIEW statement is used to create view structures.
CREATE VIEW ViewName AS
{SQL SELECT statement};
3A-5KROENKE and AUER - DATABASE CONCEPTS (5th Edition) Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall
SQL CREATE VIEW Example
CREATE VIEW EmployeePhoneView AS
SELECT FirstName, LastName,
Phone AS EmployeePhone
FROM EMPLOYEE;
3A-6KROENKE and AUER - DATABASE CONCEPTS (5th Edition) Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall
Creating an SQL View inSQL Server 2008 R2
3A-7KROENKE and AUER - DATABASE CONCEPTS (5th Edition) Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall
Creating an SQL ViewMySQL 5.1
3A-8KROENKE and AUER - DATABASE CONCEPTS (5th Edition) Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall
Creating an SQL View-Equivalent Query in
Microsoft Access 2010
3A-9KROENKE and AUER - DATABASE CONCEPTS (5th Edition) Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall
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 EmployeePhoneView
ORDER BY LastName;
3A-10KROENKE and AUER - DATABASE CONCEPTS (5th Edition) Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall
Using the EmployeePhoneView in SQL Server 2008 R2
3A-11KROENKE and AUER - DATABASE CONCEPTS (5th Edition) Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall
Using the EmployeePhoneView in MySQL 5.1
3A-12KROENKE and AUER - DATABASE CONCEPTS (5th Edition) Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall
Using an SQL View-Equivalent Query in
Microsoft Access 2010 I
3A-13KROENKE and AUER - DATABASE CONCEPTS (5th Edition) Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall
Using an SQL View-Equivalent Query in
Microsoft Access 2010 II
3A-14KROENKE and AUER - DATABASE CONCEPTS (5th Edition) Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall
Using an SQL View-Equivalent Query in
Microsoft Access 2010 III
3A-15KROENKE and AUER - DATABASE CONCEPTS (5th Edition) Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall
Some Uses for SQL Views
• Hide columns or rows• Display results of computations• Hide complicated SQL syntax• Layer built-in functions
C-16KROENKE and AUER - DATABASE CONCEPTS (5th Edition) Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall
Using SQL Views: Hide columns or rows I
CREATE VIEW BasicDepartmentDataView AS SELECT DepartmentName,
Phone AS DepartmentPhone FROM DEPARTMENT;
SELECT * FROM BasicDepartmentDataViewORDER BY DepartmentName;
3A-17KROENKE and AUER - DATABASE CONCEPTS (5th Edition) Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall
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;
3A-18KROENKE and AUER - DATABASE CONCEPTS (5th Edition) Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall
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;
3A-19KROENKE and AUER - DATABASE CONCEPTS (5th Edition) Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall
Using SQL Views: Display results of computations – Results
SELECT * FROM ProjectHoursToDateViewORDER BY PROJECT.ProjectID;
3A-20KROENKE and AUER - DATABASE CONCEPTS (5th Edition) Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall
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;
3A-21KROENKE and AUER - DATABASE CONCEPTS (5th Edition) Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall
Using SQL Views: Hide complicated SQL syntax – Results
SELECT * FROM EmployeeProjectHoursWorkedView;
3A-22KROENKE and AUER - DATABASE CONCEPTS (5th Edition) Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall
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;
3A-23KROENKE and AUER - DATABASE CONCEPTS (5th Edition) Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall
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;
3A-24KROENKE and AUER - DATABASE CONCEPTS (5th Edition) Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall
Using SQL Views: Layering Computations and Built-in Functions
Results
SELECT ProjectID, ProjectName, ProjectMaxHours, ProjectHoursWorkedToDate, (ProjectHoursWorkedToDate
- ProjectMaxHours) AS HoursOverMaxAllocated
FROM ProjectsOverAllotedMaxHoursViewORDER BY ProjectID;
3A-25KROENKE and AUER - DATABASE CONCEPTS (5th Edition) Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall
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