0% found this document useful (0 votes)
4 views6 pages

DBMS - Lab Practical Assignment #8

The JSON contains SQL queries to create views on various tables based on conditions and perform operations like update, delete on them. It also contains reasons for non-updateable views. Views are created on tables like Student, College, Apply to filter data based on columns and join multiple tables.

Uploaded by

thenuaguddu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
4 views6 pages

DBMS - Lab Practical Assignment #8

The JSON contains SQL queries to create views on various tables based on conditions and perform operations like update, delete on them. It also contains reasons for non-updateable views. Views are created on tables like Student, College, Apply to filter data based on columns and join multiple tables.

Uploaded by

thenuaguddu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 6

Experiment No.

Environment: Microsoft Windows


Tools/ Language: Oracle

Objective: To implement the concept of views.

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;

Renaming the columns of a view:-

Syntax:-
CREATE OR REPLACE VIEW viewname (newcolumnname, … ) AS
SELECT columnname….
FROM tablename
WHERE columnname=expression_list;

Selecting a data set from a view-

Syntax:-
SELECT columnname, columnname
FROM viewname
WHERE search condition;

Destroying a view-

Syntax:- DROP VIEW viewname;


Classification of Views
Views are of two types based on their behavior during data manipulation

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

Non Updatable Views


Non Updatable View is the one in which no one can INSERT, UPDATE or DELETE
i.e, no changes can be applicable.

1. It is created from more than one table i.e. using JOINS


2. Having aggregate function or GROUP BY, DISTINCT or HAVING be used it view
Definition. (Even if view is of single table and have any of these clause then still it is non-
updatable)
3. Not include all NOT NULL columns or PRIMARY KEYS.

Run the following script:

drop table Apply;


drop table Student;
drop table College;
create table Student(sID int primary key, sName varchar2(10), GPA
real, sizeHS int, DoB date);
insert into Student values (123, 'Amy', 3.9, 1000, '26-JUN-96');
insert into Student values (234, 'Bob', 3.6, 1500, '7-Apr-95');
insert into Student values (345, 'Craig', 3.5, 500, '4-Feb-95');
insert into Student values (456, 'Doris', 3.9, 1000, '24-Jul-97');
insert into Student values (567, 'Edward', 2.9, 2000, '21-Dec-96');
insert into Student values (678, 'Fay', 3.8, 200, '27-Aug-96');
insert into Student values (789, 'Gary', 3.4, 800, '8-Oct-96');
insert into Student values (987, 'Helen', 3.7, 800, '27-Mar-97');
insert into Student values (876, 'Irene', 3.9, 400, '7-Mar-96');
insert into Student values (765, 'Jay', 2.9, 1500, '8-Aug-98');
insert into Student values (654, 'Amy', 3.9, 1000, '26-May-96');
insert into Student values (543, 'Craig', 3.4, 2000, '27-Aug-05');
commit;
EXAMPLES
Updatable View

Creating View named as STU_VIEW on Student Table:

create or replace view STU_VIEW as


select GPA, sNAme, DoB from Student;

Display Records from View


Select * from STU_VIEW;

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

Select * from STU_VIEW; Select * from Student;

Non-Updatable View
Create Non Updatable View:

create or replace view sView as select sName, GPA from Student;

Display records:
select * from sView;

Add record to sView:


Insert into sView values ('Shikhar', 4.0);

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.

View with check option


We have created a view bornAUG on Student Table with check option.
create or Replace view bornAUG
as Select * from Student where EXTRACT(month from DoB)=8
with check option;

Check the view using:


Select * from bornAUG;
Now, we try to insert an row:
insert into bornAUG values (546,'Ram',3.1,900,'15-Aug-1998');
Now , Again check both view bornAUG and Student have entry for Ram or not (you will
find him in both):

Select * from bornAUG;

Select * from Student;

Now, try to insert a row for Raj who born in December:

insert into bornAUG values (547,'Raj',3.2,1500,'23-Dec-1998');

SQL Error: ORA-01402: view WITH CHECK OPTION where-clause


violation
01402. 00000 - "view WITH CHECK OPTION where-clause violation"
Practical Assignment - 8

Department: Computer Engineering & Applications


Course: BCA
Subject: Database Management System Lab (BCAC0816)
Year: 1ST Semester: 2ND

Create these tables which consist of following attributes

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

876 Ishan 3.9 400 345 Cornell EE N

765 Jay 2.9 1500 678 Stanford history Y


987 Stanford CS Y
654 Amit 3.9 1000
987 Berkeley CS Y
543 chirag 3.4 2000
876 Stanford CS N
College 876 MIT biology Y
cName state enrollment 876 MIT marine biology N
Stanford CA 15000 765 Stanford history Y
Berkeley CA 36000 765 Cornell history N
MIT MA 10000 765 Cornell psychology Y
Cornell NY 21000 543 MIT CS N

Harvard MA 50040

State the SQL Queries for each of the following:


Create the following views and run the update and delete commands as specified. State
reasons in case the view is non-updateable.

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.

You might also like