1

I am trying to create a trigger for a cinema database. I need it to update once a rating is added for a movie showing the text "rating added". The table name is

movie_ratings

the primary key is = movie_rating

I am not really sure how to do it, I have looked online but still are not too sure. I was wondering if anyone could help.

Thanks

1
  • Where do you want the "rating added" text to go? Commented May 6, 2010 at 10:41

1 Answer 1

2

Here is the syntax to create a trigger which will fire when a row is inserted.

create trigger movie_rating_added on movie_ratings for insert
as 
   -- trigger code goes here

go

Inside the trigger, you have access to a virtual table called inserted, which has the same schema as movie_ratings, but which contains only the rows which were inserted.

I'm not clear on exactly what you want the trigger to do, but for example you could do something like this:

create trigger movie_rating_added on movie_ratings for insert
as
    update m set last_action = "rating added"
    from movies m
    join inserted i on i.movie_id=m.id
go

Which is supposing the existence of some fields and tables that you might not have, but hopefully it gives you a useful example.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help, thats useful. Im pretty new to this so not 100% sure on what you mean as -- trigger code goes here, Im not sure what to insert there. Again Thanks for you time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.