SQL 触发器

创建表tbl_student,tbl_luofx

CREATE TABLE [dbo].[tbl_luofx](
[t_name] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[id] [int] NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[tbl_Student](
[ID] [int] IDENTITY(1,1) NOT NULL,
[学生ID] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[学生姓名] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[课程ID] [nchar](10) COLLATE Chinese_PRC_CI_AS NULL,
[课程名称] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[成绩] [int] NULL,
[教师ID] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[教师姓名] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]

创建触发器[Tri_tbl_Student_I]、[Tri_tbl_Student_D]

CREATE TRIGGER [dbo].[Tri_tbl_Student_I]
ON [dbo].[tbl_Student] FOR INSERT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @id int;
select @id=id from inserted
INSERT INTO tbl_luofx values('测试触发器',@id)
-- Insert statements for trigger here
SET NOCOUNT off;
END
CREATE TRIGGER [dbo].[Tri_tbl_Student_D]
ON [dbo].[tbl_Student] FOR DELETE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for trigger here
DELETE FROM tbl_luofx where id in(select id from deleted)
SET NOCOUNT OFF;
END

 

你可能感兴趣的:(sql)