数据库实验五
还是用实验一建立的那三个表进行操作
相关知识请看这个博客,尤其是游标说的很清楚
(1)设置一个触发器,该触发器仅允许“dbo”用户可以删除学生表内数据,否则出错
说明:deleted表用于存储INSERT和UPDATE语句所影响的行的副本
CREATE TRIGGER dbo_delete
ON S
FOR DELETE
AS
--这里指定了条件
IF EXISTS (SELECT * FROM deleted)
BEGIN
IF USER != 'dbo'
END
DELETE FROM S WHERE S.Sno = '1'
(2)针对学生表写一个DELETE触发器
CREATE TRIGGER S_delete
ON S
FOR DELETE
AS
PRINT('DELETE')
DELETE FROM S WHERE S.Sno = '1'
(3)针对学生表写一个UPDATE触发器
CREATE TRIGGER S_update
ON S
FOR UPDATE
AS
PRINT(UPDATE)
UPDATE S SET S.sdept = 'CS' WHERE S.Sno = '1'
(4)统计学生的平均成绩,输出低于平均分的成绩(使用游标)
这篇博客比较详细
DECLARE lowavg CURSOR
FOR
SELECT SC.grade
FROM SC
WHERE SC.grade < (SELECT avg(SC.grade) FROM SC)
OPEN lowavg
FETCH NEXT
FROM S
INTO @grade
WHILE @@fetch_status=0
BEGIN
PRINT(@grade)
FETCH NEXT FROM lowavg INTO @grade
END
CLOSE lowavg
DEALLOCATE lowavg
如图:
实验五结束。