Anda di halaman 1dari 8

VIEWS

A view is like a virtual table. It takes the output of a query and treats it like a table. We can
create, update, and drop a VIEW.

 A view can be based on one or more tables or other views. These tables/views are called
base tables.

 A view contains no data. All the data it shows comes from the base tables.

 A view can provide an additional level of table security by restricting access to a set of
rows or columns of a table.

 A view lets you change the data you can access, applying operators, aggregation
functions, filters etc. on the base table.

 A view can be replaced with a CREATE OR REPLACE VIEW statement. The


REPLACE option updates the current view definition but preserves the present security
authorizations.

Example relation: Student


Create VIEW [To create a view]

Syntax

CREATE VIEW view_name AS

SELECT column_name

FROM table_name

[WHERE conditions];

EXPLANATION:

view_name
The name of the Oracle VIEW that you wish to create.

WHERE conditions
This is Optional. The conditions that must be met for the records to be included in the
VIEW.

Example

CREATE VIEW student_view AS

SELECT s_name

FROM student

WHERE s_name LIKE 'J%';

Output:
This Oracle CREATE VIEW example would create a virtual table based on the result set of the
SELECT statement. You can now query the Oracle VIEW as follows

SELECT *

FROM student_view;

CREATE VIEW WITHOUT USING WHERE CONDITION:

Syntax:

CREATE VIEW view_name AS

SELECT *

FROM table_name;

Example:

CREATE VIEW student_view AS

SELECT *

FROM student;

Output: view created


To display view table:

Note: We can do all DML commands like INSERT, UPDATE and DELETE in a view table,
All the changes that is made in a view table will affect the base table.

To insert a row in view table:

Query:

INSERT INTO student_view

VALUES (6,'GEETHA','ETE','24-JAN-1991','THIRUNELVELI','D103');

OUTPUT:
TO DISPLAY THE DATA IN VIEW TABLE: VIEW TABLE IS ‘ student_view’

TO CHECK THE DATA IN BASE TABLE: BASE TABLE IS ‘STUDENT’

To delete a record from view table:

Query:

DELETE FROM student_view

WHERE s_id =4;


Output:

TO DISPLAY THE DATA IN VIEW TABLE: VIEW TABLE IS ‘ student_view’

Record of s_id ‘4’ is deleted.

TO CHECK THE DATA IN BASE TABLE: BASE TABLE IS ‘STUDENT’

Record of s_id ‘4’ is deleted from base table.


UPDATE VIEW

You can modify the definition of an Oracle VIEW without dropping it by using the Oracle
CREATE OR REPLACE VIEW Statement.

Syntax

CREATE OR REPLACE VIEW view_name AS

SELECT columns

FROM table

WHERE conditions;

Example:

CREATE OR REPLACE VIEW student_view

AS SELECT *

FROM student

WHERE s_address='COIMBATORE';

Output:
DROP VIEW

The DROP VIEW statement is used to remove or delete the VIEW completely.

Syntax:

DROP VIEW view_name;

Example:

DROP VIEW student_view;

Note: After dropping the view table data inside the base table remains same.

Anda mungkin juga menyukai