From 93dc64e6f0c6ee48d233a661b6d0b586f683140f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=81=E7=AB=A0=E5=BD=AC?= <2629351295@qq.com> Date: Wed, 28 Sep 2022 03:46:12 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 翁章彬 <2629351295@qq.com> --- ...1\345\231\250 \344\275\234\344\270\232.md" | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 "01\347\277\201\347\253\240\345\275\254/\344\275\234\344\270\232/2022-09-27 \350\247\246\345\217\221\345\231\250 \344\275\234\344\270\232.md" diff --git "a/01\347\277\201\347\253\240\345\275\254/\344\275\234\344\270\232/2022-09-27 \350\247\246\345\217\221\345\231\250 \344\275\234\344\270\232.md" "b/01\347\277\201\347\253\240\345\275\254/\344\275\234\344\270\232/2022-09-27 \350\247\246\345\217\221\345\231\250 \344\275\234\344\270\232.md" new file mode 100644 index 0000000..dbe79a4 --- /dev/null +++ "b/01\347\277\201\347\253\240\345\275\254/\344\275\234\344\270\232/2022-09-27 \350\247\246\345\217\221\345\231\250 \344\275\234\344\270\232.md" @@ -0,0 +1,121 @@ +### --(1)假设有部门表和员工表,在添加员工的时候,该员工的部门编号如果在部门表中找不到,则自动添加部门信息,部门名称为"新部门"。 + +```sql +go +create trigger t1 +on people after insert +as +begin +if (select DepartmentId from inserted)not in(select DepartmentId from Department) +begin + insert into Department values((select DepartmentId from inserted),'新部门') +end +end +go +insert into People values('005','猴子','男',2000) +drop trigger t2 +``` + + + +### --(2)触发器实现,删除一个部门的时候将部门下所有员工全部删除。 + +```sql +go +create trigger t2 +on Department after delete +as +begin +delete People where DepartmentId = (select DepartmentId from deleted) +end +go +delete Department where DepartmentId ='005' +``` + + + +### --(3)创建一个触发器,删除一个部门的时候判断该部门下是否有员工,有则不删除,没有则删除。 + +```sql +go +create trigger t3 +on Department instead of delete +as +begin +if (select count(*) from People where DepartmentId = (select DepartmentId from deleted)) = 0 +begin + delete Department where DepartmentId =(select DepartmentId from deleted) +end +end +go +delete Department where DepartmentId ='006' +insert into Department values('006','新新部门') +``` + + + +### --(4)修改一个部门编号之后,将该部门下所有员工的部门编号同步进行修改 + +```sql +go +create trigger t4 +on Department instead of update +as +begin + +update People set DepartmentId = (select DepartmentId from inserted) +where DepartmentId = (select DepartmentId from Department where DepartmentName = '新部门') + +update Department set DepartmentId =(select DepartmentId from inserted) +where DepartmentName = '新部门' + +end +go +update Department set DepartmentId ='007' where DepartmentName = '新部门' +select * from Department +select * from People +drop trigger t1 +drop trigger t2 +drop trigger t3 +drop trigger t4 +``` + + + +### 数据库 + +```sql +--部门 +create table Department +( + DepartmentId varchar(10) primary key , --主键,自动增长 + DepartmentName nvarchar(50), --部门名称 +) +--人员信息 +create table People +( + PeopleId int primary key identity(1,1), --主键,自动增长 + DepartmentId varchar(10), --部门编号,外键,与部门表关联 + PeopleName nvarchar(20), --人员姓名 + PeopleSex nvarchar(2), --人员性别 + PeopleSalary money, --薪水 +) +insert into Department(DepartmentId,DepartmentName) +values('001','总经办') +insert into Department(DepartmentId,DepartmentName) +values('002','市场部') +insert into Department(DepartmentId,DepartmentName) +values('003','人事部') +insert into Department(DepartmentId,DepartmentName) +values('004','财务部') + +insert into People(DepartmentId,PeopleName,PeopleSex,PeopleSalary) +values('001','刘备','男',8000) +insert into People(DepartmentId,PeopleName,PeopleSex,PeopleSalary) +values('001','关羽','男',5000) +insert into People(DepartmentId,PeopleName,PeopleSex,PeopleSalary) +values('002','张飞','男',3000) + +select * from Department +select * from People +``` \ No newline at end of file -- Gitee From 2e3cae2cf4b2d7893edb8215c8b402dd7224b581 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=81=E7=AB=A0=E5=BD=AC?= <2629351295@qq.com> Date: Wed, 28 Sep 2022 03:46:33 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 翁章彬 <2629351295@qq.com> --- ...7 \350\247\246\345\217\221\345\231\250.md" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 "01\347\277\201\347\253\240\345\275\254/\347\254\224\350\256\260/2022-09-27 \350\247\246\345\217\221\345\231\250.md" diff --git "a/01\347\277\201\347\253\240\345\275\254/\347\254\224\350\256\260/2022-09-27 \350\247\246\345\217\221\345\231\250.md" "b/01\347\277\201\347\253\240\345\275\254/\347\254\224\350\256\260/2022-09-27 \350\247\246\345\217\221\345\231\250.md" new file mode 100644 index 0000000..9871cec --- /dev/null +++ "b/01\347\277\201\347\253\240\345\275\254/\347\254\224\350\256\260/2022-09-27 \350\247\246\345\217\221\345\231\250.md" @@ -0,0 +1,49 @@ +# 触发器分类 + +分为 DML触发器和DDL触发器 + +## DML触发器 : 增删改 + +### after触发器: + +after触发器主要用于:insert,update,delete + +### instead of 触发器 + +# 触发器 + +## 创建 + +```sql +go +create trigger 触发器名称 +on 表名 <增/删/改> +语句 +go +<增/删/改>语句 +``` + +### 或 + +``` +go +create trigger 触发器名称 +on 表名 +[WITH ENCRYPTION] +for <增/删/改> +as +语句 +go +--WITH ENCRYPTION表示加密触发器定义的SQL文本 +``` + + + +## 删除 + +```sql +delete trigger 触发器名称 +``` + + + -- Gitee