From 42a47475e3dfe134d2d336757e2ef7947db71235 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B0=B8=E6=BD=98?= <1924969174@qq.com> Date: Tue, 11 Oct 2022 10:13:30 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E5=88=98=E6=B0=B8=E6=BD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 刘永潘 <1924969174@qq.com> --- ...1\345\231\250\344\275\234\344\270\232.sql" | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 "16\345\210\230\346\260\270\346\275\230/\344\275\234\344\270\232/2022-10-10\350\247\246\345\217\221\345\231\250\344\275\234\344\270\232.sql" diff --git "a/16\345\210\230\346\260\270\346\275\230/\344\275\234\344\270\232/2022-10-10\350\247\246\345\217\221\345\231\250\344\275\234\344\270\232.sql" "b/16\345\210\230\346\260\270\346\275\230/\344\275\234\344\270\232/2022-10-10\350\247\246\345\217\221\345\231\250\344\275\234\344\270\232.sql" new file mode 100644 index 0000000..c426a52 --- /dev/null +++ "b/16\345\210\230\346\260\270\346\275\230/\344\275\234\344\270\232/2022-10-10\350\247\246\345\217\221\345\231\250\344\275\234\344\270\232.sql" @@ -0,0 +1,40 @@ +use master +select * from Department +select * from People +--1вűԱԱʱ򣬸ԱIJűڲűҲԶӲϢΪ"²" +--ɾ DROP TRIGGER tri_a +create trigger tri_a +on People for insert +as + declare @departmentid varchar(20) (select DepartmentId from inserted) + if not exists(select * from Department where DepartmentId=@departmentid) + begin + insert into Department values (@departmentid,'²') + end + +insert into People(DepartmentId,PeopleName,PeopleSex,PeopleSalary) values('005','','',9000) + +--2ʵ֣ɾһŵʱ򽫲Աȫɾ +create trigger tri_b +on Department for delete +as +delete from people where DepartmentId=(select DepartmentId from deleted) + +delete from Department where DepartmentId='005' + + +--3һɾһŵʱжϸòǷԱɾûɾ** + +--4޸һű֮󣬽òԱIJűͬ޸ +create trigger tri_d +on Department for update +as + declare @oldid varchar(20),@newid varchar(20) + select @oldid=DepartmentId from deleted + select @newid=DepartmentId from deleted + update people set DepartmentId=@newid where DepartmentId=@oldid + +update Department set DepartmentId='006' where DepartmentId='001' + + +DROP TRIGGER tri_d -- Gitee From 2fd76ed14beb7b654846634c47aeb809f54e04a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B0=B8=E6=BD=98?= <1924969174@qq.com> Date: Tue, 11 Oct 2022 10:13:55 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E5=88=98=E6=B0=B8=E6=BD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 刘永潘 <1924969174@qq.com> --- ...60\350\247\246\345\217\221\345\231\250.md" | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 "16\345\210\230\346\260\270\346\275\230/\347\254\224\350\256\260/2022-10-10\347\254\254\345\215\201\344\272\214\346\254\241\347\254\224\350\256\260\350\247\246\345\217\221\345\231\250.md" diff --git "a/16\345\210\230\346\260\270\346\275\230/\347\254\224\350\256\260/2022-10-10\347\254\254\345\215\201\344\272\214\346\254\241\347\254\224\350\256\260\350\247\246\345\217\221\345\231\250.md" "b/16\345\210\230\346\260\270\346\275\230/\347\254\224\350\256\260/2022-10-10\347\254\254\345\215\201\344\272\214\346\254\241\347\254\224\350\256\260\350\247\246\345\217\221\345\231\250.md" new file mode 100644 index 0000000..f43e8fd --- /dev/null +++ "b/16\345\210\230\346\260\270\346\275\230/\347\254\224\350\256\260/2022-10-10\347\254\254\345\215\201\344\272\214\346\254\241\347\254\224\350\256\260\350\247\246\345\217\221\345\231\250.md" @@ -0,0 +1,34 @@ +触发器(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