Laboratory Course IVWrite PL/SQL block, Cursors, Triggers, Procedures and functions. ... Delete all...

29
Laboratory Manual Department of Computer Science Aligarh Muslim University Aligarh 2017-18 B.Sc. IV Semester Laboratory CourseIV (CCB-4P1) (Computer Applications)

Transcript of Laboratory Course IVWrite PL/SQL block, Cursors, Triggers, Procedures and functions. ... Delete all...

La

bo

ra

tor

y M

an

ua

l

Department of Computer Science

Aligarh Muslim University Aligarh

2017-18

B.Sc.

IV Semester

Laboratory Course—IV (CCB-4P1)

(Computer Applications)

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. Sunil Kumar Sharma

First Edition: July 2017

Approved by BoS, Dated– 31/07/2017

Department of Computer Science, A.M.U., Aligarh, (U.P.), India

LAB MANUAL/B.Sc. (CA)/CCB–4P1/CSD-AMU 2 A

MU

AM

U

CS

D

COURSE TITLE: Laboratory Course-IV COURSE CODE: CCB – 4P1

CREDIT: 02 PERIODS PER WEEK: 03

CONTINUOUS ASSESSMENT: 40 EXAMS: 60

COURSE DESCRIPTION

This assignment on DBMS is designed for the students of B.Sc.

(Computer Application), IV-Semester to learn the concepts of DBMS. DBMS

is actually 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.

Student can use Oracle/MySQL for this assignment.

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 databases from an ER diagram.

Normalize the database to remove redundancy.

LAB MANUAL/B.Sc. (CA)/CCB–4P1/CSD-AMU 3 A

MU

AM

U

CS

D

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.

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.

LAB MANUAL/B.Sc. (CA)/CCB–4P1/CSD-AMU 4 A

MU

AM

U

CS

D

The students need to submit the following two deliverables for each

exercise duly signed by the Teacher:

ER Diagram (Only for the questions 1,2,3,6, 7,8,9,10 and 11)

SQL query with output

Each question will be evaluated on a scale of 10 points, 4 for E-R

Diagram (where applicable) and 6/10 for queries.

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-1

Template for the Index of Lab File W

EE

K N

O.

PROBLEMS WITH DESCRIPTION

PA

GE

NO

. SIGNATURE

OF THE

TEACHER

WITH DATE

1

1#

2#

3#

2

1#

2#

3#

3

1#

2#

3#

4

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

To learn to draw E-R diagram.

To learn to design database based on E-R diagram.

.

After completing this, the students would be able to:

draw E-R diagram.

design Database based on E-R diagram.

1# 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.

OU

TC

OM

E

S

PROBLEMS

S

OB

JE

CT

IVE

S

LAB MANUAL/B.Sc. (CA)/CCB–4P1/CSD-AMU 7 A

MU

AM

U

CS

D

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 construct database for the above. Give logical reasoning for the

database design.

LAB MANUAL/B.Sc. (CA)/CCB–4P1/CSD-AMU 8 A

MU

AM

U

CS

D

WEEK #2

To learn to draw E-R diagram using automated tools.

To learn to design database using automated tools.

.

After completing this, the students would be able to:

understand E-R Diagram.

understand Database.

design of ER Diagram and database using automated tools

professionally.

1# Consider a MAIL_ORDER database in which employees take orders for partsfrom

customers. The data requirements are summarized as follows:

a) The mail order company has employees, each identified by a unique employee

number, first and last name, and Zip Code.

b) Each customer of the company is identified by a unique customer number,first

and last name, and Zip Code.

c) Each part sold by the company is identified by a unique part number, a part name,

price, and quantity in stock.

OU

TC

OM

ES

PROBLEMS

S

OB

JE

CT

IVE

S

LAB MANUAL/B.Sc. (CA)/CCB–4P1/CSD-AMU 9 A

MU

AM

U

CS

D

d) Each order placed by a customer is taken by an employee and is given a unique

order number. Each order contains specified quantities of one or more parts.

Each order has a date of receipt as well as an expected ship date. The actual ship

date is also recorded.

Design an Entity-Relationship Diagram and Database for the MAIL_ORDER systemusing

