Introduction to Views and Reports

Slides:



Advertisements
Similar presentations
Relational database - student system As always please use speaker notes!
Advertisements

Creating an Oracle Database Table Additional information is available in speaker notes!
Importing and linking through Access Please use speaker notes for additional information!
Reports Using SQL Script Please check speaker notes for additional information!
Group functions using SQL Additional information in speaker notes!
Using input variables Please use speaker notes for additional information!
PL/SQL - Using IF statements Please use speaker notes for additional information!
More on IF statements Use speaker notes for additional information!
A Guide to SQL, Seventh Edition. Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT.
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
Microsoft Access 2010 Chapter 7 Using SQL.
Table maintenance revisited (again) Please use speaker notes for additional information!
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
SQL Use of Functions Character functions Please use speaker notes for additional information!
SQL functions - numeric and date Speaker notes contain additional information!
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
ASP.NET Programming with C# and SQL Server First Edition
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
XP 1 Excel Tables Purpose of tables – Process data in a group – Used to facilitate calculations – Used to enhance readability of output Types of tables.
More on variables with Oracle’s SQL*Plus As always, speaker notes will contain additional information!
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Examples dealing with cursors and tables Please use speaker notes for additional information!
More on views Please refer to speaker notes for additional information!
SQL and Conditions Speaker notes will provide additional information!
Manipulating data within PL/SQL Please use speaker notes for additional information!
Indexes in Oracle An Introduction Please check speaker notes for additional information!
Introduction to Oracle - SQL Additional information is available in speaker notes!
Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
Working with Columns, Characters, and Rows. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Apply the concatenation.
Task #1 Create a relational database on computers in computer classroom 308, using MySQL server and any client. Create the same database, using MS Access.
More on Primary and Foreign Keys Please see speaker notes for additional information!
Relational Database in Access Student System As always please use speaker notes!
Introduction to PL/SQL As usual, use speaker notes for additional information!
More about maintaining a table Use speaker notes for additional information!
1 Working with MS SQL Server Beginning ASP.NET in C# and VB Chapter 12.
Unbound data fields, find, filter etc. using Data Environment Please use speaker notes for additional information!
More SQL functions As usual,there is additional information in the speaker notes!
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Emdeon Office Batch Management Services This document provides detailed information on Batch Import Services and other Batch features.
3 A Guide to MySQL.
Retrieving Data Using the SQL SELECT Statement
Find, filter etc with connection to Access code internally
Aggregating Data Using Group Functions
PL/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-1
Subqueries.
This shows the user interface and the SQL Select for a situation with two criteria in an AND relationship.
Introduction to Triggers
Using the Set Operators
Introduction to Procedures
Introduction to the Array
Access Database for CIS17
MySQL - Creating donorof database offline
Please use speaker notes for additional information!
(SQL) Aggregating Data Using Group Functions
Using the Set Operators
Please use speaker notes for additional information!
Please use speaker notes for additional information!
Access and Condition Statements
Aggregating Data Using Group Functions
Microsoft Official Academic Course, Access 2016
Access Database for CIT12
Please use speaker notes for additional information!
Please use speaker notes for additional information!
Notes on SQL This slide show will introduce SQL using Access. It assumes only an introductory level of knowledge about Access.
More and Still More on Procedures and Functions
IST 318 Database Administration
Using the Set Operators
Please use speaker notes for additional information!
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Presentation transcript:

Introduction to Views and Reports Please use speaker notes for additional information! Information on creating views and the need for views.

