SQL Views Appendix C DATABASE CONCEPTS, 3rd Edition

Slides:



Advertisements
Similar presentations
4d. Structured Query Language – JOIN Operation Lingma Acheson Department of Computer and Information Science IUPUI CSCI N207 Data Analysis with Spreadsheets.
Advertisements

SQL-week5-1 In-Class Exercise Answer IST 210 Organization of Data IST2101.
Murach's MySQL, C1© 2012, Mike Murach & Associates, Inc.Slide 1.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 11-1 David M. Kroenke’s Chapter Eleven: Managing Databases with SQL Server.
SQL Views Chapter 3A. Appendix Objectives Learn basic SQL statements for creating views Learn basic SQL statements for using views Learn basic SQL statements.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Getting Started (Excerpts) Chapter One DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
Structured Query Language (SQL)
SQL Views Chapter 3A DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition.
Getting Started with Oracle MySQL 5.5 Community Server Edition Appendix C DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
Getting Started with Microsoft Visio 2010 Appendix G DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
Getting Started Chapter One DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition.
Getting Started with Microsoft SQL Server 2012 Express Edition Appendix A DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
David M. Kroenke and David J. Auer Database Processing—12 th Edition Fundamentals, Design, and Implementation Chapter One: Introduction KROENKE AND AUER.
Getting Started Chapter One DATABASE CONCEPTS, 7th Edition
Web Database Processing Using Microsoft Access The Access Workbench: Section Seven DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 3 rd Edition.
Getting Started with Microsoft Access The Access Workbench: Section One DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
SQL for Data Retrieval. Save your SQL Scripts When working with SQL Management Studio, you should keep saving your scripts as a.sql file to somewhere.
+ Structured Query Language Part 2 KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall.
Structured Query Language
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Download three SQL script files from wiki page.
Getting Started with Oracle Database 11g Release 2 Express Edition Appendix B DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
Getting Started Chapter One DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition.
Working with Queries in Microsoft Access The Access Workbench: Section Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 3 rd Edition.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
David M. Kroenke’s Chapter One: Introduction Part Two Database Processing: Fundamentals, Design, and Implementation.
SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
Working with Multiple Tables in Microsoft Access The Access Workbench: Section Two DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 3 rd Edition.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 11-1 David M. Kroenke’s Chapter Eleven: Managing Databases with SQL Server.
SQL Views Chapter 3A DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
Structured Query Language (SQL) IST2101. Structured Query Language – Acronym: SQL – Pronounced as “S-Q-L” [“Ess-Que-El”] – Originally developed by IBM.
Getting Started Chapter One DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
SQL for Data Retrieval. Review Questions of Previous Class Q1. Show the sum of hours worked for project with ID 1200 (use ASSIGNMENT table) – Use “SUM”
Unit 5 Microsoft SQL Server and MySQL. Key Concepts DBMS variations SQL Server features SQL Server Management Studio MySQL features Scripts Queries Database.
Database Processing with Microsoft Access Appendix DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
4a. Structured Query Language - SELECT Statement Lingma Acheson Department of Computer and Information Science IUPUI CSCI N207 Data Analysis with Spreadsheets.
STRUCTURED QUERY LANGUAGE SQL-III IST 210 Organization of Data IST210 1.
SQL for Data Retrieval. Save your SQL Scripts When working with SQL Management Studio, you should keep saving your scripts as a.sql file to somewhere.
Getting Started Chapter One DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
SQL Views Chapter 3A. Appendix Objectives Learn basic SQL statements for creating views Learn basic SQL statements for using views Understand the reasons.
SQL-5 In-Class Exercise Answer IST 210 Organization of Data IST2101.
David M. Kroenke and David J. Auer Database Processing: F undamentals, Design, and Implementation Chapter Seven: SQL for Database Construction and Application.
Structured Query Language
+ Complex SQL Week 9. + Today’s Objectives TOP GROUP BY JOIN Inner vs. Outer Right vs. Left.
+ Structured Query Language Part 2 KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall.
David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Appendix E: Getting Started with MySQL Workbench Data Modeling.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
ISQS 3358, Business Intelligence Database vs. Data Warehouse
3d. Structured Query Language – JOIN Operations
Chapter 1 Introduction.
The Relational Model and Database Normalization
The Relational Model Chapter Two DATABASE CONCEPTS, 3rd Edition
ISQS 6339, Business Intelligence Database vs. Data Warehouse
David M. Kroenke and David J
Database Processing: David M. Kroenke’s Chapter One: Introduction
David M. Kroenke and David J
Chapter 1 Introduction.
Database Processing: David M. Kroenke’s Chapter Seven:
Chapter 1 Introduction.
Chapter 1 Introduction.
Getting Started Chapter One DATABASE CONCEPTS, 5th Edition
Getting Started Chapter One DATABASE CONCEPTS, 4th Edition
Presentation transcript:

