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 0000000000000000000000000000000000000000..a2fea51f32c1dbfbb2069cd7fbc149512716143e --- /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)假设有部门表和员工表,在添加员工的时候,该员工的部门编号如果在部门表中找不到,则自动添加部门信息,部门名称为"新部门"。 +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)修改一个部门编号之后,将该部门下所有员工的部门编号同步进行修改 +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 + 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 0000000000000000000000000000000000000000..aab757e8a300526dc1babbe677247cb58edcb309 --- /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.瑙﹀彂鍣ㄥ彲浠ュ己鍒堕檺鍒躲傝繖浜涢檺鍒舵瘮鐢–HECK绾︽潫鎵瀹氫箟鐨勬洿澶嶆潅銆備笌CHECK绾︽潫涓嶅悓鐨勬槸锛岃Е鍙戝櫒鍙互寮曠敤鍏朵粬琛ㄤ腑鐨勫垪銆 + +***瑙﹀彂鍣ㄧ殑浣滅敤 +鈥冭Е鍙戝櫒鐨勪富瑕佷綔鐢ㄥ氨鏄叾鑳藉瀹炵幇鐢变富閿拰澶栭敭鎵涓嶈兘淇濊瘉鐨勫鏉傚弬鐓у畬鏁存у拰鏁版嵁鐨勪竴鑷存э紝瀹冭兘澶熷鏁版嵁搴撲腑鐨勭浉鍏宠〃杩涜绾ц仈淇敼 +鎻愰珮姣擟HECK绾︽潫鏇村鏉傜殑鐨勬暟鎹畬鏁存э紝骞惰嚜瀹氫箟閿欒娑堟伅銆 + +***瑙﹀彂鍣ㄧ被鍨 +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