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

Assignment 7 SET A

The document outlines the creation of two tables, 'library' and 'books', along with their respective data entries. It includes the definition of three stored functions for handling exceptions: one for counting books in a library, another for summing copies by an author, and a third for retrieving books added in a specific year. Each function raises an exception for invalid inputs, ensuring robust error handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Assignment 7 SET A

The document outlines the creation of two tables, 'library' and 'books', along with their respective data entries. It includes the definition of three stored functions for handling exceptions: one for counting books in a library, another for summing copies by an author, and a third for retrieving books added in a specific year. Each function raises an exception for invalid inputs, ensuring robust error handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

-- ASSIGNMENT 7 (exception handling)

-- SET A 1

-- Tables - library, books

CREATE TABLE library


(
library_id int PRIMARY KEY,
library_name varchar(50),
location varchar(50)
);

INSERT INTO library


VALUES
(1, 'balbharti', 'pune'),
(2, 'rekhta', 'pune'),
(3, 'savitribai phule library', 'pune'),
(4, 'm k gandhi library', 'pune'),
(5, 'all in one library', 'pune');

CREATE TABLE books


(
book_id int PRIMARY KEY,
book_name varchar(50),
author varchar(50),
library_id int REFERENCES library(library_id),
year int, -- additionally added column for query 3
copies int
);

INSERT INTO books


VALUES
(101, 'aachary charak', 'parul joshi', 1, 2015, 20),
(102, 'akho', 'kanaiyalal joshi', 1, 2016, 50),
(103, 'ashtavkra', 'dhanraj pandit', 1, 2018, 10),
(104, 'The art of ghazal writing', 'syed', 2, 2017, 20),
(105, 'The great Alaama Iqbaal', 'shahrukh', 2, 2014, 40),
(106, 'Learn Urdu', 'mustaqeem', 2, 2020, 100),
(107, 'c programming', 'aryan', 3, 2015, 30),
(108, 'advance c programming', 'aryan', 3, 2016, 30),
(109, 'cpp programming', 'aryan', 3, 2018, 30),
(110, 'fundamentals of web developments', 'aman', 3, 2019, 40),
(111, 'india before 1947', 'raj', 4, 2016, 50),
(112, 'the history of taj mahel', 'rahul', 4, 2014, 50),
(113, 'harry potter', 'jk rowling', 5, 2010, 100),
(114, 'the twilight saga', 'stephenie meyer', 5, 2015, 70);

SELECT * FROM library;

SELECT * FROM books;

-- 1. Write a stored function to print the total number of books in a particular


library. Display an appropriate message for an invalid library name

CREATE OR REPLACE FUNCTION number_of_books(name varchar(50)) RETURNS int AS $$


DECLARE
n int;
i int;
BEGIN

SELECT COUNT(*) INTO i


FROM library
WHERE library_name = name;

IF i = 0
THEN RAISE EXCEPTION ' Invalid Library Name ! ';
RETURN 0;
END IF;

SELECT SUM(copies) INTO n


FROM books
WHERE library_id = (
SELECT library_id
FROM library
WHERE library_name = name
);
RETURN n;

END ;
$$ LANGUAGE plpgsql;

SELECT number_of_books('balbharti');

-- SELECT number_of_books('pandit nehru');

-- 2. Write a sored function to print the sum of available copies of books written
by a given author. Display an appropriate message for an invalid author name

CREATE OR REPLACE FUNCTION book_copies(name varchar(50)) RETURNS int AS $$


DECLARE
n int;
i int;
BEGIN
SELECT COUNT(*) INTO i
FROM books
WHERE author = name;

IF i = 0
THEN RAISE EXCEPTION 'Invalid Author Name !';
RETURN 0;
END IF;

SELECT SUM(copies) INTO n


FROM books
WHERE author = name;

RETURN n;

END;
$$ LANGUAGE plpgsql;

SELECT book_copies('syed');
-- SELECT book_copies('mohan');

-- 3. Write a stored function to display details of books that were added in a


specific year (accept year as input). Raise an error for an invalid year.

CREATE OR REPLACE FUNCTION books_of_year(y int) RETURNS SETOF books AS $$


DECLARE
i int;
BEGIN
SELECT COUNT(*) INTO i
FROM books
WHERE year = y;

IF i = 0
THEN RAISE EXCEPTION 'Invalid Year !';
END IF;

RETURN QUERY
SELECT *
FROM books
WHERE year = y;
END;
$$ LANGUAGE plpgsql;

SELECT * FROM books_of_year(2015);

-- SELECT * FROM books_of_year(1900);

You might also like