a tool such as ERDPlus etc.

LAB MANUAL/B.Sc. (CA)/CCB–4P1/CSD-AMU 10 A

MU

AM

U

CS

D

WEEK #3

To learn to create tables and insert some records.

.

After completing this, the students would be able to understand

table and its related concepts.

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.

OU

TC

OM

E

S

PROBLEMS

S

OB

JE

CT

IVE

S

LAB MANUAL/B.Sc. (CA)/CCB–4P1/CSD-AMU 11 A

MU

AM

U

CS

D

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 Pincode 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:

LAB MANUAL/B.Sc. (CA)/CCB–4P1/CSD-AMU 12 A

MU

AM

U

CS

D

Product

No

Description Profit

Percent

Unit

Measure

QtyOn

Hand

Reorder

Lvl

SellPrice CostPrice

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 SALAMT

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

LAB MANUAL/B.Sc. (CA)/CCB–4P1/CSD-AMU 13 A

MU

AM

U

CS

D

WEEK # 4 – 5

To learn to write queries for retrieving, updating and deleting

the records.

To learn to alter table structure, delete and rename table.

After completing this, the students would be able to understand

how to write DDL, DML based queries.

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 sate 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.

OU

TC

OM

ES

PROBLEMS

S

OB

JE

CT

IVE

S

LAB MANUAL/B.Sc. (CA)/CCB–4P1/CSD-AMU 14 A

MU

AM

U

CS

D

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.

VI. Exercise on renaming the table

a. Change the name of the Salesman_Master table to sman_mast.

LAB MANUAL/B.Sc. (CA)/CCB–4P1/CSD-AMU 15 A

MU

AM

U

CS

D

WEEK #6 – 7

To learn how o write queries for creating tables with constraints

and inserting and retrieving the records.

To learn to add columns to table.

.

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

1# A database is to be designed for Book dealer with the following details:

AUTHOR (Author-id: int, Name: string, City: string, Country: string)

PUBLISHER (Publisher-id: int, Name: string, City: string, Country: string)

CATALOG (Book-id: int, title: string, Publisher-id: int, Category-id: int, Year: int,

Price: int)

BOOK_AUTHOR(Book-id: int,author-id: int)

CATEGORY (Category-id: int, Description: string)

ORDER-DETAILS (Order-no : int, Book-id: int, Quantity: int)

a. Create above tables and mention primary keys and foreign keys.

b. Insert at least five relevant records in each of the created relations.

c. Write the following SQL queries based on the created Database:

i) Retrieve details of the authors who have authored 1 or more books.

ii) Retrieve details of the books whose price is greater than the average

price of the books in the catalog and the year of publication are after 2010.

OU

TC

OM

ES

PROBLEMS

S

OB

JE

CT

IVE

S

LAB MANUAL/B.Sc. (CA)/CCB–4P1/CSD-AMU 16 A

MU

AM

U

CS

D

iii) Retrieve details of the books which have more than 1 author.

iv) Retrieve details of publishers associated with author “John”.

v) Demonstrate how you increase the price of books published by a

specific publisher by 10%.

d. Write a SQL query to add two more columns, Association and Qualification

in AUTHOR table.

LAB MANUAL/B.Sc. (CA)/CCB–4P1/CSD-AMU 17 A

MU

AM

U

CS

D

WEEK #8 – 9

To learn to create tables based on given scenario.

To learn to write queries for inserting and retrieving the

records.

After completing this, the students would be able to:

Understanding of creating tables based on given scenario.

Understanding of writing DML, DDL based queries

1# A database is to be designed for sales information system. A product can be

described with a unique product number, product name, sellingprice, 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.

OU

TC

OM

ES

PROBLEMS

S

OB

JE

CT

IVE

S

LAB MANUAL/B.Sc. (CA)/CCB–4P1/CSD-AMU 18 A

MU

AM

U

CS

D

iv) List all the clients who are located in ALIGARH.

v) Find the products whose selling price is greater than 2000 and less than

orequal to 5000.

vi) List the products in sorted order of their description.

LAB MANUAL/B.Sc. (CA)/CCB–4P1/CSD-AMU 19 A

