From 42a0bb93010095ed2635fa22ee0f61546256b4b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE?= <3032059138@qq.com> Date: Tue, 11 Oct 2022 11:36:34 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 袁贵森 <3032059138@qq.com> --- ...\345\231\250\344\275\234\344\270\232s.sql" | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 "28\350\242\201\350\264\265\346\243\256/\344\275\234\344\270\232/2022-1010\350\247\246\345\217\221\345\231\250\344\275\234\344\270\232s.sql" diff --git "a/28\350\242\201\350\264\265\346\243\256/\344\275\234\344\270\232/2022-1010\350\247\246\345\217\221\345\231\250\344\275\234\344\270\232s.sql" "b/28\350\242\201\350\264\265\346\243\256/\344\275\234\344\270\232/2022-1010\350\247\246\345\217\221\345\231\250\344\275\234\344\270\232s.sql" new file mode 100644 index 0000000..a2fea51 --- /dev/null +++ "b/28\350\242\201\350\264\265\346\243\256/\344\275\234\344\270\232/2022-1010\350\247\246\345\217\221\345\231\250\344\275\234\344\270\232s.sql" @@ -0,0 +1,75 @@ +-- +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вűԱԱʱ򣬸ԱIJűڲűҲԶӲϢΪ"²" +go +create trigger yuangong +on Department for insert +as + if exists(select DepartmentId from inserted) + print 'ûë' + else + begin + select DepartmentId,DepartmentName from inserted + end +go +insert into People (DepartmentId,PeopleName,PeopleSex,PeopleSalary) values (006,'','Ů',20000) +insert into Department(DepartmentId,DepartmentName) values(006,'²') + +--drop trigger yuangong + +--2ʵ֣ɾһŵʱ򽫲Աȫɾ +go +create trigger bumen +on department for delete +as + delete from People where DepartmentId=(select DepartmentId from deleted ) +go +delete from Department where DepartmentId=006 + +--drop trigger bumen +--3޸һű֮󣬽òԱIJűͬ޸ +go +create trigger id +on department for update +as + declare @old varchar(20) =(select Departmentid from deleted) + declare @new varchar(20) =(select Departmentid from inserted) + update people set departmentid=@new where Departmentid=@old + +go +update Department set DepartmentId='000' where DepartmentId='001' + +--drop trigger id + -- Gitee From c0a1bbdc15aaaa4ffbd8e6016b53ccebc7b9e2a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE?= <3032059138@qq.com> Date: Tue, 11 Oct 2022 11:36:48 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 袁贵森 <3032059138@qq.com> --- ...1\345\231\250\347\254\224\350\256\260.txt" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 "28\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260/2022-1010\350\247\246\345\217\221\345\231\250\347\254\224\350\256\260.txt" diff --git "a/28\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260/2022-1010\350\247\246\345\217\221\345\231\250\347\254\224\350\256\260.txt" "b/28\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260/2022-1010\350\247\246\345\217\221\345\231\250\347\254\224\350\256\260.txt" new file mode 100644 index 0000000..aab757e --- /dev/null +++ "b/28\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260/2022-1010\350\247\246\345\217\221\345\231\250\347\254\224\350\256\260.txt" @@ -0,0 +1,28 @@ +***触发器的优点 + 1.触发器是自动的。当对表中的数据做了任何修改之后立即被激活。 + 2.触发器可以通过数据库中的相关表进行层叠修改。 + 3.触发器可以强制限制。这些限制比用CHECK约束所定义的更复杂。与CHECK约束不同的是,触发器可以引用其他表中的列。 + +***触发器的作用 + 触发器的主要作用就是其能够实现由主键和外键所不能保证的复杂参照完整性和数据的一致性,它能够对数据库中的相关表进行级联修改 +提高比CHECK约束更复杂的的数据完整性,并自定义错误消息。 + +***触发器类型 +insert触发器:向表中插入数据时被触发; +delete触发器:从表中删除数据时被触发; +update触发器:修改表中数据时被触发。 + +**触发器创建语句: +CREATE TRIGGER trigger_name + +ON table_name + +[WITH ENCRYPTION] + +FOR [DELETE, INSERT, UPDATE] + +AS + + T-SQL语句 + +GO \ No newline at end of file -- Gitee