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 0000000000000000000000000000000000000000..c426a52dfcc7bff1eb6cc9a7f47307ed0bab483f --- /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)假设有部门表和员工表,在添加员工的时候,该员工的部门编号如果在部门表中找不到,则自动添加部门信息,部门名称为"新部门"。 +--这是删除触发器 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)修改一个部门编号之后,将该部门下所有员工的部门编号同步进行修改 +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 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 0000000000000000000000000000000000000000..f43e8fd9e82071fc0bfc3ca7c51f7bec6257e4a5 --- /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 --銆 涓绯诲垪闅忕潃鎻掑叆鎿嶄綔鑷姩鎵ц鐨剆ql璇彞闆嗗悎 + 瑙﹀彂鍣紙 trigger 锛夋槸浣滀负瀵规暟鎹簱淇敼鐨勮繛甯︽晥鏋滆岀敱绯荤粺鑷姩鎵ц鐨勪竴鏉¤鍙ャ傚畠鏄竴绉嶇壒娈婄殑瀛樺偍杩囩▼銆備篃鏄竴涓簨鍔★紙鍙互鍥炴粴锛夈備负浜嗗畾涔変竴涓紝鎴戜滑蹇呴』锛 +- 鎸囨槑浠涔堟椂鍊欐墽琛岃Е鍙戝櫒銆傝繖琚媶鍒嗕负寮曡捣瑙﹀彂鍣ㄨ妫娴嬬殑涓涓簨浠惰Е鍙戝櫒缁х画鎵ц鎵蹇呴』婊¤冻鐨勪竴涓潯浠躲 +- 鎸囨槑褰撹Е鍙戝櫒鎵ц鏃舵墍閲囧彇鐨勫姩浣. + + + 瑙﹀彂鍣ㄥ垎绫 +鍒嗕负 DML瑙﹀彂鍣 +DML瑙﹀彂鍣 锛 澧炲垹鏀 +after瑙﹀彂鍣細 澧炲垹鏀 涔嬪悗鎵ц鐨 +after瑙﹀彂鍣ㄤ富瑕佺敤浜庯細insert锛寀pdate锛宒elete +instead of 瑙﹀彂鍣 锛 澧炲垹鏀 涔嬪墠妫鏌ョ殑 + +鍒涘缓瑙﹀彂鍣ㄧ殑璇彞 +CREATE TRIGGER <瑙﹀彂鍣ㄥ悕绉> +ON table_name +FOR [DELETE, INSERT, UPDATE] +AS + T-SQL璇彞 +GO +--WITH ENCRYPTION琛ㄧず鍔犲瘑瑙﹀彂鍣ㄥ畾涔夌殑SQL鏂囨湰 +--DELETE, INSERT, UPDATE鎸囧畾瑙﹀彂鍣ㄧ殑绫诲瀷 + 鎬荤粨 + + 1.鎻掑叆鎿嶄綔锛圛nsert锛 +*\*inserted 琛╘*鏈夋暟鎹紝*\*deleted 琛╘*鏃犳暟鎹 + +2.鍒犻櫎鎿嶄綔锛圖elete锛 +*\*inserted 琛╘*鏃犳暟鎹紝*\*deleted 琛╘*鏈夋暟鎹 + +3.鏇存柊鎿嶄綔锛圲pdate锛 +*\*inserted 琛╘*鏈夋暟鎹紙鏂版暟鎹級锛*\*deleted 琛╘*鏈夋暟鎹紙鏃ф暟鎹級 \ No newline at end of file