You are on page 1of 7

VIEWS

 A view is a virtual table based on the result-set of an SQL statement.


 A view contains rows and columns, just like a real table. The fields in a
view are fields from one or more real tables in the database.
 You can add SQL functions, WHERE, and JOIN statements to a view and
present the data as if the data were coming from one single table.
 Syntax: CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
 NOTE:
 creating view for a single table is called simple view
 Creating view for two or more tables is called complex
view.

Using Views to Update Data

 A view can be used in a query that updates data, subject to a few


restrictions. Bear in mind that a view is not a table and contains
no data—the actual modification always takes place at the table
level.
 Views cannot be used as a mechanism to override any
constraints, rules, or referential integrity defined in the base
tables.

Restrictions on Updating Data Through Views

 If the view contains joins between multiple tables, you can only
insert and update one table in the view, and you can't delete
rows.
 You can't directly modify data in views based on union queries.
You can't modify data in views that use GROUP
BY or DISTINCT statements.
 All columns being modified are subject to the same restrictions as
if the statements were being executed directly against the base
table.

 Text and image columns can't be modified through views.

 There is no checking of view criteria. For example, if the view


selects all customers who live in Paris, and data is modified to
either add or edit a row that does not have City = 'Paris', the data
will be modified in the base table but not shown in the view,
unless WITH CHECK OPTION is used when defining the view.

EXAMPLE: CREATE VIEW vwCategoriesProducts

AS

SELECT Categories.CategoryName,

Products.ProductID, Products.ProductName

FROM Products INNER JOIN

Categories ON
Products.CategoryID = Categories.CategoryID

 The following UPDATE statement will work because it's only


affecting the Products table's side of the join:
UPDATE vwCategoriesProducts
SET ProductName = 'Chay'
WHERE ProductID = 1

 This UPDATE statement will also work because only affects the
Categories table's side of the join:

UPDATE vwCategoriesProducts

SET CategoryName = 'Drinks'

WHERE ProductID = 1

 However, the following UPDATE statement attempting to modify


columns in both the Products and Categories tables won't work
(you'll get the error "View or function 'vwCategoriesProducts' is
not updateable because the FROM clause names multiple tables.

UPDATE vwCategoriesProducts
SET ProductName = 'Chay', CategoryName = 'Drinks'
WHERE ProductID = 1
Updating a view changes data in master table

 select * from a;

 create or replace view a1view as select


emp_id,emp_name from a;

 update a1view
set emp_name = 'THOMAS'
where emp_id = 1;
 select * from a;

VIEWS WITH CHECK OPTION

 To ensure the consistency of the view, you use the WITH CHECK
OPTION clause when you create or modify the view.
 The WITH CHECK OPTION clause prevents you from updating or
inserting rows that are not visible through the view.

Example:
 CREATE OR REPLACE VIEW a_det

AS

SELECT emp_name, salary , manager_id FROM a WHERE


manager_id =3

WITH CHECK OPTION;

select * from a_det;


 update a_det
set emp_name = 'ORACLE'
where manager_id = 2;

 update a_det
set emp_name = 'ORACLE'
where manager_id = 3;

FORCE VIEW

 A view can be created even if the defining query of the view


cannot be executed. We call such a view as view with errors.
 For example, if a view refers to a non-existent table or an invalid
column of an existing table or if the owner of the view does not
have the required privileges, then the view can still be created
and entered into the data dictionary.
 We can create such views (i.e. view with errors) by using the
FORCE option in the CREATE VIEW command.
 We create force view only when we create a view using a table
but the table is not created and at that time we use force view.
 The advantage of force view is that in future if view script
becomes valid, we can start using the view.
 EAXMPLE:
create force view footba
as select * from football;

You might also like