SQL语句创建触发器表c和表ac之间级联更新与级联删除
时间: 2024-03-09 16:46:55 浏览: 113
可以使用以下SQL语句创建触发器实现表c和表ac之间的级联更新和级联删除:
创建触发器实现级联更新:
```
CREATE TRIGGER trigger_name
AFTER UPDATE ON c
FOR EACH ROW
BEGIN
UPDATE ac SET ac.col1 = NEW.col1 WHERE ac.col2 = OLD.col2;
END;
```
创建触发器实现级联删除:
```
CREATE TRIGGER trigger_name
AFTER DELETE ON c
FOR EACH ROW
BEGIN
DELETE FROM ac WHERE ac.col2 = OLD.col2;
END;
```
其中,`trigger_name` 是触发器的名称,`c` 和 `ac` 分别是需要进行级联更新或删除的表,`col1` 和 `col2` 是两个表中需要进行关联的列,`NEW` 和 `OLD` 分别代表更新前和更新后的数据行。
相关问题
SQL sever中为表SC创建级联删除和级联更新触发器
好的,我了解您的要求。下面是为表SC创建级联删除和级联更新触发器的 SQL 语句:
创建级联删除触发器:
```
CREATE TRIGGER tr_SC_Delete
ON dbo.SC
FOR DELETE
AS
BEGIN
SET NOCOUNT ON;
DELETE FROM dbo.SC WHERE Cno IN (SELECT Cno FROM deleted);
END
```
创建级联更新触发器:
```
CREATE TRIGGER tr_SC_Update
ON dbo.SC
FOR UPDATE
AS
BEGIN
SET NOCOUNT ON;
UPDATE dbo.SC SET Score = inserted.Score
FROM dbo.SC
INNER JOIN inserted ON dbo.SC.Sno = inserted.Sno AND dbo.SC.Cno = inserted.Cno;
END
```
请确认您已经掌握了这些 SQL 语句并且理解其含义,如果还有其他问题,请随时向我提出。
要求:用SQL语句创建两个触发器,保证数据库的完整性。 创建触发器,当学生表中某个“学号”的学生被删除时,自动将“stc"表中该学生的学习记录删除,即在“学生表”中建立删除触发器,实现“student”表和"stc”表的级联删除。
触发器一:
CREATE TRIGGER del_student
AFTER DELETE ON student
FOR EACH ROW
BEGIN
DELETE FROM stc WHERE stc.sno = old.sno;
END;
触发器二:
CREATE TRIGGER del_course
AFTER DELETE ON course
FOR EACH ROW
BEGIN
DELETE FROM stc WHERE stc.cno = old.cno;
END;
阅读全文