Dbms Unit4 Views
Dbms Unit4 Views
DBMS(7BIT5C1)
VIEWS
Ms.R.Kalaivani
Dept. of I.T
Views
• A view is a virtual or logical table.
• It allows the viewing or
manipulating of the contents of one
or more tables in a window.
• A view looks like and works
similarly to a normal table.
• Its contents can be drawn from
several different tables and from
other views also.
• The tables containing the origin
columns are called base tables.
• A view reflects the current contents
of the base table.
• It is different from a snapshot, which
displays only past data.
Views
• A view has no physical space allocated to its data.
• Created by a query that uses origin or base tables(tables/views) from which to extract
data.
• The definition of a view is stored in the data dictionary, which stores the query that
generated the table.
How a View Works
• When a view is referenced by a SQL command, Oracle combines this command with
that of the view’s definition, and returns the data.
• Oracle controls the dependencies of a view.
• When a table is deleted or changed by the view, Oracle determines when a new table
will be available for use in the view’s definition.
Advantages of Using a View
• They can restrict the viewing of a table’s contents by limiting the columns that are
displayed and the rows that are filtered.
• The same view can be used by different users and display distinct information,
depending on the way it was designed.
• A table for general use can be divided into specific views for certain users.
For example, a table with employee data
can display - name and phone number - general users
can include the salary -users having the proper permission(HR Dept)
• The execution of the SELECT command is simplified, which otherwise would
involve a great number of fields in many tables, and would require the specification
of a schema in every column.
VIEWS
• A view can be referenced in the following SQL commands:
COMMENT DELETE INSERT LOCK TABLE UPDATE SELECT
• Restrictions on Using a View
• Insert, update, and delete operations cannot be performed in a view formed by
columns from more than one table when there is a grouping function, a GROUP BY
clause, or restriction operators.
• Views cannot mention the nextval or currval pseudocolumns.
• A row cannot be inserted in a view that has a base table containing a column with
the NOT NULL restriction, and that has a default value specified.
• When the view selects the rowid, rownum, or level pseudo-columns, their alias must
be specified in the query.
• Properties and Privileges of a View
• To see the SQL command responsible for the view’s design,
▪ open the view’s property window by clicking with the right mouse button on the
view’s name and selecting the Properties option.
CREATE VIEW Command
• The command responsible for creating and modifying views is CREATE VIEW.
• Syntax:
CREATE [OR REPLACE] VIEW name AS
SELECT field(s) FROM table(s)
FORCE/NOFORCE
WHERE condition
WITH CHECK OPTION CONSTRAINT control
WITH READ ONLY
• OR REPLACE
Re-creates an existing view. It functions as an alternative to ALTER VIEW and is
used to change an existing view without having to drop and grant privileges.
• FORCE
Allows the creation of a view even when the base table does not exist, and the user
doesn’t have the privileges to create it.
CREATE VIEW Command
• NOFORCE
Allows the creation of the view only when the user has the permissions and the
specified tables exist. The default is NOFORCE.
• WITH READ ONLY
Specifies that the INSERT, UPDATE, or DELETE commands cannot be executed in
the view.
• WITH CHECK OPTION
Allows the inclusion and update only of the rows that the view can select.
• CONSTRAINT
▪ The name attributed to the restriction of the CHECK OPTION clause.
▪ If the view has been created with GROUP BY, CONNECT BY, or START
WITH, or by the DISTINCT operator or group functions, the INSERT,
UPDATE, and DELETE commands cannot be executed in the view.
A View Based on a Table
• SQL> CREATE VIEW NEWDEP AS SELECT * FROM DEPARTMENT;
View created.
• SQL> SELECT * FROM NEWDEP;
DEPARTMENT_ID NAME LOCATION_ID
------------- ---------- -----------
10 ACCOUNTING 122
20 RESEARCH 124
30 SALES 123
40 OPERATIONS 167
12 RESEARCH 122
13 SALES 122
14 OPERATIONS 122
23 SALES 124
24 OPERATIONS 124
34 OPERATIONS 123
43 SALES 167
A View Based on a Table
• SQL> create view empbasic as select employee_id, last_name, first_name, salary from
employee;
• SQL> select * from empbasic where rownum <5;
EMPLOYEE_ID LAST_NAME FIRST_NAME SALARY
----------- --------- ---------- ------
7369 SMITH JOHN 800
7499 ALLEN KEVIN 1600
7505 DOYLE JEAN 2850
7506 DENNIS LYNN 2750
A View Based on Two Tables (Join View)
• SQL> select * from location;
LOCATION_ID REGIONAL_GROUP
----------- --------------
122 NEW YORK
124 DALLAS
123 CHICAGO
167 BOSTON
• We can create an alias for reducing the size of the command - use the desired
alias- one letter to minimize typing, and specify this alias in the FROM clause
after the name of the department.
A View Based on Two Tables (Join View)
• Example:
• SQL> create view funcdep as select e.employee_id, e.last_name, e.first_name,
e.salary, d.department_id, d.name from employee e, department d where
e.department_id=d.department_id;
• Here’s a partial list of the result:
• SQL> select * from funcdep order by name;
EMPLOYEE_ID LAST_NAME FIRST_NAME SALARY DEPARTMENT_ID NAME
----------- --------- ---------- ------ ------------- ----------
7782 CLARKY CAROL 2450 10 ACCOUNTING
7839 KING FRANCIS 5000 10 ACCOUNTING
7934 MILLER BARBARA 1300 10 ACCOUNTING
7507 BAKER LESLIE 2200 14 OPERATIONS
7609 LEWIS RICHARD 1800 24 OPERATIONS
7676 SOMMERS DENISE 1850 34 OPERATIONS
Using the SELECT Command in a View
GENDEP
select department.name, department.location_id,location.regional_group from dep
NEWDEP
SELECT “DEPARTMENT_ID”,"NAME","LOCATION_ID" FROM DEPARTMENT
SALES
SELECT SALESPERSON_ID, SALES_ORDER.CUSTOMER_ID,CUSTOMER.NAME
CUSTOMER,P
Materialized Views
• A materialized view is a physical copy of the base table with the results moved to
another schema object.
• Materialized views are also called snapshots, because they are a kind of
photograph of the base table.
Designing Multiple Tables and Views Simultaneously
• Can create tables and views at the same time with the CREATE SCHEMA
command.
• Can specify the definitions of all the tables and views, along with the access
privileges of each of them.
• A schema is created by the CREATE USER command, which creates some of
the schema objects at the same time.
Create Schema Command
• Syntax:
CREATE SCHEMA AUTHORIZATION name_of_schema
create_table_statement create_view_statement grant_statement
• Arguments:
name_of_schema
This must be identical to the user name created by the CREATE USER
command.
create_table_statement
A CREATE TABLE command that is part of the schema. Several commands
can be specified. You cannot use the final semicolon.
create_view_statement
A CREATE VIEW command that is part of the schema. Several commands can
be specified. You cannot use the final semicolon.
Create Schema Command
grant_statement
An optional GRANT command used to attribute privileges.Cannot be returned
with semicolon.
Example:
CREATE TABLE Dept_tab ( Deptno NUMBER(3) PRIMARY KEY, Dname VARCHAR2(15),
Loc VARCHAR2(25))
CREATE TABLE Emp_tab (Empno NUMBER(5) PRIMARY KEY, Ename VARCHAR2(15) NOT
NULL, Job VARCHAR2(10), Mgr NUMBER(5),Hiredate DATE DEFAULT (sysdate), Sal
NUMBER(7,2), Comm NUMBER(7,2),Deptno NUMBER(3) NOT NULL CONSTRAINT
Dept_fkey REFERENCES Dept_tab(Deptno)) GRANT SELECT ON Sales_staff TO
human_resources;