From 4573273f9bcdd9b50ec19284a86e91602deca482 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=96=87=E6=BA=90?= <10033692+zhengwenyuan@user.noreply.gitee.com> Date: Thu, 15 Sep 2022 16:13:34 +0000 Subject: [PATCH 1/7] =?UTF-8?q?=E6=96=B0=E5=BB=BA=2013=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "13\345\217\267/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "13\345\217\267/.keep" diff --git "a/13\345\217\267/.keep" "b/13\345\217\267/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 35ebcc3802ea02d20ce3b824eda84b7f59a7b695 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=96=87=E6=BA=90?= <10033692+zhengwenyuan@user.noreply.gitee.com> Date: Thu, 15 Sep 2022 16:14:36 +0000 Subject: [PATCH 2/7] =?UTF-8?q?13=E5=8F=B7=E9=83=91=E6=96=87=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 郑文源 <> --- ...7\351\203\221\346\226\207\346\272\220.sql" | 53 +++++++++++++++++ "13\345\217\267/\347\254\224\350\256\260.txt" | 58 +++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 "13\345\217\267/13\345\217\267\351\203\221\346\226\207\346\272\220.sql" create mode 100644 "13\345\217\267/\347\254\224\350\256\260.txt" diff --git "a/13\345\217\267/13\345\217\267\351\203\221\346\226\207\346\272\220.sql" "b/13\345\217\267/13\345\217\267\351\203\221\346\226\207\346\272\220.sql" new file mode 100644 index 0000000..0d81c57 --- /dev/null +++ "b/13\345\217\267/13\345\217\267\351\203\221\346\226\207\346\272\220.sql" @@ -0,0 +1,53 @@ +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) + 20, -- 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 diff --git "a/13\345\217\267/\347\254\224\350\256\260.txt" "b/13\345\217\267/\347\254\224\350\256\260.txt" new file mode 100644 index 0000000..3631665 --- /dev/null +++ "b/13\345\217\267/\347\254\224\350\256\260.txt" @@ -0,0 +1,58 @@ +--关系型数据库: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) + + + + select * from StuInfo; \ No newline at end of file -- Gitee From 61fd90fbc621420c6eaa8fa4a9fbf479552e0826 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=96=87=E6=BA=90?= <10033692+zhengwenyuan@user.noreply.gitee.com> Date: Sun, 18 Sep 2022 13:13:42 +0000 Subject: [PATCH 3/7] =?UTF-8?q?=E6=96=B0=E5=BB=BA=2013=E5=8F=B7=E9=83=91?= =?UTF-8?q?=E6=96=87=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "13\345\217\267\351\203\221\346\226\207\346\272\220/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "13\345\217\267\351\203\221\346\226\207\346\272\220/.keep" diff --git "a/13\345\217\267\351\203\221\346\226\207\346\272\220/.keep" "b/13\345\217\267\351\203\221\346\226\207\346\272\220/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 6d10676f29a321da03954f763f7558efaaf0eeaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=96=87=E6=BA=90?= <10033692+zhengwenyuan@user.noreply.gitee.com> Date: Sun, 18 Sep 2022 13:13:51 +0000 Subject: [PATCH 4/7] =?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 "13\345\217\267\351\203\221\346\226\207\346\272\220/\347\254\224\350\256\260/.keep" diff --git "a/13\345\217\267\351\203\221\346\226\207\346\272\220/\347\254\224\350\256\260/.keep" "b/13\345\217\267\351\203\221\346\226\207\346\272\220/\347\254\224\350\256\260/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 1201ad6cc02bbd1ec674bf6255791079c35e9a44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=96=87=E6=BA=90?= <10033692+zhengwenyuan@user.noreply.gitee.com> Date: Sun, 18 Sep 2022 13:13:59 +0000 Subject: [PATCH 5/7] =?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 "13\345\217\267\351\203\221\346\226\207\346\272\220/\344\275\234\344\270\232/.keep" diff --git "a/13\345\217\267\351\203\221\346\226\207\346\272\220/\344\275\234\344\270\232/.keep" "b/13\345\217\267\351\203\221\346\226\207\346\272\220/\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From a14a6eb94149fe551ef619bbcfa8eacde61eabf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=96=87=E6=BA=90?= <10033692+zhengwenyuan@user.noreply.gitee.com> Date: Sun, 18 Sep 2022 13:14:18 +0000 Subject: [PATCH 6/7] =?UTF-8?q?13=E5=8F=B7=E9=83=91=E6=96=87=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 郑文源 <> --- .../2022.9.16\344\275\234\344\270\232.sql" | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 "13\345\217\267\351\203\221\346\226\207\346\272\220/\344\275\234\344\270\232/2022.9.16\344\275\234\344\270\232.sql" diff --git "a/13\345\217\267\351\203\221\346\226\207\346\272\220/\344\275\234\344\270\232/2022.9.16\344\275\234\344\270\232.sql" "b/13\345\217\267\351\203\221\346\226\207\346\272\220/\344\275\234\344\270\232/2022.9.16\344\275\234\344\270\232.sql" new file mode 100644 index 0000000..ebf1aee --- /dev/null +++ "b/13\345\217\267\351\203\221\346\226\207\346\272\220/\344\275\234\344\270\232/2022.9.16\344\275\234\344\270\232.sql" @@ -0,0 +1,81 @@ +--ѯз 6.22--7.22 ԱϢ +select * from People +where datename(month,PeopleBirth) = 6 and day(PeopleBirth) between 22 and 30 +or datename(month,PeopleBirth) = 7 and day(PeopleBirth) between 1 and 22; + + +--ѯԱϢһʾ(,ţ,,,,,,,,,,) +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 + + +--1. ѯ人еԱϢҪʾԼԱϸ +select p.*,d.DepartmentName from People p +inner join Department d +on p.DepartmentId = d.DepartmentId +where p.PeopleAddress = '人'; + + +--2. ѯ人еԱϢҪʾƣְԼԱϸ +select p.*,d.DepartmentName,r.RankName from People p +inner join Department d +on p.DepartmentId = d.DepartmentId +inner join Rank r on r.RankId = p.RankId +where p.PeopleAddress = '人'; + + +--3. ݲŷͳԱԱܺͣƽʣ߹ʺ͹ʡ +select count(D.DepartmentName)Ա,sum(p.PeopleSalary)Աܺ,convert(decimal(10,2), +avg(p.PeopleSalary))ƽ,max(p.PeopleSalary)߹,min(p.PeopleSalary)͹ +from People p +inner join Department d +on p.DepartmentId = d.DepartmentId +group by d.DepartmentName + + +--4. ݲŷͳԱԱܺͣƽʣ߹ʺ͹ʣƽ10000 µIJͳƣ +--ҸƽʽС +select count(D.DepartmentName)Ա,sum(p.PeopleSalary)Աܺ,convert(decimal(10,2), +avg(p.PeopleSalary))ƽ,max(p.PeopleSalary)߹,min(p.PeopleSalary)͹ +from People p +inner join Department d +on p.DepartmentId = d.DepartmentId +group by d.DepartmentName +having avg(p.PeopleSalary) > 10000 +order by ƽ desc + + +--5. ݲƣȻְλƣͳԱԱܺͣƽʣ߹ʺ͹ +select d.DepartmentName,r.RankName,count(*)Ա, +sum(p.PeopleSalary)Աܺ,convert(decimal(10,2), +avg(p.PeopleSalary))ƽ,max(p.PeopleSalary)߹,min(p.PeopleSalary)͹ +from People p +inner join Department d +on p.DepartmentId = d.DepartmentId +inner join Rank r on r.RankId = p.RankId +group by d.DepartmentName,r.RankName \ No newline at end of file -- Gitee From 0c0f978a37c129cb3d9a7827b49c2086a57c8a10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=96=87=E6=BA=90?= <10033692+zhengwenyuan@user.noreply.gitee.com> Date: Sun, 18 Sep 2022 13:14:36 +0000 Subject: [PATCH 7/7] =?UTF-8?q?13=E5=8F=B7=E9=83=91=E6=96=87=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 郑文源 <> --- .../2022.9.16\347\254\224\350\256\260.md" | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 "13\345\217\267\351\203\221\346\226\207\346\272\220/\347\254\224\350\256\260/2022.9.16\347\254\224\350\256\260.md" diff --git "a/13\345\217\267\351\203\221\346\226\207\346\272\220/\347\254\224\350\256\260/2022.9.16\347\254\224\350\256\260.md" "b/13\345\217\267\351\203\221\346\226\207\346\272\220/\347\254\224\350\256\260/2022.9.16\347\254\224\350\256\260.md" new file mode 100644 index 0000000..1e7e728 --- /dev/null +++ "b/13\345\217\267\351\203\221\346\226\207\346\272\220/\347\254\224\350\256\260/2022.9.16\347\254\224\350\256\260.md" @@ -0,0 +1,45 @@ +# 1.模糊查询 + +```sql +[]:代表匹配范围内 +[^]:代表匹配不在范围内 +``` + +```sql +select * from Department where DepartmentId like '[1-3]' + +// [1-3] == 1,2,3 + +select * from Department where DepartmentId like '[^1-3]' + +//[^1-3] == 4 +``` + +# 2.CONVERT()与CAST()函数: + +```sql + +convert(decimal(13,2),12.45454) +cast(12.45454 as decimal(13,2)) +``` + +# 3.时间函数 + +```sql +select DATEDIFF(day, '2019-08-20', getDate()); --获取指定时间单位的差值 +SELECT DATEADD(MINUTE,-5,GETDATE()) --加减时间,此处为获取五分钟前的时间,MINUTE 表示分钟,可为 YEAR,MONTH,DAY,HOUR +select DATENAME(month, getDate()); --当前月份 +select DATENAME(WEEKDAY, getDate()); --当前星期几 +select DATEPART(month, getDate()); --当前月份 +select DAY(getDate()); --返回当前日期天数 +select MONTH(getDate()); --返回当前日期月数 +select YEAR(getDate()); --返回当前日期年数 + +SELECT CONVERT(VARCHAR(22),GETDATE(),20) --2020-01-09 14:46:46 +SELECT CONVERT(VARCHAR(24),GETDATE(),21) --2020-01-09 14:46:55.91 +SELECT CONVERT(VARCHAR(22),GETDATE(),23) --2020-01-09 +SELECT CONVERT(VARCHAR(22),GETDATE(),24) --15:04:07 +Select CONVERT(varchar(20),GETDATE(),14) --15:05:49:330 +``` + +# \ No newline at end of file -- Gitee