I have three tables in my database - Students
, Books
and Books2student
.
Students
table hasStudentID, First_Name, Last_Name
.Books
table hasISBN_no, name, no. of copies available
.Books2Students
table which hasStudentID, books issued, issue date, due date
.
I want to create a trigger that when a student is deleted from books2students
then no. of copies
in Books
table should be increased and if I insert a book in books2student
then no of copies
in books should be decreased.
I wrote a function and trigger, but I am getting error in the function
CREATE FUNCTION student_to_book() RETURNS trigger AS '
BEGIN
IF tg_op = ''DELETE'' THEN
UPDATE books
SET books.no_of_copies_available = no_of_copies_available+1
WHERE Books2Students.Book_Issued=books.ISBN_no;
END IF;
IF tg_op = ''INSERT'' THEN
UPDATE books
SET books.no_of_copies_available = no_of_copies_available-1
WHERE Books2Students.Book_Issued=books.ISBN_no;
END IF;
END
' LANGUAGE plpgsql;
I get error an error:
ERROR: missing FROM-clause entry for table "books2students" Where: PL/pgSQL function student_to_book() line 10 at SQL statement