diff --git "a/19\351\231\210\345\277\227\346\242\201/\344\275\234\344\270\232/2022-09-26\345\255\230\345\202\250\350\277\207\347\250\213.sql" "b/19\351\231\210\345\277\227\346\242\201/\344\275\234\344\270\232/2022-09-26\345\255\230\345\202\250\350\277\207\347\250\213.sql" new file mode 100644 index 0000000000000000000000000000000000000000..614d1504466db9771a02d813f59ac36a214713f1 --- /dev/null +++ "b/19\351\231\210\345\277\227\346\242\201/\344\275\234\344\270\232/2022-09-26\345\255\230\345\202\250\350\277\207\347\250\213.sql" @@ -0,0 +1,86 @@ +select * from AccountInfo + +--1. 定义存储过程实现查询出账户余额最低的银行卡账户信息,显示银行卡号,姓名,账户余额 + +go +create proc proc_min +as +select top 1 cardno,RealName,CardMoney from AccountInfo a join BankCard b on a.AccountId=b.AccountId order by CardMoney +go + +exec proc_min +--2. 模拟银行卡存钱操作,传入银行卡号,存钱金额,实现存钱操作 + +go +create proc proc_inbank +@no varchar(20),@money money +as +update BankCard set CardMoney=CardMoney+@money where CardNo=@no +insert CardExchange values(@no,@money,0,GETDATE()) +go + +exec proc_inbank '6225125478544587',500 +--3. 模拟银行卡取钱操作,传入银行卡号,取钱金额,实现取钱操作,取钱成功,返回1,取钱失败返回-1 + +go +create proc proc_outbank +@no varchar(20),@money money +as +declare @moneys money +select @moneys=CardMoney from BankCard where CardNo=@no +if(@money> @moneys) +begin +return -1 +end +else +begin +update BankCard set CardMoney=CardMoney-@money where CardNo=@no +insert CardExchange values(@no,0,@money,GETDATE()) +return 1 +end +go + +declare @i int +exec @i= proc_outbank '6225125478544587',500 +print @i +--4. 查询出某时间段的银行存取款信息以及存款总金额,取款总金额, --传入开始时间,结束时间,显示存取款交易信息的同时,返回存款总金额,取款总金额。 + +go +create proc proc_moneyinbank +@bgindate date,@enddate date,@inbank money output,@outbank money output +as +select * from CardExchange where ExchangeTime between @bgindate and @enddate +select @inbank=sum(MoneyInBank),@outbank=sum(MoneyOutBank) from CardExchange where ExchangeTime between @bgindate and @enddate +go + +declare @inbank money ,@outbank money ,@x varchar(20) +exec proc_moneyinbank '2022-09-01','2022-09-30',@inbank output,@outbank output +print '存款金额'+cast(@inbank as varchar(20))+'取款金额'+cast(@outbank as varchar(20)) +--5. **密码升级**,传入用户名和密码,如果用户名密码正确,并且密码长度<8,自动升级成8位密码 +go +create proc proc_pass +@no varchar(20),@pass varchar(20) +as +declare @i varchar(20) +select @i=a.AccountId from AccountInfo a join BankCard b on a.AccountId=b.AccountId where AccountCode=@no and CardPwd=@pass +if @i=null + begin + raiserror('账号或密码错误',16,1) + end +else + begin + if len(@pass)<8 + begin + declare @xx varchar(20) + select @xx=floor(rand()*10) + while len(@pass)<8 + begin + set @pass=@pass+@xx + end + update BankCard set CardPwd=@pass where AccountId=@i + end + end + +go + +exec proc_pass '420107198905064135','123456' \ No newline at end of file diff --git "a/19\351\231\210\345\277\227\346\242\201/\344\275\234\344\270\232/2022-09-27\350\247\246\345\217\221\345\231\250.sql" "b/19\351\231\210\345\277\227\346\242\201/\344\275\234\344\270\232/2022-09-27\350\247\246\345\217\221\345\231\250.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d1c0707f8c7883c9a56121c3a756e23e010f8d50 --- /dev/null +++ "b/19\351\231\210\345\277\227\346\242\201/\344\275\234\344\270\232/2022-09-27\350\247\246\345\217\221\345\231\250.sql" @@ -0,0 +1,79 @@ +select * from Department select * from People --(1)假设有部门表和员工表,在添加员工的时候,该员工的部门编号如果在部门表中找不到,则自动添加部门信息,部门名称为"新部门"。 + +go +create trigger tr_departmentname +on People for insert +as +declare @id varchar(20) +select @id=(select DepartmentId from inserted) +if exists(select * from Department where DepartmentId=@id) + begin + print '部门已经存在' + end + else + begin + insert Department values(@id,'新部门') + end +go +insert People values('007','赵云','男',10000) + +drop trigger tr_departmentname +go +--(2)触发器实现,删除一个部门的时候将部门下所有员工全部删除。 + +create trigger tr_deletename +on Department for delete +as +delete People where DepartmentId=(select DepartmentId from deleted) +go +delete Department where DepartmentName='市场部' +--(3)创建一个触发器,删除一个部门的时候 判断该部门下是否有员工,有则不删除,没有则删除。 + +--方法1 +go +create trigger tr_deletenames +on department after delete +as +declare @id varchar(20) +select @id=(select DepartmentId from deleted) +if exists(select * from People where DepartmentId=@id) + begin + print '不删除' + rollback TransAction + end +go + +drop trigger tr_deletenames + + + +--方法2 +go +create trigger tr_deletenss +on department instead of delete +as +if exists(select * from People where DepartmentId=(select DepartmentId from deleted)) + print '有员工,不删除' +else + delete Department where DepartmentId=(select DepartmentId from deleted) +go + +delete Department where DepartmentId='004' +--(4)修改一个部门编号之后,将该部门下所有员工的部门编号同步进行修改 先删除,后插入 + +go +create trigger tr_updateid +on Department after update +as +declare @id varchar(20) +declare @ids varchar(20) +select @ids=DepartmentId from deleted --删除之后 +select @id=Departmentid from inserted --更新 +update People set DepartmentId=@id where DepartmentId=@ids + +go + + +update Department set DepartmentId='005'where DepartmentName='总经办' + +drop trigger tr_updateid \ No newline at end of file diff --git "a/19\351\231\210\345\277\227\346\242\201/\347\254\224\350\256\260/2022-09-28\347\254\224\350\256\260-\350\247\246\345\217\221\345\231\2502.md" "b/19\351\231\210\345\277\227\346\242\201/\347\254\224\350\256\260/2022-09-28\347\254\224\350\256\260-\350\247\246\345\217\221\345\231\2502.md" new file mode 100644 index 0000000000000000000000000000000000000000..398c6392e3d61b97f3293da9601763d9d9947d27 --- /dev/null +++ "b/19\351\231\210\345\277\227\346\242\201/\347\254\224\350\256\260/2022-09-28\347\254\224\350\256\260-\350\247\246\345\217\221\345\231\2502.md" @@ -0,0 +1,59 @@ +### 瑙﹀彂鍣 + +#### 鍒涘缓瑙﹀彂鍣ㄦ牸寮 + +``` +go +create trigger tri_xx(瑙﹀彂鍣ㄥ悕绉) +on 琛ㄥ悕 after/instead of insert/update/delete +as + + begin + 璁剧疆鍙橀噺 + 鍒ゆ柇 + 澧炲垹鏀(鏌ヨ鏃朵娇鐢 from inserted/deleted) + end + + insert into People values('005','鏉庡洓','濂',1000) +``` + +#### after + +鍏堝畬鎴愯鍙ュ啀杩涜瑙﹀彂鍣 + +#### instead of + +璇彞涓嶆墽琛岋紝浣滀负鏉′欢寮曞彂瑙﹀彂鍣 + +#### 鍒犻櫎瑙﹀彂鍣 + +drop TRIGGER [IF EXISTS] [schema_name.]trigger_name + +#### 琛岃浆鍒 + +``` +MAX(CASE WEHN)鏂规硶 + +SELECT name as '濮撳悕', + MAX(CASE WHEN course = '璇枃' THEN score END) AS '璇枃', + MAX(CASE WHEN course = '鏁板' THEN score END) AS '鏁板', + MAX(CASE WHEN course = '鑻辫' THEN score END) AS '鑻辫', + MAX(CASE WHEN course = '鍘嗗彶' THEN score END) AS '鍘嗗彶' +FROM t_student +GROUP BY name; + + + +SUM(IF(鏉′欢,鍒楀,0)) + +SELECT name as '濮撳悕', + SUM(IF(course = '璇枃',score,0)) AS '璇枃', + SUM(IF(course = '鏁板',score,0)) AS '鏁板', + SUM(IF(course = '鑻辫',score,0)) AS '鑻辫', + SUM(IF(course = '鍘嗗彶',score,0)) AS '鍘嗗彶' +FROM t_student +GROUP BY name; +select * from 琛ㄥ悕 pivot (琛 for 鍒 in锛堛1銆戙2銆戯級) 琛ㄥ悕 +``` + +#### 鍒楄浆琛 \ No newline at end of file