0% found this document useful (0 votes)
7 views3 pages

DBMS Lab Assgn7

The document outlines a series of PL/SQL assignments for a DBMS lab, including creating functions to count drivers by last name initial, calculate passenger last name lengths, retrieve destination characters, convert passenger names to uppercase, and display driver and payment details using cursors. Each assignment includes SQL code snippets demonstrating the required functionality. The document serves as a practical guide for implementing PL/SQL procedures and functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

DBMS Lab Assgn7

The document outlines a series of PL/SQL assignments for a DBMS lab, including creating functions to count drivers by last name initial, calculate passenger last name lengths, retrieve destination characters, convert passenger names to uppercase, and display driver and payment details using cursors. Each assignment includes SQL code snippets demonstrating the required functionality. The document serves as a practical guide for implementing PL/SQL procedures and functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment-7 ( DBMS Lab)

Name : Kinjal Bhattacharyya


Roll: 122EE0146
1. Create a PL/SQL procedure to calculate the number of Drivers in the Driver table whose last names
start with a specific character provided as input. Use the CHAR function to compare the first character
of each last name.

SQL
1 ⌄ CREATE OR REPLACE FUNCTION count_drivers_by_initial(p_initial CHAR)
2 RETURNS INT AS $$
3 DECLARE
4 driver_count INT;
5 ⌄ BEGIN
6 SELECT COUNT(*) INTO driver_count
7 FROM driver
8 WHERE SUBSTRING(driver_name FROM 1 FOR 1) = p_initial;
9
10 RETURN driver_count;
11 END;
12 $$ LANGUAGE plpgsql;
13

2. Write a PL/SQL block to calculate the length of each Passenger’s last name in the Passenger’s table.

SQL
1 ⌄ CREATE OR REPLACE FUNCTION character_count()
2 RETURNS VOID AS $$
3 DECLARE
4 rec RECORD;
5 ⌄ BEGIN
6 FOR rec IN SELECT passenger_id, lname FROM passenger LOOP
7 RAISE NOTICE 'Passenger ID: %, Last Name: %, Length: %',
8 rec.passenger_id, rec.lname, LENGTH(rec.lname);
9 END LOOP;
10 END;
11 $$ LANGUAGE plpgsql;
12

3. Write a PL/SQL block to retrieve the first 3 characters of each Destination in the reservation table.

SQL
1 ⌄ CREATE OR REPLACE FUNCTION dest_char()
2 RETURNS VOID AS $$
3 DECLARE
4 rec RECORD;
5 ⌄ BEGIN
6 FOR rec IN SELECT DISTINCT destination FROM reservation LOOP
7 RAISE NOTICE 'Characters: %',
8 LEFT(rec.destination,3);
9 END LOOP;
10 END;
11 $$ LANGUAGE plpgsql;
12

4. Write a PL/SQL block that retrieves the first name and last name of Passenger’s FNAME in the
Passengers table and converts them to uppercase. Display the uppercase names.

SQL
1 ⌄ CREATE OR REPLACE FUNCTION passenger_names()
2 RETURNS VOID AS $$
3 DECLARE
4 rec RECORD;
5 ⌄ BEGIN
6 FOR rec IN SELECT fname FROM passenger LOOP
7 RAISE NOTICE 'Name: %',
8 UPPER(rec.fname);
9 END LOOP;
10 END;
11 $$ LANGUAGE plpgsql;
12

5. Write a program in PL/SQL to display a table based detail information for the Driver of ID 5 from the
Drivers table. (using Cursor)

SQL
1 ⌄ CREATE OR REPLACE FUNCTION get_driver_details()
2 RETURNS VOID AS $$
3 DECLARE
4 rec RECORD;
5 ⌄ driver_cursor CURSOR FOR
6 SELECT * FROM driver WHERE driver_id = 5;
7 ⌄ BEGIN
8 OPEN driver_cursor;
9
10 ⌄ LOOP
11 FETCH driver_cursor INTO rec;
12 EXIT WHEN NOT FOUND;
13
14 RAISE NOTICE 'Driver ID: %', rec.driver_id;
15 RAISE NOTICE 'Name: %', rec.driver_name;
16 RAISE NOTICE 'Bus Number: %', rec.bus_id;
17
18 END LOOP;
19
20 CLOSE driver_cursor;
21 END;
22 $$ LANGUAGE plpgsql;
23

6. Write a program in PL/SQL to retrieve the records from the Payments table and display them using
cursors.

SQL
1 ⌄ CREATE OR REPLACE FUNCTION get_payment_details()
2 RETURNS VOID AS $$
3 DECLARE
4 rec RECORD;

5 payments_cursor CURSOR FOR
6 SELECT * FROM payments;

7 BEGIN
8 OPEN payments_cursor;
9

10 LOOP
11 FETCH payments_cursor INTO rec;
12 EXIT WHEN NOT FOUND;
13

14 RAISE NOTICE 'Payment_id: %, Passenger_id: %, Payment_date:%, Payment_amount:%',
15 rec.payment_id, rec.passenger_id, rec.payment_date, rec.payment_amount;
16
17 END LOOP;
18
19 CLOSE payments_cursor;
20 END;
21 $$ LANGUAGE plpgsql;
22

7. Write a PL/SQL program to display the number of passengers in each city (destination) on a given
date. Return city name and number of passengers.

SQL
1 ⌄ CREATE OR REPLACE FUNCTION get_passenger_count(p_travel_date DATE, p_city VARCHAR)
2 RETURNS VOID AS $$
3 DECLARE
4 rec RECORD;
5 ⌄ passenger_cursor CURSOR FOR
6 SELECT destination, COUNT(*) AS passenger_count
7 FROM reservation
8 WHERE reservation_date = p_travel_date AND destination = p_city
9 GROUP BY destination;
10 ⌄ BEGIN
11 OPEN passenger_cursor;
12
13 ⌄ LOOP
14 FETCH passenger_cursor INTO rec;
15 EXIT WHEN NOT FOUND;
16
17 RAISE NOTICE 'City: %, Travel Date: %, Passenger Count: %', rec.destination, p_travel_da
18 END LOOP;
19
20 CLOSE passenger_cursor;
21 END;
22 $$ LANGUAGE plpgsql;
23

You might also like