From cf9f824e6480224dda3ab2fae957874406f5c13d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80=E4=BB=81?= <1579614208@qq.com> Date: Tue, 11 Oct 2022 14:25:53 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E5=BC=A0=E8=80=80=E4=BB=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张耀仁 <1579614208@qq.com> --- .../1010\344\275\234\344\270\232.sql" | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 "40\345\274\240\350\200\200\344\273\201/\344\275\234\344\270\232/1010\344\275\234\344\270\232.sql" diff --git "a/40\345\274\240\350\200\200\344\273\201/\344\275\234\344\270\232/1010\344\275\234\344\270\232.sql" "b/40\345\274\240\350\200\200\344\273\201/\344\275\234\344\270\232/1010\344\275\234\344\270\232.sql" new file mode 100644 index 0000000..b1bdffe --- /dev/null +++ "b/40\345\274\240\350\200\200\344\273\201/\344\275\234\344\270\232/1010\344\275\234\344\270\232.sql" @@ -0,0 +1,127 @@ +-- +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 + +--(trigger) +--űһ('005','') ֮ --- >Զִе ²벿ŵidͲơ +go +--after +create trigger tri_PrintDepartmentName +on Department for insert +as + + declare @name varchar(20), @id varchar(20) + --ҵողϢ inserted:ӰеĽ + select @name = DepartmentName, @id=DepartmentId from inserted + print @name + char(9) + @id +go + +insert into Department values('006','Բ') +update Department set DepartmentName='' where DepartmentId='006' + +--drop trigger tri_PrintDepartmentName + + +--һԱ ԱĻϢ +create trigger tri_CheckSalary +on People for insert +as + declare @salary money + select @salary = PeopleSalary from inserted + if(@salary>3000) + begin + update People set PeopleSalary=3000 where PeopleId = (select PeopleId from inserted) + end + +insert into People(DepartmentId,PeopleName,PeopleSalary) values('005','',9000) + +--after: delete +--ʵ֣ɾһʱгòµԱһɾ(ű) +create trigger tri_DeletePeopleByDepartment +on Department for delete +as + --ɾԱ + delete from People where DepartmentId = (select DepartmentId from deleted) --updated + +delete from Department where DepartmentId = '005' + + +--ʵ֣޸ijԱʺùԱǰʺ͸º +--(:001Ĺʼ2000) + +create trigger tri_PrintSalary +on People for update +as + declare @oldmon varchar(20), @newmon varchar(20) + --ϵĽ浽deleted + select @oldmon = PeopleSalary from deleted + --µĽ浽inserted + select @newmon = peopleSalary from inserted + print 'ɵĹ:'+@oldmon + char(9) + 'µĹ:'+@newmon + + +update People set PeopleSalary -= 2000 where PeopleId = 001 + +-----ҵ +select*from Department +select*from People +--1вűԱԱʱ򣬸ԱIJűڲűҲ +--ԶӲϢΪ"²" +create trigger a_insert on People after insert +as +if not exists (select *from Department where DepartmentId=(select DepartmentId from inserted)) +begin +insert into Department(DepartmentId,DepartmentName) +values((select DepartmentId from inserted),'²') +end +go +-- +insert into People(DepartmentId,PeopleName,PeopleSex,PeopleSalary) +values('003','','',540) +DELETE FROM People WHERE PeopleName='' +--2ʵ֣ɾһŵʱ򽫲Աȫɾ +create trigger a_delete on People after delete +as +delete from People where DepartmentId=(select DepartmentId from deleted) +go +-- +delete from Department WHERE DepartmentId='006' +--4޸һű֮󣬽òԱIJűͬ޸ +create trigger a_update on Department after update +as +update People set DepartmentId=(select DepartmentId from inserted) +where DepartmentId=(select DepartmentId FROM deleted) +GO +-- +update Department set DepartmentId='005'where DepartmentId='001' -- Gitee From 2c1563858b465d93409f2ce5b4d50d51f1715062 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80=E4=BB=81?= <1579614208@qq.com> Date: Tue, 11 Oct 2022 14:26:16 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E5=BC=A0=E8=80=80=E4=BB=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张耀仁 <1579614208@qq.com> --- .../1010\347\254\224\350\256\260.txt" | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 "40\345\274\240\350\200\200\344\273\201/\347\254\224\350\256\260/1010\347\254\224\350\256\260.txt" diff --git "a/40\345\274\240\350\200\200\344\273\201/\347\254\224\350\256\260/1010\347\254\224\350\256\260.txt" "b/40\345\274\240\350\200\200\344\273\201/\347\254\224\350\256\260/1010\347\254\224\350\256\260.txt" new file mode 100644 index 0000000..e109062 --- /dev/null +++ "b/40\345\274\240\350\200\200\344\273\201/\347\254\224\350\256\260/1010\347\254\224\350\256\260.txt" @@ -0,0 +1,45 @@ +存储过程的优点: + +模块化程序设计 +提高执行速度:预编译 +降低网络通信量: +保证系统的安全性: +练习:创建存储过程查询姓黄的同学的学生信息 '黄%' + +触发器(trigger) +什么是触发器: insert --》 一系列随着插入操作自动执行的sql语句集合 +触发器( trigger )是作为对数据库修改的连带效果而由系统自动执行的一条语句。它是一种特殊的存储过程。也是一个事务(可以回滚)。为了定义一个触发器,我们必须: + +指明什么时候执行触发器。这被拆分为引起触发器被检测的一个*事件*和触发器继续执行所必须满足的一个条件。 +指明当触发器执行时所采取的动作。 +触发器分类 +分为 DML触发器 + +DML触发器 : 增删改 +after触发器: 增删改 之后执行的 +after触发器主要用于:insert,update,delete + +instead of 触发器 : 增删改 之前检查的 +创建触发器的语句 +CREATE TRIGGER <触发器名称> + +ON table_name +FOR [DELETE, INSERT, UPDATE] + +AS + + T-SQL语句 + +GO +--WITH ENCRYPTION表示加密触发器定义的SQL文本 + +--DELETE, INSERT, UPDATE指定触发器的类型 + + +总结 +1.插入操作(Insert) ****inserted 表*有数据,*deleted 表****无数据 + +2.删除操作(Delete) ****inserted 表*无数据,*deleted 表****有数据 + +3.更新操作(Update) ****inserted 表*有数据(新数据),*deleted 表****有数据(旧数据) + -- Gitee