0% found this document useful (0 votes)
61 views16 pages

Assignment 01 CSE464

This document contains 9 questions regarding PL/SQL programming concepts including table manipulation, conditional statements, procedures, exceptions, and user-defined exceptions. The questions involve writing PL/SQL code to perform operations such as calculating bills, validating data, and handling exceptions.

Uploaded by

Anannya Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views16 pages

Assignment 01 CSE464

This document contains 9 questions regarding PL/SQL programming concepts including table manipulation, conditional statements, procedures, exceptions, and user-defined exceptions. The questions involve writing PL/SQL code to perform operations such as calculating bills, validating data, and handling exceptions.

Uploaded by

Anannya Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Assignment 01

Course Code: CSE464


Course Title: Advanced Database Systems
Section: 02

Submitted To:

Dr. Md. Ezharul Islam, PhD

Professor, Department of Computer Science and Engineering.

Submitted By:
Student name: Anannya Das
Student ID: 2016-2-60-135
Date of Submission: 5/12/2022
1. (Basic Table Manipulation) Write a program that declares an integer variable called num,
assigns a value to it, and computes and inserts into the tempp table the value of the variable
itself, its square, and its cube. Consider the following DDL statements to create tempp table.

CREATE TABLE tempp ( item number, square number, CUBE number );

Answer:

