From e8ad2c6de5fd6923139374b1ade1bbee5131a03e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=88=B4=E4=BF=8A=E9=94=8B?= <95995809+zzzzzzz123000@users.noreply.github.com> Date: Tue, 11 Oct 2022 19:09:32 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E5=8D=81=E4=BA=8C=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...-\350\247\246\345\217\221\345\231\250.sql" | 55 +++++++++++++++++++ ...--\350\247\246\345\217\221\345\231\250.md" | 54 ++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 "25\346\210\264\344\277\212\351\224\213/\344\275\234\344\270\232/2022.10.10\344\275\234\344\270\232--\350\247\246\345\217\221\345\231\250.sql" create mode 100644 "25\346\210\264\344\277\212\351\224\213/\347\254\224\350\256\260/2022.10.10\347\254\224\350\256\260--\350\247\246\345\217\221\345\231\250.md" diff --git "a/25\346\210\264\344\277\212\351\224\213/\344\275\234\344\270\232/2022.10.10\344\275\234\344\270\232--\350\247\246\345\217\221\345\231\250.sql" "b/25\346\210\264\344\277\212\351\224\213/\344\275\234\344\270\232/2022.10.10\344\275\234\344\270\232--\350\247\246\345\217\221\345\231\250.sql" new file mode 100644 index 0000000..cf46e97 --- /dev/null +++ "b/25\346\210\264\344\277\212\351\224\213/\344\275\234\344\270\232/2022.10.10\344\275\234\344\270\232--\350\247\246\345\217\221\345\231\250.sql" @@ -0,0 +1,55 @@ +--(1)假设有部门表和员工表,在添加员工的时候,该员工的部门编号如果在部门表中找不到, +--则自动添加部门信息,部门名称为"新部门"。 +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)修改一个部门编号之后,将该部门下所有员工的部门编号同步进行修改 +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 + diff --git "a/25\346\210\264\344\277\212\351\224\213/\347\254\224\350\256\260/2022.10.10\347\254\224\350\256\260--\350\247\246\345\217\221\345\231\250.md" "b/25\346\210\264\344\277\212\351\224\213/\347\254\224\350\256\260/2022.10.10\347\254\224\350\256\260--\350\247\246\345\217\221\345\231\250.md" new file mode 100644 index 0000000..6054f06 --- /dev/null +++ "b/25\346\210\264\344\277\212\351\224\213/\347\254\224\350\256\260/2022.10.10\347\254\224\350\256\260--\350\247\246\345\217\221\345\231\250.md" @@ -0,0 +1,54 @@ +## 瑙﹀彂鍣紙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鎸囧畾瑙﹀彂鍣ㄧ殑绫诲瀷 +``` + +## 鎬荤粨 + + 1.鎻掑叆鎿嶄綔锛圛nsert锛 +***\*inserted 琛╘****鏈夋暟鎹紝***\*deleted 琛╘****鏃犳暟鎹 + +2.鍒犻櫎鎿嶄綔锛圖elete锛 +***\*inserted 琛╘****鏃犳暟鎹紝***\*deleted 琛╘****鏈夋暟鎹 + +3.鏇存柊鎿嶄綔锛圲pdate锛 +***\*inserted 琛╘****鏈夋暟鎹紙鏂版暟鎹級锛***\*deleted 琛╘****鏈夋暟鎹紙鏃ф暟鎹級 + -- Gitee