diff --git "a/12 \351\231\210\345\215\216\344\274\237/2022-10-10 \347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260/SQLQuery13.sql" "b/12 \351\231\210\345\215\216\344\274\237/2022-10-10 \347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260/SQLQuery13.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e9e063ad16bad6e3358768ffe0657de633a761be --- /dev/null +++ "b/12 \351\231\210\345\215\216\344\274\237/2022-10-10 \347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260/SQLQuery13.sql" @@ -0,0 +1,89 @@ +use master +--部门 +create table Department +( + DepartmentId varchar(10) primary key , --主键,自动增长 + DepartmentName nvarchar(50), --部门名称 +) +go +--人员信息 +create table People +( + PeopleId int primary key identity(1,1), --主键,自动增长 + DepartmentId varchar(10), --部门编号,外键,与部门表关联 + PeopleName nvarchar(20), --人员姓名 + PeopleSex nvarchar(2), --人员性别 + PeopleSalary money, --薪水 +) +go +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','财务部') +go +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) +insert into People(DepartmentId,PeopleName,PeopleSex,PeopleSalary) +values('003','赵云','男',5000) +insert into People(DepartmentId,PeopleName,PeopleSex,PeopleSalary) +values('004','马超','男',7000) + +drop table Department +drop table People +select * from Department +select * from People +--(1)假设有部门表和员工表,在添加员工的时候,该员工的部门编号如果在部门表中找不到 +--,则自动添加部门信息,部门名称为"新部门" + select * from People + + create trigger tri_insert + on People for insert + as + declare @new varchar(20) + select @new = PeopleId from inserted + if not exists (select * from Department where DepartmentId = @new) + begin + insert into Department values(@new,'新部门') + end; + + insert into People values ('007','说的是','男',5000); + insert into People values ('008','大蛇丸','男',7000); + +--(2)触发器实现,删除一个部门的时候将部门下所有员工全部删除 + drop trigger tri_delete + + create trigger tri_delete + on Department for delete + as + delete from People where DepartmentId= (select DepartmentId from deleted) + + delete from Department where DepartmentId='004' + +--(4)修改一个部门编号之后,将该部门下所有员工的部门编号同步进行修改 + --有缺陷 + + drop trigger tri_update + select * from Department + select * from People + + create trigger tri_update + on Department for update + as + declare @oldid varchar(20), @newid varchar(20) + + select @oldid= DepartmentId from deleted + select @newid= DepartmentId from inserted + -- update Department set DepartmentId =@oldid where DepartmentId=@newid --没什么用 + print '旧:'+@oldid + char(9) + '新:'+@newid + + update Department set DepartmentId = '006' where DepartmentId='001' --有缺陷 + update People set DepartmentId = '006' where DepartmentId='001' --有缺陷 + diff --git "a/12 \351\231\210\345\215\216\344\274\237/2022-10-10 \347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260/\350\247\246\345\217\221\345\231\250.sql" "b/12 \351\231\210\345\215\216\344\274\237/2022-10-10 \347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260/\350\247\246\345\217\221\345\231\250.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a9b577f699a79271ebdf74cf101c1d54bdec3ed3 --- /dev/null +++ "b/12 \351\231\210\345\215\216\344\274\237/2022-10-10 \347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260/\350\247\246\345\217\221\345\231\250.sql" @@ -0,0 +1,62 @@ +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' + + +--创建触发器实现,当修改某员工工资后,输出该雇员的姓名,更新前工资和更新后工资 +--(测试数据:将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