1 Agenda for Class 03/02/2006  Do SQL join “quiet participation” exercise in class.  Learn to simplify queries for complex questions through the use.

Slides:



Advertisements
Similar presentations
Ashley Ohmann June 20, * What is Custom SQL? * What can I do with it? * Join conditions * Unions and Self Joins * Ranks * Derived Tables.
Advertisements

4d. Structured Query Language – JOIN Operation Lingma Acheson Department of Computer and Information Science IUPUI CSCI N207 Data Analysis with Spreadsheets.
Creating Tables. 2 home back first prev next last What Will I Learn? List and provide an example of each of the number, character, and date data types.
SQL Rohit Khokher.
Data Bits Many to Many Subkeys JoinsQueries Attributes $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 Final DataBit.
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
Computer Science 1620 Programming & Problem Solving.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
View and Materialized view. What is a view? Logically represents subset of data from one or more table. In sql, a view is a virtual relation based on.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
Advanced SQL SMSU Computer Services Short Course.
1 PL/SQL programming Procedures and Cursors Lecture 1 Akhtar Ali.
Structured Query Language
Security and Integrity
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 8: Subqueries.
SQL 資料庫查詢語言 取材自 EIS, 3 rd edition By Dunn et al..
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
Banner and the SQL Select Statement: Part Four (Multiple Connected Select Statements) Mark Holliday Department of Mathematics and Computer Science Western.
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
Oracle Database Administration Lecture 3  Transactions  SQL Language: Additional information  SQL Language: Analytic Functions.
Inner Join vs. Outer Join
Programming using C# Joins SQL Injection Stored Procedures
1 Agenda – 03/25/2014 Login to SQL Server 2012 Management Studio. Answer questions about HW#7 – display answers. Exam is 4/1/2014. It will be in the lab.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-column subqueries, Multiple-row Subqueries, Correlated Subqueries 11/2/10,
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
1 Agenda for Class 03/07/2006  Learn to simplify queries for complex questions through the use of views.  Concept of a view.  Syntax to create and access.
9/7/2012ISC329 Isabelle Bichindaritz1 The Relational Database Model.
 Agenda 2/20/13 o Review quiz, answer questions o Review database design exercises from 2/13 o Create relationships through “Lookup tables” o Discuss.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
Agenda for 02/21/2006 Learn how to use more than one table in a query. Discuss how DBMS processes multiple tables. Explain the different types of joins.
1 Agenda – 10/24/2013 Answer questions from lab on 10/22. Present SQL View database object. Present SQL UNION statement.
Oracle 11g DATABASE DEVELOPMENT LAB1. Introduction  Oracle 11g Database:-  Oracle 11g database is designed for some features, which helps to the organizations.
Views Lesson 7.
NULLs & Outer Joins Objectives of the Lecture : To consider the use of NULLs in SQL. To consider Outer Join Operations, and their implementation in SQL.
INFO1408 Database Design Concepts Week 15: Introduction to Database Management Systems.
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
Agenda for 02/16/2006 Answer any questions about SQL project. Do you want to see any of the answers for the queries? Discuss additional formatting options.
Structured Query Language Introduction. Basic Select SELECT lname, fname, phone FROM employees; Employees Table LNAMEFNAMEPHONE JonesMark SmithSara
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Lecture 8 – SQL Joins – assemble new views from existing tables INNER JOIN’s The Cartesian Product Theta Joins and Equi-joins Self Joins Natural Join.
While you are waiting for class to start... (1)Login to SQL Server 2012 Management Studio (2) Execute the file called “SQLLab4.sql”. It is located on the.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Aliya Farheen October 29,2015.
Structured Query Language
(SQL - Structured Query Language)
+ Complex SQL Week 9. + Today’s Objectives TOP GROUP BY JOIN Inner vs. Outer Right vs. Left.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
Session id: Darrell Hilliard Senior Delivery Manager Oracle University Oracle Corporation.
PL/SQL programming Procedures and Cursors Lecture 1 [Part 2]
Agenda for Class - 03/04/2014 Answer questions about HW#5 and HW#6 Review query syntax. Discuss group functions and summary output with the GROUP BY statement.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-row Subqueries, Correlated Subqueries.
 Review quiz. Answer questions.  Discuss queries: ◦ What is a query? Turning data stored in a database into information for decision making. ◦ You: Completed.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
