From 2b14a812cc17ba02f5264183b52b33f1c00b8dd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E9=B9=8F?= <3460863727@qq.com> Date: Tue, 11 Oct 2022 23:26:34 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E5=8D=81=E4=B8=80=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" | 71 +++++++++++++++++++ ...21\345\231\250\347\254\224\350\256\260.md" | 41 +++++++++++ 2 files changed, 112 insertions(+) create mode 100644 "18\351\231\210\351\271\217/\344\275\234\344\270\232/\350\247\246\345\217\221\345\231\250.sql" create mode 100644 "18\351\231\210\351\271\217/\347\254\224\350\256\260/10-10\350\247\246\345\217\221\345\231\250\347\254\224\350\256\260.md" diff --git "a/18\351\231\210\351\271\217/\344\275\234\344\270\232/\350\247\246\345\217\221\345\231\250.sql" "b/18\351\231\210\351\271\217/\344\275\234\344\270\232/\350\247\246\345\217\221\345\231\250.sql" new file mode 100644 index 0000000..0d47851 --- /dev/null +++ "b/18\351\231\210\351\271\217/\344\275\234\344\270\232/\350\247\246\345\217\221\345\231\250.sql" @@ -0,0 +1,71 @@ +--部门 +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 + +--(1)假设有部门表和员工表,在添加员工的时候,该员工的部门编号如果在部门表中找不到, +--则自动添加部门信息,部门名称为"新部门"。 +go +create trigger xinbu +on people for insert +as +if not exists (select * from Department where DepartmentId=(select DepartmentId from inserted)) +insert Department values((select DepartmentId from inserted),'新部门') +go +insert People values ('006','赵云','男',5000) + +--(2)触发器实现,删除一个部门的时候将部门下所有员工全部删除。 +go +create trigger sanc +on people for delete +as +delete from people where DepartmentId =(select DepartmentId from deleted) +go + +delete from people where DepartmentId='006' + + + +--(4)修改一个部门编号之后,将该部门下所有员工的部门编号同步进行修改 +go +create trigger jjj +on Department for update +as +update People set DepartmentId=(select DepartmentId from inserted) + where DepartmentId=(select DepartmentId from deleted) + + + +update Department set DepartmentId='007' where DepartmentId='001' + + +drop trigger \ No newline at end of file diff --git "a/18\351\231\210\351\271\217/\347\254\224\350\256\260/10-10\350\247\246\345\217\221\345\231\250\347\254\224\350\256\260.md" "b/18\351\231\210\351\271\217/\347\254\224\350\256\260/10-10\350\247\246\345\217\221\345\231\250\347\254\224\350\256\260.md" new file mode 100644 index 0000000..84bff7b --- /dev/null +++ "b/18\351\231\210\351\271\217/\347\254\224\350\256\260/10-10\350\247\246\345\217\221\345\231\250\347\254\224\350\256\260.md" @@ -0,0 +1,41 @@ +### 浠涔堟槸瑙﹀彂鍣 + +瑙﹀彂鍣ㄥ氨鏄埆浜哄湪璺笂缁欎綘涓宸存帉浣犱細鎵撳洖鍘 + + + +### 瑙﹀彂鍣ㄥ垎涓 + +##### DML瑙﹀彂鍣 锛 澧炲垹鏀 + +##### after瑙﹀彂鍣細 澧炲垹鏀 涔嬪悗鎵ц鐨 + +### 鍒涘缓瑙﹀彂鍣ㄧ殑璇彞 + +``` +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