From 1ded3dabf735701e643e4e485607d4cf5068118b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A9=B9=E6=BA=90=E9=93=A0?= <1905540517@qq.com> Date: Tue, 27 Sep 2022 00:29:21 +0800 Subject: [PATCH] zz --- .../8.30\346\237\245\350\257\242.md" | 60 ++++++++ .../9.19\344\272\213\345\212\241.md" | 44 ++++++ ....1\344\270\211\350\214\203\345\274\217.md" | 139 ++++++++++++++++++ .../9.21\345\207\275\346\225\260.md" | 97 ++++++++++++ .../9.5\345\217\230\351\207\217.md" | 101 +++++++++++++ ....7\345\255\220\346\237\245\350\257\242.md" | 59 ++++++++ .../9.22\346\255\273\351\224\201.md" | 82 +++++++++++ .../9.26\345\202\250\345\255\230.md" | 93 ++++++++++++ 8 files changed, 675 insertions(+) create mode 100644 "23\350\251\271\346\272\220\351\223\240/\344\275\234\344\270\232/8.30\346\237\245\350\257\242.md" create mode 100644 "23\350\251\271\346\272\220\351\223\240/\344\275\234\344\270\232/9.19\344\272\213\345\212\241.md" create mode 100644 "23\350\251\271\346\272\220\351\223\240/\344\275\234\344\270\232/9.1\344\270\211\350\214\203\345\274\217.md" create mode 100644 "23\350\251\271\346\272\220\351\223\240/\344\275\234\344\270\232/9.21\345\207\275\346\225\260.md" create mode 100644 "23\350\251\271\346\272\220\351\223\240/\344\275\234\344\270\232/9.5\345\217\230\351\207\217.md" create mode 100644 "23\350\251\271\346\272\220\351\223\240/\344\275\234\344\270\232/9.7\345\255\220\346\237\245\350\257\242.md" create mode 100644 "23\350\251\271\346\272\220\351\223\240/\347\254\224\350\256\260/9.22\346\255\273\351\224\201.md" create mode 100644 "23\350\251\271\346\272\220\351\223\240/\347\254\224\350\256\260/9.26\345\202\250\345\255\230.md" diff --git "a/23\350\251\271\346\272\220\351\223\240/\344\275\234\344\270\232/8.30\346\237\245\350\257\242.md" "b/23\350\251\271\346\272\220\351\223\240/\344\275\234\344\270\232/8.30\346\237\245\350\257\242.md" new file mode 100644 index 0000000..bf26695 --- /dev/null +++ "b/23\350\251\271\346\272\220\351\223\240/\344\275\234\344\270\232/8.30\346\237\245\350\257\242.md" @@ -0,0 +1,60 @@ +```` +--4. 查询公司员工所在城市(不需要重复数据) + +``` +select distinct PeopleAddress from People +``` + +--5. 假设工资普调10%,查询原始工资和调整后的工资,显示(姓名,性别,月薪,加薪后的月薪)(添加列查询)。 +``` +select top 10 percent * from People +``` + +--16. 查询出巨蟹 6.22--7.22 的员工信息 + +``` +select * from People where +(month(PeopleBirth) = 6 and DAY(PeopleBirth) >= 22) or +(month(PeopleBirth) = 7 and DAY(PeopleBirth) <= 22) +--17. 查询工资比赵云高的人 +select * from People where PeopleSalary >( +select PeopleSalary from People where PeopleName='赵云' +) +``` + +--18. 查询出和赵云在同一个城市的人 + +``` +select * from People where PeopleAddress in( +select PeopleAddress from People where PeopleName='赵云' +) +``` + +--19. 查询出生肖为鼠的人员信息 + +``` +select * from People where year(PeopleBirth) % 12 = 4 +``` + +--20. 查询所有员工信息,添加一列显示属相(鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪) + +``` +select *, +case year(PeopleBirth) % 12 + when 4 then '鼠' + when 5 then '牛' + when 6 then '虎' + when 7 then '兔' + when 8 then '龙' + when 9 then '蛇' + when 10 then '马' + when 11 then '羊' + when 0 then '猴' + when 1 then '鸡' + when 2 then '狗' + when 3 then '猪' + ELSE '' +end 生肖 +from People +``` +```` \ No newline at end of file diff --git "a/23\350\251\271\346\272\220\351\223\240/\344\275\234\344\270\232/9.19\344\272\213\345\212\241.md" "b/23\350\251\271\346\272\220\351\223\240/\344\275\234\344\270\232/9.19\344\272\213\345\212\241.md" new file mode 100644 index 0000000..1912718 --- /dev/null +++ "b/23\350\251\271\346\272\220\351\223\240/\344\275\234\344\270\232/9.19\344\272\213\345\212\241.md" @@ -0,0 +1,44 @@ +```` +--刘备向张飞转账1000元,(添加check约束,设置账户余额必须>=0) + +``` +alter table bankcard add constraint CK_money check(cardmoney>=0) + + +begin transaction +declare @money money=1000;--钱 +declare @qcarno varchar(20);--转账卡号 +declare @hcarno varchar(20);--转入卡号 +declare @errorsum int=0;--计数器 + +select @qcarno=CardNo from AccountInfo a join BankCard b on a.AccountId=b.AccountId +where RealName='刘备'--查找刘备的卡号 + + +update BankCard set CardMoney=CardMoney-@money where CardNo=@qcarno--刘备转账-1000 +set @errorsum+=@@error --报错的话计数器+1 + +insert CardExchange values(@qcarno,0,@money,GETDATE())--刘备转账信息插入交易信息表 + +select @hcarno=CardNo from AccountInfo a join BankCard b on a.AccountId=b.AccountId +where RealName='张飞'--查找张飞卡号 + +update BankCard set CardMoney=CardMoney+@money where CardNo=@hcarno--张飞余额+1000 +set @errorsum+=@@error --报错的话计数器+1 + +insert CardExchange values(@hcarno,@money,0,GETDATE())--张飞转账信息插入交易信息表 + +insert CardTransfer values(@qcarno,@hcarno,@money,GETDATE());--插入转账记入 + +if(@errorsum>0)--计数器大于0 +begin + print '转账失败' + rollback transaction--回滚事务 +end +else +begin + print '转账成功' + commit transaction --提交事务 +end +``` +```` \ No newline at end of file diff --git "a/23\350\251\271\346\272\220\351\223\240/\344\275\234\344\270\232/9.1\344\270\211\350\214\203\345\274\217.md" "b/23\350\251\271\346\272\220\351\223\240/\344\275\234\344\270\232/9.1\344\270\211\350\214\203\345\274\217.md" new file mode 100644 index 0000000..93a545a --- /dev/null +++ "b/23\350\251\271\346\272\220\351\223\240/\344\275\234\344\270\232/9.1\344\270\211\350\214\203\345\274\217.md" @@ -0,0 +1,139 @@ +```` +```sql +create database Rabobank +go +use Rabobank +go +create table [user](--账号信息表 + useNO int primary key identity(1,1),--用户id + useName nvarchar(20) ,--用户姓名 + usephone char(11) unique,--手机号 + usepcard int check(usepcard<=3),--拥有卡的数量 + useAge int check(useAge between 18 and 100),--用户年龄 + useSex varchar(10) check(useSex in ('男','女'))--用户性别 +) +go + +create table bankcard--银行卡表 +( + banCardNO char(16) primary key ,--银行卡号 + remaining decimal(12,2),--余额 + banpass varchar(30) check(len(banpass)>=8),--密码 + useNO int references [user](useNO)--用户id +) +go + +create table Transinfo(--交易信息表 + traNO int primary key identity(1,1),--交易id + traTime date ,--交易时间 + trastype varchar(20) check(trastype in ('存钱','取钱')), --交易类型 + trasmoney decimal(12,2),--交易金额 + banNO char(16) references bankcard(banCardNO)--交易卡号 +) +go + +create table [transfer](--转账表 + tranNO int primary key identity(1,1),--交易id + tranTime date ,--交易时间 + transmoney decimal(12,2),--交易金额 + banNO char(16) references bankcard(banCardNO),--交易卡号 + trancaed char(16),--接收账号 +) +go + +create table Ttype(--银行卡状态表 + Tno int primary key identity(1,1),--状态id + banNO char(16) references bankcard(banCardNO),--卡号 + tType varchar(20) check(tType in('正常','挂失','冻结','注销')) +) + +insert [user] values +('张三',17828902422,1,20,'女'), +('李四',18528944562,1,18,'男'), +('王五',15856212433,1,19,'男') +go + +insert bankcard values +(5896549536512458,200,12345678,1), +(3659854652135654,25000,12345678,2), +(2365984512369411,10000,12345678,3) +go + +create trigger tr1_Transinfo +on Transinfo +for insert +as + declare @banNO char(16) --交易卡号 + declare @trastype char(16)--交易类型 + declare @smoney decimal(12,2)--金额 + declare @smoneyh decimal(12,2)--更新后的金额 + select @banNO=banNO,@trastype=trastype,@smoney=trasmoney from Transinfo + if(@trastype='取钱') + begin + update bankcard set @smoneyh=remaining-@smoney where banCardNO=@banNO + if(@smoneyh>0) + begin + update bankcard set remaining=remaining-@smoney where banCardNO=@banNO + end + else + begin + print'交易失败' + rollback transaction + end + + end + else + begin + update bankcard set remaining=remaining+@smoney where banCardNO=@banNO + end +go + +create trigger tr2_transfer +on [transfer] +for insert +as + declare @banNO char(16) --交易卡号 + declare @smoney decimal(12,2)--金额 + declare @smoneyh decimal(12,2)--更新后的金额 + declare @trancaed char(16)--接收卡号 + select @banNO=banNO,@smoney=transmoney,@trancaed=trancaed from [transfer] + update bankcard set @smoneyh=remaining-@smoney where banCardNO=@banNO + + if(@smoneyh>0) + begin + update bankcard set remaining=remaining-@smoney where banCardNO=@banNO + + update bankcard set remaining=remaining+@smoney where banCardNO=@trancaed + end + else + begin + print'交易失败' + rollback transaction + end +go + +insert Transinfo values +(GETDATE(),'取钱',50,5896549536512458), +(GETDATE(),'取钱',5000,3659854652135654), +(GETDATE(),'存钱',500,2365984512369411) +go + + +insert [transfer] values +(GETDATE(),500,2365984512369411,5896549536512458) +go + + +insert Ttype values +(2365984512369411,'正常'), +(5896549536512458,'正常'), +(3659854652135654,'正常') + +select * from [user] +select * from bankcard +select * from Transinfo +select * from [transfer] +select * from Ttype + +``` +```` \ No newline at end of file diff --git "a/23\350\251\271\346\272\220\351\223\240/\344\275\234\344\270\232/9.21\345\207\275\346\225\260.md" "b/23\350\251\271\346\272\220\351\223\240/\344\275\234\344\270\232/9.21\345\207\275\346\225\260.md" new file mode 100644 index 0000000..231274d --- /dev/null +++ "b/23\350\251\271\346\272\220\351\223\240/\344\275\234\344\270\232/9.21\345\207\275\346\225\260.md" @@ -0,0 +1,97 @@ +```` +--(1)编写一个函数求该银行的金额总和 + +``` +go +create function moneysum() +returns table +as + return select sum(CardMoney) 金额总和 from BankCard +go + +select * from moneysum() +``` + +--(2)传入账户编号,返回账户真实姓名 + +``` +go +create function zname(@id int) +returns varchar(20) +as +begin + declare @name varchar(20) + select @name=RealName from AccountInfo where AccountId=@id +return @name +end +go +select dbo.zname(2) +``` + +--(3)传递开始时间和结束时间,返回交易记录(存钱取钱),交易记录中包含 真实姓名,卡号,存钱金额,取钱金额,交易时间。 + + + +``` +go +create function cards(@begindate varchar(20),@enddate varchar(20)) +returns table +as + +return select RealName,b.CardNo,MoneyInBank,MoneyOutBank,ExchangeTime from CardExchange c +join BankCard b on c.CardNo=b.CardNo +join AccountInfo a on b.AccountId=a.AccountId +where ExchangeTime > '2022-09-01 00:00:00' and ExchangeTime < '2022-09-30 23:59:59' +go + +select * from cards('2022-09-01','2022-09-30') + +drop function cards +``` + +--方案一(逻辑复杂,函数内容除了返回结果的sql语句还有其他内容,例如定义变量等): + +--(4)查询银行卡信息,将银行卡状态1,2,3,4分别转换为汉字“正常,挂失,冻结,注销”,根据银行卡余额显示银行卡等级 30万以下为“普通用户”,30万及以上为"VIP用户",分别显示卡号,身份证,姓名,余额,用户等级,银行卡状态。 + +--方案一:直接在sql语句中使用case when + +--方案二:将等级和状态用函数实现 + +``` +go +create function messag() +returns table +as +return select CardNo ,AccountCode ,RealName ,CardMoney ,CardState, +case +when CardState='1' then '正常' +when CardState='2' then '挂失' +when CardState='3' then '冻结' +when CardState='4' then '注销' +end 银行卡状态, +case +when CardMoney >=300000 then 'VIP用户' +when CardMoney <300000 then '普通用户' +end 用户等级 +from BankCard b +join AccountInfo a on b.AccountId=a.AccountId +go + +select * from messag() +``` + + + +--(5)编写函数,根据出生日期求年龄,年龄求实岁,例如: +-- 生日为2000-5-5,当前为2018-5-4,年龄为17岁 +-- 生日为2000-5-5,当前为2018-5-6,年龄为18岁 + +``` +go +create function births() +returns table +as + return select DATEDIFF(yy,empBirth,GETDATE()) as 岁数 from emp +go +``` +```` \ No newline at end of file diff --git "a/23\350\251\271\346\272\220\351\223\240/\344\275\234\344\270\232/9.5\345\217\230\351\207\217.md" "b/23\350\251\271\346\272\220\351\223\240/\344\275\234\344\270\232/9.5\345\217\230\351\207\217.md" new file mode 100644 index 0000000..a5accd6 --- /dev/null +++ "b/23\350\251\271\346\272\220\351\223\240/\344\275\234\344\270\232/9.5\345\217\230\351\207\217.md" @@ -0,0 +1,101 @@ +```` +--2. 练习:要求根据座位号找出张无忌的前后同学? + +``` +declare @seat int +select @seat=StuSeat from StuInfo where StuName='张无忌' +select StuName from StuInfo where StuSeat in (@seat+1,@seat-1) +``` + +--2.练习:查询年龄最大的学生信息 + +``` +declare @age int +select @age = max(StuAge) from StuInfo +select * from StuInfo where StuAge= @age +``` + +***** + +```sql +create database Projects + +use Projects + + +--工程表:工程号 工程名称 +create table Project( +pno varchar(20) primary key , +pname varchar(20) +) +go + +insert Project values +('A1','花园大厦'), +('A2','立交桥'), +('A3','临江饭店') + + + +--职务表:职务号 职务 小时工资 +create table functin( +fno int primary key identity(1,1), +fname varchar(20), +ftime int +) +go + +insert functin values +('工程师',65), +('技术员',60), +('工人',55) + + + +--职工表: 职工号 姓名 +create table staff( +sno int primary key identity(1001,1), +sname varchar(20), +fno int references functin(fno) +) +go +insert staff values +('齐光明',1), +('李思岐',2), +('鞠明亮',3), +('葛宇洪',2) + + + + + +--工时表:工时号 工程+职工号 工时 +create table manhour( +mno int primary key identity(1,1), +pno varchar(20) references Project(pno) , +sno int references staff(sno), +mtime int +) + +go + +insert manhour values +('A1',1001,13), +('A1',1002,16), +('A2',1001,15), +('A2',1003,17), +('A3',1002,18), +('A3',1004,14) + +select * from Project +select * from functin +select * from staff +select * from manhour + + +select p.pno,p.pname,s.sno,s.sname,fname,ftime,mtime from manhour m +left join Project p on m.pno=p.pno +left join staff s on s.sno=m.sno +join functin f on s.fno=f.fno +``` +```` \ No newline at end of file diff --git "a/23\350\251\271\346\272\220\351\223\240/\344\275\234\344\270\232/9.7\345\255\220\346\237\245\350\257\242.md" "b/23\350\251\271\346\272\220\351\223\240/\344\275\234\344\270\232/9.7\345\255\220\346\237\245\350\257\242.md" new file mode 100644 index 0000000..ec3bc0e --- /dev/null +++ "b/23\350\251\271\346\272\220\351\223\240/\344\275\234\344\270\232/9.7\345\255\220\346\237\245\350\257\242.md" @@ -0,0 +1,59 @@ +```` +--1. 关羽的银行卡号为"6225547858741263",查询出余额比关羽多的银行卡信息,显示卡号,身份证,姓名,余额。 + +``` +select CardNo,AccountCode,RealName,CardMoney from AccountInfo a +join BankCard b on a.AccountId=b.AccountId +where CardMoney >any (select CardMoney from BankCard where CardNo='6225547858741263') +``` + +--2. 从所有账户信息中查询出余额最高的交易明细(存钱取钱信息)。 + +``` +select * from CardExchange where MoneyInBank =(select max(MoneyInBank) from CardExchange) +and MoneyOutBank =(select max(MoneyOutBank) from CardExchange) +``` + +--3. 查询有取款记录的银行卡及账户信息,显示卡号,身份证,姓名,余额。 + +``` +select CardNo,AccountCode,RealName,CardMoney from AccountInfo a +join BankCard b on a.AccountId=b.AccountId +where CardNo in(select cardno from CardExchange where MoneyOutBank is not null) +``` + +--4. 查询出没有存款记录的银行卡及账户信息,显示卡号,身份证,姓名,余额。 + +``` +select CardNo,AccountCode,RealName,CardMoney from AccountInfo a +join BankCard b on a.AccountId=b.AccountId +where CardNo in(select cardno from CardExchange where MoneyInBank is null) +``` + +--5. 关羽的银行卡号为"6225547858741263",查询当天是否有收到转账。 + +``` +if exists(select * from CardTransfer where TransferTime=getdate() and CardNoOut='6225547858741263') +print '有收账' +else +print '无收账' +``` + +--6. 查询出交易次数(存款取款操作)最多的银行卡账户信息,显示:卡号,身份证,姓名,余额,交易数。 + +``` +select top 1 b.CardNo,AccountCode,RealName,CardMoney,交易次数 from AccountInfo a +join BankCard b on a.AccountId=b.AccountId +join (select CardNo,count(*) 交易次数 from CardExchange group by CardNo +) ta on b.CardNo=ta.CardNo +order by 交易次数 desc +``` + +--7. 查询出没有转账交易记录的银行卡账户信息,显示卡号,身份证,姓名,余额。 + +``` +select CardNo,AccountCode,RealName,CardMoney from AccountInfo a +join BankCard b on a.AccountId=b.AccountId +where CardNo not in (select CardNoOut from CardTransfer) and CardNo not in(select CardNoIn from CardTransfer) +``` +```` \ No newline at end of file diff --git "a/23\350\251\271\346\272\220\351\223\240/\347\254\224\350\256\260/9.22\346\255\273\351\224\201.md" "b/23\350\251\271\346\272\220\351\223\240/\347\254\224\350\256\260/9.22\346\255\273\351\224\201.md" new file mode 100644 index 0000000..fd371c8 --- /dev/null +++ "b/23\350\251\271\346\272\220\351\223\240/\347\254\224\350\256\260/9.22\346\255\273\351\224\201.md" @@ -0,0 +1,82 @@ +```` +## 关联子查询 + +#### 同表关联一个一个对照查询 + +``` +--子查询 +select * from tb_student +where birth in (select min(birth) from tb_student group by gender) + +--关联子查询 +select * from tb_student t1 where birth =(select min(birth) from tb_student t2 + where t2.gender=t1.gender) + --=返回标量 + +--先在t1(外层查询)查询数据: gender已经查询出来了 +--在内部一行一行查询 + + +--部门工资最高的员工:部门名称,员工名称,薪水 +select * from Department d join Employee e on d.id=e.departmentId +where salary in (select max(salary) from Employee group by departmentId) + +--关联子查询 +select * from Department d join Employee e on d.id=e.departmentId +where salary = +(select max(salary) from +Employee e1 where e.departmentId=e1.departmentId) + +同表关联一个一个对照查询 +``` + +## 排序 + +``` +over +frame: rows between unbounded preceding and current row +unbounded preceding:第一行 +current row:当前行 +select score,max(score) +over (order by stuid asc rows between current row and 1 following) frame +from stuscore + +frame:rows, range +between ... and ... +unbounded preceding: 第一行 +unbounded following: 最后一行 +CURRENT ROW: 当前行 +ROWS:逻辑位置存储 RANGE:物理位置存储 + 行号 preceding: 前面几行 + 行号 following: 后面几行 +``` + +## 死锁(事务) + +#### 死锁产生的原因 + +A事务和B事务需要使用资源1,2,但是A事务占用了资源1,B事务占用了资源2,两个事务同时等待对方资源释放。 死锁是指在一组进程中的各个进程均占有不会释放的资源,但因互相申请被其他进程所占用不会释放的资源而处于的一种永久等待状态。 + +#### 防止死锁产生: + +语句保持一致 事务语句不要写太长 + +``` +--A用户 +--begin transaction: A-->语句1 +--语句1 update StuInfo set classid = 2 where StuName='黄炜杰' +--语句2 update StuInfo set classid = 2 where StuName='李雅芸' +--commit transaction + + +--B用户 +--begin transaction: B-->语句1 +--语句1: update StuInfo set classid = 2 where StuName='李雅芸' +--语句2: update StuInfo set classid = 2 where StuName='黄炜杰' +--commit transaction +``` + +事务只能整体完成 + +当一个事务无法结束时,另一个与之有关的事务就会陷入死锁,占用资源永久等待 +```` \ No newline at end of file diff --git "a/23\350\251\271\346\272\220\351\223\240/\347\254\224\350\256\260/9.26\345\202\250\345\255\230.md" "b/23\350\251\271\346\272\220\351\223\240/\347\254\224\350\256\260/9.26\345\202\250\345\255\230.md" new file mode 100644 index 0000000..4dd1d78 --- /dev/null +++ "b/23\350\251\271\346\272\220\351\223\240/\347\254\224\350\256\260/9.26\345\202\250\345\255\230.md" @@ -0,0 +1,93 @@ +```` +## 存储 + +#### 什么是存储过程 + +存储过程是**预编译**SQL语句集合,这些语句存储在一个名称(存储过程的名称)下并作为单元来处理。存储过程代替了传统的逐条执行SQL语句的方式,一个存储过程中可以包含查询、插入、删除、更新等操纵的一系列SQL语句,当这个存储过程被调用执行时,这些操作也会同时执行。 + +封装好 --> 调用 + +# 定义存储过程 + +## 无参无返回值 + +### 没有输入参数,没有输出参数的存储过程 + +```sql +go +create proc 存储过程名 +as +语句 +go +exec 存储过程名 +``` + +## 有参无返回值 + +### 有输入参数,没有输出参数的存储过程 + +```sql +go +create proc 存储过程名 +declare @变量1 数据类型, +declare @变量2 数据类型 +as +语句 +go +exec 存储过程名 参数1,参数2 +``` + +## 有参有返回值 + +### 有输入参数,没有输出参数,但是有返回值的存储过程(返回值必须整数) + +```sql +go +create proc 存储过程名 +declare @变量1 数据类型, +declare @变量2 数据类型 +as +语句 +go +declare @f 数据类型 +exec @f = 存储过程名 参数1,参数2 +print @f +``` + +### 有输入参数,有输出参数的存储过程 + +```sql +go +create proc 存储过程名 +declare @变量1 数据类型, +declare @变量2 数据类型, +declare @变量3 数据类型 output, +declare @变量4 数据类型 output +as +语句 +go +declare @变量 数据类型 +declare @变量 数据类型 +exec 存储过程名 参数1,参数2,@变量 output,@变量 output +``` + +### 一个变量具备同时输入输出参数的存储过程 + +```sql +go +create proc 存储过程名 +declare @变量1 数据类型, +declare @变量2 数据类型, +as +语句 +go +declare @变量 数据类型 = 值 +exec 存储过程名 参数1,参数2,@变量 output +``` + +## **删除存储过程** + +```sql +drop procedure 存储过程名 +``` +```` \ No newline at end of file -- Gitee