diff --git "a/37\345\274\240\346\255\243\350\261\252/.keep" "b/37\345\274\240\346\255\243\350\261\252/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/37\345\274\240\346\255\243\350\261\252/2022-09.26\350\247\206\345\233\276\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" "b/37\345\274\240\346\255\243\350\261\252/2022-09.26\350\247\206\345\233\276\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fac0f2ee4719afda10caf461462bccb9cc49e5e5 --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/2022-09.26\350\247\206\345\233\276\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,30 @@ +--编写视图实现查询出所有银行卡账户信息,显示卡号,身份证,姓名,余额。 +create view v_information +as +select AccountCode,CardNo,RealName,CardMoney from AccountInfo inner join BankCard on BankCard.AccountId=AccountInfo.AccountId +go +select * from v_information +drop view v_information + + + +--行转列 +create table huatai( + year int, + month int, + amount float +) + +insert into huatai(year,month,amount) values (1991,1,1.1) +insert into huatai(year,month,amount) values (1991,2,1.2) +insert into huatai(year,month,amount) values (1991,3,1.3) +insert into huatai(year,month,amount) values (1991,4,1.4) + +insert into huatai(year,month,amount) values (1992,1,2.1) +insert into huatai(year,month,amount) values (1992,2,2.2) +insert into huatai(year,month,amount) values (1992,3,2.3) +insert into huatai(year,month,amount) values (1992,4,2.4) + +select * from huatai + +select * from huatai pivot(max(amount) for month in ([1],[2],[3],[4])) a \ No newline at end of file diff --git "a/37\345\274\240\346\255\243\350\261\252/2022-09.26\350\247\206\345\233\276\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" "b/37\345\274\240\346\255\243\350\261\252/2022-09.26\350\247\206\345\233\276\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" new file mode 100644 index 0000000000000000000000000000000000000000..c7f3301916cfd0ee4805e5508ef3477329b74444 --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/2022-09.26\350\247\206\345\233\276\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" @@ -0,0 +1,27 @@ +瑙嗗浘 + +鍒涘缓瑙嗗浘 + + create view V_name as select row_name from table_name + +鍒犻櫎瑙嗗浘 + + drop view V_name + +### union + + SELECT _column_name(s)_ FROM _table1_ + UNION + SELECT _column_name(s)_ FROM _table2_ + +### union all + + SELECT _column_name(s)_ FROM _table1_ + UNION ALL + SELECT _column_name(s)_ FROM _table2_ + +### unpivot + +select from Sales + + unpivot (Amount for people in (A1,A2,A3,A4) as a \ No newline at end of file diff --git "a/37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/DBTEST.sql" "b/37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/DBTEST.sql" new file mode 100644 index 0000000000000000000000000000000000000000..afff8826659878a9f09e71c34eda54468501b6b9 --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/DBTEST.sql" @@ -0,0 +1,225 @@ +if exists(select * from sys.databases where name='DBTEST') +drop database DBTEST1; +go + +create database DBTEST1; +go + +use DBTEST1; +go + +--创建部门表 +create table Department +( + --创建部门编号;int代表整数类型;primary key代表主键;identity(1,1)代表从1开始步长为1自增长; + DepartmentId int primary key identity(1,1), + --创建部门名称;nvarchar(50)代表长度50的字符串;not null代表不能为空; + DepartmentName nvarchar(50) not null, + --创建部门描述;text代表长文本; + DepartmentRemark text +); +go + +--创建职级表,rank为系统关键字,此处使用[]代表自定义名字,而非系统关键字 +create table [Rank] +( + RankId int primary key identity(1,1), + RankName nvarchar(50) not null, + RankRemark text +); +go + +--创建员工信息表 +create table People +( + PeopleId int primary key identity(1,1), + --references代表外键引用,此字段必须符合与其它表的外键约束 + DepartmentId int references Department(DepartmentId) not null, + RankId int references [Rank](RankId) not null, + PeopleName nvarchar(50) not null, + --default代表字段默认值; check可以规定字段值的约束条件; + PeopleSex nvarchar(1) default('男') check(PeopleSex='男' or PeopleSex='女') not null, + PeopleBirth datetime not null, + PeopleSalary decimal(12,2) check(PeopleSalary>= 1000 and PeopleSalary <= 100000) not null, + --unique代表唯一约束,为数据提供唯一性保证; + PeoplePhone nvarchar(20) unique not null, + PeopleAddress nvarchar(100), + --datetime和smalldatetime都可以表示时间类型,getdate()用于获取系统当前时间 + PeopleAddTime smalldatetime default(getdate()) +); +go + + + +------------------------------插入数据部分------------------------------ +--部门表插入数据 +insert into Department values('行政部','公司主管行政工作的部门'); +go + +--一次性插入多条数据 +insert into Department(DepartmentName,DepartmentRemark) +select '市场部','吹牛的部门' union +select '产品部','天马星空的部门' union +select '总经办','都是领导的部门' ; +go + +-------职级表插入数据 +insert into [Rank](RankName,RankRemark) +values('初级','辅助其他人完成任务') +insert into [Rank](RankName,RankRemark) +values('中级','具备独立处理事务的能力') +insert into [Rank](RankName,RankRemark) +values('高级','具备可以带动全场节奏的能力'); +go + +---------向员工表插入数据 +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,3,'刘备','男','1984-7-9',20000,'13554785452','成都',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,2,'孙尚香','女','1987-7-9',15000,'13256854578','荆州',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,1,'关羽','男','1988-8-8',12000,'13985745871','荆州',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,1,'张飞','男','1990-8-8',8000,'13535987412','宜昌',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,3,'赵云','男','1989-4-8',9000,'13845789568','宜昌',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,3,'马超','男','1995-4-8',9500,'13878562568','香港',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,2,'黄盖','男','1989-4-20',8500,'13335457412','武汉',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,1,'貂蝉','女','1989-4-20',6500,'13437100050','武汉',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,2,'曹操','男','1987-12-20',25000,'13889562354','北京',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,3,'许褚','男','1981-11-11',9000,'13385299632','北京',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,1,'典韦','男','1978-1-13',8000,'13478545263','上海',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,1,'曹仁','男','1998-12-12',7500,'13878523695','深圳',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,3,'孙坚','男','1968-11-22',9000,'13698545841','广州',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,3,'孙策','男','1988-1-22',11000,'13558745874','深圳',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,2,'孙权','男','1990-2-21',12000,'13698745214','深圳',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,2,'大乔','女','1995-2-21',13000,'13985478512','上海',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,1,'小乔','女','1996-2-21',13500,'13778787874','北京',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,2,'周瑜','男','1992-10-11',8000,'13987455214','武汉',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,3,'鲁肃','男','1984-9-10',5500,'13254785965','成都',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,3,'吕蒙','男','1987-5-19',8500,'13352197364','成都',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,1,'陆逊','男','1996-5-19',7500,'13025457392','南京',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,2,'太史慈','男','1983-6-1',7500,'13077778888','上海',getdate()) + + +---查询 +select * from Department +select * from [Rank] +select * from People + + +--1. 查询所有行所有列 + +--2. 指定列查询(姓名,性别,月薪,电话) +select PeopleName as 姓名 ,Peoplesex as 性别 from People; +--3. 指定列查询,并自定义中文列名(姓名,性别,月薪,电话) +--4. 查询公司员工所在城市(不需要重复数据) + +--消除重复行:distinct +select distinct peopleAddress from People; +--5. 假设工资普调10%,查询原始工资和调整后的工资,显示(姓名,性别,月薪,加薪后的月薪)(添加列查询)。 +select top 3 PeopleName 姓名, PeopleSalary 月薪, PeopleSalary*1.1 加薪后月薪 from People; + +--top ordey by + + +select * from People +--根据指定列(姓名,性别,月薪,电话)查询性别为女的员工信息,并自定义中文列名 +--2. 查询月薪大于等于10000 的员工信息( 单条件 ) +--3. 查询月薪大于等于10000 的女员工信息(多条件) +select * from People where PeopleSalary>=10000 and PeopleSex='女' +--4. 显示出生年月在1980-1-1之后,而且月薪大于等于10000的女员工信息。 +select * from People where PeopleSalary>=10000 and PeopleSex='女' and PeopleBirth>'1995-1-1'; + +--5. 显示出月薪大于等于15000 的员工,或者月薪大于等于8000的女员工信息。 +select * from People where (PeopleSalary>=8000 and peoplesex='女') or PeopleSalary>=15000 +--6. 查询月薪在10000-20000 之间员工信息( 多条件 ) +select * from People where PeopleSalary between 10000 and 20000 +--7. 查询出地址在北京或者上海的员工信息 +--8. 查询所有员工信息(根据工资排序,降序排列): asc(默认):升序 desc:降序 +select * from People order by PeopleSalary desc + +--9. 显示所有的员工信息,按照名字的长度进行倒序排列 +--10. 查询工资最高的5个人的信息 +select top 5 * from People order by PeopleSalary desc +--11. 查询工资最高的10%的员工信息 +select top 10 percent * from People order by PeopleSalary desc +--12. 查询出地址没有填写的员工信息 +--为空不能用=,is null, is not null +select * from People where PeopleAddress is null; + +--13. 查询出地址已经填写的员工信息 +--14. 查询所有的80后员工信息 + +--15. 查询年龄在30-40 之间,并且工资在15000-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 > 9000 +--18. 查询出和赵云在同一个城市的人 +--19. 查询出生肖为鼠的人员信息 +--20. 查询所有员工信息,添加一列显示属相(鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪) +--case when end diff --git "a/37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/SQLQuery1.sql" "b/37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4cace76748c61e5aaf062f6057ee9c7b89b81777 --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/SQLQuery1.sql" @@ -0,0 +1,57 @@ +USE DBTEST1; +select * from Department;--部门表 +select * from [Rank];--职级表 +select * from People;--员工信息表 +--1. 查询出武汉地区所有的员工信息,要求显示部门名称以及员工的详细资料 +--select * from People where PeopleAddress +--in +--(select PeopleAddress from People where PeopleAddress = '武汉') + +select De.DepartmentRemark 所在部门 ,Pe.* from People Pe +join Department De on De.DepartmentId = Pe.DepartmentId +where PeopleAddress = '武汉'; + + +--2. 查询出武汉地区所有的员工信息,要求显示部门名称,职级名称以及员工的详细资料 +select De.DepartmentRemark 所在部门,Ra.RankRemark 职级名称,Pe.* from People Pe +join Department De on De.DepartmentId = Pe.DepartmentId +join [Rank] Ra on Ra.RankId = Pe.RankId +where PeopleAddress = '武汉'; + +--3. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资。 +select count(DepartmentId)员工人数,sum(PeopleSalary)员工工资总和,AVG(PeopleSalary)平均工资,max(PeopleSalary)最高工资,min(PeopleSalary)最低工资 from People + group by DepartmentId; +-- select * from People;--员工信息表 + +--4. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资,平均工资在10000 以下的不参与统计,并且根据平均工资降序排列。 +select count(DepartmentId)员工人数,sum(PeopleSalary)员工工资总和,AVG(PeopleSalary)平均工资,max(PeopleSalary)最高工资,min(PeopleSalary)最低工资 from People + group by DepartmentId + having Avg (PeopleSalary) >=10000 + order by 平均工资 desc; + -- select * from People;--员工信息表 + +--5. 根据部门名称,然后根据职位名称,分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资 +select d.DepartmentName ,COUNT(*) 员工人数,SUM(PeopleSalary) 员工工资总和 ,AVG(PeopleSalary) 平均工资,MAX(PeopleSalary) 最高工资,MIN(PeopleSalary) 最低工资 from People p +join Department d on d.DepartmentId=p.DepartmentId +group by d.DepartmentName,RankId + + +--6.查询出巨蟹 6.22--7.22 的员工信息 +select * from People where PeopleAddTime between '2022-06-22' and '2022-07-22' +update People set PeopleAddTime='2017-07-22 19:08:00' where PeopleId = 10 +--7.查询所有员工信息,添加一列显示属相(鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪) +select *,case year(PeopleAddTime) % 12 +when 0 then '猴' +when 1 then '鸡' +when 2 then '狗' +when 3 then '猪' +when 4 then '鼠' +when 5 then '牛' +when 6 then '虎' +when 7 then '兔' +when 8 then '龙' +when 9 then '蛇' +when 10 then '马' +when 11 then '羊' +end '生肖' + from People diff --git "a/37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232.txt" "b/37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232.txt" new file mode 100644 index 0000000000000000000000000000000000000000..6e8855c7e497d10e741b9e1c2c2976002427bb03 --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232.txt" @@ -0,0 +1,12 @@ + 1. 闅忔満鐢熸垚鐭俊楠岃瘉鐮侊紙6浣嶆暟瀛楋級 + + +-- 2. 缁熻姣忎釜鐢ㄦ埛鐨勬墍鏈夊崱浣欓涔嬪拰锛屽苟鍥涜垗浜斿叆锛屼繚鐣欎竴浣嶅皬鏁 + + +-- 3.鏌ヨ鐢ㄦ埛琛ㄤ腑鍚嶅瓧閮ㄥ垎涓轰腑鏂囩殑鎵鏈夌敤鎴疯褰曪紙鍗冲繀椤诲惈鏈変腑鏂囷紝浣嗕笉鑳藉叏閮ㄦ槸涓枃锛屾瘮濡傗樺垬澶嘺1鈥欙紝浣嗕笉鑳芥槸'鍒樺'锛 + +-- 4.鎵惧嚭鎵鏈夊惈鏈夐潪娉曞瓧绗︼紙'绗ㄨ泲'锛'鍌荤摐','姝'锛夌殑璁板綍锛屽苟杩欎簺闈炴硶瀛楃锛屾瘮濡 '姝诲紶涓夋槸鍌荤摐' 鍙樻垚 '*寮犱笁鏄**'锛堝嚑涓瓧绗︼紝灏辨浛鎹负鍑犱釜*锛 + + +-- 5.鏌ユ壘璐﹀彿琛ㄤ腑鐩稿悓鏈堜唤寮鎴风殑鐢ㄦ埛 diff --git "a/37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/\347\263\273\347\273\237\345\207\275\346\225\2601.sql" "b/37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/\347\263\273\347\273\237\345\207\275\346\225\2601.sql" new file mode 100644 index 0000000000000000000000000000000000000000..890919b10c2a3541ecc9fdacd68f4a53a4b2fdfc --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/\347\263\273\347\273\237\345\207\275\346\225\2601.sql" @@ -0,0 +1,54 @@ +##################鏁板煎嚱鏁 + + +SELECT CardMoney 閲戦, CEIL(CardMoney) 鍚戜笂鍙栨暣1,CEILING(CardMoney)鍚戜笂鍙栨暣2, +FLOOR(CardMoney)鍚戜笅鍙栨暣, ROUND(CardMoney) +鍥涜垗浜斿叆1,ROUND(CardMoney,1) 鍥涜垗浜斿叆2 FROM `bankcard` ; + + + +## 鍙栧緱1-10涔嬮棿鐨勯殢鏈烘暟 +SELECT CEIL(RAND()*10); + + + +##################瀛楃涓插嚱鏁 +####### 闀垮害 +#LENGTH: 瀛楄妭闀垮害(鏁板瓧锛屽瓧姣嶏紝绗﹀彿鍗犵敤涓涓瓧鑺傦紝涓涓眽瀛楀崰3涓瓧鑺--utf8) +#CHAR_LENGTH 瀛楃闀垮害 +SELECT RealName,LENGTH(RealName) 瀛楄妭闀垮害,CHAR_LENGTH(RealName) 瀛楃闀垮害1,CHARACTER_LENGTH(RealName) as 瀛楃闀垮害2 from accountinfo; + + +##鎵惧嚭鍚湁涓枃鐨 +SELECT * from accountinfo +where LENGTH(RealName) != CHAR_LENGTH(RealName) ; + +##鎵惧嚭鍚嶅瓧鍏ㄩ儴鏄眽瀛楃殑璁板綍 +SELECT * from accountinfo +where LENGTH(RealName) != CHAR_LENGTH(RealName) and LENGTH(RealName)%CHAR_LENGTH(RealName) =0; + +SELECT * from accountinfo +where LENGTH(RealName) =3* CHAR_LENGTH(RealName); + + + + +####### 鏇挎崲 +##INSERT(str,pos,len,newstr) 鎸囧畾浣嶇疆銆佹寚瀹氶暱搴︽浛鎹 +##REPLACE(str,from_str,to_str) 妯$硦鏌ユ壘鏇挎崲 + +SELECT RealName,INSERT(RealName,2,1,'涓変赴'),REPLACE(RealName,'椋','涓変赴') +from accountinfo +where AccountId=4 +; +##鑾峰彇鏃ユ湡涓殑骞存湀 +select CardTime,INSERT(CardTime,5,-1,'') as 骞,INSERT(INSERT(CardTime,1,5,''),3,-1,'') as 鏈 from bankcard; + + +####鎴彇 LEFT(str,len) RIGHT(str,len) substr + +select CardTime,LEFT(CardTime,4)骞,right(CardTime,8),SUBSTR(CardTime,6,2),SUBSTR(CardTime,6),SUBSTRing(CardTime,6,2),SUBSTRing(CardTime,6) from bankcard +; + + + diff --git "a/37\345\274\240\346\255\243\350\261\252/9-20\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" "b/37\345\274\240\346\255\243\350\261\252/9-20\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..24152e6a8d29f81bf392792327702666319a80e0 --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/9-20\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,50 @@ +use banktest; +select * from AccountInfo;--账户信息表:存储个人信息 +select * from BankCard;--银行卡表:存储银行卡信息 +select * from CardExchange;--交易信息表(存储存钱和取钱的记录) +select * from CardStateChange;--状态信息变化表(存储银行卡状态变化的记录,状态有1:正常,2:挂失,3:冻结,4:注销) +select * from CardTransfer;--转账信息表(存储转账信息记录) +--1. 关羽的银行卡号为"6225547858741263",查询出余额比关羽多的银行卡信息,显示卡号,身份证,姓名,余额。 +select * from BankCard Bk +join AccountInfo Ac on Bk.AccountId = Ac.AccountId +where CardMoney > (select CardMoney from BankCard where CardNo = 6225547858741263); +--2. 从所有账户信息中查询出余额最高的交易明细(存钱取钱信息)。 +select Bk.CardMoney 卡余额,Ca.* from BankCard Bk +join CardExchange Ca on Bk.CardNo = Ca.CardNo +where CardMoney =(select Max(CardMoney) 余额最高 from BankCard); +--3. 查询有取款记录的银行卡及账户信息,显示卡号,身份证,姓名,余额。 +select * from BankCard Ba +join AccountInfo Ac on Ba.AccountId = Ac.AccountId + where CardNo not in +(select case MoneyOutBank +when 0 then CardNo +end '卡号' + from CardExchange ); +--4. 查询出没有存款记录的银行卡及账户信息,显示卡号,身份证,姓名,余额。 +select * from BankCard Ba +join AccountInfo Ac on Ba.AccountId = Ac.AccountId +where CardNo in ( +select case MoneyInBank +when 0 then CardNo +end '卡号' + from CardExchange ) +--5. 关羽的银行卡号为"6225547858741263",查询当天是否有收到转账。 +select case CardNoIn +when 6225547858741263 then '是' +end '是否收到转账' + from CardTransfer ; +--6. 查询出交易次数(存款取款操作)最多的银行卡账户信息,显示:卡号,身份证,姓名,余额,交易次数。 +select * from BankCard Ba +join AccountInfo Ac on Ba.AccountId = Ac.AccountId +where CardNo in ( +select top 1 CardNo from CardExchange +group by CardNo +order by count(*) desc); +--7.查询出没有转账交易记录的银行卡账户信息,显示卡号,身份证,姓名,余额。 +--select * from AccountInfo;--账户信息表:存储个人信息 +--select * from BankCard;--银行卡表:存储银行卡信息 +--select * from CardTransfer;--转账信息表(存储转账信息记录) +select Bk.CardNo 卡号,Bk.CardMoney 余额,Ac.* from BankCard Bk +join AccountInfo Ac on Bk.AccountId = Ac.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/37\345\274\240\346\255\243\350\261\252/9-20\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/banktest.sql" "b/37\345\274\240\346\255\243\350\261\252/9-20\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/banktest.sql" new file mode 100644 index 0000000000000000000000000000000000000000..92f931695499eec94e4aad4648ca9f9df14222f1 --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/9-20\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/banktest.sql" @@ -0,0 +1,107 @@ +create database BankTest; +go +use banktest; +go +--账户信息表:存储个人信息 +create table AccountInfo +( + AccountId int primary key identity(1,1), --账户编号 + AccountCode varchar(20) not null, --身份证号码 + AccountPhone varchar(20) not null, --电话号码 + RealName varchar(20) not null, --真实姓名 + OpenTime smalldatetime not null, --开户时间 +) +--银行卡表:存储银行卡信息 +create table BankCard +( + CardNo varchar(30) primary key, --银行卡卡号 + AccountId int not null, --账户编号(与账户信息表形成主外键关系) + CardPwd varchar(30) not null, --银行卡密码 + CardMoney money not null, --银行卡余额 + CardState int not null,--1:正常,2:挂失,3:冻结,4:注销 + CardTime smalldatetime default(getdate()) --开卡时间 +) +--交易信息表(存储存钱和取钱的记录) +create table CardExchange +( + ExchangeId int primary key identity(1,1), --交易自动编号 + CardNo varchar(30) not null, --银行卡号(与银行卡表形成主外键关系) + MoneyInBank money not null, --存钱金额 + MoneyOutBank money not null, --取钱金额 + ExchangeTime smalldatetime not null, --交易时间 +) +--转账信息表(存储转账信息记录) +create table CardTransfer +( + TransferId int primary key identity(1,1),--转账自动编号 + CardNoOut varchar(30) not null, --转出银行卡号(与银行卡表形成主外键关系) + CardNoIn varchar(30) not null, --转入银行卡号(与银行卡表形成主外键关系) + TransferMoney money not null,--交易金额 + TransferTime smalldatetime not null, --交易时间 +) +--状态信息变化表(存储银行卡状态变化的记录,状态有1:正常,2:挂失,3:冻结,4:注销) +create table CardStateChange +( + StateId int primary key identity(1,1),--状态信息自动编号 + CardNo varchar(30) not null, --银行卡号(与银行卡表形成主外键关系) + OldState int not null, --银行卡原始状态 + NewState int not null, --银行卡新状态 + StateWhy varchar(200) not null, --状态变化原因 + StateTime smalldatetime not null, --记录产生时间 +) + + + +--为刘备,关羽,张飞三个人进行开户开卡的操作 +--刘备身份证:420107198905064135 +--关羽身份证:420107199507104133 +--张飞身份证:420107199602034138 +insert into AccountInfo(AccountCode,AccountPhone,RealName,OpenTime) +values('420107198905064135','13554785425','刘备',GETDATE()) +insert into BankCard(CardNo,AccountId,CardPwd,CardMoney,CardState) +values('6225125478544587',1,'123456',0,1) + +insert into AccountInfo(AccountCode,AccountPhone,RealName,OpenTime) +values('420107199507104133','13454788854','关羽',GETDATE()) +insert into BankCard(CardNo,AccountId,CardPwd,CardMoney,CardState) +values('6225547858741263',2,'123456',0,1) + +insert into AccountInfo(AccountCode,AccountPhone,RealName,OpenTime) +values('420107199602034138','13456896321','张飞',GETDATE()) +insert into BankCard(CardNo,AccountId,CardPwd,CardMoney,CardState) +values('6225547854125656',3,'123456',0,1) + +select * from AccountInfo +select * from BankCard + + + +--进行存钱操作,刘备存钱2000元,关羽存钱:8000元,张飞存钱:500000元 +select * from AccountInfo +update BankCard set CardMoney = CardMoney + 2000 where CardNo = '6225125478544587' +insert into CardExchange(CardNo,MoneyInBank,MoneyOutBank,ExchangeTime) +values('6225125478544587',2000,0,GETDATE()) + +update BankCard set CardMoney = CardMoney + 8000 where CardNo = '6225547858741263' + +insert into CardExchange(CardNo,MoneyInBank,MoneyOutBank,ExchangeTime) +values('6225547858741263',8000,0,GETDATE()) + +update BankCard set CardMoney = CardMoney + 500000 where CardNo = '6225547854125656' +insert into CardExchange(CardNo,MoneyInBank,MoneyOutBank,ExchangeTime) +values('6225547854125656',500000,0,GETDATE()) + +--转账:刘备给张飞转账1000元 +update BankCard set CardMoney = CardMoney -1000 where CardNo = '6225125478544587' +update BankCard set CardMoney = CardMoney + 1000 where CardNo = '6225547854125656' + +insert into CardTransfer(CardNoOut,CardNoIn,TransferMoney,TransferTime) +values('6225125478544587','6225547854125656',1000,GETDATE()) + + + +select * from AccountInfo --个人信息 +select * from BankCard --银行卡信息 +select * from CardStateChange --银行卡状态更改表 +select * from CardTransfer --转账表 +select * from CardExchange --交易记录 \ No newline at end of file diff --git "a/37\345\274\240\346\255\243\350\261\252/9-20\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\345\255\220\346\237\245\350\257\242\347\254\224\350\256\260.txt" "b/37\345\274\240\346\255\243\350\261\252/9-20\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\345\255\220\346\237\245\350\257\242\347\254\224\350\256\260.txt" new file mode 100644 index 0000000000000000000000000000000000000000..a4a488b8726056ee129fa13ee97eaa1ad9a4309d --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/9-20\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\345\255\220\346\237\245\350\257\242\347\254\224\350\256\260.txt" @@ -0,0 +1,7 @@ +any -- 婊¤冻鍏朵腑涓涓潯浠 +all -- 婊¤冻鍏ㄩ儴鏉′欢 +case 瀛楁鍚 +when 鏉′欢 then 缁撴灉 +end '缁撴灉瀛楁鍚嶇О' --绛変簬 java 鐨剆witch case 鐢ㄦ硶,鍔犲湪select 鍚庨潰 +top 鏁板瓧 -- 绛変簬 sql 鐨刲imit鐢ㄦ硶锛屽姞鍦╯elect 鍚庨潰 + diff --git "a/37\345\274\240\346\255\243\350\261\252/9-23\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" "b/37\345\274\240\346\255\243\350\261\252/9-23\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..aa26ef324d5c30fdfe25e09a87e6cefbff4150d4 --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/9-23\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,14 @@ +--使用STUDENTS数据库 +use STUDENTS; +--1.创建tb_student(name)索引 填充因子设为50 +create nonclustered index IX_name on tb_student(name) +with( +fillfactor = 50 +); +--2.创建tb_record(borrow_time,return_time) 使用索引查询没还书的同学并且让没还书的同学将图书归还 +create nonclustered index IX_return on tb_record(return_time); +select * from tb_record with (index = IX_return) where return_time is null; +--3.增加新列id() 创建聚集索引tb_book(id) +alter table tb_book add id int; +create index IX_id on tb_book(id); +drop index IX_RealName on AccountInfo \ No newline at end of file diff --git "a/37\345\274\240\346\255\243\350\261\252/9-23\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" "b/37\345\274\240\346\255\243\350\261\252/9-23\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" new file mode 100644 index 0000000000000000000000000000000000000000..902741eba5307a10a9f764eadc120d7f9d76ff0e --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/9-23\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" @@ -0,0 +1,9 @@ +IX_ -- 绱㈠紩鍛藉悕瑙勮寖 +nonclustered -- 闈炶仛闆嗙储寮 +clustered -- 鑱氶泦绱㈠紩 +with( +fillfactor = 鏁板 +) -- 濉厖鍥犲瓙 +with (index = 绱㈠紩鍚) -- 绱㈠紩鏌ヨ + + diff --git "a/37\345\274\240\346\255\243\350\261\252/9.21\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" "b/37\345\274\240\346\255\243\350\261\252/9.21\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6139d2eae3c1ba743bfae4dc67bbd6f2639eef4a --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/9.21\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,41 @@ +use TEST04; +select * from Student;--学生表 +select * from Course;--课程表 +select * from Teacher;--教师表 +select * from SC;--成绩表 +--13. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 +select * from SC;--成绩表 + +select * from SC S1 +join (select [SId],AVG(score) 平均分 from SC +group by [SId]) S2 on S2.[SID] = S1.[SID] +order by 平均分 desc; +--14. 查询各科成绩最高分、最低分和平均分: + +--以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +--及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + +--要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 +select * from (select'01' 课程ID,'语文'课程name, max(score) 最高分,min(score)最低分,avg(score)平均分 from SC where CID = 01) S1, +(select'02' 课程ID,'数学'课程name, max(score) 最高分,min(score)最低分,avg(score)平均分 from SC where CID = 02)S2, +(select'03' 课程ID,'数学'课程name, max(score) 最高分,min(score)最低分,avg(score)平均分 from SC where CID = 03)S3; + +--15. 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 +select S.*,Rank() over (order by score) as '排名' from + SC S; +--15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 +select S.*,dense_rank() over (order by score) as '排名' from + SC S; +--16. 查询学生的总成绩,并进行排名,总分重复时保留名次空缺 +select S.*,Rank() over (order by 总分) as '排名' from + (select [SID],sum(score)总分 from SC group by [SID]) S; +--16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 +select * from SC;--成绩表 + +select S.*,ROW_NUMBER() over (order by 总分) as '排名' from + (select [SID],sum(score)总分 from SC group by [SID]) S; + +--select [SID],sum(score) 总分 from SC +--group by [SID] +--order by sum(score) desc; \ No newline at end of file diff --git "a/37\345\274\240\346\255\243\350\261\252/9.21\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" "b/37\345\274\240\346\255\243\350\261\252/9.21\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" new file mode 100644 index 0000000000000000000000000000000000000000..ff6fe98c1de521efce1febb8d412749eb85c476e --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/9.21\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" @@ -0,0 +1,6 @@ +row_number() over (partition by 瀛楁鍚 order by 瀛楁鍚 ) +-- partition by 绛変簬 group by 鐨勭敤娉曪紝row_number() 绐楀彛鍑芥暟銆愭暟鎹噸澶嶆椂缁х画鎸夐『搴忔帓銆 + +rank() --銆愭暟鎹噸澶嶆椂锛屾帓搴忛噸澶嶏紝涓嶄繚鐣欏悕娆$┖缂恒 + +dense_rank() --銆愭暟鎹噸澶嶆椂锛屾帓搴忛噸澶嶏紝淇濈暀鍚嶆绌虹己銆 \ No newline at end of file diff --git "a/37\345\274\240\346\255\243\350\261\252/9.22\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" "b/37\345\274\240\346\255\243\350\261\252/9.22\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..11f636c2f54ea9cd6dc78963577b396e799defab --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/9.22\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,85 @@ +锘縰se banktest1; +select * from AccountInfo; --涓汉淇℃伅 +select * from BankCard; --閾惰鍗′俊鎭 +select * from CardStateChange; --閾惰鍗$姸鎬佹洿鏀硅〃 +select * from CardTransfer; --杞处琛 +select * from CardExchange; --浜ゆ槗璁板綍 +--1. 涓鸿档浜戞浜鸿繘琛屽紑鎴峰紑鍗℃搷浣滐紝璧典簯韬唤璇侊細420107199904054233 +declare @name varchar(20),@id varchar(20) +set @name = '璧典簯' +set @id = '420107199904054233' +insert into AccountInfo values(@id,'13554785426',@name,'2022-09-19 19:20:00') +go +--2. 闇瑕佹眰鍑哄紶椋炵殑閾惰鍗″崱鍙峰拰浣欓锛屽紶椋炶韩浠借瘉锛420107199602034138 锛1.浣跨敤杩炴帴鏌ヨ锛2.浣跨敤鍙橀噺锛 + +--#### 閫昏緫鎺у埗 + +--##### 鏉′欢鍒嗘敮if-else +declare @id varchar(20) +set @id = '420107199602034138' +select b.CardNo 鍗″彿,b.CardMoney 浣欓 from BankCard b +inner join AccountInfo a +on a.AccountId = b.AccountId +where a.AccountCode = @id +go +--鈥 3.鏌愮敤鎴烽摱琛屽崱鍙蜂负鈥6225547854125656鈥濓紝璇ョ敤鎴锋墽琛屽彇閽辨搷浣滐紝鍙栭挶5000鍏冿紝浣欓鍏呰冻鍒欒繘琛屽彇閽辨搷浣滐紝骞舵彁绀"鍙栭挶鎴愬姛"锛屽惁鍒欐彁绀衡滀綑棰濅笉瓒斥濄 + +--##### 鏉′欢鍒嗘敮:case-when +declare @Moey int +select @Moey = CardMoney from BankCard where CardNo='6225547854125656' +print @Moey +select *,case +when @Moey>5000 then '鍙栭挶鎴愬姛' +else '浣欓涓嶈冻' +end + from BankCard where CardNo = '6225547854125656' + +--鈥 4.鏌ヨ閾惰鍗′俊鎭紝灏嗛摱琛屽崱鐘舵1,2,3,4鍒嗗埆杞崲涓烘眽瀛椻滄甯,鎸傚け,鍐荤粨,娉ㄩ攢鈥濓紝骞朵笖鏍规嵁閾惰鍗′綑棰濇樉绀洪摱琛屽崱绛夌骇 30涓囦互涓嬩负鈥滄櫘閫氱敤鎴封,30涓囧強浠ヤ笂涓"VIP鐢ㄦ埛"锛 + +----鏄剧ず鍒楀垎鍒负鍗″彿锛岃韩浠借瘉锛屽鍚嶏紝浣欓锛岀敤鎴风瓑绾э紝閾惰鍗$姸鎬併 + +--##### **while** +select t.CardNo 鍗″彿,a.AccountCode 韬唤璇,a.RealName 濮撳悕,t.CardMoney 浣欓, +case +when t.CardMoney < 300000 then '鏅氱敤鎴' +when t.CardMoney >= 300000 then 'VIP鐢ㄦ埛' +end 鐧昏 +from AccountInfo a +inner join +( +select * , +case +when b.CardState = 1 then '姝e父' +when b.CardState = 2 then '鎸傚け' +when b.CardState = 3 then '鍐荤粨' +when b.CardState = 4 then '娉ㄩ攢' +end 鐘舵 +from BankCard b +) t +on a.AccountId = t.AccountId +go + +--鈥 5.寰幆鎵撳嵃1-10銆 +declare @i int +set @i = 1 + +while (@i<=10) +begin + print @i + set @i = @i +1 +end +go +--鈥 6.鎵撳嵃99涔樻硶琛 +declare @i int = 1 +while @i <= 9 + begin + declare @j int = 1 + declare @result varchar(200) = '' + while @j <= @i + begin + set @result = @result + cast(@i as varchar(1))+ ' * ' + cast(@j as varchar(1)) + ' = '+ cast(@i*@j as varchar(2)) + char(9) + set @j = @j + 1 + end + print @result + set @i = @i + 1 + end \ No newline at end of file diff --git "a/37\345\274\240\346\255\243\350\261\252/9.22\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" "b/37\345\274\240\346\255\243\350\261\252/9.22\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" new file mode 100644 index 0000000000000000000000000000000000000000..a084603d315d9390890ddb6919b50d232852cd2f --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/9.22\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" @@ -0,0 +1,8 @@ +- set @鍙橀噺鍚 = 鍊 + +- select @鍙橀噺鍚 = 鍊 +--渚: + select @id = 1001 + set @name = '鍛ㄩ' +case +when then