SQL Views Appendix C DATABASE CONCEPTS, 3rd Edition DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 3rd Edition Appendix C SQL Views

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 (3rd Edition) © 2008 Pearson 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 KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) © 2008 Pearson Prentice Hall

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 (3rd Edition) © 2008 Pearson Prentice Hall

SQL CREATE VIEW Example CREATE VIEW EmployeePhoneView AS SELECT FirstName, LastName, Phone AS EmployeePhone FROM EMPLOYEE; KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) © 2008 Pearson Prentice Hall

Creating a SQL View in SQL Server 2005 Click the New Query button, enter the SQL for the view just as you would enter the SQL for a query, and then click the Execute button to create the query The “Command(s) completed successfully” message in the Messages window indicates that the view has been created The new view appears in the expanded Views folder after it is created KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) © 2008 Pearson Prentice Hall

Creating a SQL View MySQL 5.x Enter the SQL for the view just as you would enter the SQL for a query, and then click the Execute button to create the query The new view appears in the expanded wpc schema after it is created (you may have to refresh the schema for it to appear) The “Query returned no resultset” message indicates that the view has been created KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) © 2008 Pearson Prentice Hall

Using an SQL CREATE 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; KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) © 2008 Pearson Prentice Hall

Using the EmployeePhoneView in SQL Server 2005 Click the New Query button, enter the SQL for the query that uses the view, and then click the Execute button to create the query The query results appear in the Results window. KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) © 2008 Pearson Prentice Hall

Using the EmployeePhoneView in MySQL 5.X Enter the SQL for the query that uses the view, and then click the Execute button to create the query The query results appear in the Resultset window. Use the File | New Resultset Tab command to open a new Resultset window KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) © 2008 Pearson Prentice Hall

Some Uses for SQL Views Hide columns or rows Display results of computations Hide complicated SQL syntax Layer built-in functions KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) © 2008 Pearson Prentice Hall

Using SQL Views: Hide columns or rows I CREATE VIEW BasicDepartmentDataView AS SELECT DepartmentName, Phone AS DepartmentPhone FROM DEPARTMENT; SELECT * FROM BasicDepartmentDataView ORDER BY DepartmentName; KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) © 2008 Pearson 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 MarkingDepartmentProjectView ORDER BY ProjectID; KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) © 2008 Pearson Prentice Hall

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; KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) © 2008 Pearson Prentice Hall

Using SQL Views: Display results of computations - Results SELECT * FROM ProjectHoursToDateView ORDER BY PROJECT.ProjectID; KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) © 2008 Pearson Prentice Hall

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; KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) © 2008 Pearson Prentice Hall

Using SQL Views: Hide complicated SQL syntax - Results SELECT * FROM EmployeeProjectHoursWorkedView; KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) © 2008 Pearson Prentice Hall

Using SQL Views: Layering Computations and Built-in Functions 1st 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; KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) © 2008 Pearson 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; KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) © 2008 Pearson Prentice Hall

Using SQL Views: Layering Computations and Built-in Functions Results SELECT ProjectID, ProjectName, ProjectMaxHours, ProjectHoursWorkedToDate, (ProjectHoursWorkedToDate - ProjectMaxHours) AS HoursOverMaxAllocated FROM ProjectsOverAllotedMaxHoursView ORDER BY ProjectID; KROENKE and AUER - DATABASE CONCEPTS (3rd Edition) © 2008 Pearson Prentice Hall

End of Presentation on Appendix C DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 3rd Edition End of Presentation on Appendix C SQL Views