我正在开发一个反馈表,允许学生就特定主题提供反馈。我有一个包含 3 个字段“ID、唯一编号、密码”的表格,其中存储了学生的录取编号。现在这就是我想要的。每个学生完成反馈后,必须自动从表中删除他的特定数据。请帮忙。
2 回答
This can be done with a JOIN
, but I'll demonstrate a trigger here, because I mentioned it in my comment above.
I assume you've got another table where you store the students feedback data. Let's call it students_feedback
and the other students_admission
for this example.
Using MySQL Triggers, you assing the Database to delete the student admission data automatically ON INSERT
. You'll want to use on create, because as soon as the feedback data is stored in the students_feedback table, the INSERT event is triggered.
So, for example:
CREATE TRIGGER delete_admission AFTER INSERT
ON students_feedback FOR EACH ROW
BEGIN
DELETE FROM students_admission WHERE students_admission.id=OLD.id LIMIT 1;
END;
Use whatever DELETE query you want here.
NOTE: Before MySQL 5.0.10, triggers cannot contain direct references to tables by name.
就像在使用触发器之前解释的那样。只需单击触发器并创建一个触发器,该触发器会在INSERT
记录学生反馈的表中的 an 之后发生。你可以做这样的事情
尽管使用触发器是一种好习惯,但我并不同意。触发器是业务逻辑,它们的逻辑应该在您的代码中实现。在您的应用程序和数据库中分离业务逻辑会使下一个开发人员更难工作,因为他不知道在哪里寻找。我认为使用它们可行的唯一原因是当它用于分布式数据库以保持它们相互更新时。