Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有两个表作为学生和证书,从学生表中删除学生时,我需要从证书表中删除与特定学生相关的证书。我对mysql触发器不太熟悉。请帮助我。
这是我尝试过的,但我不确定它是否正确
DELIMITER $$ CREATE TRIGGER delCerts AFTER DELETE ON students FOR EACH ROW BEGIN DELETE FROM certificates as certs WHERE certs.stCode = students.stCode; END; $$
在 InnoDB 数据库中,您可以通过在证书表中使用外键来实现它:
idStudent INT NOT NULL, FOREIGN KEY (idStudent) REFERENCES Students (id) ON DELETE CASCADE ON UPDATE CASCADE
当您从 stuends 表中删除学生时 - 所有相关证书也将被删除。我认为它比触发器更好,因为如果您或某人将禁用触发器(意外或故意),您的数据库将不一致。