From 5acc6afe2b78b176a4e0b4c244ce1e50c1f78084 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=B8=96=E8=B4=A2?= <10033750+wang-shicaishicai@user.noreply.gitee.com> Date: Tue, 27 Sep 2022 23:26:46 +0000 Subject: [PATCH] =?UTF-8?q?20=E7=8E=8B=E4=B8=96=E8=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 王世财 <10033750+wang-shicaishicai@user.noreply.gitee.com> --- ...7\346\234\254\346\226\207\346\241\243.sql" | 62 +++++++++++++++++++ ...03\346\254\241\350\247\206\345\233\276.md" | 29 +++++++++ 2 files changed, 91 insertions(+) create mode 100644 "20\347\216\213\344\270\226\350\264\242/2022-9-26\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\347\254\224\350\256\260/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.sql" create mode 100644 "20\347\216\213\344\270\226\350\264\242/2022-9-26\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\347\254\224\350\256\260/\347\254\254\344\270\203\346\254\241\350\247\206\345\233\276.md" diff --git "a/20\347\216\213\344\270\226\350\264\242/2022-9-26\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\347\254\224\350\256\260/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.sql" "b/20\347\216\213\344\270\226\350\264\242/2022-9-26\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\347\254\224\350\256\260/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.sql" new file mode 100644 index 0000000..149514e --- /dev/null +++ "b/20\347\216\213\344\270\226\350\264\242/2022-9-26\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\347\254\224\350\256\260/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.sql" @@ -0,0 +1,62 @@ +--编写视图实现查询出所有银行卡账户信息,显示卡号,身份证,姓名,余额。 +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 table [mod] ( + [year] int, + [month] int, + amount float +) +insert into [mod] values +(1991,1,1.1), +(1991,2,1.2), +(1991,3,1.3), +(1991,4,1.4), +(1992,1,2.1), +(1992,2,2.2), +(1992,3,2.3), +(1992,4,2.4) +go + +--列转行 +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 [mod] +group by [year] + + +select * from [mod] +pivot +( +sum(amount) +for [month] in ([1],[2],[3],[4]) +) as amount + + +CREATE TABLE Sales( +[Year] INT, +A1 INT, +A2 INT, +A3 INT, +A4 INT); +GO +INSERT INTO Sales VALUES +(2017,12,123,324,123), +(2018,13,455,324,878), +(2019,29,786,473,633); +--行转列 +select [Year],'A1' as peopel,A1 as Amonut from Sales union all +select [Year],'A2' as peopel,A2 as Amonut from Sales union all +select [Year],'A3' as peopel,A3 as Amonut from Sales union all +select [Year],'A4' as peopel,A4 as Amonut from Sales + +select * from Sales +unpivot (Amount for people in (A1,A2,A3,A4)) as a \ No newline at end of file diff --git "a/20\347\216\213\344\270\226\350\264\242/2022-9-26\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\347\254\224\350\256\260/\347\254\254\344\270\203\346\254\241\350\247\206\345\233\276.md" "b/20\347\216\213\344\270\226\350\264\242/2022-9-26\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\347\254\224\350\256\260/\347\254\254\344\270\203\346\254\241\350\247\206\345\233\276.md" new file mode 100644 index 0000000..b64e637 --- /dev/null +++ "b/20\347\216\213\344\270\226\350\264\242/2022-9-26\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\347\254\224\350\256\260/\347\254\254\344\270\203\346\254\241\350\247\206\345\233\276.md" @@ -0,0 +1,29 @@ +## 视图 + +**对视图的总结:** + +1. 视图是通过基表(可以是多个基表)来创建的,视图是虚拟的表 +2. 视图也有字段,数据来自基表 +3. 通过视图可以修改基表的数据 +4. 基表的改变,也会影响到视图的数据 + +**视图的使用:** + + + +1. 创建视图:create view 视图名 as select 字段1,字段2 .......from 表名 +2. 修改视图:alter view 视图名 as select 字段1,字段2 .......from 表名 +3. 查看创建视图的指令:show create view 视图名 +4. 删除视图:drop view 视图名 + +**细节** + +1. 创建视图后,到数据库去看,对应的=视图只有一个视图结构文件(格式:视图名.frm),并没有数据文件 +2. 视图的数据变化会影响到基本,基表的数据变化也会影响到视图【dml操作】 +3. 视图中可以再使用视图 + +**视图好处** + +1. **安全**。一些数据表有着重要的信息。有些字段是保密的,不能让其他用户直接看到,这时就可以创建一个视图,在这张视图中只保留一部分字段,这样,用户就可以查询自己需要的字段,不能查看保密的字段 +2. **性能**。关系数据库的数据常常会分表存储,使用外键进行关联。这时数据库查询通常会建立连接(join)。这样做不但麻烦,效率相对也比较低。如果建立一个视图,将相关的表和字段组合在一起,就可以避免使用join查询数据 +3. **灵活**。如果系统中有一张旧表,这张表由于设计的问题,即将废弃。然而,很多应用都是基于这张表的,不易修改,这时就可以建立一张视图表,视图中的数据直接映射到新建的表。这样,就可以少做很多改动,也达到了了升级数据表的目的 \ No newline at end of file -- Gitee