diff --git "a/24\345\274\240\346\267\221\350\212\263/9-26-\350\247\206\345\233\276/9-26-\344\275\234\344\270\232.sql" "b/24\345\274\240\346\267\221\350\212\263/9-26-\350\247\206\345\233\276/9-26-\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8c49d1e27d16333d8fd56d668b3ba93e9d32d060 --- /dev/null +++ "b/24\345\274\240\346\267\221\350\212\263/9-26-\350\247\206\345\233\276/9-26-\344\275\234\344\270\232.sql" @@ -0,0 +1,85 @@ + +use BankTest +go + +--1)编写视图实现查询出所有银行卡账户信息,显示卡号,身份证,姓名,余额。 +create view v_account +as +select CardNo,AccountCode,RealName,CardMoney from AccountInfo a +join BankCard b on b.AccountId = a.AccountId +go +select * from v_account +go + + +--2)行转列常用做法: group by + sum(case when) /+count(case when) 数据分析+ over (paritition by, order by) +--​ 提示: pivot + +create database sec +go +use sec +go +create table secc +( +[year] int, +[month] int, +amount float +) +go +insert into secc([year],[month],amount) 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 * from secc +go + +------ +select * from secc +pivot( +MIN(amount) for [month] in ([1],[2],[3],[4]) +) as aa + +------ +select [year], +MIN((case when [month] = 1 then amount else 404 end))m1, +MIN((case when [month] = 2 then amount else 404 end))m2, +MIN((case when [month] = 3 then amount else 404 end))m3, +MIN((case when [month] = 4 then amount else 404 end))m4 +from secc +group by [year] + + +--列转行 union +--drop table stu +create table stu( +name varchar(20), +english int, +maths int, +music int +) +go + +insert into stu(name,english,maths,music) values +('Jim',90,88,99) +go + +----- +select name,'english' [subject],90 score from stu +union +select name,'maths' [subject],88 score from stu +union +select name,'music' [subject],99 score from stu +go + +----- +select name,subject,score from stu +unpivot( +score for subject in(english,maths,music) +)as v +go diff --git "a/24\345\274\240\346\267\221\350\212\263/9-26-\350\247\206\345\233\276/9-26-\347\254\224\350\256\260-\350\247\206\345\233\276.md" "b/24\345\274\240\346\267\221\350\212\263/9-26-\350\247\206\345\233\276/9-26-\347\254\224\350\256\260-\350\247\206\345\233\276.md" new file mode 100644 index 0000000000000000000000000000000000000000..4b4ed6d8f5be662c80e0ea84e7a8e7d40ded208b --- /dev/null +++ "b/24\345\274\240\346\267\221\350\212\263/9-26-\350\247\206\345\233\276/9-26-\347\254\224\350\256\260-\350\247\206\345\233\276.md" @@ -0,0 +1,272 @@ +# SQL SERVER + +## -- 9.15 + +#### 创建数据库 + +```sql +//1 +create database 数据库名; +//2 +if exists (select * from sys.databases where name = '数据库名') +drop database 数据库名; +``` + +#### 创建数据表 + +```sql +create table 表名( + +); +``` + +#### 删除表 + +```sql +drop table 表名; +``` + +#### 修改表结构 + +```sql +修改表结构 表名 add constraint 约束名 foreign key(要引用的字段) references 主键表(字段) +``` + + + +非空 not null + +主键(标识列) primary key + +自增 identity(从几开始,一次多少) + +检查约束 check() + +外键 references + + + +## --9.16 + +不重复 distinct + +排序 order by (desc 降序 asc 升序) + +**count()** + +count(*):空值也统计 + +count(字段):当前字段的空值不统计 + +**保留小数点后几位** + +```sql +round(小数,保留位数); +decimal(5,2); +``` + +**强制转换** + +```sql +CONVERT(VARCHAR(19),GETDATE()); +CAST('9.0' AS decimal); +``` + +**删除** +drop 删除整张表 +truncate删除整张表,表还在 + +## --9.16 + +![image-20220920232835355](C:\Users\Agares\AppData\Roaming\Typora\typora-user-images\image-20220920232835355.png) + +### exists + +关键字EXISTS构造子查询时,当子查询的结果集不为空时,则EXISTS返回的结果为TRUE,外层查询语句进行查询;当子查询的结果集为空时,则EXISTS返回的结果为FALSE,外层查询语句不进行查询。 + +```sql +--(1)查询学生表(tb-student)中,是否有学生读者的姓名是"黄弘" +if exists(select stu_num from tb_record where stu_num in (select stu_num from tb_student where name='黄弘') and return_time is null) + print '有' +else + print '没有' + +--(2)查询书目表(tb-bibliography)中,库存数为0的书目名称(name) +``` + + + +排名 rank() over() + +### 排序函数 + +**排序函数 OVER( [分组子句] 排序子句[DESC][ASC] )** + +**排序子句 :ORDER BY 排序列,排序列…** + +练习:使用各个排名函数对学员的Java 成绩进行排名,并仔细体会其中排序函数的具体用法与其中的区别。 + +```sql + +``` + +- ROW_NUMBER()函数生成的排序根据排序子句给出**递增连续的序号** +- RANK()函数生成的排序根据排序子句给出**递增的序号,但是存在并列并且跳空** +- DENSE_RANK() 函数生成的排序根据排序子句给出**递增的序号,但是存在并列不跳空** + + + + + +### 9-21 + +--查询男生和女生中年龄最大的学生信息,要求显示:姓名,性别,年龄 +--非关联子查询 + +```sql +select birth,name 姓名,gender 性别 from tb_student +where birth in (select min(birth) from tb_student group by gender) +``` + +--关联子查询:内部查询需要引用外部表已查询出的信息 + +```sql +select birth,name 姓名,gender 性别 from tb_student t1 +where birth = (select min(birth) from tb_student t2 where t2.gender=t1.gender) +``` + +--窗口函数:排序 + +```sql +select birth,姓名,性别 from ( +select birth,name 姓名,gender 性别,row_number() over (partition by gender order by birth ) as birth_rank from tb_student +) as T1 +where T1.birth_rank = 1 +``` + + + +### 9-22 + +#### 局部变量 + +局部变量必须以标记@作为前缀 ,如@age +局部变量的使用是先声明,再赋值 +局部变量只在定义它的局部范围内有效 + +声明一个局部变量:**declare @变量名 数据类型** + +```sql +--例: + declare @id int --声明一个名为id的整型变量 + declare @name varchar(50) --声明一个可变长度为50的存放姓名的字符串变量 + +``` + +赋值: + +- set @变量名 = 值 + +- select @变量名 = 值 + +```sql +--例: + select @id = 1001 + set @name = '周飘' +``` + +set与select赋值的区别: + +set赋值给变量指定的值,select一般用于表中查询出的数据赋值给变量,如果查询结果有多条,取最后一条赋值给变量 + +### 输出语句 + +1. print变量或表达式 +2. select变量或表达式 + +```sql +--例: + print '数据库服务器名:' + @@servicename + select 15*8 +``` + +### 逻辑控制语句 + +```sql +-- if() else +if() +begin + ... +end +else + ... +end + +-- switch case +case +when 条件 then 结果 +else 其他结果 +end +``` + +### 循环控制语句 + +```sql +while() +begin + ... +end +``` + + + +## --9.26 + +### 视图 + +视图是一张虚拟表,它表示一张表的部分数据或多张表的综合数据,其结构和数据是建立在对表的查询基础上 +视图中并不存放数据,而是存放在视图所引用的原始表(基表)中,即基本中的数据发生变化,从视图中查询的数据也随之改变。 +同一张原始表,根据不同用户的不同需求,可以创建不同的视图 + +```sql +--建立视图 +create view <视图名> [(<列名>[,<列名>]...)] + as <子查询> [with check option] +``` + +### pivot(行转列) + +```sql +pivot( +数据 for 要转成列的玩意 in (列名1,列名2,...) +) as 名字 +``` + +### union(列转行) + +```sql +--union,拼接多表 +select * from 表1 +union +select * from 表2 + +--unpivot,什么玩意 +unpivot( +score for subject in(english,maths,music) +)as v +``` + + + + + + + + + + + + + + +