Download presentation
Presentation is loading. Please wait.
1
Introduction to Views and Reports
Please use speaker notes for additional information! Information on creating views and the need for views.
2
Views SQL> SELECT * FROM course00; COURS COURSENAME CREDITS CIS11 Intro to Computer Info Systems CIS44 Internet User/Developer CIS50 Oracle and SQL CIS56 Visual Basic MAN11 Intro to Management MAR11 Marketing Principles 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 CIS A- 1111 CIS A 1212 CIS A 2222 CIS A 3333 CIS B 3333 CIS B+ 2345 CIS I 3333 CIS A- 2222 MAN A- 1111 MAR 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
3 WHERE grade IN ('A+','A','A-'); STUD COURS CREDITS GR
View SQL> SELECT * FROM stucrswithcr; STUD COURS CREDITS GR 1111 CIS A- 1111 CIS A 1212 CIS A 2222 CIS A 3333 CIS B 3333 CIS B+ 2345 CIS I 3333 CIS A- 2222 MAN A- 1111 MAR A 11 rows selected. SQL> SELECT * 2 FROM stucrswithcr 3 WHERE grade IN ('A+','A','A-'); STUD COURS CREDITS GR 1111 CIS A- 1111 CIS A 1212 CIS A 2222 CIS A 3333 CIS A- 2222 MAN A- 1111 MAR 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.
4
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 CIS A- 1111 CIS A 1212 CIS A 2222 CIS A 3333 CIS B 3333 CIS B+ 2345 CIS I 1111 CIS A+ 3333 CIS A- 2222 MAN A- 1111 MAR 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.
5
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 A- 1111 CIS44 Internet User/Developer A 1212 CIS44 Internet User/Developer A 2222 CIS44 Internet User/Developer A 3333 CIS44 Internet User/Developer B 3333 CIS50 Oracle and SQL B+ 2345 CIS50 Oracle and SQL I 1111 CIS50 Oracle and SQL A+ 3333 CIS56 Visual Basic A- 2222 MAN11 Intro to Management A- 1111 MAR11 Marketing Principles A 12 rows selected. Note that the information that was added to stucourse00 is included.
6
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.
7
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.
8
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
9
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.
10
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,
11
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.
12
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.
13
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)
14
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 5555 Richard Jones CI 6666 Joanne Brown IN 8888 Paula Adams IN 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.
15
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 CI CM IN 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 IN AP CM CI IN CI IN
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.