Professional Documents
Culture Documents
Database 8
Database 8
Database 8
4 Creating a View
The CREATE VIEW statement is used to create a view.
Syntax:
CREATE VIEW viewname
• Assign a name to each column in the view. If a list of column names is specified, it must have the
same number of items as the number of columns in the query.
• Only the column names are specified; we should not give the other characteristics of columns.
• If the list of column names is omitted from the CREATE VIEW statement, each column in the
view takes the name of the corresponding column in the query.
• The list of column names must be specified if the query includes calculated columns or if it
produces two columns with identical names.
Horizontal View:
Example:
CREATE VIEW PASSED AS
SELECT *
FROM STUDENTS
WHERE RESULT = ‘P’
A horizontal view will restrict the user’s access to only a few rows of the table. A “horizontal view” will permit
a user to see only the records of those who have passed. A horizontal view slices the table horizontally to create
the view. All columns are included in the view but only some of the rows are displayed.
Prof. Mukesh N. Tekwani [9869 488 356] Page 1
SQL - Ch 13 – SQL VIEWS
Example 2:
Define a view for Sue (employee number 102) containing only orders placed by customers assigned
to her.
Vertical View:
A vertical view restricts a user’s access to only certain columns of a table.
Example 1:
CREATE VIEW STUD_ADDRESS AS
SELECT ROLLNO, NAME, ADD1, ADD2, CITY
FROM STUDENTS
Example:
CREATE VIEW STUD_PASSED AS
SELECT ROLLNO, NAME, PCTG
FROM STUDENTS
WHERE RESULT = ‘P’
Grouped View:
A grouped view is one in which a query includes the GROUP BY clause. Related rows of data are grouped
together and produce one row of result for each group.
Define a view that contains summary order data for each salesperson.
CREATE VIEW ORD_BY_REP (WHO, HOW_MANY, TOTAL, LOW, HIGH, AVERAGE)
AS
SELECT REP, COUNT(*), SUM(AMOUNT), MIN(AMOUNT), MAX(AMOUNT), AVG(AMOUNT)
FROM ORDERS
GROUP BY REP
c) Create a view to find all people who live in a state where a movie studio is located.
CREATE VIEW PS
AS
SELECT FName, LName, StudioName, Person_State
FROM People, Studio
WHERE Person_State = Studio_State
Page 2 mukeshtekwani@hotmail.com
SQL – Ch 13 – SQL VIEWS
6 Updating a View
Records can be updated, inserted, and deleted though views. Views against which INSERT,
DELETE, and UPDATE statements can be used are called as updateable views.
For example, consider the EASTREPS view contains which only few rows of the SALESREPS table:
The DBMS will add the new row to the underlying SALESREPS table, and the row will be visible
through the EASTREPS view. The row we are trying to insert satisfies the condition that the
REP_OFFICE should be 11, 12, or 13.
Here the REP_OFFICE is 21. The row in inserted into the underlying table, but it is not displayed by
the view.
This can cause problems with data integrity. To avoid such problems (table updated but record not
displayed by view), SQL provides the CHECK OPTION as follows:
Now, if we try to use an update view, SQL will automatically check each UPDATE and INSERT query
to make sure that the resultant query meets the condition “REP_OFFICE IN (11, 12, 13)”.
If the new view VC is created WITH CASCADED CHECK OPTION, and we attempt to update the
view, it causes the DBMS go to view VB and then VA to check option for each view.
If the new view is created WITH LOCAL CHECK OPTION, then the DBMS checks only that view; the
underlying views are not checked. So if we define view VC with LOCAL CHECK OPTION, then only
conditions specified in that view are checked.
E.g., consider that we have view VA and VB. View VB depends on VA.
DROP VIEW VA
If we drop VA, then cascading effect takes place and view VB is also dropped. Thus the default
option for dropping a view is CASCADE. The CASCADE option tells the DBMS to delete not only the
named view, but also any views that depend on its definition.
Now the query fails because RESTRICT option tells the DBMS to remove the view only if no other
views depend on it. Since VB depends on VA, it will cause an error.
Page 4 mukeshtekwani@hotmail.com