数据库课程设计心得【4】---触发器

利用触发器实现级联删除

有两个表

一个是教师表,一个是教师的开课情况表,实现当删除教师的记录时,自动将该教师的开课情况的记录删除

 

在定义数据库关系时,要注意以下设置,我还没找到原因,但是这样设置肯定没问题

附图:

数据库课程设计心得【4】---触发器_第1张图片

删除规则和更新规则原来都是无操作,现在我将之选为层叠。

 

 

触发器代码1如下:

SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: 阿杜 -- Create date: 2010-12-19 -- Description: 实现教师开课情况的级联删除 -- ============================================= CREATE TRIGGER Teacher_delete ON CM_Teacher AFTER DELETE AS BEGIN delete CM_Teach from CM_Teach t,Deleted d where t.CM_Teacher_id=d.CM_Teacher_id; END GO

触发器代码2如下:

set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go -- ============================================= -- Author: 阿杜 -- Create date: 2010-12-19 -- Description: 当添加一个新的学生时,自动为他(她) --在CM_Users表中创建一个用户账号,权限为用户权限0 -- ============================================= ALTER TRIGGER [Student_insert] ON [dbo].[CM_Student] AFTER INSERT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; declare @sname [Varchar] (50); declare @sid [Varchar] (50); select @sname=CM_Student_name from Inserted; select @sid=CM_Student_id from Inserted; insert into CM_Users(CM_User_id,CM_User_password,CM_User_power,CM_Student_id) Values(@sname,'11111','0',@sid) END

你可能感兴趣的:(数据库,Date,user,delete,insert,2010)