Nidhi
Nidhi

Reputation: 795

How can I create a trigger in sqlite database?

I have to create trigger in my SQLite database.

There are 3 tables in database

Whenever any row is deleted from question table, the corresponding data should also be deleted from option and answer tables.

I am trying to create trigger using

CREATE TRIGGER question_delete
BEFORE DELETE ON questions
FOR EACH ROW BEGIN
DELETE from options WHERE question_id= OLD.question_id AND
DELETE from answers WHERE question_id= OLD.question_id;
END

I get an error. Should I create two different trigger to perform this operation or any changes are required in above statement?

Please let me know.

Thanks Nidhi

Upvotes: 0

Views: 968

Answers (2)

Joel
Joel

Reputation: 3454

try replacing the "AND" with ";" and it you still get an error, post the error's text here

Upvotes: 0

Alex K.
Alex K.

Reputation: 175766

Remove the AND after the first DELETE statement:

DELETE from options WHERE question_id = OLD.question_id;
DELETE from answers WHERE question_id = OLD.question_id;

Upvotes: 2

Related Questions