(CSE3083) Lab Practical Assignment #7 (Updated)
(CSE3083) Lab Practical Assignment #7 (Updated)
Theory:
Views:-
Logical data is how we want to see the current data in our database. Physical data is how this
data is actually placed in our database.
Views are masks placed upon tables. This allows the programmer to develop a method via
which we can display predetermined data to users according to our desire.
Views may be created for the following reasons:
1. The DBA stores the views as a definition only. Hence there is no duplication of data.
2. Simplifies Questionnaires.
3. Can be queried as a base table itself.
4. Provides data security.
5. Avoids data redundancy.
Creation of Views:-
Syntax:-
CREATE OR REPLACE VIEW viewname AS
SELECT columnname,columnname
FROM tablename
WHERE columnname=expression_list;
Syntax:-
CREATE OR REPLACE VIEW viewname (newcolumnname, … ) AS
SELECT columnname….
FROM tablename
WHERE columnname=expression_list;
Syntax:-
SELECT columnname, columnname
FROM viewname
WHERE search condition;
Destroying a view-
Updatable Views
Updatable here signifies that one can insert, update and delete rows and data from view.
(Actually all DML manipulations are reflected to base table)
1. It is created from single table
2. The view must include the PRIMARY KEY and NOT NULL columns of the table based
upon which the view has been created.
3. No Aggregate function such as SUM, Count, AVG be used.
4. The view must not have any DISTINCT, GROUP BY or HAVING clause in it's
definition.
5. Any of the selected output fields (of the view) must not use constants, strings or value
expressions.
6. If the view you want to update is based upon another view, the later should be updatable.
7. The view must not have any SUBQUERIES in it's definitions
Now, try to update the view (check DoB of sID 765 before running the query )
Update STU_VIEW
Set DoB=Add_months(DoB,4)
Where sName='Jay';
Check both base table as well as view to see it changes are reflecting or not
Non-Updatable View
Create Non Updatable View:
Display records:
select * from sView;
Error Generated:
SQL Error: ORA-01400: cannot insert NULL into ("SYSTEM"."STUDENT"."SID")
This error is generated because view does not include Primary Key of Student Table.
Q1. Create view WeakStudent on Student whose GPA is less than 3.7.
Q2. Create a view cView on college (containing all the columns) and rename the
column cName as collegeName and enrollment as seats respectively.
Q3. Create view CSaccept having IDs and college of student who applied to CS major
and their application is accepted.
Q4. Create view CSberkeley having IDs, name, GPA, sizeHS of those student who are
accepted in CS at Berkeley and comes from High School that is greater than 500.
(Instruction: You are required to use view CSaccept in this query with Student
table for joining)
Q5. Display information about student in CSberk having GPA greater than 3.8.
Q8. Update GPA by 0.8 of students in view WeakStudent who having high school size
greater than 1000.
Q9. Create a view AppCount which contains sID of Student and number of
applications they filed. Name the column sID and NoOfApp.
Q11. Create a view StuName contain student name and their GPA. Is this View
Updatable? If not specify why.
Q12. Create view studentHS having details of student comes from High School of size
more than 1000 using with check option.
Q13. Try insert detail of a new student Ram with sID 999 having GPA 9.9 and sizeHS
9999 to newly created view studentHS. (Comment is this view updatable?)
Q14. Clerk realize he wrongly type sizeHS of Ramu as 9999 it is actually 999. Can you
help him to update the value of sizeHS of Ramu.
Q15. Now, another boy registered to our system named Ramu with sID 998 having GPA
9.8 and sizeHS 989.
Pre-Experiment Questions:
1. What is the need of creating a view?
2. What are advantages that a view offers?