Views SQL> SELECT * FROM course00; COURS COURSENAME CREDITS ----- ------------------------------ --------- CIS11 Intro to Computer Info Systems 3 CIS44 Internet User/Developer 3 CIS50 Oracle and SQL 3 CIS56 Visual Basic 3 MAN11 Intro to Management 3 MAR11 Marketing Principles 3 SQL> SELECT * FROM stucourse00; STUD COURS SEMTA GR ---- ----- ----- -- 1111 CIS11 F2000 A- 1111 MAR11 F2000 A 1111 CIS44 S2000 A 1212 CIS44 S2000 A 2222 CIS44 S2000 A 2222 MAN11 F2000 A- 3333 CIS44 F2000 B 3333 CIS50 F2000 B+ 3333 CIS56 S2000 A- 2345 CIS50 F2000 I SQL> CREATE VIEW stucrswithcr 2 AS 3 SELECT studentidno, stucourse00.coursecd, credits, grade 4 FROM stucourse00, course00 5 WHERE stucourse00.coursecd = course00.coursecd; View created. SQL> SELECT * FROM stucrswithcr; STUD COURS CREDITS GR ---- ----- --------- -- 1111 CIS11 3 A- 1111 CIS44 3 A 1212 CIS44 3 A 2222 CIS44 3 A 3333 CIS44 3 B 3333 CIS50 3 B+ 2345 CIS50 3 I 3333 CIS56 3 A- 2222 MAN11 3 A- 1111 MAR11 3 A 11 rows selected. A view carries information from one or more tables. It does not physically carry any data, it is simply a way to view data from one or more tables. Please read the notes on Introduction to Views and Reports for more specific comments. A view which contains information from stucourse00 and course00 was created. To see the output of the view, do a SELECT just as you would for a table.

3 WHERE grade IN ('A+','A','A-'); STUD COURS CREDITS GR View SQL> SELECT * FROM stucrswithcr; STUD COURS CREDITS GR ---- ----- --------- -- 1111 CIS11 3 A- 1111 CIS44 3 A 1212 CIS44 3 A 2222 CIS44 3 A 3333 CIS44 3 B 3333 CIS50 3 B+ 2345 CIS50 3 I 3333 CIS56 3 A- 2222 MAN11 3 A- 1111 MAR11 3 A 11 rows selected. SQL> SELECT * 2 FROM stucrswithcr 3 WHERE grade IN ('A+','A','A-'); STUD COURS CREDITS GR ---- ----- --------- -- 1111 CIS11 3 A- 1111 CIS44 3 A 1212 CIS44 3 A 2222 CIS44 3 A 3333 CIS56 3 A- 2222 MAN11 3 A- 1111 MAR11 3 A 7 rows selected. This SELECT uses the view created on the previous slide and shown above. The SELECT is taking records that meet specific criteria from the view just as it would from at table. The advantage of this view is that the data from two tables is linked together and the results are stored as a view. Instead of doing the select with the added complications of linking the two tables, you can now do a select using the view name. Remember views are not permanent data stores. When a change is made to one of the tables, the view will reflect that change.

Insert new record into stucourse00. Views SQL> SELECT * FROM stucourse00; STUD COURS SEMTA GR ---- ----- ----- -- 1111 CIS11 F2000 A- 1111 MAR11 F2000 A 1111 CIS44 S2000 A 1212 CIS44 S2000 A 2222 CIS44 S2000 A 2222 MAN11 F2000 A- 3333 CIS44 F2000 B 3333 CIS50 F2000 B+ 3333 CIS56 S2000 A- 2345 CIS50 F2000 I SQL> INSERT INTO stucourse00 2 VALUES ('1111','CIS50','S2000','A+'); 1 row created. SQL> SELECT * FROM stucourse00; STUD COURS SEMTA GR ---- ----- ----- -- 1111 CIS11 F2000 A- 1111 MAR11 F2000 A 1111 CIS44 S2000 A 1212 CIS44 S2000 A 2222 CIS44 S2000 A 2222 MAN11 F2000 A- 3333 CIS44 F2000 B 3333 CIS50 F2000 B+ 3333 CIS56 S2000 A- 2345 CIS50 F2000 I 1111 CIS50 S2000 A+ 12 rows selected. Insert new record into stucourse00. Stucoure00 prior to insert Stucourse00 after the insert. SQL> SELECT * FROM stucrswithcr; STUD COURS CREDITS GR ---- ----- --------- -- 1111 CIS11 3 A- 1111 CIS44 3 A 1212 CIS44 3 A 2222 CIS44 3 A 3333 CIS44 3 B 3333 CIS50 3 B+ 2345 CIS50 3 I 1111 CIS50 3 A+ 3333 CIS56 3 A- 2222 MAN11 3 A- 1111 MAR11 3 A 12 rows selected. Note: No alternation or updating had to be made to the view. I simply added the record to the stucoure00 table and did the select from the view. The new record was incorporated. The new record has now been added to the stucrswithcr view.

