From 75670b01b152ad39e9eaa9fea35eecbe9b38af9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Mon, 19 Sep 2022 09:31:08 +0000 Subject: [PATCH 01/19] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E6=9D=A8=E5=87=8C?= =?UTF-8?q?=E7=BF=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\346\235\250\345\207\214\347\277\224/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\346\235\250\345\207\214\347\277\224/.keep" diff --git "a/\346\235\250\345\207\214\347\277\224/.keep" "b/\346\235\250\345\207\214\347\277\224/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From f4ecce90114f9594d2cdba32f738afeee8d8042a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Mon, 19 Sep 2022 09:31:22 +0000 Subject: [PATCH 02/19] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20=E6=9D=A8?= =?UTF-8?q?=E5=87=8C=E7=BF=94=20=E4=B8=BA=2022=20=E6=9D=A8=E5=87=8C?= =?UTF-8?q?=E7=BF=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.keep" => "22 \346\235\250\345\207\214\347\277\224/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "\346\235\250\345\207\214\347\277\224/.keep" => "22 \346\235\250\345\207\214\347\277\224/.keep" (100%) diff --git "a/\346\235\250\345\207\214\347\277\224/.keep" "b/22 \346\235\250\345\207\214\347\277\224/.keep" similarity index 100% rename from "\346\235\250\345\207\214\347\277\224/.keep" rename to "22 \346\235\250\345\207\214\347\277\224/.keep" -- Gitee From fb0db0c890ec8743d077d81b7d649384aec164b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Mon, 19 Sep 2022 09:31:43 +0000 Subject: [PATCH 03/19] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E7=AC=AC=E4=B8=80?= =?UTF-8?q?=E6=AC=A1=E4=BD=9C=E4=B8=9A?= 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 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/.keep" diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/.keep" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 6623fea7cca2bbe86265279fe17f839bb526da71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Mon, 19 Sep 2022 09:31:57 +0000 Subject: [PATCH 04/19] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2022?= =?UTF-8?q?=20=E6=9D=A8=E5=87=8C=E7=BF=94/.keep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "22 \346\235\250\345\207\214\347\277\224/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 "22 \346\235\250\345\207\214\347\277\224/.keep" diff --git "a/22 \346\235\250\345\207\214\347\277\224/.keep" "b/22 \346\235\250\345\207\214\347\277\224/.keep" deleted file mode 100644 index e69de29..0000000 -- Gitee From 1a37e5349cd4a73a8c368b9baca3cf4ef5cfaa4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Mon, 19 Sep 2022 09:32:18 +0000 Subject: [PATCH 05/19] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E7=AC=AC=E4=BA=8C?= =?UTF-8?q?=E6=AC=A1=E4=BD=9C=E4=B8=9A?= 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 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/.keep" diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/.keep" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From c1b3ce0ee4cf4a24d63405c8bfb3329e3f2bc921 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Mon, 19 Sep 2022 09:36:05 +0000 Subject: [PATCH 06/19] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 杨凌翔 <963304364@qq.com> --- .../\344\275\234\344\270\232/student.sql" | 54 +++++++++++++++++++ ...72\345\272\223\345\273\272\350\241\250.md" | 54 +++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/student.sql" create mode 100644 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/2020-9-16-\345\273\272\345\272\223\345\273\272\350\241\250.md" diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/student.sql" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/student.sql" new file mode 100644 index 0000000..a8b86a8 --- /dev/null +++ "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/student.sql" @@ -0,0 +1,54 @@ +CREATE DATABASE DBTEST +create table sectionInfo( + sectionID int primary key identity(1,1), + sectionName varchar(10) NOT NULL + ); + + create table userInfo( + userNo int primary key identity(1,1) NOT NULL, + userName varchar(10) NOT NULL UNIQUE, + userSex varchar(2) not null default('男') check(userSex='男' or userSex='女'), + userAge INT NOT NULL CHECK(userAge>=1 AND userAge<=100), + userAddress VARCHAR(50) DEFAULT('江西') NOT NULL, + userSection INT + ); + Alter table userInfo add constraint FK_StuInfo_ClassId foreign key(userSection) references sectionInfo(sectionID) + CREATE TABLE workInfo( + workId INT primary key identity(1,1), + userId INT, + workTime DATETIME NOT NULL, + workDescription varchar(40) CHECK(workDescription='迟到' OR workDescription='早退' OR workDescription='旷工'OR workDescription='病假'OR workDescription='事假') + ); + Alter table workInfo add constraint FK_workInfo_ClassId foreign key(userId) references userInfo(userNo); + insert into sectionInfo VALUES('运行部'); + INSERT INTO userInfo + ( + userName, + userSex, + userAge, + userAddress, + userSection + ) + VALUES + ( '张三', -- userName - varchar(10) + '男', -- userSex - varchar(2) + 18, -- userAge - int + '零零', -- userAddress - varchar(50) + 2 -- userSection - int + ); + INSERT INTO workInfo + ( + userId, + workTime, + workDescription + ) + VALUES + ( 1, -- userId - int + GETDATE(), -- workTime - datetime + '迟到' -- workDescription - varchar(40) + ) + + SELECT * FROM sectionInfo; + SELECT * FROM userInfo; + SELECT * FROM workInfo; +\ No newline at end of file \ No newline at end of file diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/2020-9-16-\345\273\272\345\272\223\345\273\272\350\241\250.md" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/2020-9-16-\345\273\272\345\272\223\345\273\272\350\241\250.md" new file mode 100644 index 0000000..6afe816 --- /dev/null +++ "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/2020-9-16-\345\273\272\345\272\223\345\273\272\350\241\250.md" @@ -0,0 +1,54 @@ +--关系型数据库:SQL server, Mysql, Oracle +--创建数据库:create database 数据库名 +--database:数据库 + +if exists (select * from sys.databases where name='DBTEST') + drop database DBTEST + + create database DBTEST + + --使用数据库 + use dbtest + + --创建班级表 + create table ClassInfo( + ClassId int primary key identity(1,1), + ClassName varchar(20) + ); + + --插入数据: insert [into] 表名(字段名) values(值) + insert into ClassInfo( ClassName) values('软件1班'); + + insert ClassInfo values('软件2班') + + select * from ClassInfo + + --创建数据表 + create table StuInfo( + stuId int primary key identity(1001,1), --学生ID + --添加一个检查约束,判断用户插入/新增的数据,性别字段是不是男或者女 + --default:默认约束 + --check + stugender varchar(2) not null default('男') check(stugender='男' or stugender='女'), --学生性别 + stuphone char(11) check(len(stuphone)=11) unique, + --创建班级外键 + --ClassID int references ClassInfo(ClassID) + ClassID int + + ); + + + --增加外键 + --修改表结构 表名 add constraint 约束名 foreign key(要引用的字段) references 主键表(字段) + Alter table StuInfo add constraint FK_StuInfo_ClassId foreign key(ClassID) references ClassInfo(ClassID) + + + --新增姓名列 + alter table StuInfo add stuName varchar(20) + + + + --如果没给出列名,默认是按照顺序一个个添加 + --insert StuInfo values('女',13888888888) + + --insert StuInfo(stuphone) values(15888888888) -- Gitee From a3c4c86aab88cf506ce221df2e4186367b08baef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Mon, 19 Sep 2022 09:37:05 +0000 Subject: [PATCH 07/19] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 杨凌翔 <963304364@qq.com> --- .../\344\275\234\344\270\232/test.sql" | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/test.sql" diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/test.sql" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/test.sql" new file mode 100644 index 0000000..03d5e5a --- /dev/null +++ "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/test.sql" @@ -0,0 +1,65 @@ +--1. 查询出武汉地区所有的员工信息,要求显示部门名称以及员工的详细资料 + +select * from People +inner join Department on People.DepartmentId=Department.DepartmentId +where PeopleAddress='武汉' + +--2. 查询出武汉地区所有的员工信息,要求显示部门名称,职级名称以及员工的详细资料 + +select * from People +inner join Department on People.DepartmentId=Department.DepartmentId +inner join Rank on People.RankId=Rank.RankId +where PeopleAddress ='武汉' + +--3. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资。 + +select DepartmentName,COUNT(People.DepartmentId)人数,SUM(PeopleSalary)工资总和,AVG(PeopleSalary)平均工资,MAX(PeopleSalary)最高工资,MIN(PeopleSalary)最低工资 from People +inner join Department on People.DepartmentId=Department.DepartmentId +group by DepartmentName + +--4. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资,平均工资在10000 以下的不参与统计,并且根据平均工资降序排列。 + +select DepartmentName,COUNT(People.DepartmentId)人数,SUM(PeopleSalary)工资总和,AVG(PeopleSalary)平均工资,MAX(PeopleSalary)最高工资,MIN(PeopleSalary)最低工资 from People +inner join Department on People.DepartmentId=Department.DepartmentId +group by DepartmentName +having AVG(PeopleSalary)>=10000 +order by AVG(PeopleSalary) desc + +--5. 根据部门名称,然后根据职位名称,分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资 + +select DepartmentName,RankId,COUNT(People.DepartmentId)人数,SUM(PeopleSalary)工资总和,AVG(PeopleSalary)平均工资,MAX(PeopleSalary)最高工资,MIN(PeopleSalary)最低工资 from People +inner join Department on People.DepartmentId=Department.DepartmentId +group by RankId,DepartmentName + +--6.查询出巨蟹 6.22--7.22 的员工信息 + +select * from People where MONTH(PeopleBirth)+CONVERT(float,DAY(PeopleBirth))/100 between 6.22 and 7.22 + +--7.查询所有员工信息,添加一列显示属相(鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪) + +select *, case + when year(PeopleBirth)%12 =0 + then '猴' + when YEAR(PeopleBirth)%12=1 + then '鸡' + when YEAR(PeopleBirth)%12=2 + then '狗' + when YEAR(PeopleBirth)%12=3 + then '猪' + when YEAR(PeopleBirth)%12=4 + then '鼠' + when YEAR(PeopleBirth)%12=5 + then '牛' + when YEAR(PeopleBirth)%12=6 + then '虎' + when YEAR(PeopleBirth)%12=7 + then '兔' + when YEAR(PeopleBirth)%12=8 + then '龙' + when YEAR(PeopleBirth)%12=9 + then '蛇' + when YEAR(PeopleBirth)%12=10 + then '马' + when YEAR(PeopleBirth)%12=11 + then '羊' + end 生肖 from People \ No newline at end of file -- Gitee From b78dcede645ec99f7dcecbd7d0f1bd4733a7faea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Mon, 19 Sep 2022 09:40:13 +0000 Subject: [PATCH 08/19] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 杨凌翔 <963304364@qq.com> --- .../2022-9-18.md" | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2022-9-18.md" diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2022-9-18.md" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2022-9-18.md" new file mode 100644 index 0000000..f19bbe1 --- /dev/null +++ "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2022-9-18.md" @@ -0,0 +1,41 @@ +| create database 创建数据库 | +| -------------------------- | +| | + +| use DBTEST使用当前数据库 | +| ------------------------ | +| | + +| create table 创建表 | +| ------------------- | +| | + +| primary key主键 | +| --------------- | +| | + +| identity(1,1)自增 | +| ----------------- | +| | + +| not null非空 | +| ------------ | +| | + +| check 约束 | +| ---------- | +| | + +| unique 唯一约束 | +| --------------- | +| | + +| references +表名(字段) 外键 | +| ----------------------------- | +| | + +| check(userAge between 1 and 100) | +| -------------------------------- | +| | + +限制年龄 \ No newline at end of file -- Gitee From b0c7fda823498c3591f3f288a79bf964fab85d40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Mon, 19 Sep 2022 09:40:30 +0000 Subject: [PATCH 09/19] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\347\254\224\350\256\260/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/.keep" diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/.keep" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 8a21b142582ce51551ee4ad46a0cdbd11f54eb4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Tue, 20 Sep 2022 07:10:33 +0000 Subject: [PATCH 10/19] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20?= =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94=2022=2001?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.keep" | 0 .../\344\275\234\344\270\232/student.sql" | 54 ------------------- ...72\345\272\223\345\273\272\350\241\250.md" | 54 ------------------- 3 files changed, 108 deletions(-) delete mode 100644 "\346\235\250\345\207\214\347\277\224 22 01/.keep" delete mode 100644 "\346\235\250\345\207\214\347\277\224 22 01/\346\235\250\345\207\214\347\277\224 22/\344\275\234\344\270\232/student.sql" delete mode 100644 "\346\235\250\345\207\214\347\277\224 22 01/\346\235\250\345\207\214\347\277\224 22/\347\254\224\350\256\260/2020-9-16-\345\273\272\345\272\223\345\273\272\350\241\250.md" diff --git "a/\346\235\250\345\207\214\347\277\224 22 01/.keep" "b/\346\235\250\345\207\214\347\277\224 22 01/.keep" deleted file mode 100644 index e69de29..0000000 diff --git "a/\346\235\250\345\207\214\347\277\224 22 01/\346\235\250\345\207\214\347\277\224 22/\344\275\234\344\270\232/student.sql" "b/\346\235\250\345\207\214\347\277\224 22 01/\346\235\250\345\207\214\347\277\224 22/\344\275\234\344\270\232/student.sql" deleted file mode 100644 index a8b86a8..0000000 --- "a/\346\235\250\345\207\214\347\277\224 22 01/\346\235\250\345\207\214\347\277\224 22/\344\275\234\344\270\232/student.sql" +++ /dev/null @@ -1,54 +0,0 @@ -CREATE DATABASE DBTEST -create table sectionInfo( - sectionID int primary key identity(1,1), - sectionName varchar(10) NOT NULL - ); - - create table userInfo( - userNo int primary key identity(1,1) NOT NULL, - userName varchar(10) NOT NULL UNIQUE, - userSex varchar(2) not null default('男') check(userSex='男' or userSex='女'), - userAge INT NOT NULL CHECK(userAge>=1 AND userAge<=100), - userAddress VARCHAR(50) DEFAULT('江西') NOT NULL, - userSection INT - ); - Alter table userInfo add constraint FK_StuInfo_ClassId foreign key(userSection) references sectionInfo(sectionID) - CREATE TABLE workInfo( - workId INT primary key identity(1,1), - userId INT, - workTime DATETIME NOT NULL, - workDescription varchar(40) CHECK(workDescription='迟到' OR workDescription='早退' OR workDescription='旷工'OR workDescription='病假'OR workDescription='事假') - ); - Alter table workInfo add constraint FK_workInfo_ClassId foreign key(userId) references userInfo(userNo); - insert into sectionInfo VALUES('运行部'); - INSERT INTO userInfo - ( - userName, - userSex, - userAge, - userAddress, - userSection - ) - VALUES - ( '张三', -- userName - varchar(10) - '男', -- userSex - varchar(2) - 18, -- userAge - int - '零零', -- userAddress - varchar(50) - 2 -- userSection - int - ); - INSERT INTO workInfo - ( - userId, - workTime, - workDescription - ) - VALUES - ( 1, -- userId - int - GETDATE(), -- workTime - datetime - '迟到' -- workDescription - varchar(40) - ) - - SELECT * FROM sectionInfo; - SELECT * FROM userInfo; - SELECT * FROM workInfo; -\ No newline at end of file \ No newline at end of file diff --git "a/\346\235\250\345\207\214\347\277\224 22 01/\346\235\250\345\207\214\347\277\224 22/\347\254\224\350\256\260/2020-9-16-\345\273\272\345\272\223\345\273\272\350\241\250.md" "b/\346\235\250\345\207\214\347\277\224 22 01/\346\235\250\345\207\214\347\277\224 22/\347\254\224\350\256\260/2020-9-16-\345\273\272\345\272\223\345\273\272\350\241\250.md" deleted file mode 100644 index 6afe816..0000000 --- "a/\346\235\250\345\207\214\347\277\224 22 01/\346\235\250\345\207\214\347\277\224 22/\347\254\224\350\256\260/2020-9-16-\345\273\272\345\272\223\345\273\272\350\241\250.md" +++ /dev/null @@ -1,54 +0,0 @@ ---关系型数据库:SQL server, Mysql, Oracle ---创建数据库:create database 数据库名 ---database:数据库 - -if exists (select * from sys.databases where name='DBTEST') - drop database DBTEST - - create database DBTEST - - --使用数据库 - use dbtest - - --创建班级表 - create table ClassInfo( - ClassId int primary key identity(1,1), - ClassName varchar(20) - ); - - --插入数据: insert [into] 表名(字段名) values(值) - insert into ClassInfo( ClassName) values('软件1班'); - - insert ClassInfo values('软件2班') - - select * from ClassInfo - - --创建数据表 - create table StuInfo( - stuId int primary key identity(1001,1), --学生ID - --添加一个检查约束,判断用户插入/新增的数据,性别字段是不是男或者女 - --default:默认约束 - --check - stugender varchar(2) not null default('男') check(stugender='男' or stugender='女'), --学生性别 - stuphone char(11) check(len(stuphone)=11) unique, - --创建班级外键 - --ClassID int references ClassInfo(ClassID) - ClassID int - - ); - - - --增加外键 - --修改表结构 表名 add constraint 约束名 foreign key(要引用的字段) references 主键表(字段) - Alter table StuInfo add constraint FK_StuInfo_ClassId foreign key(ClassID) references ClassInfo(ClassID) - - - --新增姓名列 - alter table StuInfo add stuName varchar(20) - - - - --如果没给出列名,默认是按照顺序一个个添加 - --insert StuInfo values('女',13888888888) - - --insert StuInfo(stuphone) values(15888888888) -- Gitee From d031a4e2f8af82946ae1ab21014944c809f1833c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Tue, 20 Sep 2022 07:10:43 +0000 Subject: [PATCH 11/19] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20?= =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94=2022=2002?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.keep" | 0 .../\344\275\234\344\270\232/test.sql" | 65 ------------------- ...37\346\202\211\350\257\255\346\263\225.md" | 5 -- 3 files changed, 70 deletions(-) delete mode 100644 "\346\235\250\345\207\214\347\277\224 22 02/.keep" delete mode 100644 "\346\235\250\345\207\214\347\277\224 22 02/\344\275\234\344\270\232/test.sql" delete mode 100644 "\346\235\250\345\207\214\347\277\224 22 02/\347\254\224\350\256\260/2022-09-18-\347\206\237\346\202\211\350\257\255\346\263\225.md" diff --git "a/\346\235\250\345\207\214\347\277\224 22 02/.keep" "b/\346\235\250\345\207\214\347\277\224 22 02/.keep" deleted file mode 100644 index e69de29..0000000 diff --git "a/\346\235\250\345\207\214\347\277\224 22 02/\344\275\234\344\270\232/test.sql" "b/\346\235\250\345\207\214\347\277\224 22 02/\344\275\234\344\270\232/test.sql" deleted file mode 100644 index 03d5e5a..0000000 --- "a/\346\235\250\345\207\214\347\277\224 22 02/\344\275\234\344\270\232/test.sql" +++ /dev/null @@ -1,65 +0,0 @@ ---1. 查询出武汉地区所有的员工信息,要求显示部门名称以及员工的详细资料 - -select * from People -inner join Department on People.DepartmentId=Department.DepartmentId -where PeopleAddress='武汉' - ---2. 查询出武汉地区所有的员工信息,要求显示部门名称,职级名称以及员工的详细资料 - -select * from People -inner join Department on People.DepartmentId=Department.DepartmentId -inner join Rank on People.RankId=Rank.RankId -where PeopleAddress ='武汉' - ---3. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资。 - -select DepartmentName,COUNT(People.DepartmentId)人数,SUM(PeopleSalary)工资总和,AVG(PeopleSalary)平均工资,MAX(PeopleSalary)最高工资,MIN(PeopleSalary)最低工资 from People -inner join Department on People.DepartmentId=Department.DepartmentId -group by DepartmentName - ---4. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资,平均工资在10000 以下的不参与统计,并且根据平均工资降序排列。 - -select DepartmentName,COUNT(People.DepartmentId)人数,SUM(PeopleSalary)工资总和,AVG(PeopleSalary)平均工资,MAX(PeopleSalary)最高工资,MIN(PeopleSalary)最低工资 from People -inner join Department on People.DepartmentId=Department.DepartmentId -group by DepartmentName -having AVG(PeopleSalary)>=10000 -order by AVG(PeopleSalary) desc - ---5. 根据部门名称,然后根据职位名称,分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资 - -select DepartmentName,RankId,COUNT(People.DepartmentId)人数,SUM(PeopleSalary)工资总和,AVG(PeopleSalary)平均工资,MAX(PeopleSalary)最高工资,MIN(PeopleSalary)最低工资 from People -inner join Department on People.DepartmentId=Department.DepartmentId -group by RankId,DepartmentName - ---6.查询出巨蟹 6.22--7.22 的员工信息 - -select * from People where MONTH(PeopleBirth)+CONVERT(float,DAY(PeopleBirth))/100 between 6.22 and 7.22 - ---7.查询所有员工信息,添加一列显示属相(鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪) - -select *, case - when year(PeopleBirth)%12 =0 - then '猴' - when YEAR(PeopleBirth)%12=1 - then '鸡' - when YEAR(PeopleBirth)%12=2 - then '狗' - when YEAR(PeopleBirth)%12=3 - then '猪' - when YEAR(PeopleBirth)%12=4 - then '鼠' - when YEAR(PeopleBirth)%12=5 - then '牛' - when YEAR(PeopleBirth)%12=6 - then '虎' - when YEAR(PeopleBirth)%12=7 - then '兔' - when YEAR(PeopleBirth)%12=8 - then '龙' - when YEAR(PeopleBirth)%12=9 - then '蛇' - when YEAR(PeopleBirth)%12=10 - then '马' - when YEAR(PeopleBirth)%12=11 - then '羊' - end 生肖 from People \ No newline at end of file diff --git "a/\346\235\250\345\207\214\347\277\224 22 02/\347\254\224\350\256\260/2022-09-18-\347\206\237\346\202\211\350\257\255\346\263\225.md" "b/\346\235\250\345\207\214\347\277\224 22 02/\347\254\224\350\256\260/2022-09-18-\347\206\237\346\202\211\350\257\255\346\263\225.md" deleted file mode 100644 index e77da7d..0000000 --- "a/\346\235\250\345\207\214\347\277\224 22 02/\347\254\224\350\256\260/2022-09-18-\347\206\237\346\202\211\350\257\255\346\263\225.md" +++ /dev/null @@ -1,5 +0,0 @@ -掌握了以下2个MS SQL的新语法 - -identify 识别列 - -check 检查条件 \ No newline at end of file -- Gitee From bc0bdacd73bc76d8ecbff9f030ac6cce488b769c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Tue, 20 Sep 2022 07:13:18 +0000 Subject: [PATCH 12/19] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2022?= =?UTF-8?q?=20=E6=9D=A8=E5=87=8C=E7=BF=94/=E7=AC=AC=E4=BA=8C=E6=AC=A1?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A/2022-9-18.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2022-9-18.md" | 41 ------------------- 1 file changed, 41 deletions(-) delete mode 100644 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2022-9-18.md" diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2022-9-18.md" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2022-9-18.md" deleted file mode 100644 index f19bbe1..0000000 --- "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2022-9-18.md" +++ /dev/null @@ -1,41 +0,0 @@ -| create database 创建数据库 | -| -------------------------- | -| | - -| use DBTEST使用当前数据库 | -| ------------------------ | -| | - -| create table 创建表 | -| ------------------- | -| | - -| primary key主键 | -| --------------- | -| | - -| identity(1,1)自增 | -| ----------------- | -| | - -| not null非空 | -| ------------ | -| | - -| check 约束 | -| ---------- | -| | - -| unique 唯一约束 | -| --------------- | -| | - -| references +表名(字段) 外键 | -| ----------------------------- | -| | - -| check(userAge between 1 and 100) | -| -------------------------------- | -| | - -限制年龄 \ No newline at end of file -- Gitee From f04fc9e18d0c0e01ab5c7a3062542cdb48f3a8f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Tue, 20 Sep 2022 07:13:55 +0000 Subject: [PATCH 13/19] =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94=2022?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 杨凌翔 <963304364@qq.com> --- ...55\346\263\225\347\224\250\346\263\225.md" | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/2022-9-18-\350\257\255\346\263\225\347\224\250\346\263\225.md" diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/2022-9-18-\350\257\255\346\263\225\347\224\250\346\263\225.md" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/2022-9-18-\350\257\255\346\263\225\347\224\250\346\263\225.md" new file mode 100644 index 0000000..0459b2d --- /dev/null +++ "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/2022-9-18-\350\257\255\346\263\225\347\224\250\346\263\225.md" @@ -0,0 +1,40 @@ +| create database 创建数据库 | +| -------------------------- | +| | + +| use DBTEST使用当前数据库 | +| ------------------------ | +| | + +| create table 创建表 | +| ------------------- | +| | + +| primary key主键 | +| --------------- | +| | + +| identity(1,1)自增 | +| ----------------- | +| | + +| not null非空 | +| ------------ | +| | + +| check 约束 | +| ---------- | +| | + +| unique 唯一约束 | +| --------------- | +| | + +| references +表名(字段) 外键 | +| ----------------------------- | +| | + +| check(userAge between 1 and 100) | +| -------------------------------- | +| 限制年龄 | + -- Gitee From f2889342a2953028c2b6d51a38faab75493eb592 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Tue, 20 Sep 2022 07:14:03 +0000 Subject: [PATCH 14/19] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2022?= =?UTF-8?q?=20=E6=9D=A8=E5=87=8C=E7=BF=94/=E7=AC=AC=E4=BA=8C=E6=AC=A1?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A/=E7=AC=94=E8=AE=B0/.keep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\347\254\224\350\256\260/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/.keep" diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/.keep" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/.keep" deleted file mode 100644 index e69de29..0000000 -- Gitee From 8511490f3a9d3aa7bcb11c4c0e533430c246918b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Tue, 20 Sep 2022 07:21:06 +0000 Subject: [PATCH 15/19] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E7=AC=AC=E4=B8=89?= =?UTF-8?q?=E6=AC=A1=E4=BD=9C=E4=B8=9A?= 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 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/.keep" diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/.keep" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From dca5abaa7e243efff18f441aa6ba3ca5e77f4250 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Tue, 20 Sep 2022 07:21:20 +0000 Subject: [PATCH 16/19] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\347\254\224\350\256\260/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/.keep" diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/.keep" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 43d9eff063d01155888c23488aff013178ebb3f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Tue, 20 Sep 2022 07:21:35 +0000 Subject: [PATCH 17/19] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\344\275\234\344\270\232/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/.keep" diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/.keep" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 724bcafd369902d6b5c043a70de9b785525237a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Tue, 20 Sep 2022 07:21:58 +0000 Subject: [PATCH 18/19] =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94=2022?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 杨凌翔 <963304364@qq.com> --- .../\344\275\234\344\270\232/test.sql" | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/test.sql" diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/test.sql" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/test.sql" new file mode 100644 index 0000000..8c1b201 --- /dev/null +++ "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/test.sql" @@ -0,0 +1,79 @@ +select * from AccountInfo --个人信息 +select * from BankCard --银行卡信息 +select * from CardStateChange --银行卡状态更改表 +select * from CardTransfer --转账表 +select * from CardExchange --交易记录 + +--1. 关羽的银行卡号为"6225547858741263",查询出余额比关羽多的银行卡信息,显示卡号,身份证,姓名,余额。 +select BankCard.CardNo 卡号,AccountInfo.AccountCode 身份证,AccountInfo.RealName 姓名,BankCard.CardMoney 余额 from AccountInfo +join BankCard on BankCard.AccountId=AccountInfo.AccountId +where AccountInfo.AccountId=( +select AccountId from BankCard +where CardMoney>( +select CardMoney from BankCard where CardNo='6225547858741263' +) +) +--2. 从所有账户信息中查询出余额最高的交易明细(存钱取钱信息)。 +select * from CardTransfer --转账表 +select * from CardExchange --交易记录 + + +--3. 查询有取款记录的银行卡及账户信息,显示卡号,身份证,姓名,余额。 + + --个人信息 +select BankCard.CardNo 卡号,AccountInfo.AccountCode 身份证,AccountInfo.RealName 姓名,BankCard.CardMoney 余额 from AccountInfo +join BankCard on BankCard.AccountId=AccountInfo.AccountId +where AccountInfo.AccountId=( +select BankCard.AccountId from BankCard +join CardExchange on CardExchange.CardNo=BankCard.CardNo +where BankCard.CardMoney-CardExchange.MoneyInBank>0 +) + + +--4. 查询出没有存款记录的银行卡及账户信息,显示卡号,身份证,姓名,余额。 + + +select BankCard.CardNo 卡号,AccountInfo.AccountCode 身份证,AccountInfo.RealName 姓名,BankCard.CardMoney 余额 from AccountInfo +join BankCard on BankCard.AccountId=AccountInfo.AccountId +where AccountInfo.AccountId=( +select BankCard.AccountId from BankCard +join CardExchange on CardExchange.CardNo=BankCard.CardNo +where BankCard.CardMoney-CardExchange.MoneyInBank=0 +) + + +--5. 关羽的银行卡号为"6225547858741263",查询当天是否有收到转账。 + +if exists( select * from CardTransfer where CardNoIn = '6225547858741263') +print '有收到转账' +else +print'未收到转账' --个人信息 + + +select * from BankCard +where BankCard.CardNo='6225547858741263' + --银行卡信息 +select * from CardStateChange --银行卡状态更改表 +select * from CardTransfer --转账表 +select * from CardExchange --交易记录 + +--6. 查询出交易次数(存款取款操作)最多的银行卡账户信息,显示:卡号,身份证,姓名,余额,交易次数。 +select * from BankCard +join AccountInfo on AccountInfo.AccountId=BankCard.AccountId +where BankCard.AccountId in ( +select AccountId from BankCard +join CardExchange on BankCard.CardNo=CardExchange.CardNo +where BankCard.CardMoney-CardExchange.MoneyInBank <>0 +) + +--7. 查询出没有转账交易记录的银行卡账户信息,显示卡号,身份证,姓名,余额 +select BankCard.CardNo 卡号,AccountInfo.AccountCode 身份证,AccountInfo.RealName 姓名, BankCard.CardMoney 余额 from BankCard +join AccountInfo on AccountInfo.AccountId=BankCard.AccountId + +where BankCard.CardNo <>( + +select CardNoOut from CardTransfer +)and BankCard.CardNo <>( + +select CardNoIn from CardTransfer +) \ No newline at end of file -- Gitee From 1251681993520de8a15314f066060dc21a2dc022 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Tue, 20 Sep 2022 07:28:46 +0000 Subject: [PATCH 19/19] =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94=2022?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 杨凌翔 <963304364@qq.com> --- ...20\345\255\220\346\237\245\350\257\242.md" | 285 ++++++++++++++++++ 1 file changed, 285 insertions(+) create mode 100644 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/2022-9-20\345\255\220\346\237\245\350\257\242.md" diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/2022-9-20\345\255\220\346\237\245\350\257\242.md" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/2022-9-20\345\255\220\346\237\245\350\257\242.md" new file mode 100644 index 0000000..e0fee72 --- /dev/null +++ "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/2022-9-20\345\255\220\346\237\245\350\257\242.md" @@ -0,0 +1,285 @@ +## 回顾 + +1. 变量的使用。要先用DECLARE 关键字声明,然后用SET或SELECT赋值。局部变量前必须有 “@” 作前缀,全局变量必须有 “@@” 作前缀。 +2. 变量的输出可以用:PRINT或SELECT语句。 +3. 逻辑控制语句提供了条件操作所需的顺序和逻辑。 +4. 批处理可以提高语句执行的效率,使用“GO”作为结束标志。 + + + +## 嵌套/子查询 + +#### 什么是子查询? + +**问题:编写T-SQL语句,查询“黄炜杰”同学的分数大于80 分的考试成绩记录?** + +```sql +--实现方式1:连接查询 +--实现方式2: 子查询 +``` + + + +#### 子查询的特点和优势 + +1)使用灵活。 + +1. 可以成为SQL 语句的多个部分。 +2. **子查询作为查询条件使用**。 + +```sql +--练习:查询学号在曾鹏前面的同学信息 +``` + +1. **子查询作为临时表使用**。 from () + +```sql +--练习:查询所有学生的HTML成绩,如果学生没有成绩显示null +``` + +1. **子查询作为列使用**。 + +```sql +--练习:查询所有学生的HTML成绩,如果学生没有成绩显示null +``` + +2)降低SQL 的语句的复杂度,提高SQL 语句的可读性。 + +#### 子查询 + +![](https://gitee.com/snailclass/tuchuang/raw/master/img/image-20220906173549480-2022-9-1913:24:45.png) + +##### 按结果分类 + +###### 标量子查询 : + +**通常可以使用连接查询替代** + +标量子查询是指子查询返回的是**单一值**,如一个数字或一个字符串。 + +```sql +--1.查询书目表(tb_bibliography)中,条码号(barcode)为"TP311.13"图书(需要在图书表中先查找"TP31L13"的ISBN号)的图书名称(name)和作者(author) + + +--2.查询借阅表(tb-record)中姓名为"邓承明"(需要在学生表中先查找"邓承明'的学号)的学生的借阅信息,显示学号(stu-num)和条码号(barcode) +``` + + + +###### 列子查询 + +**列子查询是指子查询返回的结果集是N行1列**,该结果通常来自对表某个字段的查询结果,带in关键字的子查询是最常用的一类子查询,在使用in关键字进行查询时,子查询语句返回的结果应该是一个数据列中的多个值,如果仅返回1个数值,则可用标量子查询代替。 + +```sql +--in习题 +--查询学生表(tb-student)中和黄弘相同专业学生读者的学号(stu-num)姓名(name)和专业(major) +--先查询 黄宏的 专业 +select major from tb_student where name='黄弘' +--in代表:只要符合后面的条件就筛选出来 +select * from tb_student where major in (select major from tb_student where name='黄弘') + + +--查询学生表(tb-student)中还未还书的读者的学号(stu-num)和姓名(name) +--查询未还书学生的学号 +select stu_num from tb_record where return_time is null +--通过学号查询学生姓名 +select * from tb_student where stu_num in (select stu_num from tb_record where return_time is null) + + +--all any some 习题 +--1.查询学生表(tb-student)中,比信息学院出生日期最大的还要大的所有学生记录,即比信息学院年龄最小的还要小的所有记录,使用ALL关键字,显示学生的姓名(name)、出生日期(birth)和所属学院(school),同样的结果尝试用max()函数再实现一次。 +--信息学院学生最小年龄 +select birth from tb_student where school='信息学院' +select max(birth) from tb_student where school='信息学院' + +select * from tb_student where birth>(select max(birth) from tb_student where school='信息学院' ) + +--all关键字:>,<,= +select * from tb_student where birth>all(select birth from tb_student where school='信息学院') +--2.查询学生表(tb-student)中,比信息学院出生日期最小的还要大的所有学生记录,即比信息学院年龄最大的还要小的所有记录,使用ANY关键字,显示学生的姓名(name)、出生日期(birth)和所属学院(school)。尝试用SOME关键字代替ANY,重新执行后观察查询结果。 +select * from tb_student where birth>any(select birth from tb_student where school='信息学院') +select * from tb_student where birth>some(select birth from tb_student where school='信息学院') +--3.查询书目表(tb-bibliography)中,使用ANY关键字,每种类型中最贵的图 书名称(name)和价格(price) +-- =any 与 in 等价 +select max(price) 最贵的价格 from tb_bibliography group by category +select name 图书名称,price 价格 from tb_bibliography +where price in (select max(price) 最贵的价格 from tb_bibliography group by category) + +``` + + + +###### 行子查询 + +行子查询是指子查询返回的结果集是1行N列,该子查询的结果通常是对表的某行数据进行查询而返回的结果集。 + +```sql +--(1)查询书目表(tb_bibliography)中与《管理信息系统实用教程(第3版)》同作者(author),同类型(category)的书。 +--(2)查询学生表(tb_student)中与"邹睿睿'在同一个学院(school)且同一年级(学号头两位相同则表明在同一年级)学生的学号、姓名和学院。 +``` + + + +###### 表子查询(from子查询) + +表子查询是指子查询返回是M行N列的结果集,查询语句可嵌套在FROM关 +键字后,且需要定义别名。 + +```sql +--查询每种图书在馆的本数,显示ISBN号(ISBN)、图书名称(name)和该图书的在馆数量。 +``` + + + +##### 按位置分类 + +###### 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) +``` + +## 排序函数 + +**排序函数 OVER( [分组子句] 排序子句[DESC][ASC] )** + +**排序子句 :ORDER BY 排序列,排序列…** + +练习:使用各个排名函数对学员的Java 成绩进行排名,并仔细体会其中排序函数的具体用法与其中的区别。 + +```sql + +``` + +- ROW_NUMBER()函数生成的排序根据排序子句给出**递增连续的序号** +- RANK()函数生成的排序根据排序子句给出**递增的序号,但是存在并列并且跳空** +- DENSE_RANK() 函数生成的排序根据排序子句给出**递增的序号,但是存在并列不跳空** + +根据上面三个函数,我们可以增加一个序列,但是有时候我们需要对数据进行分组,然后对分组后的数据进行增加序列,PARTITION BY可以与以上三个函数联合使用 + +```sql +--学生的Java成绩,并根据成绩排名 +--row_number() +select StuName 姓名,ROW_NUMBER() over(order by score desc) 排名,score 分数 from StuScore +join stuinfo on StuInfo.StuID = StuScore.StuID +where subject = 'Java' + +--rank() +select StuName 姓名,rank() over(order by score desc) 排名,score 分数 from StuScore +join stuinfo on StuInfo.StuID = StuScore.StuID +where subject = 'Java' + +--DENSE_RANK() + select StuName 姓名,dense_rank() over(order by score desc) 排名,score 分数 from StuScore +join stuinfo on StuInfo.StuID = StuScore.StuID +where subject = 'Java' + +``` + + + + + +## 分页查询 + +#### 分页方案一 + +**使用top分页** + +```sql +declare @PageSize int = 5 -- 每页5条 +declare @PageIndex int = 2 --第2页 +select top(@PageSize) * from Student +where Stuid not in (select top(@PageSize)*(@PageIndex-1)) StuId from Student +``` + + + +#### 分页方案二 + +**使用row_number分页** + +```sql +declare @PageSize int = 5 +declare @PageIndex int = 3 +select * from +(select ROW_NUMBER() over(order by StuId) RowId,*from Student) as Temp +where RowId between (@PageIndex-1)*@PageSize+1 and @PageIndex*PageSize +``` + + + + + + + +## 作业 + +#### 子查询练习 + +1. 关羽的银行卡号为"6225547858741263",查询出余额比关羽多的银行卡信息,显示卡号,身份证,姓名,余额。 +2. 从所有账户信息中查询出余额最高的交易明细(存钱取钱信息)。 + +1. 查询有取款记录的银行卡及账户信息,显示卡号,身份证,姓名,余额。 +2. 查询出没有存款记录的银行卡及账户信息,显示卡号,身份证,姓名,余额。 +3. 关羽的银行卡号为"6225547858741263",查询当天是否有收到转账。 +4. 查询出交易次数(存款取款操作)最多的银行卡账户信息,显示:卡号,身份证,姓名,余额,交易次数。 + +1. 查询出没有转账交易记录的银行卡账户信息,显示卡号,身份证,姓名,余额。 + + + +#### 分页查询练习 + +```sql +--数据结构和数据如下: +create table Student +( + StuId int primary key identity(1,2), --自动编号 + StuName varchar(20), + StuSex varchar(4) +) +insert into Student(StuName,StuSex) values('刘备','男') +insert into Student(StuName,StuSex) values('关羽','男') +insert into Student(StuName,StuSex) values('张飞','男') +insert into Student(StuName,StuSex) values('赵云','男') +insert into Student(StuName,StuSex) values('马超','男') +insert into Student(StuName,StuSex) values('黄忠','男') +insert into Student(StuName,StuSex) values('魏延','男') +insert into Student(StuName,StuSex) values('简雍','男') +insert into Student(StuName,StuSex) values('诸葛亮','男') +insert into Student(StuName,StuSex) values('徐庶','男') +insert into Student(StuName,StuSex) values('周仓','男') +insert into Student(StuName,StuSex) values('关平','男') +insert into Student(StuName,StuSex) values('张苞','男') +insert into Student(StuName,StuSex) values('曹操','男') +insert into Student(StuName,StuSex) values('曹仁','男') +insert into Student(StuName,StuSex) values('曹丕','男') +insert into Student(StuName,StuSex) values('曹植','男') +insert into Student(StuName,StuSex) values('曹彰','男') +insert into Student(StuName,StuSex) values('典韦','男') +insert into Student(StuName,StuSex) values('许褚','男') +insert into Student(StuName,StuSex) values('夏侯敦','男') +insert into Student(StuName,StuSex) values('郭嘉','男') +insert into Student(StuName,StuSex) values('荀彧','男') +insert into Student(StuName,StuSex) values('贾诩','男') +insert into Student(StuName,StuSex) values('孙权','男') +insert into Student(StuName,StuSex) values('孙坚','男') +insert into Student(StuName,StuSex) values('孙策','男') +insert into Student(StuName,StuSex) values('太史慈','男') +insert into Student(StuName,StuSex) values('大乔','女') +insert into Student(StuName,StuSex) values('小乔','女') +``` + +**方式一:使用row_number** + +**方式二:使用top分页** + -- Gitee