From 7f7f13bad9ca47951cb34a22053e0f608a29b896 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=B6=9B?= <1986456126@qq.com> Date: Tue, 27 Sep 2022 15:20:56 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=BB=BA=202022-09-27=EF=BC=88?= =?UTF-8?q?=E7=AC=AC=E4=B8=83=E6=AC=A1=E4=BD=9C=E4=B8=9A=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "09\346\235\216\346\266\233/2022-09-27\357\274\210\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\357\274\211/.keep" diff --git "a/09\346\235\216\346\266\233/2022-09-27\357\274\210\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\357\274\211/.keep" "b/09\346\235\216\346\266\233/2022-09-27\357\274\210\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\357\274\211/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 10bf6c870a19641bf095ffd4b9f418568d51bb27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=B6=9B?= <1986456126@qq.com> Date: Tue, 27 Sep 2022 15:21:42 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9D=8E=E6=B6=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 鏉庢稕 <1986456126@qq.com> --- .../SQLQuery2.sql" | 41 ++++++++ .../\347\254\224\350\256\260.sql" | 97 +++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 "09\346\235\216\346\266\233/2022-09-27\357\274\210\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\357\274\211/SQLQuery2.sql" create mode 100644 "09\346\235\216\346\266\233/2022-09-27\357\274\210\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\357\274\211/\347\254\224\350\256\260.sql" diff --git "a/09\346\235\216\346\266\233/2022-09-27\357\274\210\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\357\274\211/SQLQuery2.sql" "b/09\346\235\216\346\266\233/2022-09-27\357\274\210\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\357\274\211/SQLQuery2.sql" new file mode 100644 index 0000000..c422e48 --- /dev/null +++ "b/09\346\235\216\346\266\233/2022-09-27\357\274\210\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\357\274\211/SQLQuery2.sql" @@ -0,0 +1,41 @@ +--编写视图实现查询出所有银行卡账户信息,显示卡号,身份证,姓名,余额。 +create view V_AIfo(卡号,身份证,姓名,余额) +as +select b.CardNo,a.AccountCode,a.RealName,b.CardMoney from AccountInfo a +inner join BankCard b on a.AccountId = b.AccountId +go + +select * from V_AIfo + + +create database HTZQ +go +use HTZQ +go + +create table Info( +id int primary key identity(1,1) not null, +[year] int not null, +[month] int not null, +[amount] decimal(10,2) not null +) + +insert into Info values +('1991',1,1.1), +('1991',2,1.2), +('1991',3,1.3), +('1991',4,1.4), +('1992',5,1.5), +('1992',6,1.6), +('1992',7,1.7), +('1992',8,1.8) + + +select [year], +max(case when [month] = 1 then amount else 0 end) m1, +max(case when [month] = 2 then amount else 0 end) m2, +max(case when [month] = 3 then amount else 0 end) m3, +max(case when [month] = 4 then amount else 0 end) m4 + from Info +group by [year] + diff --git "a/09\346\235\216\346\266\233/2022-09-27\357\274\210\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\357\274\211/\347\254\224\350\256\260.sql" "b/09\346\235\216\346\266\233/2022-09-27\357\274\210\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\357\274\211/\347\254\224\350\256\260.sql" new file mode 100644 index 0000000..a28c953 --- /dev/null +++ "b/09\346\235\216\346\266\233/2022-09-27\357\274\210\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\357\274\211/\347\254\224\350\256\260.sql" @@ -0,0 +1,97 @@ +use Students +go + +select * from tb_inf_student +--建立各个生源地学生的荣誉总数视图 + + + +--触发器,存储过程 +--视图: +--数据表:1.基表 2.临时表(select,) 3.视图(虚拟表) + + +--创建 view:视图 index:索引 proc:存储过程 + + + +--查询学生信息:学号,姓名,学院 +select stu_num, name, school from tb_student + +go +create view V_GetInfo +as +select stu_num, name, school from tb_student +go +--使用 +--视图是一张虚拟表 +select * from V_GetInfo --》视图转化: select stu_num, name, school from tb_student + +--删除 +--drop view V_GetInfo + + +--统计各个生源地的荣誉总数 +select * from tb_inf_student + +select ori_loca , sum(prize) 荣誉总数 from tb_inf_student group by ori_loca + + +create view V_GetTotalPrize(生源地,荣誉总数) +as +select ori_loca , sum(prize) from tb_inf_student group by ori_loca + +select * from V_GetTotalPrize + +drop view V_GetTotalPrize + + +--建立信息学院学生的视图 +create view V_Info +as +select * from tb_student where school='信息学院' +with check option --涉及的增删只能对信息学院的学生进行操作 + +--drop view V_Info + +select * from V_Info + +update V_Info set gender=1 where name = '博文' + +insert into V_info values('10114','蜗牛',1,getdate(),'理学院','计科') + + +--定义一个反映学生年龄的视图 +create view V_GetAgeByBirth(学号,姓名,年龄) +as +select stu_num,name, (year(getdate()) - year(birth)) from tb_student + +select * from V_GetAgeByBirth + +--建立信息学院学生的特长属于艺术特长视图 +select * from tb_student +select * from tb_inf_student + +select * from tb_student join tb_inf_student on tb_student.stu_num = tb_inf_student.stu_num where school='信息学院' and speciality='艺术特长' + +go +create view V_GetSpecBySch +as +select tb_student.*,hobby,speciality,ori_loca,prize from tb_student join tb_inf_student on tb_student.stu_num = tb_inf_student.stu_num where school='信息学院' and speciality='艺术特长' +go + +select * from V_GetSpecBySch where ori_loca = '北京' + +--drop view V_GetSpecBySch + + + +create view V_GetOriInBJ +as +select * from V_GetSpecBySch where ori_loca = '北京' + +select * from V_GetOriInBJ + +pivot + + -- Gitee