创建触发器:当删除学生表S中的一条学生记录时,自动删除选修表SC中该学生的成绩记录 5
MSSQL:
首先你的学号在sc的外键必须是 on delete cascade
alter table sc
add constraint fk_test foreign key (学号) references s(学号) on delete cascade
create trigger test on s
after delete
as
begin
delete from sc where 学号 in (select 学号 from deleted)
end
Oracle:
create or replace trigger test
after delete on s
for each row
begin
delete from sc where 学号=:old.学号;
end;
扩展资料:
SQL语法
DELIMITER |
CREATE TRIGGER `<databaseName>`.`<triggerName>`
< [ BEFORE | AFTER ] > < [ INSERT | UPDATE | DELETE ] >
ON [dbo]<tableName> //dbo代表该表的所有者
FOR EACH ROW
BEGIN
--do something
END |
参考资料来源:百度百科-触发器
首先你的学号在sc的外键必须是 on delete cascade
alter table sc
add constraint fk_test foreign key (学号) references s(学号) on delete cascade
然后
create trigger test on s
after delete
as
begin
delete from sc where 学号 in (select 学号 from deleted)
end
Oracle:
create or replace trigger test
after delete on s
for each row
begin
delete from sc where 学号=:old.学号;
end;
DELIMITER $$
CREATE TRIGGER trg_s
BEFORE
DELETE
ON s #触发地点
FOR EACH ROW
BEGIN
DELETE FROM sc WHERE 学号 = OLD.学号;
END $$
DELIMITER;