Week 11b Views
Week 11b Views
Up to now, we have created many queries but all are lost now…
EXAMPLE
CREATE VIEW salesman
AS
SELECT * FROM emp WHERE job=‘SALESMAN’ ;
FORCE :creates the view regardless of whether or not the base table exist
WITH CHECK OPTION :specifies that only rows accesible to the view can be inserted or
updated
DESCRIBE emp10 ;
-The subquery that defines a view can contain complex SELECT syntax,
-The subquery that defines the view cannot contain an ORDER BY clause.
The ORDER BY clause is specified when you retrieve data from the view.
(In version ORACLE 9i)
-If you do not specify a constraint name for a view created with the
-You can use the OR REPLACE option to change the definition of the view
View created .
You can control the column name by including column aliases within a subquery.
2- This example creates a view containing the employee number with the alias NO, name
(ename) with the alias EMPLOYEE, and annual salary (sal) with the alias ANNUAL for every
employees in department 20.
As an alternative, you can use an alias after CREATE statement and prior to the SELECT
subquery.
The number of aliases listed match the number of expressions selected in the subquery.
CREATE OR REPLACE VIEW emp10 AS SELECT empno,ename,deptno FROM emp WHERE deptno=30;
View created.
0 row deleted
Updating rows in a view
0 row deleted
Creating a Complex View
Create a complex view that contains group functions to display values from table.
CREATE VIEW job_salary AS SELECT job,SUM(sal) as SALARY FROM emp GROUP BY job ;
DESC job_salary
DELETING DATA
You can perform DML operations on simple views.
You can not remove a row if the view containts the following :
-Group function
-A GROUP BY clause
-The DISTINCT keyword
-The ROWNUM keyword
MODIFY DATA
MODIFY DATA
Any attempt to change the JOB for any row in the view fails because it violates
The WITH CHECK OPTION constraint.
SELET * FROM CLERKS ;
If there is an attempt to perform DML operations on rows that the view has not selected,
An error is displayed, with the constraint name if that has been specified.
Denying DML Operations
You can ensure that no DML operations occur by adding the WITH READ ONLY option to
your view definition.
Any attempt to perform a DML on any row in the view results in an Oracle Server error.
You can remove a view without losing data because a view is based on underlying tables
in the database.
3- Select the view name and text from the USER_VIEWS data dictionary view
4- Using your EMPLOYEE_1 view, write a query to display all employes names and department
number.
5-Create a view DEPT50 that contains the employee number, employee last names,and
department numbers for all employees in department 50 in EMPLOYEES table.
Label the view columns EMPNO,EMPLOYEE, AND DEPTNO
Don’t allow an employee to be reassigned to another department through the view.
8- Create a view called SALARY_1 based on the employee last names, department names,
9-Create a view which returns all employees have not got any commission in department 30
in EMP. Nobody can delete,or update any rows and insert any rows to that view.