I am trying to write a sql server update trigger for my blockbuster movie company. In my MOVIES table, I have movie_id as PK, and another column called num_rented, which contains a total of how many times a movie is shot. This amount is done through input, deletion, and update triggers. (I understand that there are much better ways to do this, but my assignment specifically requires this, so please understand this). The CUSTOMER_RENTALS table has item_rental_id, since PK and movie_id from MOVIES are FK.
I need an update trigger that will update the num_rentals column in MOVIES whenever an update for CUSTOMER_RENTALS is done. For example, let's say that movie_id 1 was entered, but it was a mistake, and it was really movie_id 2. I would like num_rentals to reflect the update.
Here is what I still have, but I really don't know what to add to the SET part to make this happen:
CREATE TRIGGER tr_num_rentals_update ON customer_rentals AFTER UPDATE AS BEGIN UPDATE m SET num_rentals = ?????? FROM movies AS m INNER JOIN inserted as i on m.movie_id=i.movie_id; END;
I think I need to access the values ββof the deleted table in order to restore the num_rental column to its previous value, but I don't know how to do it. Thanks for the billion!
Steve source share