Powered By Blogger

Saturday 24 March 2012

Views


    VIEWS


A view is a database object that is a logical representation of a table. It is delivered from a table but has no storage of its own and often may be used in the same manner as a table.
A view takes the output of the query and treats it as a table, therefore a view can be thought of as a stored query or a virtual table.

TYPES
Ø  Simple view
Ø  Complex view

Simple view can be created from one table where as complex view can be created from multiple tables.

WHY VIEWS?

Ø  Provides additional level of security by restricting access to a predetermined set of rows and/or columns of a table.
Ø  Hide the data complexity.
Ø  Simplify commands for the user.

VIEWS WITHOUT DML

Ø  Read only view
Ø  View with group by
Ø  View with aggregate functions
Ø  View with rownum
Ø  Partition view
Ø  View with distinct
Ex:
      SQL> Create view dept_v as select *from dept with read only;
      SQL> Create view dept_v as select deptno, sum(sal) t_sal from emp group by deptno;
      SQL> Create view stud as select rownum no, name, marks from student;
      SQL> Create view student as select *from student1 union select *from student2;
      SQL> Create view stud as select distinct no,name from student;
VIEWS WITH DML

Ø  View with not null column  --  insert with out not null column not possible
                                                        --  update not null column to null is not possible
                                                             --  delete possible
Ø  View with out not null column which was in base table -- insert not possible
                                                                                                     -- update, delete possible
Ø  View with expression -- insert , update not possible
                                               -- delete possible
Ø  View with  functions (except aggregate) -- insert, update not possible
                                                                              -- delete possible
Ø  View was created but the underlying table was dropped then we will get the message like “ view has errors ”.
Ø  View was created but the base table has been altered but still the view was with the               initial definition, we have to replace the view to affect the changes.
Ø  Complex view (view with more than one table) -- insert not possibl
Ø   -- update, delete possible (not always)

CREATING VIEW WITHOUT HAVING THE BASE TABLE

SQL> Create force view stud as select *From student;
-- Once the base table was created then the view is validated.

VIEW WITH CHECK OPTION CONSTRAINT

SQL> Create view stud as select *from student where marks = 500 with check option constraint
         Ck;
       - Insert possible with marks value as 500
       - Update possible excluding marks column
       - Delete possible

DROPPING VIEWS

SQL> drop view dept_v;

ROLLUP Grouping Cube


                                             ROLLUP GROUPING CUBE


These are the enhancements to the group by feature.

USING ROLLUP

This will give the salaries in each department in each job category along wih the total salary   
for individual departments and the total salary of all the departments.

SQL> Select deptno,job,sum(sal) from emp group by rollup(deptno,job);

                        DEPTNO   JOB         SUM(SAL)
----------  ---------   ----------
        10    CLERK           1300
        10    MANAGER     2450
        10    PRESIDENT   5000
        10                          8750
        20    ANALYST       6000
        20    CLERK           1900
        20    MANAGER     2975
        20                        10875
        30    CLERK             950
        30    MANAGER      2850
        30    SALESMAN     5600
        30                          9400
                          29025

USING GROUPING

 In the above query it will give the total salary of the individual departments but with a  
 blank in the job column and gives the total salary of all the departments with blanks in
 deptno and job columns.
 
 To replace these blanks with your desired string grouping will be used
  SQL> select decode(grouping(deptno),1,'All Depts',deptno),decode(grouping(job),1,'All             
          jobs',job),sum(sal) from emp group by rollup(deptno,job);

DECODE(GROUPING(DEPTNO),1,'ALLDEPTS',DEP DECODE(GR   SUM(SAL)
-----------------------------------  ----------------------------------    --------------
10                                                        CLERK             1300
10                                                         MANAGER                 2450
10                                                        PRESIDENT               5000
10                                                        All jobs                       8750
20                                                        ANALYST                   6000
20                                                        CLERK             1900
20                                                        MANAGER                  2975
20                                                        All jobs                    10875
30                                                        CLERK              950
30                                                        MANAGER                  2850
30                                                        SALESMAN                 5600
30                                                        All jobs                       9400
All Depts                                                        All jobs                    29025

   Grouping will return 1 if the column which is specified in the grouping function has been
   used in rollup.
   Grouping will be used in association with decode.

USING CUBE

This will give the salaries in each department in each job category, the total salary for individual departments, the total salary of all the departments and the salaries in each job category.

SQL> select decode(grouping(deptno),1,’All Depts’,deptno),decode(grouping(job),1,’All 
        Jobs’,job),sum(sal) from emp group by cube(deptno,job);

DECODE(GROUPING(DEPTNO),1,'ALLDEPTS',DEP DECODE(GR   SUM(SAL)
-----------------------------------  ------------------------------------  ------------
10                                            CLERK                         1300
10                                            MANAGER                              2450
10                                            PRESIDENT                           5000
10                                            All Jobs                                  8750
20                                            ANALYST                               6000
20                                            CLERK                         1900
20                                            MANAGER                              2975
20                                            All Jobs                               10875
30                                            CLERK                            950
30                                            MANAGER                               2850
30                                            SALESMAN                             5600
30                                            All Jobs                                  9400
All Depts                                            ANALYST                               6000
All Depts                                            CLERK                         4150
All Depts                                            MANAGER                              8275
All Depts                                            PRESIDENT                           5000
All Depts                                            SALESMAN                             5600
All Depts                                            All Jobs                               29025