creating views
DESCRIPTION
Creating Views. Objectives. Explain the concept of a view. Create simple and complex views. Retrieve data through a view. Alter the definition of a view. Insert, Update, Delete data through a view. Drop a view. What Is a View?. ID LAST_NAME FIRST_NAME TITLE DEPT_ID - PowerPoint PPT PresentationTRANSCRIPT
Creating ViewsCreating Views
14-14-22
Database Systems
ObjectivesObjectives
• Explain the concept of a view.
• Create simple and complex views.
• Retrieve data through a view.
• Alter the definition of a view.
• Insert, Update, Delete data through a view.
• Drop a view.
14-14-33
Database Systems
ID LAST_NAME FIRST_NAME TITLE DEPT_ID-- ------------ ---------- -------------------- ------- 1 Velasquez Carmen President 50 2 Ngao LaDoris VP, Operations 41 3 Nagayama Midori VP, Sales 31 4 Quick-To-See Mark VP, Finance 10 5 Ropeburn Audry VP, Administration 50 6 Urguhart Molly Warehouse Manager 41 7 Menchu Roberta Warehouse Manager 42 8 Biri Ben Warehouse Manager 43 9 Catchpole Antoinette Warehouse Manager 4410 Havel Marta Warehouse Manager 4511 Magee Colin Sales Representative 3112 Giljum Henry Sales Representative 3213 Sedeghi Yasmin Sales Representative 3314 Nguyen Mai Sales Representative 3415 Dumas Andre Sales Representative 3516 Maduro Elena Stock Clerk 4117 Smith George Stock Clerk 4118 Nozaki Akira Stock Clerk 4219 Patel Vikram Stock Clerk 4220 Newman Chad Stock Clerk 4321 Markarian Alexander Stock Clerk 4322 Chang Eddie Stock Clerk 4423 Patel Radha Stock Clerk 3424 Dancs Bela Stock Clerk 4525 Schwartz Sylvie Stock Clerk 45
What Is a View?What Is a View?
ID LAST_NAME TITLE -- ----------- ---------------10 Havel Warehouse Manager24 Dancs Stock Clerk25 Schwartz Stock Clerk
EMP TableEMP Table
EMPVU45 ViewEMPVU45 View
14-14-44
Database Systems
Why Use Views?Why Use Views?
• To restrict database access
• To make complex queries easy
• To allow data independence
• To present different views of the same data
14-14-55
Database Systems
Simple Views and Complex ViewsSimple Views and Complex Views
Number of tables
Contain functions
Contain groups of data
DML through view
Complex ViewsComplex Views
One or moreOne or more
YesYes
YesYes
Not alwaysNot always
Simple ViewsSimple Views
OneOne
NoNo
NoNo
YesYes
14-14-66
Database Systems
Creating a View: SyntaxCreating a View: Syntax
• Embed a subquery within the CREATE VIEW statement.
• The subquery can contain complex SELECT syntax.
• The subquery cannot contain an ORDER BY clause.
CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW viewview
[([(aliasalias[, [, aliasalias]...)]]...)]
AS AS subquerysubquery
[WITH CHECK OPTION [CONSTRAINT [WITH CHECK OPTION [CONSTRAINT constraintconstraint]]]]
[WITH READ ONLY][WITH READ ONLY]
CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW viewview
[([(aliasalias[, [, aliasalias]...)]]...)]
AS AS subquerysubquery
[WITH CHECK OPTION [CONSTRAINT [WITH CHECK OPTION [CONSTRAINT constraintconstraint]]]]
[WITH READ ONLY][WITH READ ONLY]
14-14-77
Database Systems
Creating a View: SyntaxCreating a View: Syntax
• OR REPLACE – re-create the view if it already exists.
• FORCE – create the view regardless whether the base table exists or not.
• WITH CHECK OPTION – specifies that only rows accessible to the view can be inserted or updated.
• WITH READ ONLY – no DML can be performed.
CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW viewview
[([(aliasalias[, [, aliasalias]...)]]...)]
AS AS subquerysubquery
[WITH CHECK OPTION [CONSTRAINT [WITH CHECK OPTION [CONSTRAINT constraintconstraint]]]]
[WITH READ ONLY][WITH READ ONLY]
CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW viewview
[([(aliasalias[, [, aliasalias]...)]]...)]
AS AS subquerysubquery
[WITH CHECK OPTION [CONSTRAINT [WITH CHECK OPTION [CONSTRAINT constraintconstraint]]]]
[WITH READ ONLY][WITH READ ONLY]
14-14-88
Database Systems
Creating a View: ExampleCreating a View: Example
• Create the EMPVU10 view, which contains the employee number, last name, and job title for employees in department 10.
• Describe the structure of the view by using the SQL*Plus DESCRIBE command.
• Display data from the view by entering a SELECT statement against the view.
SQL> CREATE VIEW SQL> CREATE VIEW empvu10empvu10
2 AS SELECT2 AS SELECT empno, ename, jobempno, ename, job
3 FROM3 FROM empemp
4 WHERE4 WHERE deptno = 10;deptno = 10;
View created.View created.
SQL> CREATE VIEW SQL> CREATE VIEW empvu10empvu10
2 AS SELECT2 AS SELECT empno, ename, jobempno, ename, job
3 FROM3 FROM empemp
4 WHERE4 WHERE deptno = 10;deptno = 10;
View created.View created.
14-14-99
Database Systems
Creating a View: ExampleCreating a View: Example
• Create a view by using column aliases in the subquery.
• Select the columns from this view by the given alias name.
SQL> CREATE VIEW salvu30SQL> CREATE VIEW salvu30
2 AS SELECT2 AS SELECT empno EMPLOYEE_NUMBER, ename FIRST_NAME,empno EMPLOYEE_NUMBER, ename FIRST_NAME,
3 3 sal MONTHLY_SALARYsal MONTHLY_SALARY
4 FROM4 FROM empemp
5 WHERE5 WHERE deptno = 30;deptno = 30;
View created.View created.
SQL> CREATE VIEW salvu30SQL> CREATE VIEW salvu30
2 AS SELECT2 AS SELECT empno EMPLOYEE_NUMBER, ename FIRST_NAME,empno EMPLOYEE_NUMBER, ename FIRST_NAME,
3 3 sal MONTHLY_SALARYsal MONTHLY_SALARY
4 FROM4 FROM empemp
5 WHERE5 WHERE deptno = 30;deptno = 30;
View created.View created.
14-14-1010
Database Systems
Modifying a View: ExampleModifying a View: Example
• Modify the EMPVU10 view by using CREATE OR REPLACE. Add an alias for each column name.
• Column aliases in the CREATE VIEW clause are listed in the same order as the columns in the subquery.
SQL> CREATE OR REPLACE VIEW empvu10SQL> CREATE OR REPLACE VIEW empvu10
2 (id_number, employee, title)2 (id_number, employee, title)
3 AS SELECT3 AS SELECT empno, ename, jobempno, ename, job
4 FROM4 FROM empemp
5 WHERE5 WHERE deptno = 10;deptno = 10;
View created.View created.
SQL> CREATE OR REPLACE VIEW empvu10SQL> CREATE OR REPLACE VIEW empvu10
2 (id_number, employee, title)2 (id_number, employee, title)
3 AS SELECT3 AS SELECT empno, ename, jobempno, ename, job
4 FROM4 FROM empemp
5 WHERE5 WHERE deptno = 10;deptno = 10;
View created.View created.
14-14-1111
Database Systems
Creating a Complex View: ExampleCreating a Complex View: Example
Create a complex view that contains group functions to display values from two tables.
SQL> CREATE VIEW dept_sum_vuSQL> CREATE VIEW dept_sum_vu
2 (name, minsal, maxsal, avgsal)2 (name, minsal, maxsal, avgsal)
3 AS SELECT3 AS SELECT d.dname, MIN(e.sal),d.dname, MIN(e.sal),
4 MAX(e.sal), AVG(e.sal)4 MAX(e.sal), AVG(e.sal)
5 FROM5 FROM emp e, dept demp e, dept d
6 WHERE6 WHERE e.deptno = d.deptnoe.deptno = d.deptno
7 GROUP BY 7 GROUP BY d.dname;d.dname;
View created.View created.
SQL> CREATE VIEW dept_sum_vuSQL> CREATE VIEW dept_sum_vu
2 (name, minsal, maxsal, avgsal)2 (name, minsal, maxsal, avgsal)
3 AS SELECT3 AS SELECT d.dname, MIN(e.sal),d.dname, MIN(e.sal),
4 MAX(e.sal), AVG(e.sal)4 MAX(e.sal), AVG(e.sal)
5 FROM5 FROM emp e, dept demp e, dept d
6 WHERE6 WHERE e.deptno = d.deptnoe.deptno = d.deptno
7 GROUP BY 7 GROUP BY d.dname;d.dname;
View created.View created.
14-14-1212
Database Systems
Rules for Performing DML Rules for Performing DML Operations on a ViewOperations on a View
• You can perform DML operations on simple views.
• You cannot remove a row if the view contains
– Group functions.
– A GROUP BY clause.
– The DISTINCT keyword.
14-14-1313
Database Systems
Rules for Performing DML Rules for Performing DML Operations on a View Operations on a View continuedcontinued
• You cannot modify data in a view if it contains
– Any of the above conditions.
– Columns defined by expressions.
– The ROWNUM pseudocolumn.
• You cannot add data if the view contains
– Any of the above conditions.
– Any NOT NULL columns in the base table not selected by the view.
14-14-1414
Database Systems
Using the WITH CHECK OPTION Using the WITH CHECK OPTION ClauseClause
• Ensure that DML on the view stays within the domain of the view.
• If you attempt to change the department number for any rows in the view, the statement will fail because it violates the CHECK OPTION constraint.
SQL> CREATE OR REPLACE VIEW empvu20SQL> CREATE OR REPLACE VIEW empvu20
2 AS SELECT2 AS SELECT **
3 FROM3 FROM empemp
4 WHERE4 WHERE deptno = 20deptno = 20
5 WITH CHECK OPTION CONSTRAINT empvu20_ck;5 WITH CHECK OPTION CONSTRAINT empvu20_ck;
View created.View created.
SQL> CREATE OR REPLACE VIEW empvu20SQL> CREATE OR REPLACE VIEW empvu20
2 AS SELECT2 AS SELECT **
3 FROM3 FROM empemp
4 WHERE4 WHERE deptno = 20deptno = 20
5 WITH CHECK OPTION CONSTRAINT empvu20_ck;5 WITH CHECK OPTION CONSTRAINT empvu20_ck;
View created.View created.
14-14-1515
Database Systems
Using the WITH CHECK OPTION Using the WITH CHECK OPTION ClauseClause
• Without check option, employee with empno = 7788 will be removed from the view.
SQL> UPDATE empvu20SQL> UPDATE empvu20
2 SET deptno = 102 SET deptno = 10
3 WHERE empno = 7788;3 WHERE empno = 7788;
Update empvu20Update empvu20
**
ERROR at line 1:ERROR at line 1:
ORA-01402: view WITH CHECK OPTION where-clause ORA-01402: view WITH CHECK OPTION where-clause
violationviolation
SQL> UPDATE empvu20SQL> UPDATE empvu20
2 SET deptno = 102 SET deptno = 10
3 WHERE empno = 7788;3 WHERE empno = 7788;
Update empvu20Update empvu20
**
ERROR at line 1:ERROR at line 1:
ORA-01402: view WITH CHECK OPTION where-clause ORA-01402: view WITH CHECK OPTION where-clause
violationviolation
14-14-1616
Database Systems
Denying DML OperationsDenying DML Operations
• Ensure that no DML operations occur by adding the WITH READ ONLY option to your view definition.
• If you attempt to perform a DML on any rows in the view, you will see the Oracle Server error.
SQL> CREATE OR REPLACE VIEW empvu10SQL> CREATE OR REPLACE VIEW empvu10
2 (id_number, employee, title)2 (id_number, employee, title)
3 AS SELECT3 AS SELECT empno, ename, jobempno, ename, job
4 FROM4 FROM empemp
5 WHERE5 WHERE deptno = 10deptno = 10
6 WITH READ ONLY;6 WITH READ ONLY;
View created.View created.
SQL> CREATE OR REPLACE VIEW empvu10SQL> CREATE OR REPLACE VIEW empvu10
2 (id_number, employee, title)2 (id_number, employee, title)
3 AS SELECT3 AS SELECT empno, ename, jobempno, ename, job
4 FROM4 FROM empemp
5 WHERE5 WHERE deptno = 10deptno = 10
6 WITH READ ONLY;6 WITH READ ONLY;
View created.View created.
14-14-1717
Database Systems
Confirming ViewsConfirming Views
The USER_VIEWS data dictionary table contains the name of the view and the view definition.
SQL> SELECTSQL> SELECT view_name, textview_name, text
2 FROM2 FROM user_views;user_views;
SQL> SELECTSQL> SELECT view_name, textview_name, text
2 FROM2 FROM user_views;user_views;
14-14-1818
Database Systems
Removing a View: ExampleRemoving a View: Example
Remove a view without losing data because a view is based on underlying tables in the database.
SQL> DROP VIEW empvu10; SQL> DROP VIEW empvu10;
View dropped.View dropped.
SQL> DROP VIEW empvu10; SQL> DROP VIEW empvu10;
View dropped.View dropped.
14-14-1919
Database Systems
SummarySummary
• A view is derived from data in other tables or other views.
• A view is like a window to the underlying data.
• A view provides the following advantages:
– Restrict database access
– Simplify queries
– Provide data independence
– Allow multiple views of the same data
– Can be dropped without removing the underlying data
14-14-2020
Database Systems
Practice OverviewPractice Overview
• Creating a simple view
• Creating a complex view
• Creating a view with a check constraint
• Attempting to modify data in the view
• Displaying view definitions
• Removing views
14-14-2121
Database Systems
Practice 1Practice 1
• Create a view called DEPT20 that contains the employee number, name and department number for all employees in department 20 from EMP table. Label the view column EMPLOYEE_ID, EMPLOYEE and DEPARTMENT_ID. Do not allow an employee to be reassigned to another department through the view.
• Display the structure and contents of the DEPT20 view.
• Select the view name and text from the data dictionary USER_VIEWS.
• Attempt to reassign Smith to department 30.
14-14-2222
Database Systems
Practice 2Practice 2
• Create a view called SALARY_VU based on the employee name, depart name, salary and salary grade for all employees, using tables EMP, DEPT and SALGRADE. Label the columns Employee, Department, Salary and Grade respectively.