From ddc955feb592ffa43ef1b0d842137509ead3f4dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A0=E5=BF=97=E6=96=B0?= <1759998065@qq.com> Date: Tue, 11 Oct 2022 15:48:44 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=BB=BA=2010.10?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "05\345\215\240\345\277\227\346\226\260/10.10/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "05\345\215\240\345\277\227\346\226\260/10.10/.keep" diff --git "a/05\345\215\240\345\277\227\346\226\260/10.10/.keep" "b/05\345\215\240\345\277\227\346\226\260/10.10/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From b7982630df43b48b733db0aa564613a5d8d6af29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A0=E5=BF=97=E6=96=B0?= <1759998065@qq.com> Date: Tue, 11 Oct 2022 15:49:09 +0000 Subject: [PATCH 2/2] =?UTF-8?q?05=E5=8D=A0=E5=BF=97=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 占志新 <1759998065@qq.com> --- ...1\345\231\250\344\275\234\344\270\232.sql" | 54 +++++++++++++++++++ ...1\345\231\250\347\254\224\350\256\260.txt" | 21 ++++++++ 2 files changed, 75 insertions(+) create mode 100644 "05\345\215\240\345\277\227\346\226\260/10.10/\350\247\246\345\217\221\345\231\250\344\275\234\344\270\232.sql" create mode 100644 "05\345\215\240\345\277\227\346\226\260/10.10/\350\247\246\345\217\221\345\231\250\347\254\224\350\256\260.txt" diff --git "a/05\345\215\240\345\277\227\346\226\260/10.10/\350\247\246\345\217\221\345\231\250\344\275\234\344\270\232.sql" "b/05\345\215\240\345\277\227\346\226\260/10.10/\350\247\246\345\217\221\345\231\250\344\275\234\344\270\232.sql" new file mode 100644 index 0000000..e81121b --- /dev/null +++ "b/05\345\215\240\345\277\227\346\226\260/10.10/\350\247\246\345\217\221\345\231\250\344\275\234\344\270\232.sql" @@ -0,0 +1,54 @@ +-1вűԱԱʱ򣬸ԱIJűڲűҲ +--ԶӲϢΪ"²" +create trigger tri_AddPeople +on People for insert +as +declare @DepartmentId int +select @DepartmentId = DepartmentId from People +if not exists(select * from Department where DepartmentId = @DepartmentId) + insert into Department values(@DepartmentId,'²') + +insert into People values('007','','',15000) +go +--2ʵ֣ɾһŵʱ򽫲Աȫɾ +create trigger tri_Delete +on Department for delete +as +delete People where DepartmentId = (select DepartmentId from deleted) +go +delete Department where DepartmentId = 1 +go + +--4޸һű֮󣬽òԱIJűͬ޸ +create trigger tri_UpdateDepartment +on Department for update +as +declare @Old_DepartmentId int ,@New_DepartmentId int +select @Old_DepartmentId = (select DepartmentId from deleted) +select @New_DepartmentId = (select DepartmentId from inserted) +update People set DepartmentId = @New_DepartmentId where DepartmentId = @Old_DepartmentId + +update Department set DepartmentId = '003' where DepartmentId = '001' + +--ܾ-1000 гԱ-2000 +--ô洢̺α +declare cur_PayCut cursor scroll +for select DepartmentId from Department + +go +create proc proc_PayCut +as +begin + open cur_PayCut + declare @DepartmentId int,@money money = 1000 + fetch first from cur_PayCut into @DepartmentId + while(@@FETCH_STATUS = 0) + begin + update People set PeopleSalary -= @money where DepartmentId = @DepartmentId + set @money += 1000 + fetch next from cur_PayCut into @DepartmentId + end + close cur_PayCut +end + +exec proc_PayCut \ No newline at end of file diff --git "a/05\345\215\240\345\277\227\346\226\260/10.10/\350\247\246\345\217\221\345\231\250\347\254\224\350\256\260.txt" "b/05\345\215\240\345\277\227\346\226\260/10.10/\350\247\246\345\217\221\345\231\250\347\254\224\350\256\260.txt" new file mode 100644 index 0000000..7d393bd --- /dev/null +++ "b/05\345\215\240\345\277\227\346\226\260/10.10/\350\247\246\345\217\221\345\231\250\347\254\224\350\256\260.txt" @@ -0,0 +1,21 @@ +触发器(trigger) 什么是触发器: insert --》 一系列随着插入操作自动执行的sql语句集合 触发器( trigger )是作为对数据库修改的连带效果而由系统自动执行的一条语句。它是一种特殊的存储过程。也是一个事务(可以回滚)。为了定义一个触发器,我们必须: + +指明什么时候执行触发器。这被拆分为引起触发器被检测的一个事件和触发器继续执行所必须满足的一个条件。 指明当触发器执行时所采取的动作。 触发器分类 分为 DML触发器 + +DML触发器 : 增删改 after触发器: 增删改 之后执行的 after触发器主要用于:insert,update,delete + +instead of 触发器 : 增删改 之前检查的 创建触发器的语句 CREATE TRIGGER <触发器名称> + +ON table_name FOR [DELETE, INSERT, UPDATE] + +AS + +T-SQL语句 + +GO --WITH ENCRYPTION表示加密触发器定义的SQL文本 + +--DELETE, INSERT, UPDATE指定触发器的类型 总结 1.插入操作(Insert) inserted 表有数据,deleted 表无数据 + +2.删除操作(Delete) inserted 表无数据,deleted 表有数据 + +3.更新操作(Update) inserted 表有数据(新数据),deleted 表有数据(旧数据) \ No newline at end of file -- Gitee