diff --git "a/08\351\273\204\346\245\267\351\222\212/\344\275\234\344\270\232/2022.8.29\345\273\272\345\272\223\345\273\272\350\241\250\347\273\203\344\271\240.md" "b/08\351\273\204\346\245\267\351\222\212/\344\275\234\344\270\232/2022.8.29\345\273\272\345\272\223\345\273\272\350\241\250\347\273\203\344\271\240.md" new file mode 100644 index 0000000000000000000000000000000000000000..e248dee3696f9efec56dd10c866f88d747492e3a --- /dev/null +++ "b/08\351\273\204\346\245\267\351\222\212/\344\275\234\344\270\232/2022.8.29\345\273\272\345\272\223\345\273\272\350\241\250\347\273\203\344\271\240.md" @@ -0,0 +1,57 @@ +```sql +create database DBTEST +go +use DBTEST +go +--部门信息表 +create table sectionInfo( +sectionID int primary key identity(1,1),--部门编号 +sectionName varchar(10) not null --部门名称 +) +go +insert sectionInfo +values('人事部'), + ('行政部'), + ('财会部'), + ('安全部'), + ('销售部') +go +--员工信息表 +create table userInfo( +userNo int primary key identity(1,1) not null,--员工编号 +userName varchar(10) unique not null check(len(userName)>4),--员工姓名 +userSex varchar(2) not null check(userSex='男'or userSex='女'),--员工性别 +userAge int not null check(userAge between 1 and 100),--员工年龄 +userAddress varchar(50) default('湖北'),--员工地址 +userSection int references sectionInfo(sectionID)--员工部门 +) +go +insert userInfo +values('小小小灿芳','男',20,'福建',2), + ('两年半的羿','男',20,'福建',4), + ('羿的铁杆粉','男',20,'福建',5) +insert userInfo(userName,userSex,userAge,userSection) +values('asdzxc','男',20,3), + ('aaaaaaaaaa','男',20,1) +go +--员工考勤表 +create table workInfo( +workId int primary key identity(1,1) not null,--考勤编号 +userId int references userInfo(userNo) not null,--考勤员工 +workTime datetime not null,--考勤时间 +workDescription varchar(40) not null check(workDescription='迟到'or workDescription='早退'or workDescription='旷工' +or workDescription='病假' or workDescription='事假')--考勤说明 +) +go +insert workInfo +values(2,'2022-8-29 10:44:00','事假'), + (3,'2022-8-29 10:32:50','迟到'), + (1,'2022-8-29 10:42:10','早退'), + (5,'2022-8-29 10:01:30','旷工'), + (4,'2022-8-29 10:55:16','病假') +go +select * from sectionInfo +select * from userInfo +select * from workInfo +``` + diff --git "a/08\351\273\204\346\245\267\351\222\212/\344\275\234\344\270\232/2022.8.30\346\235\241\344\273\266\346\237\245\350\257\242\344\275\234\344\270\232.md" "b/08\351\273\204\346\245\267\351\222\212/\344\275\234\344\270\232/2022.8.30\346\235\241\344\273\266\346\237\245\350\257\242\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..da55365fb57317acaf1e6bfe1cfd10bc340f62a7 --- /dev/null +++ "b/08\351\273\204\346\245\267\351\222\212/\344\275\234\344\270\232/2022.8.30\346\235\241\344\273\266\346\237\245\350\257\242\344\275\234\344\270\232.md" @@ -0,0 +1,74 @@ +```sql +use DBTEST + +--1. 根据指定列(姓名,性别,月薪,电话)查询性别为女的员工信息,并自定义中文列名 +select PeopleName 姓名,PeopleSex 性别,PeopleSalary 月薪,PeoplePhone 电话 from People where PeopleSex='女' +--2. 查询月薪大于等于10000 的员工信息( 单条件 ) +select * from People where PeopleSalary>=10000 +--3. 查询月薪大于等于10000 的女员工信息(多条件) +select * from People where PeopleSalary>=10000 and PeopleSex='女' +--4. 显示出出身年月在1980-1-1之后,而且月薪大于等于10000的女员工信息。 +select * from People where YEAR(PeopleBirth)>=1980 and PeopleSalary>=10000 +--5. 显示出月薪大于等于15000 的员工,或者月薪大于等于8000的女员工信息。 +select * from People where PeopleSalary>=15000 or PeopleSalary>=8000 +--6. 查询月薪在10000-20000 之间员工信息( 多条件 ) +select * from People where PeopleSalary between 10000 and 20000 +--7. 查询出地址在北京或者上海的员工信息 +select * from People where PeopleAddress='北京' or PeopleAddress='上海' +--8. 查询所有员工信息(根据工资排序,降序排列) +select * from People +order by PeopleSalary desc +--9. 显示所有的员工信息,按照名字的长度进行倒序排列 +select * from People +order by len(PeopleName) desc +--10. 查询工资最高的5个人的信息 +select top 5 * from People +--11. 查询工资最高的10%的员工信息 +select top 10 percent * from People +--12. 查询出地址没有填写的员工信息 +select * from People +where PeopleAddress is null +--13. 查询出地址已经填写的员工信息 +select * from People +where PeopleAddress is not null +--14. 查询所有的80后员工信息 +select * from People +where YEAR(PeopleBirth)>=1980 +--15. 查询年龄在30-40 之间,并且工资在15000-30000 之间的员工信息 +select * from People +where (year(GETDATE())-year(PeopleBirth)) between 30 and 40 and +PeopleSalary between 15000 and 30000 +--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=(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 12 then '猴' + when 1 then '鸡' + when 2 then '狗' + when 3 then '猪' + else '' +end 生肖 +from people +``` + diff --git "a/08\351\273\204\346\245\267\351\222\212/\344\275\234\344\270\232/2022.9.15\350\247\206\345\233\276\347\273\203\344\271\240.md" "b/08\351\273\204\346\245\267\351\222\212/\344\275\234\344\270\232/2022.9.15\350\247\206\345\233\276\347\273\203\344\271\240.md" new file mode 100644 index 0000000000000000000000000000000000000000..e5a0b190184bbd41a55eb4896f1694f951f6a0af --- /dev/null +++ "b/08\351\273\204\346\245\267\351\222\212/\344\275\234\344\270\232/2022.9.15\350\247\206\345\233\276\347\273\203\344\271\240.md" @@ -0,0 +1,9 @@ +```sql +--编写视图实现查询出所有银行卡账户信息,显示卡号,身份证,姓名,余额。 +create view V_Bank(卡号,身份证,姓名,余额) +as +select CardNo,AccountCode,RealName,CardMoney from AccountInfo ao +inner join BankCard bd on ao.AccountId=bd.AccountId +select * from V_Bank +``` + diff --git "a/08\351\273\204\346\245\267\351\222\212/\344\275\234\344\270\232/2022.9.21\345\207\275\346\225\260\344\275\234\344\270\232.md" "b/08\351\273\204\346\245\267\351\222\212/\344\275\234\344\270\232/2022.9.21\345\207\275\346\225\260\344\275\234\344\270\232.md" index 80aee224711c9c0aa3c791e18b00bd0c14432de1..48171b5d97258680a13be4fedfa2cfb5ed14b557 100644 --- "a/08\351\273\204\346\245\267\351\222\212/\344\275\234\344\270\232/2022.9.21\345\207\275\346\225\260\344\275\234\344\270\232.md" +++ "b/08\351\273\204\346\245\267\351\222\212/\344\275\234\344\270\232/2022.9.21\345\207\275\346\225\260\344\275\234\344\270\232.md" @@ -7,3 +7,103 @@ from where b = 1 ``` + + +```sql +--(1)编写一个函数求该银行的金额总和 +go +create function BankSum() +returns decimal +as +begin + declare @sum decimal + select @sum = (select sum(CardMoney) from BankCard) + return @sum +end +go +select dbo.BankSum() 金额总和 +--(2)传入账户编号,返回账户真实姓名 +go +create function BankName(@id int) +returns varchar(20) +as + begin + declare @name varchar(20) + select @name = (select RealName from AccountInfo join BankCard on BankCard.AccountId=AccountInfo.AccountId where AccountInfo.AccountId=@id) + return @name + end +go +select dbo.BankName(3) 账户姓名 +select * from AccountInfo --个人信息 +select * from BankCard --银行卡信息 +select * from CardStateChange +select * from CardExchange --交易记录 +--(3)传递开始时间和结束时间,返回交易记录(存钱取钱),交易记录中包含 真实姓名,卡号,存钱金额,取钱金额,交易时间。 +go +create function BankMoney(@start smalldatetime,@end smalldatetime) +returns @BankInfo table(RealName varchar(20),CardNo char(18),MoneyInBank decimal,MoneyOutBank decimal,ExchangeTime smalldatetime) +as + begin + insert into @BankInfo + select RealName,bd.CardNo,MoneyInBank,MoneyOutBank,ExchangeTime from AccountInfo ao join BankCard bd on bd.AccountId=ao.AccountId join CardExchange ce on ce.CardNo=bd.CardNo + where ExchangeTime between @start and @end + return + end +go +select * from BankMoney('2022-08-22','2022-09-19') +--(4)查询银行卡信息,将银行卡状态1,2,3,4分别转换为汉字“正常,挂失,冻结,注销”,根据银行卡余额显示银行卡等级 30万以下为“普通用户”,30万及以上为"VIP用户",分别显示卡号,身份证,姓名,余额,用户等级,银行卡状态。 +--方案一:直接在sql语句中使用case when +select CardNo 卡号,AccountCode 身份证,RealName 姓名,CardMoney 余额, +case +when CardState=1 then '正常' +when CardState=1 then '挂失' +when CardState=1 then '冻结' +when CardState=1 then '注销' +end '银行卡状态', +case +when CardMoney>=300000 then 'VIP用户' +else '普通用户' +end '用户等级' +from BankCard bd +inner join AccountInfo ao on ao.AccountId=bd.AccountId +--方案二:将等级和状态用函数实现 +go +create function BankVIP() +returns @StateVIP table(卡号 char(18),身份证 char(18),姓名 varchar(20),余额 decimal,银行卡状态 varchar(20),普通用户 varchar(20)) +as +begin + insert into @StateVIP + select CardNo,AccountCode,RealName,CardMoney, +case +when CardState=1 then '正常' +when CardState=1 then '挂失' +when CardState=1 then '冻结' +when CardState=1 then '注销' +end , +case +when CardMoney>=300000 then 'VIP用户' +else '普通用户' +end +from BankCard bd +inner join AccountInfo ao on ao.AccountId=bd.AccountId + return +end +go +select * from BankVIP(); +--(5)编写函数,根据出生日期求年龄,年龄求实岁,例如: +--​ 生日为2000-5-5,当前为2018-5-4,年龄为17岁 +--​ 生日为2000-5-5,当前为2018-5-6,年龄为18岁 +go +create function Birth() +returns @Birth table(生日日期 smalldatetime,当前年 smalldatetime,实岁 int) +as +begin + insert into @Birth + select Empbirth,GETDATE(),year(GETDATE())-YEAR(Empbirth) from Emp + where MONTH(GETDATE())>MONTH(Empbirth) + return +end +go +select * from Birth() +``` + diff --git "a/08\351\273\204\346\245\267\351\222\212/\344\275\234\344\270\232/2022.9.6\345\217\230\351\207\217\344\270\216\351\200\273\350\276\221\346\216\247\345\210\266.md" "b/08\351\273\204\346\245\267\351\222\212/\344\275\234\344\270\232/2022.9.6\345\217\230\351\207\217\344\270\216\351\200\273\350\276\221\346\216\247\345\210\266.md" new file mode 100644 index 0000000000000000000000000000000000000000..daafc138a081fe8a9d76984bd8e214fcd5f82302 --- /dev/null +++ "b/08\351\273\204\346\245\267\351\222\212/\344\275\234\344\270\232/2022.9.6\345\217\230\351\207\217\344\270\216\351\200\273\350\276\221\346\216\247\345\210\266.md" @@ -0,0 +1,46 @@ +```sql +--1. 为赵云此人进行开户开卡操作,赵云身份证:420107199904054233 +insert into AccountInfo(AccountCode,AccountPhone,RealName,OpenTime) +values('420107199904054233','16628594512','赵云',GETDATE()) +--2. 需要求出张飞的银行卡卡号和余额,张飞身份证:420107199602034138 (1.使用连接查询,2.使用变量) +--1. +select CardNo,CardMoney,AccountCode from AccountInfo ao +inner join BankCard bd on bd.AccountId=ao.AccountId +where RealName='张飞' +--2. +declare @a varchar(18) = '张飞' +select @a = CardNo from BankCard + print @a +declare @m decimal +select @m =CardMoney from AccountInfo ao + inner join BankCard bd on bd.AccountId=ao.AccountId + where AccountCode=420107199602034138 + print @m +--​ 3.某用户银行卡号为“6225547854125656”,该用户执行取钱操作,取钱5000元,余额充足则进行取钱操作,并提示"取钱成功",否则提示“余额不足”。 +declare @k CHAR(18) = 6225547854125656 +select @k =CardMoney from BankCard +if convert(decimal,@k)>5000 +begin +print '取钱成功' +end +else +begin +print '余额不足' +end +--​ 4.查询银行卡信息,将银行卡状态1,2,3,4分别转换为汉字“正常,挂失,冻结,注销”,并且根据银行卡余额显示银行卡等级 30万以下为“普通用户”,30万及以上为"VIP用户", +----显示列分别为卡号,身份证,姓名,余额,用户等级,银行卡状态。 +select CardNo,AccountCode,RealName,CardMoney,CardState, +case +when CardState=1 then '正常' +when CardState=1 then '挂失' +when CardState=1 then '冻结' +when CardState=1 then '注销' +end '银行卡状态', +case +when CardMoney>=300000 then 'VIP用户' +else '普通用户' +end '用户等级' +from BankCard bd +inner join AccountInfo ao on ao.AccountId=bd.AccountId +``` + diff --git "a/08\351\273\204\346\245\267\351\222\212/\344\275\234\344\270\232/2022.9.7\345\255\220\346\237\245\350\257\242.md" "b/08\351\273\204\346\245\267\351\222\212/\344\275\234\344\270\232/2022.9.7\345\255\220\346\237\245\350\257\242.md" new file mode 100644 index 0000000000000000000000000000000000000000..fda808dcaeb770d6e7e59849048fe0df883dc5d4 --- /dev/null +++ "b/08\351\273\204\346\245\267\351\222\212/\344\275\234\344\270\232/2022.9.7\345\255\220\346\237\245\350\257\242.md" @@ -0,0 +1,37 @@ +```sql +--1.关羽的银行卡号为"6225547858741263",查询出余额比关羽多的银行卡信息,显示卡号,身份证,姓名,余额。 +select CardNo,AccountCode,RealName,cardMoney from BankCard bd +inner join AccountInfo ao on ao.AccountId=bd.AccountId +where cardmoney>all(select CardMoney from BankCard where CardNo=6225547858741263) +--2.从所有账户信息中查询出余额最高的交易明细(存钱取钱信息)。 +select * from CardExchange where CardNo = (select top 1 CardNo from BankCard order by CardMoney desc) +--3.查询有取款记录的银行卡及账户信息,显示卡号,身份证,姓名,余额。 +select CardNo 卡号,AccountCode 身份证,RealName 姓名,CardMoney 余额 from BankCard bd +left join AccountInfo ao on bd.AccountId = ao.AccountId +where CardNo in (select CardNo from CardExchange where MoneyOutBank != 0) +--4.查询出没有存款记录的银行卡及账户信息,显示卡号,身份证,姓名,余额。 +select CardNo 卡号,AccountCode 身份证,RealName 姓名,CardMoney 余额 from BankCard bd +left join AccountInfo ao on bd.AccountId = ao.AccountId +where CardNo not in (select CardNo from CardExchange where MoneyOutBank != 0) +--5.关羽的银行卡号为"6225547858741263",查询当天是否有收到转账。 +if exists(select * from CardTransfer where CardNoIn = '6225547858741263' and convert(char(18),TransferTime) = convert(varchar(10),getdate(), 120)) +begin + print '有转账记录' +end +else +begin + print '没有转账记录' +end +--6.查询出交易次数(存款取款操作)最多的银行卡账户信息,显示:卡号,身份证,姓名,余额,交易数。 +select top 1 bd.CardNo 卡号,AccountCode 身份证,RealName 姓名,CardMoney 余额, +exchangeCount 交易次数 from BankCard bd +inner join AccountInfo ao on bd.AccountId =ao.AccountId +inner join (select CardNo,COUNT(*) exchangeCount from CardExchange group by CardNo ) CarcExchageTemp on bd.CardNo = CarcExchageTemp.CardNo +order by exchangeCount desc +--7.查询出没有转账交易记录的银行卡账户信息,显示卡号,身份证,姓名,余额。 +select CardNo 卡号,AccountCode 身份证,RealName 姓名,CardMoney 余额 from BankCard bd +left join AccountInfo ao on bd.AccountId = ao.AccountId +where bd.CardNo not in (select CardNoIn from CardTransfer) +and bd.CardNo not in (select CardNoOut from CardTransfer) +``` +