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 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 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 0000000000000000000000000000000000000000..c422e482814f3cb2534758697970e72cee994dbb --- /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 0000000000000000000000000000000000000000..a28c953b0ce48f316c8903b68581afe879617e0a --- /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 + +