So, Im trying to work on a simple book loans system and Im having problems on creating and using a function.
I have a Loans 'Table', Copies 'Table' and Available 'View'.
"Available View" looks like this:
book_id | available_copies
---------+------------------
BI6 | 1
wherein 'available_copies' column is
COUNT(copy_id) AS available_copies
FROM copies
WHERE copy_id NOT IN (SELECT copy_id FROM loans)
This is my "Copies Table"
copy_id | book_id | copy_no | copy_code
---------+---------+---------+-----------
CI8 | BI6 | 8 | CI
CI9 | BI6 | 9 | CI
CI7 | BI7 | 7 | CI
CI10 | BI7 | 10 | CI
and this is my "Loans Table"
loan_id | copy_id | user_id | borrow_date | due_date | loan_no | loan_code
---------+---------+---------+-------------+------------+---------+-----------
LI10 | CI10 | UI4 | 2013-05-21 | 2013-05-26 | 10 | LI
LI11 | CI8 | UI4 | 2013-05-21 | 2013-05-26 | 11 | LI
LI12 | CI7 | UI4 | 2013-05-22 | 2013-05-27 | 12 | LI
What i really wanted to do is.. if the available_copies is 0 (like in the "available view" above, BI7 is not in the Available View anymore because all copies where already borrowed) postgres will prompt something that you cannot borrow books in Loans anymore since the book is already out of copies.
Im kinda new to plpgsql. Please help. :(
select version().