More on views Please refer to speaker notes for additional information!

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!
PL/SQL User Defined Types Record and Table Please use speaker notes for additional information!
Reports Using SQL Script Please check speaker notes for additional information!
Order Entry System Please use speaker notes for additional information!
Line Efficiency     Percentage Month Today’s Date
Group functions using SQL Additional information in speaker notes!
Relational example using donor, donation and drive tables For additional information see the 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!
Table maintenance revisited (again) Please use speaker notes for additional information!
SQL Use of Functions Character functions Please use speaker notes for additional information!
SQL functions - numeric and date Speaker notes contain additional information!
Cursors in PL/SQL Includes cursor example and continuation of first cursor example Please use speaker notes for additional information!
Introduction to Tables/Arrays Please use the speaker notes for additional information. Tables/Arrays.
More on variables with Oracle’s SQL*Plus As always, speaker notes will contain additional information!
Using XML with ASP and the ASP:Repeater Please use speaker notes for additional information!
XML and DTD Please you speaker notes for additional information!
Examples dealing with cursors and tables Please use speaker notes for additional information!
SQL and Conditions Speaker notes will provide additional information!
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
Manipulating data within PL/SQL Please use speaker notes for additional information!
Functions Please use speaker notes for additional information!
Exceptions in PL/SQL Please use speaker notes for additional information!
Indexes in Oracle An Introduction Please check speaker notes for additional information!
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Kansas State University Department of Computing and Information Sciences CIS 560: Database System Concepts Thursday, September 13, 2000 “Structured Query.
Introduction to Oracle - SQL Additional information is available in speaker notes!
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
More on Primary and Foreign Keys Please see speaker notes for additional information!
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
Introduction to PL/SQL As usual, use speaker notes for additional information!
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
More about maintaining a table Use speaker notes for additional information!
More SQL functions As usual,there is additional information in the speaker notes!
Jan 2016 Solar Lunar Data.

Introduction to Procedures
Q1 Jan Feb Mar ENTER TEXT HERE Notes
Introduction to the Array
MySQL - Creating donorof database offline
Project timeline # 3 Step # 3 is about x, y and z # 2
Average Monthly Temperature and Rainfall
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View

“Manipulating Data” Lecture 6.
Timeline PowerPoint Template
Introduction to Views and Reports

Gantt Chart Enter Year Here Activities Jan Feb Mar Apr May Jun Jul Aug
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE

Step 3 Step 2 Step 1 Put your text here Put your text here
More and Still More on Procedures and Functions
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
IST 318 Database Administration
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Project timeline # 3 Step # 3 is about x, y and z # 2

Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Presentation transcript:

More on views Please refer to speaker notes for additional information!

SQL> CREATE VIEW stuview1 2 AS 3 SELECT name, s.majorcode, majorname 4 FROM student00 s, major00 m 5 WHERE s.majorcode = m.majorcode; View created. SQL> DESC stuview1; Name Null? Type NAME VARCHAR2(20) MAJORCODE VARCHAR2(2) MAJORNAME VARCHAR2(30) SQL> SELECT * FROM stuview1; NAME MA MAJORNAME Stephen Daniels BU Business Administration Jennifer Ames CI Computer Information Systems Carl Hersey BU Business Administration Mary Stanton CI Computer Information Systems John Richards CI Computer Information Systems Student view In this slide, I am creating a view with information from the student00 table and the major00 table.

Student view SQL> CREATE OR REPLACE VIEW stuview1 2 AS 3 SELECT name, s.majorcode, majorname, enrolled 4 FROM student00 s, major00 m 5 WHERE s.majorcode = m.majorcode; View created. SQL> DESC stuview1 Name Null? Type NAME VARCHAR2(20) MAJORCODE VARCHAR2(2) MAJORNAME VARCHAR2(30) ENROLLED DATE SQL> SELECT * FROM stuview1; NAME MA MAJORNAME ENROLLED Stephen Daniels BU Business Administration 09-SEP-00 Jennifer Ames CI Computer Information Systems 02-SEP-00 Carl Hersey BU Business Administration 02-SEP-00 Mary Stanton CI Computer Information Systems 05-SEP-00 John Richards CI Computer Information Systems 06-SEP-00 This allows you to create a new view or modify an existing view by replacing the specifications.

