DBMS - Lab Practical Assignment #8
DBMS - Lab Practical Assignment #8
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.
College Student
Column Column
Data type Size Data type Size
Name Name
cName varchar2 10 sID int
cstate varchar2 10 sName varchar2 10
enrollment int CGPA number 2,1
marks int
DoB date
Apply
Column
Data type Size
Name
sID int
cName varchar2 10
major varchar2 20
decision char 1
Insert the following data to these tables:
Student Apply
sID sName CGPA Marks sID cName major decision
123 Amit 3.9 1000 123 Stanford CS Y
234 Balbir 3.6 1500 123 Stanford EE N
345 chirag 3.5 500 123 Berkeley CS Y
456 Dev 3.9 1000 123 Cornell EE Y
567 Eshan 2.9 2000 234 Berkeley biology N
678 Faizal 3.8 200 345 MIT bioengineering Y
789 Garvit 3.4 800 345 Cornell bioengineering N
987 Himani 3.7 800 345 Cornell CS Y
Harvard MA 50040
Q1. Create view Weak Student on Student whose CGPA 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. Display information about student in CS berk having CGPA greater than 3.8.
Q5. Drop view CS accept.
Q6. Display all student in CS berkeley.
Q7. Update CGPA by 0.8 of students in view Weak Student who having high school size greater than
1000
Q8. Update No Of App so that sID 234 contains 4 applications.
Q9. Create a view StuName contain student name and their CGPA. Is this View Updatable? If not
specify why.
Q10. Create view student marks having details of student comes from High School of marks more than
1000 using with check option.