BTM 382 Database Management Chapter 8 Advanced SQL Chitu Okoli Associate Professor in Business Technology Management John Molson School of Business, Concordia.
CSED421 Database Systems Lab View. Page 2  Virtual or logical table which is defined as SELECT query with joins  Act as table but not real data, only.
IFS180 Intro. to Data Management Chapter 10 - Unions.
Table spaces.
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Database Systems: Design, Implementation, and Management Tenth Edition
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Structured Query Language
Database Systems: Design, Implementation, and Management Tenth Edition
Presentation transcript:

1 Agenda for Class 03/02/2006  Do SQL join “quiet participation” exercise in class.  Learn to simplify queries for complex questions through the use of views.  Concept of a view.  Syntax to create and access a view.  Uses of a view.

3 Examples of more complex questions  What is the description of the work type with the most time in the time table and how much time was reported for that work type description?  What is the name of the employee who worked some time, but still worked the least amount of total time worked of all employees during the month of December of the previous year?  What is the name of the client for whom we worked the most time during the month of January in the current year?  Which contracts have had more time worked than the estimated time?  Which contracts have more than 25% time worked than estimated time?

4 What is a SQL View?  A “virtual” table.  A set of SQL statements that creates a result table which can be accessed by other SQL statements.  Similar to a function in another programming language, except that the end-product of the view is a named result table.  A database object.  A view is stored in the database.  A view contains no data of its own.  A view relies on the data in the base tables used to create the view.  A set of stored SQL code.  Stores code; not data.

Let’s say we frequently look at all the information about time in our database and we always convert the time to hours and we always like to include the name of the employee who worked the time as well as the description of the type of work performed: CREATE OR REPLACE VIEW emp_time AS SELECTNVL(emp.empid,time.empid) empid, emp.name, time.contractid, time.datetime, time.worktypeid, work.description, nvl(amount/60,0) hoursworked FROMemp FULL OUTER JOINtime ONemp.empid = time.empid LEFT OUTER JOINwork ONwork.worktypeid = time.worktypeid; Any derived column (calculated, aggregate and/or with a SQL function) must have an alias, such as hoursworked in the above example.

6 How is the view used via SQL?  Views are used just as tables are used in SQL. In this example, the join is predefined, so it is easier to write access queries. SELECT * FROMemp_time ORDER BYempid; SELECT * FROM emp_time WHEREhoursworked = 0; SELECTname FROMemp_time;

Can a view be joined with tables? Yes! SELECTemp_time.empid, emp_time.name, emp_time.datetime, emp_time.contractid, client.name FROMemp_time INNER JOINcontract ONcontract.contractid = emp_time.contractid INNER JOINclient ONcontract.clientid = client.clientid ORDER BYemp_time.empid, emp_time.datetime desc; Must have a “shared” column to access the data between tables and view – same as using a foreign key between tables.

8 So, what’s the big deal?  Views allow you to break down difficult queries into smaller pieces for easier design, coding and debugging.  Views allow you to create a layer of abstraction between the data structure and the user or programmer allowing greater security.  Programmers do not know the structure of the base tables. Less risk of fraud.  Users can see “pre-joined” tables for their queries.  Users don’t have to understand the complexity of the actual database.  No one sees data that is secure (salary, for example).  Views allow you to access the results of aggregate functions more easily.

9 Group Functions and Joins are Complex  Must have all non-group attributes that are in the SELECT list also in the GROUP BY statement.  Difficult to do a group function of a group function. Examples:  The maximum of the sum of hours.  The minimum of a count of products.  Joining multiple tables can yield full or partial cartesian products making it difficult to trouble-shoot the SQL code.