MU

AM

U

CS

D

WEEK #10 – 11

To learn to create tables based on given scenario.

To learn to write queries for inserting and retrieving the

records.

.

After completing this, the students would be able to:

Understanding of creating tables based on given scenario.

Understanding of writing DML, DDL based queries

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

secondaryindexfor each table.

b. Insert at least 5 relevant records in each of the created tables.

c. Write following SQL queries based on Database created in Question No. 8:

i) List of Universities situated at Delhi.

OU

TC

OM

ES

PROBLEMS

S

OB

JE

CT

IVE

S

LAB MANUAL/B.Sc. (CA)/CCB–4P1/CSD-AMU 20 A

MU

AM

U

CS

D

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.

LAB MANUAL/B.Sc. (CA)/CCB–4P1/CSD-AMU 21 A

MU

AM

U

CS

D

WEEK #12

To learn to write PL/SQL stored procedures.

.

After completing this, the students would be able to understand

how to write procedures in PL/SQL.

1# Write a PL/SQL stored procedure to display “Hello World”.

2# Write a PL/SQL stored procedure to calculate maximum and minimum of three

numbers entered through users.

3# Write a PL/SQL stored procedure to find sum of first ten natural numbers.

OU

TC

OM

E

S

PROBLEMS

S

OB

JE

CT

IVE

S

LAB MANUAL/B.Sc. (CA)/CCB–4P1/CSD-AMU 22 A

MU

AM

U

CS

D

WEEK #13

To learn to write PL/SQL stored procedures.

.

After completing this, the students would be able to understand

how to write PL/SQL functions.

1# Write a PL-SQL function to compare three numbers entered through users and

display them in ascending and descending orders.

2# Write a PL/SQL function that performs simple arithmetic like Addition, Subtraction,

Multiplication & Division of input numbers.

OU

TC

OM

E

S

PROBLEMS

S

OB

JE

CT

IVE

S

LAB MANUAL/B.Sc. (CA)/CCB–4P1/CSD-AMU 23 A

MU

AM

U

CS

D

WEEK #14

To learn to write cursors and triggers in PL/SQL.

.

After completing this, the students would be able to understand

how to use cursors and triggers in PL/SQL.

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 )

OU

TC

OM

E

S

PROBLEMS

S

OB

JE

CT

IVE

S

LAB MANUAL/B.Sc. (CA)/CCB–4P1/CSD-AMU 24 A

MU

AM

U

CS

D

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.

LAB MANUAL/B.Sc. (CA)/CCB–4P1/CSD-AMU 25 A

MU

AM

U

CS

D

WEEK #15

To learn to write triggers and cursors in PL/SQL.

.

After completing this, the students would be able to understand the

use of cursors and triggers.

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_LOGtable.

c) Write a PL-SQL cursor that increments the salary of employees of Grade ‘D’

by 20%.

d) 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’

OU

TC

OM

E

S

PROBLEMS

S

OB

JE

CT

IVE

S

LAB MANUAL/B.Sc. (CA)/CCB–4P1/CSD-AMU 26 A

MU

AM

U

CS

D

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’

e) Write a PL-SQL cursor to find second highest salary from EMPLOYEE table.

LAB MANUAL/B.Sc. (CA)/CCB–4P1/CSD-AMU 27 A

MU

AM

U

CS

D

WEEK #16

To learn to write PL/SQL block and cursors.

After completing this, the students would be able to

understand PL/SQL block and cursors.

1# 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 selects the maximum department number in

thedepartment table and store it in a variable and print the results to screen.

b) Create a PL/SQL block to insert a new department record into the

Departments table. Use maximum dept number fetched from above and adds

10 to it.

c) Create a PL/SQL Block to delete the department no. 10.Print to the screen the

number of rows affected.

c) Write a Cursor to find MGRID of Departments located in “Mumbai”.

2# Establish a relationship between tables given in 1# and the previous week. 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

OU

TC

OM

E

S

PROBLEMS

S

OB

JE

CT

IVE

S

LAB MANUAL/B.Sc. (CA)/CCB–4P1/CSD-AMU 28 A

MU

AM

U

CS

D

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

forall 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".