You are on page 1of 5

Bahria University, Lahore Campus

Department of Computer Sciences


Lab Manual 10
(Fall 2022)

Course: Database Management System Lab


Course Code: CSL 220 Max Marks: 40
Faculty’s Name: Lab Engineer: Shoaib Khan

Name: Umer Arif Enroll No: 03-134211-029

Objectives:
The objective of this lab session is to know what is View, how to create,
update, delete the view.
By the end of this lab students will be able
 To understand what is a view
 To understand how a view is created/update/deleted.

Lab 10 VIEWS

OBJECTIVE:

Views Helps to encapsulate complex query and make it reusable.


Provides user security on each view - it depends on your data policy security.
Using view to convert units - if you have a financial data in US currency, you can create view
to convert them into Euro for viewing in Euro currency.
A view is nothing more than a SQL statement that is stored in the database with an associated
name. A view is actually a composition of a table in the form of a predefined SQL query.
A view can contain all rows of a table or selected rows from a table. A view can be created
from one or many tables which depends on the written SQL query to create a view.
Views, which are kind of virtual tables, allow users to do the following:
Structure data in a way that users or classes of users find natural or intuitive.
Restrict access to the data such that a user can see and (sometimes) modify exactly what they
need and no more.
ALGORITHM:
STEP 1: Start the DMBS.
STEP 2: Connect to the existing database (DB)
STEP 3: Create the table with its essential attributes.
STEP 4: Insert record values into the table.
STEP 5: Create the view from the above created table.
STEP 6: Display the data presented on the VIEW.
STEP 7: Insert the records into the VIEW,
STEP 8: Check the database object table and view the inserted values presented
STEP 9: Execute different Commands and extract information from the View.
STEP 10: Stop the DBMS.
%
Enrollment Number: ____________________________

COMMANDS EXECUTION
CREATION OF TABLE:
Database views are created using the CREATE VIEW statement. Views can be created from
a single table, multiple tables, or another view. To create a view, a user must have the
appropriate system privilege according to the specific implementation.

create database viewpractice (table)

CREATE TABLE EMP_VIEW(


EMP_ID INT NOT NULL,
EMP_NAME VARCHAR (20) NOT NULL,
DEPT_ID INT NOT NULL,
DEPT_NAME CHAR (25) ,
DATE_OF_JOIN DATE
);

Create view

CREATE VIEW EMP_NAME


AS
SELECT EMP_ID, EMP_NAME
FROM EMP_VIEW;

SELECT * FROM EMP_NAME;

CREATE VIEW EMP_DEPT_NOT_NULL

AS
SELECT EMP_ID, DEPT_ID
FROM EMP_DEPT
WHERE EMP_ID IS NOT NULL
WITH CHECK OPTION;

UPDATE EMP_NAME
SET EMP_ID = 35
WHERE EMP_NAME ='Ramesh';

DELETE FROM EMP_NAME


WHERE EMP_NAME = 'SAADIA';

DROP VIEW view_name;

Consider the following schema

Page 2 of 5
%
Enrollment Number: ____________________________

Sample table: salesman


salesman_id | name | city | commission
-------------+------------+----------+------------
5001 | James Hoog | New York | 0.15
5002 | Nail Knite | Paris | 0.13
5005 | Pit Alex | London | 0.11
5006 | Mc Lyon | Paris | 0.14
5007 | Paul Adam | Rome | 0.13
5003 | Lauson Hen | San Jose | 0.12

Sample table: customer


customer_id | cust_name | city | grade | salesman_id
-------------+----------------+------------+-------+-------------
3002 | Nick Rimando | New York | 100 | 5001
3007 | Brad Davis | New York | 200 | 5001
3005 | Graham Zusi | California | 200 | 5002
3008 | Julian Green | London | 300 | 5002
3004 | Fabian Johnson | Paris | 300 | 5006
3009 | Geoff Cameron | Berlin | 100 | 5003
3003 | Jozy Altidor | Moscow | 200 | 5007
3001 | Brad Guzan | London | | 5005

Sample table : orders


ord_no purch_amt ord_date customer_id salesman_id
---------- ---------- ---------- ----------- -----------
70001 150.5 2012-10-05 3005 5002
70009 270.65 2012-09-10 3001 5005
70002 65.26 2012-10-05 3002 5001
70004 110.5 2012-08-17 3009 5003
70007 948.5 2012-09-10 3005 5002
70005 2400.6 2012-07-27 3007 5001
70008 5760 2012-09-10 3002 5001
70010 1983.43 2012-10-10 3004 5006
70003 2480.4 2012-10-10 3009 5003
70012 250.45 2012-06-27 3008 5002
70011 75.29 2012-08-17 3003 5007
70013 3045.6 2012-04-25 3002 5001

Task 1

Page 3 of 5
%
Enrollment Number: ____________________________
 Write a query to create a view for those salesmen belongs to the city New York. 

create view ny_salesman


as
select *
from salesman
where city='New York';

 Write a query to create a view for all salesmen with columns salesman_id, name, and
city. 

create view snc_view


as
select salesman_id,name,city
from salesman;

Write a query to find the salesmen of the city New York who achieved the commission more
than 13%.

create view ny_salesmen_comission


as
select *
from salesman
where city='New York'
and
comission >13/100

 Write a query to create a view to getting a count of how many customers we have at
each level of a grade
 create view customer_count
 as
 select count (grade)
 from customer10

 Write a query to create a view to keeping track the number of customers ordering,
number of salesmen attached, average amount of orders and the total amount of orders
in a day.

create view customer_track


as
select count(customer10.customer_id) as customerordering ,count(salesman.salesman_id)
as salesmanattached
avg(orders.purch_amt) as avgorders,count (orders.purch_amt) as totalorders
from customer10,salesman,orders
where orders.ord_date='2012-09-10'

Task 2

 Write a query to create a view that shows for each order the salesman and customer
by name.

create view each_order


as
select orders,ord_no,customer,cust_name as customername,salesman.name

Page 4 of 5
%
Enrollment Number: ____________________________
as salesmanname
from customer10,salesman,orders

 Write a query to create a view that finds the salesman who has the customer with the
highest order of a day. 

create view highestorder


as
select customer10.custname as customername,salesman.name as salesmanname
from customer10,salesman,orders
where orders.customer_id in (
select max(purch_amt) from orders
)

 Write a query to create a view that finds the salesman who has the customer with the
highest order at least 3 times on a day

create view atleast_3times


as
select customer10.cust_name as customername,salesman.name as salesmanname
from customer, salesman,orders

 Write a query to create a view that shows all of the customers who have the highest
grade.

create view highest_grade


as
select * from customer10 where grade in (select max(grade) from customer)

Page 5 of 5

You might also like