SQL> CREATE VIEW stucrswithname 2 AS 3 SELECT studentidno, stucourse00.coursecd, coursename, credits, grade 4 FROM stucourse00, course00 5 WHERE stucourse00.coursecd = course00.coursecd; SQL> SELECT * FROM stucrswithname; STUD COURS COURSENAME CREDITS GR ---- ----- ------------------------------ --------- -- 1111 CIS11 Intro to Computer Info Systems 3 A- 1111 CIS44 Internet User/Developer 3 A 1212 CIS44 Internet User/Developer 3 A 2222 CIS44 Internet User/Developer 3 A 3333 CIS44 Internet User/Developer 3 B 3333 CIS50 Oracle and SQL 3 B+ 2345 CIS50 Oracle and SQL 3 I 1111 CIS50 Oracle and SQL 3 A+ 3333 CIS56 Visual Basic 3 A- 2222 MAN11 Intro to Management 3 A- 1111 MAR11 Marketing Principles 3 A 12 rows selected. Note that the information that was added to stucourse00 is included.

SQL> DROP VIEW stucrswithname; View dropped. DROP VIEW allows me to eliminate a view. Notice that neither DESC or SELECT can find the view once it has been dropped. SQL> DESC stucrswithname; Object does not exist. SQL> SELECT * FROM stucrswithname; SELECT * FROM stucrswithname * ERROR at line 1: ORA-00942: table or view does not exist Again note the difference in messages with a DESC and a SELECT.

SQL> CREATE VIEW majorinfo 2 AS 3 SELECT name, student00.majorcode, majorname, chair 4 FROM student00, major00 5 WHERE student00.majorcode = major00.majorcode; SQL> / View created. The select below shows the information in the view that was created using information from student00 and major00. SQL> SELECT * FROM majorinfo; NAME MA MAJORNAME CHAIR -------------------- -- ------------------------------ ---------- Stephen Daniels BU Business Administration Adams Carl Hersey BU Business Administration Adams Jennifer Ames CI Computer Information Systems Grocer Mary Stanton CI Computer Information Systems Grocer John Richards CI Computer Information Systems Grocer This slide creates another view using information from two tables. The logic is the same as doing the select for the two tables alone. The difference is the results are stored as a view for future use.

SQL> SELECT * FROM majorinfo 2 WHERE chair = 'Grocer'; view SQL> SELECT * FROM majorinfo 2 WHERE chair = 'Grocer'; NAME MA MAJORNAME CHAIR -------------------- -- ------------------------------ ---------- Jennifer Ames CI Computer Information Systems Grocer Mary Stanton CI Computer Information Systems Grocer John Richards CI Computer Information Systems Grocer SQL> SELECT * FROM majorinfo 2 WHERE name > 'J' AND majorcode = 'CI'; NAME MA MAJORNAME CHAIR -------------------- -- ------------------------------ ----------- Jennifer Ames CI Computer Information Systems Grocer Mary Stanton CI Computer Information Systems Grocer John Richards CI Computer Information Systems Grocer This slide shows three different selects using the view majorinfo. SQL> SELECT * FROM majorinfo 2 WHERE name > 'L' OR majorcode = 'BU'; NAME MA MAJORNAME CHAIR -------------------- -- ------------------------------ ---------- Stephen Daniels BU Business Administration Adams Carl Hersey BU Business Administration Adams Mary Stanton CI Computer Information Systems Grocer

