B.Sc.
Semester-III
2019
Look forward, learn modern knowledge, and
do not waste time in studies of old subjects of
no values.
Co m pu te r L ab M an ua l
DEPARTMENT OF COMPUTER SCIENCE
CCB-3P1: LABORATORY COURSE - III
Credits
Lab Manual Design Committee:
Prof. Mohammad Ubaidullah Bokhari
Dr. Arman Rasool Faridi
Dr. Faisal Anwar
Dr. Aasim Zafar (Convener)
The Committee thankfully acknowledges the efforts and contributions of the following teachers:
Dr. Faisal Anwar
Editor:
Dr. Aasim Zafar
Design & Compilation:
Mr. S. K. Sharma
Third Edition: July 2019
Approved by BoS, Dated-………
Department of Computer Science
A.M.U., Aligarh, (U.P.), India
Lab Manual: Laboratory Course – III (CCB-3P1)
Course Description 3
Content 3
Objectives 3
Outcomes 4
Rules & Regulations 4
LIST OF CONTENTS (WEEK WISE)
Week No. Contents Page No.
#1 & #2 Data Base Design, E-R diagram 7
#3 E-R diagram using automated tools 9
#4 Create tables and insert some records 11
#5 & #6
Queries for retrieving, updating, deleting the records,
alter table structure and rename table 14
#7 & #8
Queries for creating tables with constraints, inserting
and retrieving the records. 16
#9 & #10
Queries for inserting and retrieving the records based
on given scenario. 18
#11
Queries for inserting and retrieving the records based
on given scenario. 20
#12 PL/SQL stored procedures. 22
#13 Cursors and triggers in PL/SQL 23
#14 Triggers and cursors, PL/SQL block and cursors. 25
#15
OOP concepts in Oracle, SQL commands using
Object-relational concepts. 28
COURSE TITLE: LABORATORY COURSE-III COURSE CODE: CCB – 3P1 CREDIT: 4 PERIODS PER WEEK: 3 CONTINUOUS ASSESSMENT: 40 EXAMS: 60
COURSE DESCRIPTION
This assignment on DBMS is designed for the students of B.Sc.
(Computer Application) III-Semester to learn the concepts of DBMS. DBMS is system software that enables a user to create and maintain a database. It facilitates the process of defining, constructing, manipulating and sharing databases among various users.
There are several DBMS in the market, among these Oracle, MySQL are most popular and widely used. Oracle provides a free version namely Oracle Express edition for the student and independent developers for performing data base related tasks. MySQL is an open source DBMS maintained by Oracle.
CONTENT
This Lab assignment course is designed to help students to create database and apply PL/SQL to manipulate the database. Several live scenarios have been included in the assignment to design E-R Diagram and subsequently create database. Students are supposed to write queries for creating, retrieving and manipulating the database. This course is indented to develop a deep understanding of various query languages such as DDL, DCL, DML, PL
OBJECTIVES
After completing this Lab assignment, the students should be able to:
Design ER diagram and Schema diagram
Design databases from an ER diagram.
Create a full fledge database for any system.
Write different types of SQL queries.
Write PL/SQL block, Cursors, Triggers, Procedures and functions.
OUTCOMES
After completing this course, the students would be able to:
Understand the concept of relational database management system.
Understand the concept of Object-relational database management system.
Comprehend query languages and their usage.
Logically identify logical entities and their relationships.
Draw Entity relationship diagram for any system.
Create cursor, function, procedure, triggers and other database objects.
RULES AND REGULATIONS
Students are required to strictly adhere to the following rules.
The students must complete the weekly activities/assignments well in time (i.e., within the same week).
The students must maintain the Lab File of their completed activities/assignments in the prescribed format (Appendix-1).
The students must get the completed weekly activities/assignments checked and signed by the concerned teachers in the Lab in the immediate succeeding week. Failing which the activities/assignments for that week will be treated as incomplete.
At least TEN (10) such timely completed and duly signed weekly activities/assignments are compulsory, failing which students will not be allowed to appear in the final Lab Examination.
The students need to submit the following two deliverables for each exercise duly signed by the Teacher:
ER Diagram and Schema Diagram (Only for the questions 1,2,6, 7,8,9,10 and 11)
SQL or PL/SQL query with output.
Each question will be evaluated on a scale of 10 points.
The students need to ensure that each question is assessed and signed by the Teacher in the week/time.
Late submission would not be accepted after the due date.
Cooperate, collaborate and explore for the best individual learning outcomes but copying is strictly prohibited.
APPENDIX–I
Template for the Index of Lab File
WEEK NO.
PROBLEMS WITH DESCRIPTION
PAGE NO. SIGNATURE OF THE TEACHER WITH DATE
1
1#
2#
3#
2
1#
2#
3#
3
1#
2#
3#
Note: The students should use Header and Footer mentioning their roll no. &
name in footer and page no in header.
WEEK #1–2
OBJECTIVES
To learn to draw E-R diagram.
To learn to design database based on E-R diagram.
OUTCOMES
After completing this, the students would be able to:
Draw ER diagram and Schema diagram.
Design Database based on ER diagram.
PROBLEMS
1# Indian Railway wants to maintain its database for the train, driver and the platforms with the following requirements:
a) A train has attributes as train no., name, source, destination.
b) A driver has its attributes as Driver name, age,
c) Each Platform has its attributes as Platform id, name, district name.
d) Each train may be driven by different drivers but each driver drives only specific train.
e) A train may stop at different platforms and a platform may accommodate several trains at a moment of time.
2# Consider a CONFERENCE_REVIEW database in which researchers submit their research papers for consideration. Reviews by reviewers are recorded for use in the paper selection process. The database system caters
primarily to reviewers who record answers to evaluation questions for each paper they review and make recommendations regarding whether to accept or reject the paper. The data requirements are summarized as follows:
a) Authors of papers are uniquely identified by e-mail id. First and last names are also recorded.
b) Each paper is assigned a unique identifier by the system and is described by a title, abstract, and the name of the electronic file containing the paper.
c) A paper may have multiple authors, but one of the authors is designated as the contact author.
d) Reviewers of papers are uniquely identified by e-mail address. Each reviewer‟s first name, last name, phone number, affiliation, and topics of interest are also recorded.
e) Each paper is assigned between two and four reviewers. A reviewer rates each paper assigned to him or her on a scale of 1 to 10 in four categories: technical merit, readability, originality, and relevance to the conference. Finally, each reviewer provides an overall recommendation regarding each paper.
f) Each review contains two types of written comments: one to be seen by the review committee only and the other as feedback to the author(s).
Design ER diagram and Schema diagram for the above two problems.
WEEK #3
OBJECTIVES
To learn to draw E-R diagram using automated tools.
To learn to design database using automated tools.
OUTCOMES
After completing this, the students would be able to:
Understand E-R Diagram.
Understand Database.
Design of ER Diagram and database using automated tools.
PROBLEMS
1# Consider a car dealership that sells both new and used cars, and it operates a service facility. The data requirements are summarized as follows:
a) A salesperson may sell many cars, but each car is sold by only one salesperson.
b) A customer may buy many cars, but each car is bought by only one customer.
c) A salesperson writes a single invoice for each car he or she sells.
d) A customer gets an invoice for each car he or she buys.
e) A customer may come in just to have his or her car serviced; that is, a customer need not buy a car to be classified as a customer.
f) When a customer takes one or more cars in for repair or service, one service ticket is written for each car.
g) The car dealership maintains a service history for each of the cars serviced. The service records are referenced by the car‟s serial number.
h) A car brought in for service can be worked on by many mechanics, and each mechanic may work on many cars.
i) A car that is serviced may or may not need parts (e.g., adjusting a carburettor or cleaning a fuel injector nozzle does not require providing new parts).
Design ER diagram, schema diagram and construct database for the above.
Give logical reasoning for the database design.
WEEK #4
OBJECTIVES
To learn to create tables and insert some records.
OUTCOMES
After completing this, the students would be able to:
Understand table and its related concepts.
PROBLEMS
1# Create the tables described below:
Table Name: CLIENT_MASTER
Description: Used to store client information.
Column Name Data Type Size Default Attributes
CLIENTNO Varchar2 6
NAME Varchar2 20
ADDRESS 1 Varchar2 30 ADDRESS 2 Varchar2 30
CITY Varchar2 15
PINCODE Number 8
STATE Varchar2 15
BALDUE Number 10,2
Table Name: PRODUCT_MASTER
Description: Used to store product information.
Column Name Data Type Size Default Attributes PRODUCTNO Varchar2 6
DESCRIPTION Varchar2 15 PROFITPERCENT Number 4,2 UNITMEASURE Varchar2 10
QTYONHAND Number 8
REORDERLVL Number 8
SELLPRICE Number 8,2
COSTPRICE Number 8,2
Table Name: SALESMAN_MASTER
Description: Used to store salesman information working for the company.
Column Name Data Type Size Default Attributes SALESMANNO Varchar2 6
SALESMANNAME Varchar2 20 ADDRESS 1 Varchar2 30 ADDRESS 2 Varchar2 30
CITY Varchar2 20
PINCODE Number 8
STATE Varchar2 20
SALAMT Number 8,2
2# Insert the following data into their respective tables:
a) Data for CLIENT_MASTER table:
ClientNo Name City Pin code State BalDue
C00001 Ivan Bayross Mumbai 400054 Maharashtra 15000 C00002 Mamta
Muzumder Madras 780001 Tamil Nadu 0 C00003 Chhaya Bankar Mumbai 400057 Maharashtra 5000 C00004 Ashwini Joshi Bangalore 560001 Karnataka 0 C00005 Hansel Colaco Mumbai 400060 Maharashtra 2000 C00006 Deepak Sharma Mangalore 560050 Karnataka 0
b) Data for PRODUCT_MASTER table:
Product No
Descriptio n
Profit Percent
Unit Measure
Qty On Hand
Reorder Lvl
Sell Price
CostP rice
P00001 T-Shirts 5 Piece 200 50 350 250
P0345 Shirts 6 Piece 150 50 500 350 P06734 Cotton
Jeans
5 Piece 100 20 600 450
P07865 Jeans 5 Piece 100 20 750 500
P07868 Trousers 2 Piece 150 50 850 550
P07885 Pull Overs 2.5 Piece 80 30 700 450
P07965 Denim Shirts
4 Piece 100 40 350 250
P07975 Lycra Tops 5 Piece 70 30 300 175
P08865 Skirts 5 Piece 75 30 450 300
c) Data for SALESMAN_MASTER table:
Salesman No
Name Address1 Address2 City PinCode State SALAM T S00001 Aman A/14 Worli Mumbai 400002 Maharashtra 3500 S00002 Omkar 65 Nariman Mumbai 400001 Maharashtra 4000 S00003 Raj P-7 Bandra Mumbai 400032 Maharashtra 5000 S00004 Ashish A/5 Juhu Mumbai 400044 Maharashtra 3500
WEEK #5-6
OBJECTIVES
To learn to write queries for retrieving, updating and deleting the records.
To learn to alter table structure, delete and rename table.
OUTCOMES
After completing this, the students would be able to:
Understand how to write DDL, DML based queries.
PROBLEMS
1# Write the SQL queries for the following based on week #3 tables:
I. Exercise on retrieving records from a table a) Find out the names of all the clients.
b) Retrieve the entire contents of the Client_Master table.
c) Retrieve the list of names, city and the state of all the clients.
II. Exercise on updating records in a table
a) Change the city of ClientNo „C00005‟ to „Bangalore‟.
b) Change the BalDue of ClientNo „C00001‟ to Rs. 1000.
c) Change the cost price of „Trousers‟ to Rs. 950.00.
III. Exercise on deleting records in a table
a) Delete all salesman from the Salesman_Master whose salaries are equal to Rs. 3500.
b) Delete all products from Product_Master where the quantity on hand is equal to 100.
c) Delete from Client_Master where the column state holds the value
„Tamil Nadu‟.
IV. Exercise on altering the table structure
a) Add a column called „Telephone‟ of data type „number‟ and size=‟10‟
to the Client_Master table.
b) Change the size of SellPrice column in Product_Master to 10,2.
V. Exercise on deleting the table structure along with the data
a) Destroy the table Client_Master alongwith its data(Note: Copy the data to some other temporary table so that the data should not get lost).
VI. Exercise on renaming the table
a) Change the name of the Salesman_Master table to sman_mast.
WEEK #7-8
OBJECTIVES
To learn how to write queries for creating tables with constraints and inserting and retrieving the records.
OUTCOMES
After completing this, the students would be able to:
Understand the different constraints that can be applied on a table.
Write DML, DDL based queries.
PROBLEMS
1# Consider the insurance database given below. The primary keys are made bold and the data types are specified.
PERSON (driver_id:string , name:string , address: string ) CAR (regno:string , model:string , year:int )
ACCIDENT (report_number:int , accd_date:date , location:string ) OWNS (driver_id:string , regno:string )
PARTICIPATED (driver_id:string , regno:string , report_number:int , damage_amount:int).
a) Create the above tables by properly specifying the primary keys and foreign keys.
b) Enter at least five tuples for each relation.
c) Update the damage amount for the car with specific regno in the accident with report number 12 to 25000.
d) Add a new accident to the database.
e) Find the total number of people who owned cars that were involved in accidents in the year 2008.
f) Find the number of accidents in which cars belonging to a specific model were involved.
2# Consider the following database for a banking enterprise.
BRANCH( branch_name:string , branch_city:string , assets:real ) ACCOUNT( accno:int , branch_name:string , balance:real ) DEPOSITOR( customer_name:string , accno:int )
CUSTOMER( customer_name:string , customer_street:string , customer_city:string )
LOAN( loan_number:int , branch_name:string , amount:real ) BORROWER( customer_name:string , loan_number:int )
a) Create the above tables by properly specifying the primary keys and foreign keys.
b) Enter at least five tuples for each relation.
c) Find all the customers who have at least two accounts at the main branch.
d) Find all the customers who have an account at all the branches located in a specific city.
e) Demonstrate how you delete all account tuples at every branch located in a specific city.
WEEK #9-10
OBJECTIVES
To learn to create tables based on given scenario.
To learn to write queries for inserting and retrieving the records.
OUTCOMES
After completing this, the students would be able to:
Understanding of creating tables based on given scenario.
Understanding of writing DML, DDL based queries.
PROBLEMS
1# A database is to be designed for sales information system. A product can be described with a unique product number, product name, selling price, manufacturer name. The product can sale to a particular client and each client have its own unique client ID, client name, client addresses, city, pin code and total balance to be required to paid. Each client orders to buy product from the salesman. In the order, it has unique sales order number, sales order date, client number and salesman number. Each salesman has the salesman number (unique), name, addresses, city, pin code, salary of the sales man.
a) Create the above tables and mention primary keys and the foreign keys b) Insert at least five relevant records in each of the created relations.
c) Write following SQL queries based on created Database:
i) Retrieve names and cities of all the clients.
ii) Retrieve the available products.
iii) Find the names of all clients having „a‟ as the second letter in their names.
iv) List all the clients who are located in ALIGARH.
v) Find the products whose selling price is greater than 2000 and less than or equal to 5000.
vi) List the products in sorted order of their description.
.
WEEK #11
OBJECTIVES
To learn to create tables based on given scenario.
To learn to write queries for inserting and retrieving the records.
OUTCOMES
After completing this, the students would be able to:
Understanding of creating tables based on given scenario.
Understanding of writing DML, DDL based queries.
PROBLEMS
1# Create an inter University Database with the following relations. Include at least four attributes for each table.
University
Department
Program
Course
Syllabus
Faculty(Teacher)
a) Create above tables and mention primary keys and foreign keys. Also create secondary index for each table.
b) Insert at least 5 relevant records in each of the created tables.
c) Write following SQL queries based on above created database:
i) List of Universities situated at Delhi.
ii) List of all Departments of AMU.
iii) Find the location of JNU.
iv) List of all Programs run by University of Jammu.
v) List of Universities that run Program "MCA".
vi) List of Courses of "MCA" run by AMU.
vii) List of Faculties specialized in "Information Security" across different universities.
viii) Syllabus of "Computer Architecture" of different Universities.
ix) List of all faculties of Department of Computer Science of Delhi University.
x) Find the University which has maximum Departments.
WEEK #12
OBJECTIVES
To learn to write PL/SQL stored procedures.
OUTCOMES
After completing this, the students would be able to:
Understand how to write procedures in PL/SQL.
Understand how to write PL/SQL functions.
PROBLEMS
1# Write a PL/SQL stored procedure to display “Hello World”.
2# Write a PL/SQL stored procedure to calculate the sum of digits of a number entered through users.
3# Write a PL/SQL stored procedure to count no. of even and odd integers entered by the user.
4# Write a PL-SQL function to compare three numbers entered through users and display them in ascending and descending orders.
5# Write a PL/SQL function that performs simple arithmetic like Addition, Subtraction, Multiplication & Division of input numbers.
WEEK #13
OBJECTIVES
To learn to write cursors and triggers in PL/SQL.
OUTCOMES
After completing this, the students would be able to:
Understand how to use cursors and triggers in PL/SQL.
PROBLEMS
1# Create table EMPLOYEE with 5 columns:
ENo Number (Employee Number) FName Varchar2 (First name ) Age Number (Age of Employee)
Grade Varchar2 (Grade of Employee such Asst. prof., Associate Prof. or Professor)
Salary Number (Salary of the Empoyee)
Create a Cursor Emp_Cursor that fetches the record of employee and their salary are incremented according to grade such as for Asst prof, the salary increment is 10000, for Associate Prof 20,000 and for Professor 30,000.
2# Create table GRADES with 5 columns:
SENo Number (Student's Enrollment Number) M1 Number (Mark from test 1 )
M2 Number (Mark from test 2) M3 Number (Mark from test 3)
Avg_M Number (average mark from test 1, 2 and 3)
a) Create a trigger GRADES_TRG that calculates the value of the Avg_M column.
b) Create a trigger on table GRADES such that it restricts the entry of duplicate SENo and also it restricts the marks greater than 50 in each test.
WEEK #14
OBJECTIVES
To learn to write triggers and cursors in PL/SQL.
To learn to write PL/SQL block and cursors.
OUTCOMES
After completing this, the students would be able to:
Understand the use of cursors and triggers.
Understand PL/SQL block and cursors.
PROBLEMS
1# Create an EMPLOYEE table with columns:
(EMP_ID, EMP_NAME, EMP_DOB, EMP_SAL, EMP_GRADE, EMP_DNO) and an EMPLOYEE_LOG table with columns (EMP_ID, EMP_NAME, EMP_DOB, EMP_PRE_GRADE, EMP_PRE_SAL).
EMP_GRADE contains value like „A‟,‟B‟,‟C‟,‟D‟. Next solve the following:
a) Write a PL/SQL code to insert some rows in Employee table
b) Create a PL/SQL trigger called EMPLOYEE_TRIGGER on the EMPLOYEE table. This trigger will be executed after every update of EMP_SAL or EMP_GRADE which will put the previous Employee value to EMPLOYEE_LOG table.
c) Write a PL/SQL block to find the salary grade of the specified employee.
i) If grade is „D‟ display „the employee is junior engineer‟
ii) If grade is „C‟ display „the employee is engineer‟
iii) If grade is „B‟ display „the employee is lead engineer‟
iv) If grade is „A‟ display „the employee is Manager‟
2# Create a DEPARTMENT table with columns:
(DEPT_NO, DEPT_NAME, DEPT_LOC, DEPT_MGRID).
Next solve the following:
a) Write a PL/SQL block that counts the number of departments in each location. If any location has more than 5 departments then display message “too many departments”.
b) Create a PL/SQL block to insert a new department record into the Departments table.
c) Create a PL/SQL Block to delete the department no. 10.Print to the screen the number of rows affected.
3# Establish a relationship between tables given in 1# and 2#. Next solve the following:
a) Write a PL/SQL block which accepts employee number and finds the average salary of the employees working in the department where that employee works. If his salary is more than the average salary of his department, then display message that "employee‟s salary is more than average salary" else display "employee‟s salary is less than average salary".
b) Write a PL/SQL block which displays the department name, total no of employees in the department, average salary of the employees in the department for all the departments from department 10 to department 40 in the Department table. If no employee is working in the
department, then display a message "No employees are working in that department".
WEEK #15
OBJECTIVES
To learn some of the OOP concepts in Oracle.
To learn to write SQL commands using Object-relational concepts.
OUTCOMES
After completing this, the students would be able to:
Understand the use of objects in Oracle.
Understand abstract data types, nested tables and others.
PROBLEMS
1# Create type Address_Ty consisting of the following columns:
Type Name: Address_Ty
Column Name Data Type Size
ADDRESS 1 Varchar2 30
ADDRESS 2 Varchar2 30
CITY Varchar2 20
PINCODE Number 8
STATE Varchar2 20
2# Create the tables described below:
Table Name: SALESMAN_MASTER
Description: Used to store salesman information working for the company.
Column Name Data Type Size
SALESMANNO Varchar2 6
SALESMANNAME Varchar2 20
ADDRESS Address_Ty
SALAMT Number 8,2
SALETRGT Number 6,2
SALEACHVD Number 6,2
3# Exercise on retrieving records from table:
a) Retrieve the list of cities and states from Salesman_Master.
b) Change the city value for salesman who lives in Mumbai to Chennai.
c) Delete the record for the salesman who live in Channai