more on views please refer to speaker notes for additional information!

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

Upload: godfrey-copeland

Post on 04-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

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

More on views

Please refer to speaker notes for additional information!

Page 2: 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 AdministrationJennifer Ames CI Computer Information SystemsCarl Hersey BU Business AdministrationMary Stanton CI Computer Information SystemsJohn Richards CI Computer Information Systems

Student viewStudent view

In this slide, I am creating a view with information from the student00 table and the major00 table.

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

Student viewStudent 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-00Jennifer Ames CI Computer Information Systems 02-SEP-00Carl Hersey BU Business Administration 02-SEP-00Mary Stanton CI Computer Information Systems 05-SEP-00John 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.

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

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---- -------------------- -- --------- ---------1111 Linda Costa CI 45000 10002222 John Davidson IN 40000 15004444 Stephen York CM 42000 20005555 Richard Jones CI 50000 20006666 Joanne Brown IN 48000 20007777 Donald Brown CI 450008888 Paula Adams IN 45000 2000

7 rows selected.

Pay viewPay view

New names are given to the columns/fields in the view. Pay_id will be come empno, name will become empname etc.

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

Pay view for updatePay 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---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 20007777 Donald Brown CI 05-NOV-99 450008888 Paula Adams IN 12-DEC-98 45000 20009999 Joseph Souza IN 35000

SQL> SELECT * FROM new_payview1;

EMPN EMPNAME EM EMPSALARY EMPBONUS---- -------------------- -- --------- ---------1111 Linda Costa CI 45000 10002222 John Davidson IN 40000 15004444 Stephen York CM 42000 20005555 Richard Jones CI 50000 20006666 Joanne Brown IN 48000 20007777 Donald Brown CI 450008888 Paula Adams IN 45000 20009999 Joseph Souza IN 35000

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

Updating viewUpdating view SQL> SELECT * FROM new_payview1;

EMPN EMPNAME EM EMPSALARY EMPBONUS---- -------------------- -- --------- ---------1111 Linda Costa CI 45000 10002222 John Davidson IN 40000 15004444 Stephen York CM 42000 20005555 Richard Jones CI 50000 20006666 Joanne Brown IN 48000 20007777 Donald Brown CI 450008888 Paula Adams IN 45000 20009999 Joseph Souza IN 35000 1500

8 rows selected.

SQL> SELECT * FROM new_first_pay;

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 20007777 Donald Brown CI 05-NOV-99 450008888 Paula Adams IN 12-DEC-98 45000 20009999 Joseph Souza IN 35000 1500

9 rows selected.

SQL> UPDATE new_payview1 2 SET empbonus = 1500 3 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.

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

SQL> SELECT * FROM empx;

IDN NAME DE--- -------------------- --111 John Doe AP222 Mary Jones AR333 David Souza AP444 Susan Brooks AR555 Michael Brown IN

SQL> SELECT * FROM deptx;

DE DEPTNAME-- ----------AP Acct PayAR Acct RecvIN Inventory

View with multiple tablesView 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--- -------------------- -- ----------111 John Doe AP Acct Pay333 David Souza AP Acct Pay222 Mary Jones AR Acct Recv444 Susan Brooks AR Acct Recv555 Michael Brown IN Inventory

I created the two tables shown and then combined the information from the two tables into a view called empdeptx.

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

Update viewUpdate view

SQL> UPDATE empx 2 SET name = 'John Adams' 3 WHERE idno = '111';

1 row updated.

SQL> SELECT * FROM empx;

IDN NAME DE--- -------------------- --111 John Adams AP222 Mary Jones AR333 David Souza AP444 Susan Brooks AR555 Michael Brown IN

SQL> SELECT * FROM empdeptx;

IDN NAME DE DEPTNAME--- -------------------- -- ----------111 John Adams AP Acct Pay333 David Souza AP Acct Pay222 Mary Jones AR Acct Recv444 Susan Brooks AR Acct Recv555 Michael Brown IN Inventory

In this example, I updated the table empx. The change in the table is also shown in the view.

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

SQL> CREATE VIEW new_payview2 2 AS 3 SELECT * FROM new_first_pay 4 WHERE salary > 30000 5 WITH CHECK OPTION CONSTRAINT sal30K_ck;

View constraintsView constraints

SQL> UPDATE new_payview2 2 SET salary = 29500 3 WHERE pay_id = '9999';SET salary = 29500 *ERROR at line 2:ORA-01402: view WITH CHECK OPTION where-clause violation

SQL> UPDATE new_payview2 2 SET salary = 30500 3 WHERE pay_id = '9999';

1 row updated.

SQL> SELECT * FROM new_payview2;

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 20007777 Donald Brown CI 05-NOV-99 450008888 Paula Adams IN 12-DEC-98 45000 20009999 Joseph Souza IN 30500 1500

8 rows selected.

I created a view with a constraint that did not allow a salary to be in the view that was not > 30000. 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.

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

SQL> SELECT * FROM new_first_pay;

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 20007777 Donald Brown CI 05-NOV-99 450008888 Paula Adams IN 12-DEC-98 45000 20009999 Joseph Souza IN 29500 1500

9 rows selected.

SQL> SELECT * FROM new_payview2;

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 20007777 Donald Brown CI 05-NOV-99 450008888 Paula Adams IN 12-DEC-98 45000 2000

7 rows selected.

SQL> UPDATE new_first_pay 2 SET SALARY = 29500 3 WHERE pay_id = '9999';

1 row updated.

UpdateUpdate

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.

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

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---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 20007777 Donald Brown CI 05-NOV-99 450008888 Paula Adams IN 12-DEC-98 45000 20009999 Joseph Souza IN 29500 1500

9 rows selected.

SQL> UPDATE new_payview3 2 SET salary = 42000 3 WHERE pay_id = '2222';SET salary = 42000 *ERROR at line 2:ORA-01733: virtual column not allowed here

Read onlyRead only

Since this view was created as read only, it cannot be updated.

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

Drop viewDrop 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