view SQL> CREATE VIEW stucrsinfo 2 AS 3 SELECT student00.studentidno, name, stucourse00.coursecd, coursename, grade 4 FROM student00, stucourse00, course00 5 WHERE student00.studentidno = stucourse00.studentidno 6 AND stucourse00.coursecd = course00.coursecd; View created. SQL> SELECT * FROM stucrsinfo; STUD NAME COURS COURSENAME GR ---- -------------------- ----- ------------------------------ -- 1111 Stephen Daniels CIS11 Intro to Computer Info Systems A- 1111 Stephen Daniels CIS44 Internet User/Developer A 1111 Stephen Daniels CIS50 Oracle and SQL A+ 1111 Stephen Daniels MAR11 Marketing Principles A 1212 Jennifer Ames CIS44 Internet User/Developer A 2222 Carl Hersey CIS44 Internet User/Developer A 2222 Carl Hersey MAN11 Intro to Management A- 2345 Mary Stanton CIS50 Oracle and SQL I 3333 John Richards CIS44 Internet User/Developer B 3333 John Richards CIS56 Visual Basic A- 3333 John Richards CIS50 Oracle and SQL B+ 12 rows selected. This slide shows a view created using three tables.

The / will attempt execution. Correcting my errors SQL> CREATE VIEW crsinfoA 2 AS 3 SELECT student00.studentidno, name stucourseoo.coursecd, coursename, grade 4 FROM student00, stucourse00, course00 5 WHERE student00.studentidno = stucourse00.studentidno 6 AND stucourse00.coursecd = course00.coursecd 7 AND SUBSTR(coursecd,1,3) = 'CIS'; SELECT student00.studentidno, name stucourseoo.coursecd, coursename, grade * ERROR at line 3: ORA-00923: FROM keyword not found where expected My first error needed to have line 3 changed so that stucourseoo became stucourse00. SQL> 3 3* SELECT student00.studentidno, name stucourseoo.coursecd, coursename, grade SQL> c/stucourseoo/stucourse00 3* SELECT student00.studentidno, name stucourse00.coursecd, coursename, grade The / will attempt execution. SQL> / SELECT student00.studentidno, name stucourse00.coursecd, coursename, grade * ERROR at line 3: ORA-00923: FROM keyword not found where expected SQL> 3 3* SELECT student00.studentidno, name stucourse00.coursecd, coursename, grade SQL> c/name/name, 3* SELECT student00.studentidno, name, stucourse00.coursecd, coursename, grade In creating this view, I made lots of errors. I decided to show the process one more time instead of just fixing behind the scenes. My second error was omitting the comma after name. Again it was on line 3 and I did the change of name to name,

Correcting my errors continued Being optimistic, I used the ; to see the changed code and then used the / to attempt to execute. SQL> ; 1 CREATE VIEW crsinfoA 2 AS 3 SELECT student00.studentidno, name, stucourse00.coursecd, coursename, grade 4 FROM student00, stucourse00, course00 5 WHERE student00.studentidno = stucourse00.studentidno 6 AND stucourse00.coursecd = course00.coursecd 7* AND SUBSTR(coursecd,1,3) = 'CIS' SQL> / AND SUBSTR(coursecd,1,3) = 'CIS' * ERROR at line 7: ORA-00918: column ambiguously defined SQL> 7 SQL> c/coursecd/stucourse00.coursecd 7* AND SUBSTR(stucourse00.coursecd,1,3) = 'CIS' View created. The next error happened because coursecd is on two tables - I have to specify which one. I brought up line 7 and made the change. This view takes information from three related tables and also specifies that the first three characters of stucourse00.coursecd be equal to CIS for all records extracted. This time when I used the /, the view was created.

Code after the corrections were made The ; shows the code for the current SQL command. SQL> ; 1 CREATE VIEW crsinfoA 2 AS 3 SELECT student00.studentidno, name, stucourse00.coursecd, coursename, grade 4 FROM student00, stucourse00, course00 5 WHERE student00.studentidno = stucourse00.studentidno 6 AND stucourse00.coursecd = course00.coursecd 7* AND SUBSTR(stucourse00.coursecd,1,3) = 'CIS' Note the * after the current line. Once the view has been created, I can see the results by doing the select from the view. Remember the information comes from three tables. SQL> SELECT * FROM crsinfoA; STUD NAME COURS COURSENAME GR ---- -------------------- ----- ------------------------------ -- 1111 Stephen Daniels CIS11 Intro to Computer Info Systems A- 1111 Stephen Daniels CIS44 Internet User/Developer A 1111 Stephen Daniels CIS50 Oracle and SQL A+ 1212 Jennifer Ames CIS44 Internet User/Developer A 2222 Carl Hersey CIS44 Internet User/Developer A 2345 Mary Stanton CIS50 Oracle and SQL I 3333 John Richards CIS44 Internet User/Developer B 3333 John Richards CIS56 Visual Basic A- 3333 John Richards CIS50 Oracle and SQL B+ 10 rows selected. The * on the current line appears when you have been editing the text of the command. Note that the final AND on line 7 limited the records in the view to those that contained CIS as the first three characters of stucourse00.coursecd. This is what is in the view. I cannot use this view to see anything about MAR or MAN. They are not a part of the view.