SQL> CREATE VIEW payview1 2 (empno, empname, empjobcode, empsalary, empbonus) 3 AS 4 SELECT pay_id, name, jobcode, salary, bonus 5 FROM first_pay 6 WHERE salary > 30000; View created. SQL> DESC payview1 Name Null? Type EMPNO VARCHAR2(4) EMPNAME VARCHAR2(20) EMPJOBCODE CHAR(2) EMPSALARY NUMBER(9,2) EMPBONUS NUMBER(5) SQL> SELECT * FROM payview1; EMPN EMPNAME EM EMPSALARY EMPBONUS Linda Costa CI John Davidson IN Stephen York CM Richard Jones CI Joanne Brown IN Donald Brown CI Paula Adams IN rows selected. Pay view New names are given to the columns/fields in the view. Pay_id will be come empno, name will become empname etc.

Pay view for update SQL> CREATE VIEW new_payview1 2 (empno, empname, empjobcode, empsalary, empbonus) 3 AS 4 SELECT pay_id, name, jobcode, salary, bonus 5 FROM new_first_pay 6 WHERE salary > 30000; I decided to modify new_first_pay, so I recreated the view from the previous slide and named it new_payview1. SQL> SELECT * FROM new_first_pay; PAY_ NAME JO STARTDATE SALARY BONUS Linda Costa CI 15-JAN John Davidson IN 25-SEP Susan Ash AP 05-FEB Stephen York CM 03-JUL Richard Jones CI 30-OCT Joanne Brown IN 18-AUG Donald Brown CI 05-NOV Paula Adams IN 12-DEC Joseph Souza IN SQL> SELECT * FROM new_payview1; EMPN EMPNAME EM EMPSALARY EMPBONUS Linda Costa CI John Davidson IN Stephen York CM Richard Jones CI Joanne Brown IN Donald Brown CI Paula Adams IN Joseph Souza IN 35000

Updating view SQL> SELECT * FROM new_payview1; EMPN EMPNAME EM EMPSALARY EMPBONUS Linda Costa CI John Davidson IN Stephen York CM Richard Jones CI Joanne Brown IN Donald Brown CI Paula Adams IN Joseph Souza IN rows selected. SQL> SELECT * FROM new_first_pay; PAY_ NAME JO STARTDATE SALARY BONUS Linda Costa CI 15-JAN John Davidson IN 25-SEP Susan Ash AP 05-FEB Stephen York CM 03-JUL Richard Jones CI 30-OCT Joanne Brown IN 18-AUG Donald Brown CI 05-NOV Paula Adams IN 12-DEC Joseph Souza IN rows selected. SQL> UPDATE new_payview1 2 SET empbonus = WHERE empno = '9999'; 1 row updated. The update statement updates the data in new_payview1 as shown. When the view is updated, it also updates the data in the original table.

SQL> SELECT * FROM empx; IDN NAME DE John Doe AP 222 Mary Jones AR 333 David Souza AP 444 Susan Brooks AR 555 Michael Brown IN SQL> SELECT * FROM deptx; DE DEPTNAME AP Acct Pay AR Acct Recv IN Inventory View with multiple tables SQL> CREATE VIEW empdeptx 2 AS 3 SELECT idno, name, empx.dept, deptname 4 FROM empx, deptx 5 WHERE empx.dept = deptx.dept; View created. SQL> SELECT * FROM empdeptx; IDN NAME DE DEPTNAME John Doe AP Acct Pay 333 David Souza AP Acct Pay 222 Mary Jones AR Acct Recv 444 Susan Brooks AR Acct Recv 555 Michael Brown IN Inventory I created the two tables shown and then combined the information from the two tables into a view called empdeptx.