Which type of work did we do the most? SELECTwork.worktypeid, work.description, sum(time.amount/60) "Hours Worked" FROMwork INNER JOIN time ONwork.worktypeid = time.worktypeid GROUP BYwork.worktypeid, work.description HAVINGsum(time.amount/60) = max(sum(time.amount/60)); Doesn’t work! SELECTwork.worktypeid, work.description, sum(time.amount/60) "Hours Worked" FROMwork INNER JOIN time ONwork.worktypeid = time.worktypeid GROUP BYwork.worktypeid, work.description HAVINGsum(time.amount/60) = (SELECT max(sum(time.amount/60)) FROM time GROUP BYworktypeid); Works in Oracle, but is conceptually complicated

Try it with a view First, create a view. CREATE OR REPLACE VIEW time_by_work AS SELECTwork.worktypeid, work.description, sum(amount/60) hoursworked FROMwork INNER JOIN time ONwork.worktypeid = time.worktypeid GROUP BYwork.worktypeid, work.description; Second, use the view in a query with a sub-query. SELECTdescription, hoursworked FROMtime_by_work WHEREhoursworked = (SELECT MAX(hoursworked) FROM time_by_work);

What is the name of the employee who put in some time but has the least amount of time performing work with the word “java” in the description? SELECTemp.empid, emp.name, sum(amount/60) hours_worked FROMemp INNER JOINtime ONemp.empid = time.empid INNER JOINwork ONwork.worktypeid = time.worktypeid WHERElower(description) LIKE 'java%' GROUP BYemp.empid, emp.name HAVINGsum(amount/60) = (SELECT min(sum(amount/60)) FROM time INNER JOIN work ON time.worktypeid = work.worktypeid WHERE lower(description) LIKE 'java%' GROUP BY time.empid, time.worktypeid);

CREATE OR REPLACE VIEW view_hours AS SELECTemp.empid, emp.name, time.worktypeid, lower(description) description, nvl(sum(time.amount/60),0) hoursworked FROM emp INNER JOINtime ONemp.empid = time.empid INNER JOIN work ONtime.worktypeid = work.worktypeid GROUP BYemp.empid, emp.name, time.worktypeid, description; Create a View Use the View SELECTview_hours.empid, view_hours.name, hoursworked FROMview_hours WHEREdescription like 'java%' ANDhoursworked = (SELECT min(hoursworked) FROM view_hours WHERE description like 'java%');

14 Uses of views  Views are used to:  Provide easier access to data.  Enhance security.  Lessen the visible complexity of the database.  Views are usually created by the DBA for a defined workgroup of people.  Programmers.  Users.  Users in a specific functional area.

15 When should views be used?  Use to protect sensitive data.  Example: Can create a view of personnel data that does not include salary data.  Use to break down complicated queries into modular components.  Use for group/aggregate functions.  Use for multiple table joins.

16 How should a view be used?  Avoid extra joins. Avoid creating a view with an underlying table, and then using that view to join the result table from the view with that same table in a query. Try to use the view to replace the table, so that you don’t have to use the table again.  Make views flexible. Create views that can be used by more than one query. Anticipate how the data will be accessed so that one view could be used in more than one situation.  Make views useful. Be sure to include relevant foreign keys in a view.

17 Looking at the details of a view in Oracle You can look at all views or just one view. SELECTview_name FROMuser_views; Results from the statement above: VIEW_NAME EMP_TIME TIME_BY_WORK VIEW_HOURS The views above are in the account I used to show the results of the statement. Your names will be different.

18 Looking at the code in a view SELECT text FROM user_views WHERE LOWER(view_name) = ‘hours_by_emp' Results from the query above: TEXT SELECT employee.empid, name, nvl(sum(amount/60),0) hoursworked FROM employ

19 Looking at all the code in a view  The column “text” is a long data type.  Long data types will truncate at a pre-determined size.  Sometimes you won’t see all your SQL code in the “text” column because of the truncation.  To enlarge the size of display for long data types, do the following: SET LONG 500 (or whatever number of characters in the text of the view that you want to see displayed.)