如何在sqlite数据库中创建触发器?

发布于 2024-11-06 06:40:34 字数 639 浏览 0 评论 0原文

我必须在 SQLite 数据库中创建触发器。

数据库中有 3 个表

  • questions (question_id)
  • options (option_id, Question_id)
  • answers (answer_id, Question_id)

每当从 < 中删除任何行时code>question 表中,optionanswer 表中相应的数据也应删除。

我正在尝试使用

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 have to create trigger in my SQLite database.

There are 3 tables in database

  • questions (question_id)
  • options (option_id, question_id)
  • answers (answer_id, question_id)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

友谊不毕业 2024-11-13 06:40:34

删除第一个 DELETE 语句后的 AND

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

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;
冬天的雪花 2024-11-13 06:40:34

尝试将“AND”替换为“;”如果您仍然收到错误,请在此处发布错误的文本

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文