Week 11b Views

  • Upload
    kenan

  • View
    229

  • Download
    0

Embed Size (px)

Citation preview

  • 8/9/2019 Week 11b Views

    1/26

  • 8/9/2019 Week 11b Views

    2/26

    SELECT ENAME,CASEWHEN (JOB='MANAGER') THEN

    SAL+SAL*30/100WHEN (JOB='ANALYST') THEN

    SAL+SAL*40/100WHEN (JOB='CLERK') THEN

    SAL+SAL*50/100WHEN (JOB='SALESMAN') THEN

    SAL+SAL*60/100WHEN (JOB='PRESIDENT') THEN

    SAL+SAL*70/100 ELSE SAL END AS NEWFROM EMP;

    SELECT empno,ename,job,salFROM empWHEREjob=

    ( SELECTjob FROM empWHERE empno=7521 )

    AND sal>( SELECT salFROM emp

    WHERE empno=7654 )

    SELECT * FROM EMP WHERE HIREDATEBETWEEN 01-JAN-1980 AND 01-MAR-1981

    SELECT empno,ename,job,salFROM empWHERE job SALESMAN ANDSAL < ANY ( SELECT sal FROM emp

    WHERE job=SALESMAN

    SELECT NAME,( NVL(MATH,0)+NVL(PHYSICS,0)+NVL(CHEMISTRY,0)) / 3 AVERAGE FROMGRADES;

    Up to now, we have created many queries but all are lost now

    You may create a view as a table to keep and re-use your

    queries.

    A view is a logical table and it has a query which based on tablesor views.

  • 8/9/2019 Week 11b Views

    3/26

    Why Use Views?

    To restrict data access

    To make complex queries easy

    To provide data independence

    To preserve different views of the same data

    You can present logical subsets or combinations of data by creating views of tables.

    A view contains no data but is like a window through which data from tables can beviewed or changed.

    What is a View ?

  • 8/9/2019 Week 11b Views

    4/26

    There are two classifications for views : Simple and Complex

    The basic difference is related to the DML ( INSERT, UPDATE,DELETE)

    A simple view-Derives data from only one table-Contains no functions or groups of data-Can perform DML operations through the view

    A Complex view-Derives data from many tables

    -Contains functions or groups of data-Does not allow DML operations through the view

    Simple Views and Complex Views

  • 8/9/2019 Week 11b Views

    5/26

    Creating a View

    CREATE [OR REPLACE ] [FORCE| NOFORCE] VIEWview_name

    ASSub-query

    [WITH CHECK OPTION [CONSTRAINT constraint_name] ]

    [WITH READ ONLY [CONSTRAINT constraint_name] ]

    CREATE VIEW salesman

    AS

    SELECT * FROM emp WHERE job=SALESMAN ;

    EXAMPLE

    SYNTAX

    SELECT * FROM salesman ;

  • 8/9/2019 Week 11b Views

    6/26

    1 C i EMP10 h i d il f l i d 10

  • 8/9/2019 Week 11b Views

    7/26

    1- Create a view, EMP10, that contains detail of employees in department 10.

    Describe the structure of the view by using iSQL*Plus DESCRIBE command

    CREATE VIEW emp10

    AS

    SELECT empno,ename,deptno FROM emp

    WHERE deptno=10 ;

    DESCRIBE emp10 ;

    SELECT * FROM emp10 ;

    G id li f ti i

  • 8/9/2019 Week 11b Views

    8/26

    Guidelines for creating a view

    -The subquery that defines a view can contain complex SELECT syntax,

    including joins, groups, and subqueries.

    -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

    WITH CHECK OPTION,

    The system assign a default name in the format SYS_Cn.

    -You can use the OR REPLACE option to change the definition of the view

    without dro in and re-creatin it.

    C t i b i l li i th b

  • 8/9/2019 Week 11b Views

    9/26

    CREATE VIEW sal20ASSELECT empno,

    ename,

    sal*12FROM empWHERE deptno=20 ;

    Create a view by using column aliases in the subquery

    CREATE VIEW sal20ASSELECT empno as NO,

    ename as EMPLOYEE,

    sal*12 as ANNUALFROM empWHERE deptno=20 ;

    View created .

    As an alternative, you can use an alias after CREATE statement and prior to the SELECTsubquery.

    The number of aliases listed match the number of expressions selected in the subquery.

    CREATE VIEW sal20 (NO,EMPLOYEE,ANNUAL)ASSELECT empno, ename, sal*12FROM empWHERE deptno=20 ;

    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 everyemployees in department 20.

    You can control the column name by including column aliases within a subquery.

    R t i i D t f Vi

  • 8/9/2019 Week 11b Views

    10/26

    Retrieving Data from a View

    SELECT * FROM sal20 ;

    M dif i Vi

  • 8/9/2019 Week 11b Views

    11/26

    Modifying a View

    3-Modify the EMP10 view by using

    CREATE OR REPLACE VIEW clause.Change deptno=10 to deptno=30 ;

    SELECT * FROM user_viewsTo Display all views on the current user.

    CREATE VIEW emp10 AS SELECT empno,ename,deptno FROM emp WHERE deptno=30;

    CREATE OR REPLACE VIEW emp10 AS SELECT empno,ename,deptno FROM emp WHERE deptno=30;

    View created.

    SELECT * FROM user_views

    Inserting rows to a View

  • 8/9/2019 Week 11b Views

    12/26

    Inserting rows to a View

    You can insert, update and delete rows to simple views ONLY.

    SELECT * FROM USER_VIEWS

    Sal20 has a function

    Job_salary has a groups of data.

    INSERT INTO emp10 VALUES(7777,NURLAN,30)

    SELECT * FROM emp ; SELECT * FROM emp10;

  • 8/9/2019 Week 11b Views

    13/26

    INSERT INTO emp10 VALUES(8888,ASEL,20)

    SELECT * FROM emp ;SELECT * FROM emp10;

    Why is ASEL not displayed ?

    Deleting rows from a view

  • 8/9/2019 Week 11b Views

    14/26

    SELECT * FROM emp ; SELECT * FROM emp10;

    DELETE FROM emp10 where ename=NURLAN ;

    DELETE FROM emp10 where ename=ASEL ;

    0 row deleted

    1 row deleted

    Deleting rows from a view

    Updating rows in a view

  • 8/9/2019 Week 11b Views

    15/26

    SELECT * FROM emp ; SELECT * FROM emp10;

    UPDATE EMP10 SET EMPNO=1 WHERE ename=NURLAN ;

    UPDATE EMP10 SET EMPNO=2 WHERE ename=ASEL ;

    0 row deleted

    1 row updated

    Updat g o s a e

    Creating a Complex View

  • 8/9/2019 Week 11b Views

    16/26

    Creating a Complex View

    Create a complex view that contains group functions to display values from table.

    CREATE VIEWjob_salary AS SELECTjob,SUM(sal) FROM emp GROUP BY job ;

    CREATE VIEWjob_salaryAS SELECT job,SUM(sal) as SALARYFROM emp GROUP BY job ;

    SELECT * FROMjob_salary

    DESCjob_salary

    DELETING DATA

  • 8/9/2019 Week 11b Views

    17/26

    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

    You can not modify data in a view if it contains :-Group function

    -A GROUP BY clause-The DISTINCT keyword-The ROWNUM keyword-Columns defined by expressions

    MODIFY DATA

    You can not add through a view if the view includes :-Group function-A GROUP BY clause-The DISTINCT keyword-The ROWNUM keyword-Columns defined by expressions

    -NOT NULL columns in the base tables that are not selected by the view.

  • 8/9/2019 Week 11b Views

    18/26

    SELET * FROM CLERKS ;

  • 8/9/2019 Week 11b Views

    19/26

    UPDATE CLERKS SET JOB=SALESMAN

    UPDATE CLERKS SET DEPTNO=60

    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

  • 8/9/2019 Week 11b Views

    20/26

    y g p

    You can ensure that no DML operations occur by adding the WITH READ ONLY option toyour view definition.

    Any attempt to perform a DML on any row in the view results in an Oracle Server error.

    CREATE VIEW EMP_COM_NULL

    AS

    SELECT * FROM emp WHERE comm IS NULL

    WITH READ ONLY ;

    SELECT * FROM EMP_COM_NULL;

    DELETE FROM EMP_COM_NULL;

    UPDATE EMP_COM_NULLSET DEPTNO=100

    INSERT INTO EMP_COM_NULL (EMPNO)VALUES(5555)

    Removing a View

  • 8/9/2019 Week 11b Views

    21/26

    g

    You can remove a view without losing data because a view is based on underlying tablesin the database.

    DROP VIEW emp10 ;

  • 8/9/2019 Week 11b Views

    22/26

  • 8/9/2019 Week 11b Views

    23/26

    TASKS

    1-Create a view called EMPLOYEE_1 based on the employee numbers,employee name and surname and department numbers from the EMPLOYEES table

  • 8/9/2019 Week 11b Views

    24/26

    employee name and surname, and department numbers from theEMPLOYEES table.Change the heading for the firstname+lastname to EMPLOYEE.

    2- Display the contents of the EMPLOYEE_1 view.

    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

  • 8/9/2019 Week 11b Views

    25/26

    4 Using your EMPLOYEE_1 view, write a query to display all employes names and departmentnumber.

    5-Create a view DEPT50 that contains the employee number, employee last names,anddepartment numbers for all employees in department 50 in EMPLOYEES table.

    Label the view columns EMPNO,EMPLOYEE, AND DEPTNO

    Dont allow an employee to be reassigned to another department through the view.

    6- Display the structure and contents of the DEPT50 view

    7 Attempt to reassign Matos to department 80

  • 8/9/2019 Week 11b Views

    26/26

    7- Attempt to reassign Matos to department 80

    8- Create a view called SALARY_1 based on the employee last names, department names,

    salaries, And salary grades for all employees.

    Use the EMPLOYEES, DEPARTMENTS,and JOB_GRADES tables.

    Label the columns Employee,Department, Salary,and the Grade, respectiveley.

    9-Create a view which returns all employees have not got any commission in department 30in EMP. Nobody can delete,or update any rows and insert any rows to that view.