View This SELECT takes only those records with some kind of A as the grade from the crsinfoA VIEW. SQL> SELECT studentidno, name, coursecd, coursename, grade 2 FROM crsinfoA 3 WHERE grade IN ('A+','A','A-'); STUD NAME COURS COURSENAME GR ---- -------------------- ----- ------------------------------ -- 1111 Stephen Daniels CIS11 Intro to Computer Info Systems A- 1111 Stephen Daniels CIS44 Internet User/Developer A 1111 Stephen Daniels CIS50 Oracle and SQL A+ 1212 Jennifer Ames CIS44 Internet User/Developer A 2222 Carl Hersey CIS44 Internet User/Developer A 3333 John Richards CIS56 Visual Basic A- 6 rows selected. This is the description of the crsinfoA VIEW. Note that the names used in the select above are the names defined in the view. Note that in the SELECT I can refer to the fields simply by name because those are the names defined in the view. I do not have to be concerned about which column on which table the data is stored. SQL> DESC crsinfoA; Name Null? Type ------------------------------- -------- ---- STUDENTIDNO VARCHAR2(4) NAME VARCHAR2(20) COURSECD VARCHAR2(5) COURSENAME VARCHAR2(30) GRADE VARCHAR2(2)

View SQL> CREATE VIEW first_pay_2000 (idno, name, jobcd, sal, bonus) 2 AS 3 SELECT pay_id, name, jobcode, salary, bonus 4 FROM first_pay 5 WHERE bonus = 2000; View created. SQL> DESC first_pay_2000; Name Null? Type ------------------------------- -------- ---- IDNO VARCHAR2(4) NAME VARCHAR2(20) JOBCD CHAR(2) SAL NUMBER(9,2) BONUS NUMBER(5) SQL> SELECT * FROM first_pay_2000; IDNO NAME JO SAL BONUS ---- -------------------- -- --------- --------- 4444 Stephen York CM 42000 2000 5555 Richard Jones CI 50000 2000 6666 Joanne Brown IN 48000 2000 8888 Paula Adams IN 45000 2000 In this view I am using unique names for some of the columns/fields. Notice that the description of the view contains these names as does the SELECT. You want to think whether there is a reason for changing from names you are used to before making similar changes.

Original jobcode, salary and bonus data. Summary View SQL> CREATE VIEW jobview (jobcode, countjob, sumsal, sumbonus) AS 2 SELECT jobcode, COUNT(jobcode), SUM(salary), SUM(bonus) 3 FROM first_pay 4 GROUP BY jobcode; View created. SQL> DESC jobview; Name Null? Type ------------------------------- -------- ---- JOBCODE CHAR(2) COUNTJOB NUMBER SUMSAL NUMBER SUMBONUS NUMBER SQL> SELECT * FROM jobview; JO COUNTJOB SUMSAL SUMBONUS -- --------- --------- --------- AP 1 25000 500 CI 3 140000 3000 CM 1 42000 2000 IN 3 133000 5500 This view contains totals by jobcode for the number of rows in each jobcode, the sum of the salary in each jobcode and the sum of the bonus in each jobcode. Original jobcode, salary and bonus data. Notice that I gave names to each of the group totals that I accumulated for this view. Notice that the count and sum were given default numeric types with the default length. SQL> SELECT jobcode, salary, bonus 2 FROM first_pay; JO SALARY BONUS -- --------- --------- CI 45000 1000 IN 40000 1500 AP 25000 500 CM 42000 2000 CI 50000 2000 IN 48000 2000 CI 45000 IN 45000 2000