{CREATE TABLE tempp ( item number, square number, CUBE number );

TABLE created.

DECLARE num number := #

BEGIN

INSERT INTO tempp

VALUES (num, num*num, num*num*num);

END;

Enter value

FOR num: 5

SELECT *

FROM tempp;

-- ITEM SQUARE CUBE

-- 5 25 125

2. (Conditional Statements) Input three positive integers representing the sides of a triangle, and

determine whether they form a valid triangle. Hint: In a triangle, the sum of any two sides must
always be greater than the third side. Display the output on the screen using
dbms_output.put_line.

Answer:

DECLARE a number := &first_side;

b number := &second_side;

c number := &third_side;

BEGIN IF a+b>c THEN IF b+c>a THEN IF c+a>b THEN

dbms_output.put_line('valid triangle');

END IF;

END IF;

ELSE dbms_output.put_line('invalid triangle');

END IF;

END;

3. (Conditional Statements) Program should accept the age of the user. Depending upon the

following conditions it should output:-

IF age < 18 years, “child”

IF age >= 18 years and <21 years, “major”

IF age >= 21years, “adult”

Display the output on the screen using dbms_output.put_line.

Answer:
DECLARE age number:=&user_age;

USER varchar(20);

BEGIN IF (age<18) THEN USER := 'child’;

elsif (age>18) AND (age<21) THEN USER := 'major’;

ELSE USER := 'adult';

END IF;

dbms_output.put_line('user is '||USER);

END;

4. (Conditional Statements) Suppose the grade obtained by a student depends upon his scores
and

the grading rule is as follows. :-

Scores Grades

95-100 A

70-84 B

70-84 C

60-69 D

0-59 F

Write a PL/SQL block to accept a student‟s marks and accordingly output his grade. Display the

output on the screen using dbms_output.put_line.

Answer:

DECLARE score number := '&score';


grade varchar(1);

BEGIN IF score <= 59 THEN grade := 'E';

elsif score <= 69 THEN grade := 'D';

elsif score <= 84 THEN grade := 'C';

elsif score <= 94 THEN grade := 'B';

elsif score <= 100 THEN grade := 'A';

END IF;

dbms_output.put_line('your marks are'||score||' your grade is - -->'||grade);

END;

5. (Conditional Statements) A company manufactures three products:- computer stationery,


fixed

disks and computers.

The following codes are used to indicate them:-

Product Code

Computer Stationery 1

Fixed Disks 2

Computers 3

The company has a discount policy as follows:

Product Order amount Discount rate

Computer stationery 5000 and above 12%

Computer stationery 3000 and above 8%


Computer stationery Below 3000 2%

Fixed disks 20000 and above 10%

Fixed disks 15000 and above 5%

Computers 50000 and above 10%

Computers 25000 and above 5%

Write a program to accept the order details i.e. product code and order amounts for the products,

calculate the discount amounts as per this policy and output the net order amount. Display the

output on the screen using dbms_output.put_line.

Answer:

DECLARE code number := '&item_code';

amt number := '&item_amount';

disc number;

prod varchar2(30);

BEGIN IF code=1 THEN prod := 'Computer Stationary';

IF amt >= 5000 THEN disc := .12;

elsif amt >= 3000 THEN disc := .08;

ELSE disc := .02;

END IF;

elsif code = 2 THEN prod := 'Fixed Disks';

IF amt >= 20000 THEN disc := .12;


elsif amt >= 15000 THEN disc := .08;

END IF;

elsif code = 3 THEN prod := 'Computers';

IF amt >= 50000 THEN disc := .12;

elsif amt >= 25000 THEN disc := .08;

END IF;

END IF;

dbms_output.put_line('_____________________________');

dbms_output.put_line('PRODUCT '||prod);

dbms_output.put_line('ORDER AMOUNT '||amt);

dbms_output.put_line('DISCOUNT IS '||disc);

dbms_output.put_line('AFTER DISCOUNT'||((1-disc)*amt));
dbms_output.put_line('______________________________');

END;

6. (Iteration) Write a program that examines all the numbers from 1 to 999, displaying all those
for which the sum of the cubes of the digits equal the number itself. Display the output on the
screen using dbms_output.put_line.

Answer:

DECLARE i int: =1;

x int;

y int;
z int;

BEGIN LOOP x := mod(i,10);

y := mod(i,10);

z := mod(i,10);

IF (x*x*x+y*y*y+z*z*z) = i THEN dbms_output.put_line(i);

dbms_output.put_line ('____');

END IF;

exit WHEN I = 1000;

i := i + 1;

END LOOP;

END;

7. (Procedure) The CUSTOMER table of a state electricity board consists of the following
fields:

Meter_Number varchar2(4)

Meter_Type char(1)

Previous_Reading Number

Current_Reading Number

Customer_Type char(1)

Last_Bill_payment char(1) (values could be ‘Y’ or ‘N’)

There are two types of meters.

• 3- phase coded as „T‟


• 1-phase coded as „S‟

There are 4 types of customers.

• Agricultural coded as „A‟

• Industrial coded as „I‟

• Commercial coded as „C‟

• Residential coded as „R‟

Formulae used:

Units used = Current Reading - Previous Reading

Rate = 1/ 1.25/ 1.50/ 1.30 for A/I/C/R respectively.

Amount = rate*units used

Surcharge = 5% for single phase

10% for 3 phase

Excise = 30% of (amount +Surcharge)

Net = Amount +Surcharge + Excise

Write a procedure calculate_bill to calculate the bill for each customer. Consider the following

customer table to read input data.

CREATE TABLE CUSTOMER (

Meter_Number varchar2(4),

Meter_Type char(1),
Previous_Reading number,

Current_Reading number,

Customer_Type char(1),

Last_Bill_payment char(1),

check(Last_Bill_payment = 'Y' OR Last_Bill_payment = 'N')

);

--insert dummy data into table customer

Meter_number Meter_Type Previous_Reading Current_Readin Customer_Type Last_Bill_payment


g
1000 S 3000 5000 A Y
1001 T 3000 5000 R Y
1002 S 400 2000 R Y

After calculating the bill, the procedure should insert the total Amount, Surcharge, Excise and

Net into the Bill table as shown below.

CREATE TABLE BILL (

Meter_Number varchar2(4) PRIMARY KEY,

units number,

rate number,

amount number,

surcharge number,

Excise number,
Net number

);

Answer:

CREATE OR REPLACE PROCEDURE calculatebill AS v_customer

customer%rowtype;

v_bill bill%rowtype;

CURSOR c1 IS

SELECT *

FROM customer;

rate number (3,2);

units’ number;

amount number;

surcharge number;

Excise number;

Net number;

BEGIN

DELETE

FROM bill;

FOR v_customer IN c1 LOOP

SELECT decode (v_customer."Customer Type", 'A', 1, 'I', 1.25, 'C', 1.50, 'R', 1.30) INTO rate

FROM dual;
SELECT decode (v_customer."Meter Type", 'T', 10, 'S', 5) INTO surcharge

FROM dual;

units := v_customer."Current Reading" - v_customer."Previous Reading";

amount := rate*units;

surcharge := surcharge*amount;

Excise := (amount + Surcharge)*30/100;

Net := Amount + Surcharge + Excise;

INSERT INTO bill

VALUES (v_customer."Meter Number", units, rate, amount, surcharge, Excise, Net);

END LOOP;

END;

OUTPUT

--Compile:

ALTER PROCEDURE calculatebill compile;

--Call:

EXEC calculatebill

8. (Exception) Write a PL/SQL block that prompts the user to enter the salary of an employee.

Your program should display the name of the employee (from the EMP table as shown below)

who‟s getting that salary. If more than 1 employee is receiving that salary, or if no employees

exist getting that salary, your program should display appropriate messages. Use too_many_rows

and no_data_found exceptions to achieve this. Display the results on the screen using
dbms_output.put_line.

CREATE TABLE emp (empno varchar(4), empname varchar(30), designation

varchar2(10), category char(1), basicsalary number(4), joined date);

Answer:

DECLARE mysal number;

sal NUMBER := &salary;

BEGIN

SELECT basicsalary INTO mysal

FROM emp

WHERE basicsalary = sal;

exception WHEN too_many_rows THEN dbms_output.put_line ('too many data found in result
can't handel ');

WHEN no_data_found THEN dbms_output.put_line ('no data found for your query');

END;

9. (User-defined Exception) Create a user-defined exception by the name of exp_check. Select


the empname and joined date of all employees into a cursor from the emp table as shown in
problem 9. Your program should calculate the experience of all the employees in years, and
insert the empname and experience of each employee into tempp table. Create the table
beforehand.

If any employee has experience less than 2 years, the program should be aborted with a suitable

message. Raise the user-defined exception exp_check to achieve this. Display the results on the

screen using dbms_output.put_line.


Answer:

DECLARE exp_check exception;

CURSOR c1 IS

SELECT empname, joined

FROM emp;

BEGIN

FOR i IN c1 LOOP

If (trunc (months_between(sysdate, i.joined)/12) < 2) THEN RAISE exp_check;

ELSE

INSERT INTO tempp2

VALUES (i.empname, trunc(months_between(sysdate,i.joined)/12));

END IF;

END LOOP;

exception WHEN exp_check THEN dbms_output.put_line('experiance is less than 2 years not


allowed');

WHEN others THEN dbms_output.put_line('unidentified error occured');

END;

10. (Function) Write a PL/SQL function to take three parameters, the sides of a triangle. The
sides of the triangle should be accepted from the user. The function should return a Boolean
value – true if the triangle is valid, false otherwise. A triangle is valid if the length of each side is
less than the sum of the lengths of the other two sides. Check if the dimensions entered by the
user can form avalid triangle.
Display the results on the screen using dbms_output.put_line.

Answer:

CREATE OR REPLACE FUNCTION triangle (a number, b number, c number)

RETURN boolean AS invalid_triangle exception;

BEGIN

IF NOT (a+b>=c AND b+c>=a AND c+a>=b) THEN RAISE invalid_triangle;

ELSE RETURN TRUE;

END IF;

exception WHEN invalid_triangle THEN dbms_output.put_line ('xxxxxx- invalid triangle -


xxxxxxxxxx');

RETURN FALSE;

WHEN others THEN dbms_output.put_line ('un identified error occured');

END;

CALLING-CODE

DECLARE a number := &side1;

b number: =&side2;

c number := &side3;

x boolean;

BEGIN

x := triangle(a,b,c);

END /
Enter value

FOR side1: 2 OLD 2: a number := &side1;

NEW 2: a number := 2;

Enter value

FOR side2: 3 OLD 3: b number := &side2;

NEW 3: b number := 3;

Enter value

FOR side3: 5 OLD 4: c number := &side3;

NEW 4: c number := 5;

You might also like