From 967cb823e7a43226976d904d56a5af7f6842be05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=B6=9B?= <1986456126@qq.com> Date: Tue, 11 Oct 2022 04:29:55 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=BB=BA=202022-10-10=EF=BC=88?= =?UTF-8?q?=E7=AC=AC=E5=8D=81=E4=BA=8C=E6=AC=A1=E4=BD=9C=E4=B8=9A=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "09\346\235\216\346\266\233/2022-10-10\357\274\210\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/.keep" diff --git "a/09\346\235\216\346\266\233/2022-10-10\357\274\210\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/.keep" "b/09\346\235\216\346\266\233/2022-10-10\357\274\210\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 0ce8e8048c24abc0d8583731cc78588fe9682924 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=B6=9B?= <1986456126@qq.com> Date: Tue, 11 Oct 2022 04:30:40 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9D=8E=E6=B6=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 鏉庢稕 <1986456126@qq.com> --- .../\344\275\234\344\270\232.sql" | 38 +++++++++++++++++ .../\347\254\224\350\256\260.txt" | 41 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 "09\346\235\216\346\266\233/2022-10-10\357\274\210\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/\344\275\234\344\270\232.sql" create mode 100644 "09\346\235\216\346\266\233/2022-10-10\357\274\210\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/\347\254\224\350\256\260.txt" diff --git "a/09\346\235\216\346\266\233/2022-10-10\357\274\210\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/\344\275\234\344\270\232.sql" "b/09\346\235\216\346\266\233/2022-10-10\357\274\210\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/\344\275\234\344\270\232.sql" new file mode 100644 index 0000000..2451c65 --- /dev/null +++ "b/09\346\235\216\346\266\233/2022-10-10\357\274\210\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/\344\275\234\344\270\232.sql" @@ -0,0 +1,38 @@ +select * from people +select * from Department +--(1)假设有部门表和员工表,在添加员工的时候,该员工的部门编号如果在部门表中找不到,则自动添加部门信息,部门名称为"新部门"。 +go +create trigger tri_GetNewDepartment +on people for insert +as +declare @id varchar(20) +set @id = (select DepartmentId from inserted) +if not exists(select * from Department where @id=DepartmentId) + +begin +insert into Department(DepartmentId,DepartmentName) values(@id,'新部门') +end +insert into people values('007','孙策','男',6500) + +drop trigger tri_GetNewDepartment; +--(2)触发器实现,删除一个部门的时候将部门下所有员工全部删除。 +create trigger tri_delete +on Department for delete +as + --删员工 + delete from People where DepartmentId = (select DepartmentId from deleted) --updated + +delete from Department where DepartmentId = '005'; + +drop trigger tri_delete; +--(4)修改一个部门编号之后,将该部门下所有员工的部门编号同步进行修改 +create trigger tri_GetUpdate +on Department for update +as +declare @newid varchar(20) = (select DepartmentId from inserted) +declare @oldid varchar(20) = (select DepartmentId from deleted) + +update people set DepartmentId = @newid where @oldid=DepartmentId +; +update Department set DepartmentId = '005' where DepartmentId='007'; +drop trigger tri_GetUpdate; \ No newline at end of file diff --git "a/09\346\235\216\346\266\233/2022-10-10\357\274\210\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/\347\254\224\350\256\260.txt" "b/09\346\235\216\346\266\233/2022-10-10\357\274\210\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/\347\254\224\350\256\260.txt" new file mode 100644 index 0000000..6a4ddf8 --- /dev/null +++ "b/09\346\235\216\346\266\233/2022-10-10\357\274\210\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/\347\254\224\350\256\260.txt" @@ -0,0 +1,41 @@ +## 瑙﹀彂鍣紙trigger锛 + +#### 浠涔堟槸瑙﹀彂鍣細 insert --銆 涓绯诲垪闅忕潃鎻掑叆鎿嶄綔鑷姩鎵ц鐨剆ql璇彞闆嗗悎 + + **瑙﹀彂鍣**锛 trigger 锛夋槸浣滀负**瀵规暟鎹簱淇敼鐨勮繛甯︽晥鏋**鑰**鐢辩郴缁熻嚜鍔ㄦ墽琛**鐨勪竴鏉¤鍙ャ傚畠鏄竴绉嶇壒娈婄殑**瀛樺偍杩囩▼**銆備篃鏄竴涓**浜嬪姟**锛堝彲浠ュ洖婊氾級銆備负浜嗗畾涔変竴涓猍瑙﹀彂鍣╙(https://so.csdn.net/so/search?q=瑙﹀彂鍣&spm=1001.2101.3001.7020)锛屾垜浠繀椤伙細 + +- 鎸囨槑**浠涔堟椂鍊欐墽琛岃Е鍙戝櫒**銆傝繖琚媶鍒嗕负**寮曡捣瑙﹀彂鍣ㄨ妫娴嬬殑涓涓猏*浜嬩欢\***鍜**瑙﹀彂鍣ㄧ户缁墽琛屾墍蹇呴』婊¤冻鐨勪竴涓潯浠**銆 +- 鎸囨槑**褰撹Е鍙戝櫒鎵ц鏃舵墍閲囧彇鐨勫姩浣**銆 + + + +#### 瑙﹀彂鍣ㄥ垎绫 + +鍒嗕负 DML瑙﹀彂鍣 + +#### DML瑙﹀彂鍣 锛 澧炲垹鏀 + +##### after瑙﹀彂鍣細 澧炲垹鏀 涔嬪悗鎵ц鐨 + +after瑙﹀彂鍣ㄤ富瑕佺敤浜庯細insert锛寀pdate锛宒elete + +##### instead of 瑙﹀彂鍣 锛 澧炲垹鏀 涔嬪墠妫鏌ョ殑 + + + +#### 鍒涘缓瑙﹀彂鍣ㄧ殑璇彞 + +```sql +CREATE TRIGGER <瑙﹀彂鍣ㄥ悕绉> + +ON table_name +FOR [DELETE, INSERT, UPDATE] + +AS + + T-SQL璇彞 + +GO +--WITH ENCRYPTION琛ㄧず鍔犲瘑瑙﹀彂鍣ㄥ畾涔夌殑SQL鏂囨湰 + +--DELETE, INSERT, UPDATE鎸囧畾瑙﹀彂鍣ㄧ殑绫诲瀷 \ No newline at end of file -- Gitee