Question

I'm trying to create a trigger for my 'rentals' table that will change the value in a certain column ('rent_avail') from another table when an update occurs in the former table. The idea is simple: when a dvd is returned to the dvd store (i.e. date_return has a date value, the dvd has now become available again for renting. Thus, my 'rent_avail' column for the record (dvd) in question should reflect this by being set to 'Y' (the alternative is 'null' when the dvd is currently being rented out). My trigger is being created without errors, but after an insert on the date_return column, all values in my DVD table are being changed. I want to know how can I simply modify the column values in 'rent_avail' column in my dvd table for only the row being updated! This is probably very trivial, but I have researched it and can't seem to find a solution easily..

CREATE OR REPLACE TRIGGER RENTAL_RETURNS
AFTER UPDATE OF DATE_RETURN ON RENTAL
FOR EACH ROW
BEGIN
    IF :OLD.DATE.RETURN IS NULL AND :NEW.DATE_RETURN IS NOT NULL THEN
        UPDATE DVD SET RENT_AVAIL = 'Y' WHERE DVD.DVD_ID = DVD_ID;
    END IF;
END;
/
Was it helpful?

Solution

your update statement is not picking dvd_id from parent table rebtal, but evaluating like where dvd_id = dvd_id which will always be TRUE. Just ad :OLD qualifier and you should be good, considering this is same column name (dvd_id) in rental table.

CREATE OR REPLACE TRIGGER RENTAL_RETURNS
AFTER UPDATE OF DATE_RETURN ON RENTAL
FOR EACH ROW
BEGIN
    IF :OLD.DATE.RETURN IS NULL AND :NEW.DATE_RETURN IS NOT NULL THEN
        UPDATE DVD SET RENT_AVAIL = 'Y' WHERE DVD.DVD_ID = :OLD.DVD_ID;
    END IF;
END;

OTHER TIPS

It looks to me like there's a period where there should be an underscore in the IF statement - I think it should read

IF :OLD.DATE_RETURN IS NULL AND :NEW.DATE_RETURN IS NOT NULL THEN`

Share and enjoy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top