Update view SQL> UPDATE empx 2 SET name = 'John Adams' 3 WHERE idno = '111'; 1 row updated. SQL> SELECT * FROM empx; IDN NAME DE John Adams AP 222 Mary Jones AR 333 David Souza AP 444 Susan Brooks AR 555 Michael Brown IN SQL> SELECT * FROM empdeptx; IDN NAME DE DEPTNAME John Adams AP Acct Pay 333 David Souza AP Acct Pay 222 Mary Jones AR Acct Recv 444 Susan Brooks AR Acct Recv 555 Michael Brown IN Inventory In this example, I updated the table empx. The change in the table is also shown in the view.

SQL> CREATE VIEW new_payview2 2 AS 3 SELECT * FROM new_first_pay 4 WHERE salary > WITH CHECK OPTION CONSTRAINT sal30K_ck; View constraints SQL> UPDATE new_payview2 2 SET salary = WHERE pay_id = '9999'; SET salary = * ERROR at line 2: ORA-01402: view WITH CHECK OPTION where-clause violation SQL> UPDATE new_payview2 2 SET salary = WHERE pay_id = '9999'; 1 row updated. SQL> SELECT * FROM new_payview2; PAY_ NAME JO STARTDATE SALARY BONUS Linda Costa CI 15-JAN John Davidson IN 25-SEP Stephen York CM 03-JUL Richard Jones CI 30-OCT Joanne Brown IN 18-AUG Donald Brown CI 05-NOV Paula Adams IN 12-DEC Joseph Souza IN rows selected. I created a view with a constraint that did not allow a salary to be in the view that was not > Only records that met that criteria were originally placed in the view. When I tried to alter a record in the view to come in below the criteria, it was rejected as a check option violation. Here the new salary is above 30000, so the change is allowed.

SQL> SELECT * FROM new_first_pay; PAY_ NAME JO STARTDATE SALARY BONUS Linda Costa CI 15-JAN John Davidson IN 25-SEP Susan Ash AP 05-FEB Stephen York CM 03-JUL Richard Jones CI 30-OCT Joanne Brown IN 18-AUG Donald Brown CI 05-NOV Paula Adams IN 12-DEC Joseph Souza IN rows selected. SQL> SELECT * FROM new_payview2; PAY_ NAME JO STARTDATE SALARY BONUS Linda Costa CI 15-JAN John Davidson IN 25-SEP Stephen York CM 03-JUL Richard Jones CI 30-OCT Joanne Brown IN 18-AUG Donald Brown CI 05-NOV Paula Adams IN 12-DEC rows selected. SQL> UPDATE new_first_pay 2 SET SALARY = WHERE pay_id = '9999'; 1 row updated. Update In this example, I updated the table new_first_pay. The update was successful, but it put 9999 below the criteria for the view new_payview2. When I did a select on that view, the record with pay_id 9999 that now has a salary below the view criteria (salary > 30000) is no longer there.

SQL> CREATE VIEW new_payview3 2 AS 3 SELECT * FROM new_first_pay 4 WITH READ ONLY; View created. SQL> SELECT * FROM new_payview3; PAY_ NAME JO STARTDATE SALARY BONUS Linda Costa CI 15-JAN John Davidson IN 25-SEP Susan Ash AP 05-FEB Stephen York CM 03-JUL Richard Jones CI 30-OCT Joanne Brown IN 18-AUG Donald Brown CI 05-NOV Paula Adams IN 12-DEC Joseph Souza IN rows selected. SQL> UPDATE new_payview3 2 SET salary = WHERE pay_id = '2222'; SET salary = * ERROR at line 2: ORA-01733: virtual column not allowed here Read only Since this view was created as read only, it cannot be updated.

Drop view SQL> DROP VIEW new_payview3; View dropped. SQL> SELECT * FROM new_payview3; SELECT * FROM new_payview3 * ERROR at line 1: ORA-00942: table or view does not exist