From 1f64bdc772472feb1485397a39b445a7b3485a8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE?= <3032059138@qq.com> Date: Tue, 27 Sep 2022 10:52:03 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 袁贵森 <3032059138@qq.com> --- ...6\345\233\276\344\275\234\344\270\232.sql" | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 "28\350\242\201\350\264\265\346\243\256/\344\275\234\344\270\232/2022-0926-\350\247\206\345\233\276\344\275\234\344\270\232.sql" diff --git "a/28\350\242\201\350\264\265\346\243\256/\344\275\234\344\270\232/2022-0926-\350\247\206\345\233\276\344\275\234\344\270\232.sql" "b/28\350\242\201\350\264\265\346\243\256/\344\275\234\344\270\232/2022-0926-\350\247\206\345\233\276\344\275\234\344\270\232.sql" new file mode 100644 index 0000000..0a60f69 --- /dev/null +++ "b/28\350\242\201\350\264\265\346\243\256/\344\275\234\344\270\232/2022-0926-\350\247\206\345\233\276\344\275\234\344\270\232.sql" @@ -0,0 +1,27 @@ +use banktest; +go + +--1)编写视图实现查询出所有银行卡账户信息,显示卡号,身份证,姓名,余额。 +go +create view V_Acid(卡号,身份证,姓名,余额) +as +select b.CardNo,a.AccountCode,RealName,CardMoney from BankCard b join AccountInfo a on b.AccountId=a.AccountId +go +select * from V_Acid + +--2)行转列常用做法: group by + sum(case when) /+count(case when) 数据分析+ over (paritition by, order by) + +​-- 提示: pivot + +select a.RealName 姓名,a.AccountId 编号, +sum(case when a.RealName='刘备' then a.AccountId else 1 end) 'asc-14', +sum(case when a.RealName='张飞' then a.AccountId else 1 end) 'xzy-254', +sum(case when a.RealName='关羽' then a.AccountId else 1 end) 'mnb-215' +from BankCard b join AccountInfo a on b.AccountId=a.AccountId +group by a.RealName,a.AccountId + +select RealName,AccountPhone from AccountInfo where AccountId='1' +union +select RealName,AccountPhone from AccountInfo where AccountId='2' +union +select RealName,AccountPhone from AccountInfo where AccountId='3' \ No newline at end of file -- Gitee From 9c00c91e463cc8eefb17aa4f48b0e38dd0128a6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE?= <3032059138@qq.com> Date: Tue, 27 Sep 2022 10:52:24 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 袁贵森 <3032059138@qq.com> --- ...6\345\233\276\347\254\224\350\256\260.txt" | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 "28\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260/2022-0926-\350\247\206\345\233\276\347\254\224\350\256\260.txt" diff --git "a/28\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260/2022-0926-\350\247\206\345\233\276\347\254\224\350\256\260.txt" "b/28\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260/2022-0926-\350\247\206\345\233\276\347\254\224\350\256\260.txt" new file mode 100644 index 0000000..66504dc --- /dev/null +++ "b/28\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260/2022-0926-\350\247\206\345\233\276\347\254\224\350\256\260.txt" @@ -0,0 +1,51 @@ +-------------------视图---------------- +视图是一张虚拟表,但这个表中不存储数据,虚拟的表。 +作用:可以将重复、多次使用的查询语句封装到视图里面, +以供多次使用。方便使用。 +视图直接当成表来使用就可以, +可以写where条件,也可以参与跟其他表的关联查询。 +不可以使用参数,只有查询语句 + +create view v_cj +as +select s.sname,c.cname,sc.score +from Student s,course c ,sc +where s.sid=sc.sid +and c.cid=sc.cid +使用: +select * from v_cj +where sname='刘一' + + +存储过程与视图的区别 +1、视图里面只有查询语句,但存储过程里面可以写增删改查的语句 +2、视图不可以使用参数,没有返回值。存储过程可以使用参数,可以返回值 +3、使用时视图可以跟表一样使用,可以加where条件 ,但存储过程不可以 + +--------------------索引-------------------- +索引的作用:可以在查询时快速查询到数据,提高查询的效率。 +可以解决大部分数据库慢的问题。 +分类: +聚集索引 +聚集索引一般是创建在主键列上的, +一般一张表上只允许创建一个聚集索引,一般会在主键上默认创建聚集索引。 +特点:存储的数据是跟表的数据有一样的物理顺序(地址), +这个地址是连续的。类似于使用拼音查字典,查询到的文字在一起 + + +如果没有索引,则会使用全表扫描。(将整个表的数据查询一遍) +如果有索引,则会使用索引查询,例如: +select * from student +where sid=2 +索引在存储时其实就可以看成一张表,物理顺序(地址) 2 +根据2查询出物理顺序(地址),这时只是在索引中查询 +根据物理顺序(地址)--去表里面查询这条数据 + + +非聚集索引 +特点:它存储的物理地址不是连续的, +它的查询速度要低于聚集索引,但是也比没有索引强 +一个表可以创建多个非聚集索引。类似于部首查字典。查询到的文字可能不在一起 + +唯一索引 +组合索引:在多个列上创建索引 \ No newline at end of file -- Gitee