From 7e8a68bcc6f950c91777c5bb4e9a62367e394c81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE?= <3032059138@qq.com> Date: Thu, 15 Sep 2022 16:51:51 +0000 Subject: [PATCH 01/13] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E8=A2=81=E8=B4=B5?= =?UTF-8?q?=E6=A3=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\350\242\201\350\264\265\346\243\256/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\350\242\201\350\264\265\346\243\256/.keep" diff --git "a/\350\242\201\350\264\265\346\243\256/.keep" "b/\350\242\201\350\264\265\346\243\256/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 3561de904cce02e8d1ba3a9df9d663ecb0d28264 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE?= <3032059138@qq.com> Date: Thu, 15 Sep 2022 16:52:41 +0000 Subject: [PATCH 02/13] 28 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 袁贵森 <3032059138@qq.com> --- "\350\242\201\350\264\265\346\243\256/28.sql" | 52 +++++++++++++++++ .../\347\254\224\350\256\260.txt" | 58 +++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 "\350\242\201\350\264\265\346\243\256/28.sql" create mode 100644 "\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" diff --git "a/\350\242\201\350\264\265\346\243\256/28.sql" "b/\350\242\201\350\264\265\346\243\256/28.sql" new file mode 100644 index 0000000..9f2be17 --- /dev/null +++ "b/\350\242\201\350\264\265\346\243\256/28.sql" @@ -0,0 +1,52 @@ +--判断数据库是否存在 +if exists (select * from sys.databases where name='DBTEXT') +drop database DBTEXT; +--创建数据库 +create database DBTEXT +--使用数据库 + + +--创建数据表 +--primary key 主键 +--identity 自增(标识符) +--default 默约束 +--check 检查 + +--部门信息表 +create table sectionInfo( + sectionID int primary key identity(2010,1), + sectionName varchar(10) not null +); + +INSERT into sectionInfo (sectionName) VALUES ('张三','李四','全网','暗属性出','认为对方','违法的'), +--员工信息表 +create table userInfo( + userNo int identity(1000,1) primary key not null, + userName varchar(10) not null check(len(userName)>4) unique, + userSex varchar(2) not null check(userSex='男' or userSex='女'), + userAge int not null check(userAge between 1 and 100), + userAddress varchar(50) default ('湖北'), + userSection int +); +Alter table sectionInfo add constraint FK_sectionInfo_ClassId foreign key(userSection) references asd(sectionID); +INSERT into userInfo (userName,userSex,userAge,userAddress,userSection) VALUES +('张三','男',19,'湖南',1), +('张嘶','女',20,'福建',2), +('张无','女',17,'广东',1), +('张六','男',19,'湖北',2), +('张气','男',17,'湖南',1) +--员工考勤表 +create table workInfo( + workId int identity(1,1) primary key not null, + userId int not null, + workTime datetime not null, + workDescription varchar (40) not null check(workDescription='迟到' or workDescription='早退' or workDescription='旷工' or workDescription='病假' or workDescription='事假') +); +Alter table workInfo add constraint FK_workInfo_ClassId foreign key(userId) references sectionInfo(userNo); + +INSERT into workInfo (workTime,workDescription) VALUES +('2021-09-05','早退'), +('2022-08-06','迟到'), +('2023-09-10','事假'), +('2020-09-20','旷工'), +('2021-10-05','病假') \ No newline at end of file diff --git "a/\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" "b/\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" new file mode 100644 index 0000000..3631665 --- /dev/null +++ "b/\350\242\201\350\264\265\346\243\256/\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 00bf9b61a366a00919c3adad29288ae7af8b7b59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE?= <3032059138@qq.com> Date: Sun, 18 Sep 2022 14:47:25 +0000 Subject: [PATCH 03/13] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E8=A2=81=E8=B4=B5?= =?UTF-8?q?=E6=A3=AE.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\350\242\201\350\264\265\346\243\256./.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\350\242\201\350\264\265\346\243\256./.keep" diff --git "a/\350\242\201\350\264\265\346\243\256./.keep" "b/\350\242\201\350\264\265\346\243\256./.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 9142522e99f667acd88871313f441197ca3a42c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE?= <3032059138@qq.com> Date: Sun, 18 Sep 2022 14:47:46 +0000 Subject: [PATCH 04/13] 28 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 袁贵森 <3032059138@qq.com> --- .../SQLQuery1.sql" | 299 ++++++++++++++++++ .../\347\254\224\350\256\260.txt" | 111 +++++++ 2 files changed, 410 insertions(+) create mode 100644 "\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" create mode 100644 "\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" diff --git "a/\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" "b/\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" new file mode 100644 index 0000000..0231695 --- /dev/null +++ "b/\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" @@ -0,0 +1,299 @@ +if exists(select * from sys.databases where name='DBTEST') +drop database DBTEST; +go + +create database DBTEST; +go + +use DBTEST; +go + +--ű +create table Department +( + --ű;int;primary key;identity(1,1)1ʼΪ1; + DepartmentId int primary key identity(1,1), + --;nvarchar(50)50ַ;not nullΪ; + DepartmentName nvarchar(50) not null, + --textı; + DepartmentRemark text +); +go + +--ְrankΪϵͳؼ֣˴ʹ[]Զ֣ϵͳؼ +create table [Rank] +( + RankId int primary key identity(1,1), + RankName nvarchar(50) not null, + RankRemark text +); +go + +--ԱϢ +create table People +( + PeopleId int primary key identity(1,1), + --references,ֶαԼ + DepartmentId int references Department(DepartmentId) not null, + RankId int references [Rank](RankId) not null, + PeopleName nvarchar(50) not null, + --defaultֶĬֵ; checkԹ涨ֵֶԼ; + PeopleSex nvarchar(1) default('') check(PeopleSex='' or PeopleSex='Ů') not null, + PeopleBirth datetime not null, + PeopleSalary decimal(12,2) check(PeopleSalary>= 1000 and PeopleSalary <= 100000) not null, + --uniqueΨһԼΪṩΨһԱ֤; + PeoplePhone nvarchar(20) unique not null, + PeopleAddress nvarchar(100), + --datetimesmalldatetimeԱʾʱͣgetdate()ڻȡϵͳǰʱ + PeopleAddTime smalldatetime default(getdate()) +); +go + + + +------------------------------ݲ------------------------------ +--ű +insert into Department values('','˾IJ'); +go + +--һԲ +insert into Department(DepartmentName,DepartmentRemark) +select 'г','ţIJ' union +select 'Ʒ','ǿյIJ' union +select 'ܾ','쵼IJ' ; +go + +-------ְ +insert into [Rank](RankName,RankRemark) +values('','') +insert into [Rank](RankName,RankRemark) +values('м','߱') +insert into [Rank](RankName,RankRemark) +values('߼','߱Դȫ'); +go + +---------Ա +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,3,'','','1984-7-9',20000,'13554785452','ɶ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,2,'','Ů','1987-7-9',15000,'13256854578','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,1,'','','1988-8-8',12000,'13985745871','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,1,'ŷ','','1990-8-8',8000,'13535987412','˲',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,3,'','','1989-4-8',9000,'13845789568','˲',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,3,'','','1995-4-8',9500,'13878562568','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,2,'Ƹ','','1989-4-20',8500,'13335457412','人',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,1,'','Ů','1989-4-20',6500,'13437100050','人',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,2,'ܲ','','1987-12-20',25000,'13889562354','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,3,'','','1981-11-11',9000,'13385299632','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,1,'Τ','','1978-1-13',8000,'13478545263','Ϻ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,1,'','','1998-12-12',7500,'13878523695','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,3,'','','1968-11-22',9000,'13698545841','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,3,'','','1988-1-22',11000,'13558745874','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,2,'Ȩ','','1990-2-21',12000,'13698745214','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,2,'','Ů','1995-2-21',13000,'13985478512','Ϻ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,1,'С','Ů','1996-2-21',13500,'13778787874','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,2,'','','1992-10-11',8000,'13987455214','人',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,3,'³','','1984-9-10',5500,'13254785965','ɶ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,3,'','','1987-5-19',8500,'13352197364','ɶ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,1,'½ѷ','','1996-5-19',7500,'13025457392','Ͼ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,2,'̫ʷ','','1983-6-1',7500,'13077778888','Ϻ',getdate()) + + +---ѯ +select * from Department +select * from [Rank] +select * from People + + +--1. ѯ + +--2. ָвѯԱн绰 +select PeopleName as ,Peoplesex as Ա from People; +--3. ָвѯ,ԶԱн绰 +--4. ѯ˾ԱڳУҪظݣ + +--ظУdistinct +select distinct peopleAddress from People; +--5. 蹤յ10%ѯԭʼʺ͵ĹʣʾԱнннвѯ +select top 3 PeopleName , PeopleSalary н, PeopleSalary*1.1 нн from People; + +--top ordey by + + +select * from People +--ָУԱн绰ѯԱΪŮԱϢ,Զ +--2. ѯнڵ10000 ԱϢ( ) +--3. ѯнڵ10000 ŮԱϢ() +select * from People where PeopleSalary>=10000 and PeopleSex='Ů' +--4. ʾ1980-1-1֮󣬶нڵ10000ŮԱϢ +select * from People where PeopleSalary>=10000 and PeopleSex='Ů' and PeopleBirth>'1995-1-1'; + +--5. ʾнڵ15000 Ա,нڵ8000ŮԱϢ +select * from People where (PeopleSalary>=8000 and peoplesex='Ů') or PeopleSalary>=15000 +--6. ѯн10000-20000 ֮ԱϢ( ) +select * from People where PeopleSalary between 10000 and 20000 +--7. ѯַڱϺԱϢ +--8. ѯԱϢ(ݹ򣬽): ascĬϣ: desc: +select * from People order by PeopleSalary desc + +--9. ʾеԱϢֵijȽе +--10. ѯߵ5˵Ϣ +select top 5 * from People order by PeopleSalary desc +--11. ѯߵ10%ԱϢ +select top 10 percent * from People order by PeopleSalary desc +--12. ѯַûдԱϢ +--Ϊղ=is null, is not null +select * from People where PeopleAddress is null; + +--13. ѯַѾдԱϢ +--14. ѯе80ԱϢ + +--15. ѯ30-40 ֮䣬ҹ15000-30000 ֮ԱϢ +--16. ѯз 6.22--7.22 ԱϢ +select * from people where +(MONTH(PeopleBirth)=6 and DAY(PeopleBirth)>=22) or +(MONTH(PeopleBirth)=7 and DAY(PeopleBirth)<=22) +; +--17. ѯʱƸߵ +select * from people where PeopleSalary > 9000 +--18. ѯͬһе +--19. ѯФΪԱϢ +--20. ѯԱϢһʾ(,ţ,,,,,,,,,,) +--case when end + +--ѯз 6.22--7.22 ԱϢ +select * from People where +(MONTH(PeopleBirth) = 6 and DAY(PeopleBirth) >=22) or +(MONTH(PeopleBirth) = 7 and DAY(PeopleBirth) <=22) + +--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(*) Ա,sum(PeopleSalary) Աܺ,convert(decimal(15,2),avg(PeopleSalary)) ƽ,max(PeopleSalary) ߹,min(PeopleSalary) ͹ from people p +inner join Department d on d.DepartmentId=p.DepartmentId +group by d.DepartmentId + +--4. ݲŷͳԱԱܺͣƽʣ߹ʺ͹ʣƽ10000 µIJͳƣ +--ҸƽʽС +select count(*) Ա,sum(PeopleSalary) Աܺ,convert(decimal(15,2),avg(PeopleSalary)) ƽ,max(PeopleSalary) ߹,min(PeopleSalary) ͹ from people p +inner join Department d on d.DepartmentId=p.DepartmentId +group by d.DepartmentId +having convert(decimal(15,2),avg(PeopleSalary))>=10000 +order by ƽ desc + +--5. ݲƣȻְλƣͳԱԱܺͣƽʣ߹ʺ͹ +select [Rank].RankName ְλ,count(*) Ա,sum(PeopleSalary) Աܺ,convert(decimal(15,2),avg(PeopleSalary)) ƽ,max(PeopleSalary) ߹, +min(PeopleSalary) ͹ from Department +inner join People on People.DepartmentId=Department.DepartmentId +inner join [Rank] on People.RankId=[Rank].RankId +group by [Rank].RankName + +--ѯԱϢһʾ(,ţ,,,,,,,,,,) +select PeopleName ,PeopleSex Ա,PeopleSalary ,PeoplePhone 绰,PEOPLEBIRTH , +case +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 '' +when year(PeopleBirth) % 12 = 0 then '' +else '' +end "Ф" +from People + +select PeopleName ,PeopleSex Ա,PeopleSalary ,PeoplePhone 绰,PEOPLEBIRTH , +case + 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 '' + when year(PeopleBirth) % 12 = 0 then '' + when year(PeopleBirth) % 12 = 1 then '' + when year(PeopleBirth) % 12 = 2 then '' + when year(PeopleBirth) % 12 = 3 then '' + ELSE '' +end Ф +from People \ No newline at end of file diff --git "a/\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" "b/\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" new file mode 100644 index 0000000..d318cf7 --- /dev/null +++ "b/\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" @@ -0,0 +1,111 @@ +**SQL中常用运算符** + +```sql +=:等于,比较是否相等及赋值 +!=:比较不等于 +>:比较大于 +<:比较小于 +>=:比较大于等于 +<=:比较小于等于 +IS NULL:比较为空 +IS NOT NULL:比较不为空 +in:比较是否在其中 +like:模糊查询 +BETWEEN...AND...:比较是否在两者之间 50-100 BETWEEN 50 AND 100 +and:逻辑与(两个条件同时成立表达式成立) +or:逻辑或(两个条件有一个成立表达式成立) +not:逻辑非(条件成立,表达式则不成立;条件不成立,表达式则成立) +``` +#### 模糊查询 + +模糊查询使用like关键字和通配符结合来实现,通配符具体含义如下: + +```sql +%:代表匹配0个字符、1个字符或多个字符。 +_:代表匹配有且只有1个字符。 +[]:代表匹配范围内 +[^]:代表匹配不在范围内 +``` +#### 聚合函数 + +SQL SERVER中聚合函数主要有: + +```sql +count:求数量 +max:求最大值 +min:求最小值 +sum:求和 +avg:求平均值 +``` +ROUND函数用法: + +```sql +round(num,len,[type]) +其中: +num表示需要处理的数字,len表示需要保留的长度,type处理类型(0是默认值代表四舍五入,非0代表直接截取) +select ROUND(123.45454,3) --123.45500 +select ROUND(123.45454,3,1) --123.45400 + +CONVERT()与CAST()函数: + +```sql +--1.保留小数 +convert(decimal(13,2),12.45454) +cast(12.45454 as decimal(13,2)) +--2.强制转换类型 +``` + + + +#### SQL中常用的时间函数 + +```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 +``` + +**时间格式控制字符串:** + +| 名称 | 日期单位 | 缩写 | +| ------------ | ----------- | --------- | +| 年 | year | yyyy 或yy | +| 季度 | quarter | qq,q | +| 月 | month | mm,m | +| 一年中第几天 | dayofyear | dy,y | +| 日 | day | dd,d | +| 一年中第几周 | week | wk,ww | +| 星期 | weekday | dw | +| 小时 | Hour | hh | +| 分钟 | minute | mi,n | +| 秒 | second | ss,s | +| 毫秒 | millisecond | ms | + +SQL语句执行顺序: + +```sql +(7) SELECT +(8) DISTINCT +(1) FROM +(3) JOIN +(2) ON +(4) WHERE +(5) GROUP BY +(6) HAVING +(9) ORDER BY +(10) LIMIT +``` + + + -- Gitee From d1ef056fef69932f8f9ccfa985f8df4628bfe052 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE?= <3032059138@qq.com> Date: Sun, 18 Sep 2022 14:48:42 +0000 Subject: [PATCH 05/13] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20?= =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE./=E8=A2=81=E8=B4=B5=E6=A3=AE/SQLQue?= =?UTF-8?q?ry1.sql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SQLQuery1.sql" | 299 ------------------ 1 file changed, 299 deletions(-) delete mode 100644 "\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" diff --git "a/\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" "b/\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" deleted file mode 100644 index 0231695..0000000 --- "a/\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" +++ /dev/null @@ -1,299 +0,0 @@ -if exists(select * from sys.databases where name='DBTEST') -drop database DBTEST; -go - -create database DBTEST; -go - -use DBTEST; -go - ---ű -create table Department -( - --ű;int;primary key;identity(1,1)1ʼΪ1; - DepartmentId int primary key identity(1,1), - --;nvarchar(50)50ַ;not nullΪ; - DepartmentName nvarchar(50) not null, - --textı; - DepartmentRemark text -); -go - ---ְrankΪϵͳؼ֣˴ʹ[]Զ֣ϵͳؼ -create table [Rank] -( - RankId int primary key identity(1,1), - RankName nvarchar(50) not null, - RankRemark text -); -go - ---ԱϢ -create table People -( - PeopleId int primary key identity(1,1), - --references,ֶαԼ - DepartmentId int references Department(DepartmentId) not null, - RankId int references [Rank](RankId) not null, - PeopleName nvarchar(50) not null, - --defaultֶĬֵ; checkԹ涨ֵֶԼ; - PeopleSex nvarchar(1) default('') check(PeopleSex='' or PeopleSex='Ů') not null, - PeopleBirth datetime not null, - PeopleSalary decimal(12,2) check(PeopleSalary>= 1000 and PeopleSalary <= 100000) not null, - --uniqueΨһԼΪṩΨһԱ֤; - PeoplePhone nvarchar(20) unique not null, - PeopleAddress nvarchar(100), - --datetimesmalldatetimeԱʾʱͣgetdate()ڻȡϵͳǰʱ - PeopleAddTime smalldatetime default(getdate()) -); -go - - - -------------------------------ݲ------------------------------ ---ű -insert into Department values('','˾IJ'); -go - ---һԲ -insert into Department(DepartmentName,DepartmentRemark) -select 'г','ţIJ' union -select 'Ʒ','ǿյIJ' union -select 'ܾ','쵼IJ' ; -go - --------ְ -insert into [Rank](RankName,RankRemark) -values('','') -insert into [Rank](RankName,RankRemark) -values('м','߱') -insert into [Rank](RankName,RankRemark) -values('߼','߱Դȫ'); -go - ----------Ա -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(1,3,'','','1984-7-9',20000,'13554785452','ɶ',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(1,2,'','Ů','1987-7-9',15000,'13256854578','',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(1,1,'','','1988-8-8',12000,'13985745871','',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(2,1,'ŷ','','1990-8-8',8000,'13535987412','˲',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(2,3,'','','1989-4-8',9000,'13845789568','˲',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(3,3,'','','1995-4-8',9500,'13878562568','',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(3,2,'Ƹ','','1989-4-20',8500,'13335457412','人',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(3,1,'','Ů','1989-4-20',6500,'13437100050','人',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(2,2,'ܲ','','1987-12-20',25000,'13889562354','',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(2,3,'','','1981-11-11',9000,'13385299632','',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(2,1,'Τ','','1978-1-13',8000,'13478545263','Ϻ',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(1,1,'','','1998-12-12',7500,'13878523695','',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(1,3,'','','1968-11-22',9000,'13698545841','',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(3,3,'','','1988-1-22',11000,'13558745874','',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(3,2,'Ȩ','','1990-2-21',12000,'13698745214','',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(3,2,'','Ů','1995-2-21',13000,'13985478512','Ϻ',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(2,1,'С','Ů','1996-2-21',13500,'13778787874','',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(1,2,'','','1992-10-11',8000,'13987455214','人',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(2,3,'³','','1984-9-10',5500,'13254785965','ɶ',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(3,3,'','','1987-5-19',8500,'13352197364','ɶ',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(1,1,'½ѷ','','1996-5-19',7500,'13025457392','Ͼ',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(1,2,'̫ʷ','','1983-6-1',7500,'13077778888','Ϻ',getdate()) - - ----ѯ -select * from Department -select * from [Rank] -select * from People - - ---1. ѯ - ---2. ָвѯԱн绰 -select PeopleName as ,Peoplesex as Ա from People; ---3. ָвѯ,ԶԱн绰 ---4. ѯ˾ԱڳУҪظݣ - ---ظУdistinct -select distinct peopleAddress from People; ---5. 蹤յ10%ѯԭʼʺ͵ĹʣʾԱнннвѯ -select top 3 PeopleName , PeopleSalary н, PeopleSalary*1.1 нн from People; - ---top ordey by - - -select * from People ---ָУԱн绰ѯԱΪŮԱϢ,Զ ---2. ѯнڵ10000 ԱϢ( ) ---3. ѯнڵ10000 ŮԱϢ() -select * from People where PeopleSalary>=10000 and PeopleSex='Ů' ---4. ʾ1980-1-1֮󣬶нڵ10000ŮԱϢ -select * from People where PeopleSalary>=10000 and PeopleSex='Ů' and PeopleBirth>'1995-1-1'; - ---5. ʾнڵ15000 Ա,нڵ8000ŮԱϢ -select * from People where (PeopleSalary>=8000 and peoplesex='Ů') or PeopleSalary>=15000 ---6. ѯн10000-20000 ֮ԱϢ( ) -select * from People where PeopleSalary between 10000 and 20000 ---7. ѯַڱϺԱϢ ---8. ѯԱϢ(ݹ򣬽): ascĬϣ: desc: -select * from People order by PeopleSalary desc - ---9. ʾеԱϢֵijȽе ---10. ѯߵ5˵Ϣ -select top 5 * from People order by PeopleSalary desc ---11. ѯߵ10%ԱϢ -select top 10 percent * from People order by PeopleSalary desc ---12. ѯַûдԱϢ ---Ϊղ=is null, is not null -select * from People where PeopleAddress is null; - ---13. ѯַѾдԱϢ ---14. ѯе80ԱϢ - ---15. ѯ30-40 ֮䣬ҹ15000-30000 ֮ԱϢ ---16. ѯз 6.22--7.22 ԱϢ -select * from people where -(MONTH(PeopleBirth)=6 and DAY(PeopleBirth)>=22) or -(MONTH(PeopleBirth)=7 and DAY(PeopleBirth)<=22) -; ---17. ѯʱƸߵ -select * from people where PeopleSalary > 9000 ---18. ѯͬһе ---19. ѯФΪԱϢ ---20. ѯԱϢһʾ(,ţ,,,,,,,,,,) ---case when end - ---ѯз 6.22--7.22 ԱϢ -select * from People where -(MONTH(PeopleBirth) = 6 and DAY(PeopleBirth) >=22) or -(MONTH(PeopleBirth) = 7 and DAY(PeopleBirth) <=22) - ---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(*) Ա,sum(PeopleSalary) Աܺ,convert(decimal(15,2),avg(PeopleSalary)) ƽ,max(PeopleSalary) ߹,min(PeopleSalary) ͹ from people p -inner join Department d on d.DepartmentId=p.DepartmentId -group by d.DepartmentId - ---4. ݲŷͳԱԱܺͣƽʣ߹ʺ͹ʣƽ10000 µIJͳƣ ---ҸƽʽС -select count(*) Ա,sum(PeopleSalary) Աܺ,convert(decimal(15,2),avg(PeopleSalary)) ƽ,max(PeopleSalary) ߹,min(PeopleSalary) ͹ from people p -inner join Department d on d.DepartmentId=p.DepartmentId -group by d.DepartmentId -having convert(decimal(15,2),avg(PeopleSalary))>=10000 -order by ƽ desc - ---5. ݲƣȻְλƣͳԱԱܺͣƽʣ߹ʺ͹ -select [Rank].RankName ְλ,count(*) Ա,sum(PeopleSalary) Աܺ,convert(decimal(15,2),avg(PeopleSalary)) ƽ,max(PeopleSalary) ߹, -min(PeopleSalary) ͹ from Department -inner join People on People.DepartmentId=Department.DepartmentId -inner join [Rank] on People.RankId=[Rank].RankId -group by [Rank].RankName - ---ѯԱϢһʾ(,ţ,,,,,,,,,,) -select PeopleName ,PeopleSex Ա,PeopleSalary ,PeoplePhone 绰,PEOPLEBIRTH , -case -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 '' -when year(PeopleBirth) % 12 = 0 then '' -else '' -end "Ф" -from People - -select PeopleName ,PeopleSex Ա,PeopleSalary ,PeoplePhone 绰,PEOPLEBIRTH , -case - 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 '' - when year(PeopleBirth) % 12 = 0 then '' - when year(PeopleBirth) % 12 = 1 then '' - when year(PeopleBirth) % 12 = 2 then '' - when year(PeopleBirth) % 12 = 3 then '' - ELSE '' -end Ф -from People \ No newline at end of file -- Gitee From 3f95f2a077dcb04ae8b0b7572fe15fe9028e96b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE?= <3032059138@qq.com> Date: Sun, 18 Sep 2022 14:48:51 +0000 Subject: [PATCH 06/13] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20?= =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE./=E8=A2=81=E8=B4=B5=E6=A3=AE/?= =?UTF-8?q?=E7=AC=94=E8=AE=B0.txt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\347\254\224\350\256\260.txt" | 111 ------------------ 1 file changed, 111 deletions(-) delete mode 100644 "\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" diff --git "a/\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" "b/\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" deleted file mode 100644 index d318cf7..0000000 --- "a/\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" +++ /dev/null @@ -1,111 +0,0 @@ -**SQL中常用运算符** - -```sql -=:等于,比较是否相等及赋值 -!=:比较不等于 ->:比较大于 -<:比较小于 ->=:比较大于等于 -<=:比较小于等于 -IS NULL:比较为空 -IS NOT NULL:比较不为空 -in:比较是否在其中 -like:模糊查询 -BETWEEN...AND...:比较是否在两者之间 50-100 BETWEEN 50 AND 100 -and:逻辑与(两个条件同时成立表达式成立) -or:逻辑或(两个条件有一个成立表达式成立) -not:逻辑非(条件成立,表达式则不成立;条件不成立,表达式则成立) -``` -#### 模糊查询 - -模糊查询使用like关键字和通配符结合来实现,通配符具体含义如下: - -```sql -%:代表匹配0个字符、1个字符或多个字符。 -_:代表匹配有且只有1个字符。 -[]:代表匹配范围内 -[^]:代表匹配不在范围内 -``` -#### 聚合函数 - -SQL SERVER中聚合函数主要有: - -```sql -count:求数量 -max:求最大值 -min:求最小值 -sum:求和 -avg:求平均值 -``` -ROUND函数用法: - -```sql -round(num,len,[type]) -其中: -num表示需要处理的数字,len表示需要保留的长度,type处理类型(0是默认值代表四舍五入,非0代表直接截取) -select ROUND(123.45454,3) --123.45500 -select ROUND(123.45454,3,1) --123.45400 - -CONVERT()与CAST()函数: - -```sql ---1.保留小数 -convert(decimal(13,2),12.45454) -cast(12.45454 as decimal(13,2)) ---2.强制转换类型 -``` - - - -#### SQL中常用的时间函数 - -```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 -``` - -**时间格式控制字符串:** - -| 名称 | 日期单位 | 缩写 | -| ------------ | ----------- | --------- | -| 年 | year | yyyy 或yy | -| 季度 | quarter | qq,q | -| 月 | month | mm,m | -| 一年中第几天 | dayofyear | dy,y | -| 日 | day | dd,d | -| 一年中第几周 | week | wk,ww | -| 星期 | weekday | dw | -| 小时 | Hour | hh | -| 分钟 | minute | mi,n | -| 秒 | second | ss,s | -| 毫秒 | millisecond | ms | - -SQL语句执行顺序: - -```sql -(7) SELECT -(8) DISTINCT -(1) FROM -(3) JOIN -(2) ON -(4) WHERE -(5) GROUP BY -(6) HAVING -(9) ORDER BY -(10) LIMIT -``` - - - -- Gitee From e912d023a7af69b0e2a0c168bc38a50cf02f42d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE?= <3032059138@qq.com> Date: Sun, 18 Sep 2022 14:49:02 +0000 Subject: [PATCH 07/13] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20?= =?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 --- "\350\242\201\350\264\265\346\243\256./.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 "\350\242\201\350\264\265\346\243\256./.keep" diff --git "a/\350\242\201\350\264\265\346\243\256./.keep" "b/\350\242\201\350\264\265\346\243\256./.keep" deleted file mode 100644 index e69de29..0000000 -- Gitee From a261950a1f80741b36548a6bdcb133e541725dd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE?= <3032059138@qq.com> Date: Sun, 18 Sep 2022 14:49:12 +0000 Subject: [PATCH 08/13] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E8=A2=81=E8=B4=B5?= =?UTF-8?q?=E6=A3=AE.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\350\242\201\350\264\265\346\243\256./.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\350\242\201\350\264\265\346\243\256./.keep" diff --git "a/\350\242\201\350\264\265\346\243\256./.keep" "b/\350\242\201\350\264\265\346\243\256./.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 4864bd67301692890d84e021002b04b523b90444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE?= <3032059138@qq.com> Date: Sun, 18 Sep 2022 14:49:25 +0000 Subject: [PATCH 09/13] =?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> --- .../SQLQuery1.sql" | 299 ++++++++++++++++++ .../\347\254\224\350\256\260.txt" | 111 +++++++ 2 files changed, 410 insertions(+) create mode 100644 "\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" create mode 100644 "\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" diff --git "a/\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" "b/\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" new file mode 100644 index 0000000..0231695 --- /dev/null +++ "b/\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" @@ -0,0 +1,299 @@ +if exists(select * from sys.databases where name='DBTEST') +drop database DBTEST; +go + +create database DBTEST; +go + +use DBTEST; +go + +--ű +create table Department +( + --ű;int;primary key;identity(1,1)1ʼΪ1; + DepartmentId int primary key identity(1,1), + --;nvarchar(50)50ַ;not nullΪ; + DepartmentName nvarchar(50) not null, + --textı; + DepartmentRemark text +); +go + +--ְrankΪϵͳؼ֣˴ʹ[]Զ֣ϵͳؼ +create table [Rank] +( + RankId int primary key identity(1,1), + RankName nvarchar(50) not null, + RankRemark text +); +go + +--ԱϢ +create table People +( + PeopleId int primary key identity(1,1), + --references,ֶαԼ + DepartmentId int references Department(DepartmentId) not null, + RankId int references [Rank](RankId) not null, + PeopleName nvarchar(50) not null, + --defaultֶĬֵ; checkԹ涨ֵֶԼ; + PeopleSex nvarchar(1) default('') check(PeopleSex='' or PeopleSex='Ů') not null, + PeopleBirth datetime not null, + PeopleSalary decimal(12,2) check(PeopleSalary>= 1000 and PeopleSalary <= 100000) not null, + --uniqueΨһԼΪṩΨһԱ֤; + PeoplePhone nvarchar(20) unique not null, + PeopleAddress nvarchar(100), + --datetimesmalldatetimeԱʾʱͣgetdate()ڻȡϵͳǰʱ + PeopleAddTime smalldatetime default(getdate()) +); +go + + + +------------------------------ݲ------------------------------ +--ű +insert into Department values('','˾IJ'); +go + +--һԲ +insert into Department(DepartmentName,DepartmentRemark) +select 'г','ţIJ' union +select 'Ʒ','ǿյIJ' union +select 'ܾ','쵼IJ' ; +go + +-------ְ +insert into [Rank](RankName,RankRemark) +values('','') +insert into [Rank](RankName,RankRemark) +values('м','߱') +insert into [Rank](RankName,RankRemark) +values('߼','߱Դȫ'); +go + +---------Ա +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,3,'','','1984-7-9',20000,'13554785452','ɶ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,2,'','Ů','1987-7-9',15000,'13256854578','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,1,'','','1988-8-8',12000,'13985745871','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,1,'ŷ','','1990-8-8',8000,'13535987412','˲',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,3,'','','1989-4-8',9000,'13845789568','˲',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,3,'','','1995-4-8',9500,'13878562568','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,2,'Ƹ','','1989-4-20',8500,'13335457412','人',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,1,'','Ů','1989-4-20',6500,'13437100050','人',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,2,'ܲ','','1987-12-20',25000,'13889562354','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,3,'','','1981-11-11',9000,'13385299632','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,1,'Τ','','1978-1-13',8000,'13478545263','Ϻ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,1,'','','1998-12-12',7500,'13878523695','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,3,'','','1968-11-22',9000,'13698545841','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,3,'','','1988-1-22',11000,'13558745874','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,2,'Ȩ','','1990-2-21',12000,'13698745214','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,2,'','Ů','1995-2-21',13000,'13985478512','Ϻ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,1,'С','Ů','1996-2-21',13500,'13778787874','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,2,'','','1992-10-11',8000,'13987455214','人',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,3,'³','','1984-9-10',5500,'13254785965','ɶ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,3,'','','1987-5-19',8500,'13352197364','ɶ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,1,'½ѷ','','1996-5-19',7500,'13025457392','Ͼ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,2,'̫ʷ','','1983-6-1',7500,'13077778888','Ϻ',getdate()) + + +---ѯ +select * from Department +select * from [Rank] +select * from People + + +--1. ѯ + +--2. ָвѯԱн绰 +select PeopleName as ,Peoplesex as Ա from People; +--3. ָвѯ,ԶԱн绰 +--4. ѯ˾ԱڳУҪظݣ + +--ظУdistinct +select distinct peopleAddress from People; +--5. 蹤յ10%ѯԭʼʺ͵ĹʣʾԱнннвѯ +select top 3 PeopleName , PeopleSalary н, PeopleSalary*1.1 нн from People; + +--top ordey by + + +select * from People +--ָУԱн绰ѯԱΪŮԱϢ,Զ +--2. ѯнڵ10000 ԱϢ( ) +--3. ѯнڵ10000 ŮԱϢ() +select * from People where PeopleSalary>=10000 and PeopleSex='Ů' +--4. ʾ1980-1-1֮󣬶нڵ10000ŮԱϢ +select * from People where PeopleSalary>=10000 and PeopleSex='Ů' and PeopleBirth>'1995-1-1'; + +--5. ʾнڵ15000 Ա,нڵ8000ŮԱϢ +select * from People where (PeopleSalary>=8000 and peoplesex='Ů') or PeopleSalary>=15000 +--6. ѯн10000-20000 ֮ԱϢ( ) +select * from People where PeopleSalary between 10000 and 20000 +--7. ѯַڱϺԱϢ +--8. ѯԱϢ(ݹ򣬽): ascĬϣ: desc: +select * from People order by PeopleSalary desc + +--9. ʾеԱϢֵijȽе +--10. ѯߵ5˵Ϣ +select top 5 * from People order by PeopleSalary desc +--11. ѯߵ10%ԱϢ +select top 10 percent * from People order by PeopleSalary desc +--12. ѯַûдԱϢ +--Ϊղ=is null, is not null +select * from People where PeopleAddress is null; + +--13. ѯַѾдԱϢ +--14. ѯе80ԱϢ + +--15. ѯ30-40 ֮䣬ҹ15000-30000 ֮ԱϢ +--16. ѯз 6.22--7.22 ԱϢ +select * from people where +(MONTH(PeopleBirth)=6 and DAY(PeopleBirth)>=22) or +(MONTH(PeopleBirth)=7 and DAY(PeopleBirth)<=22) +; +--17. ѯʱƸߵ +select * from people where PeopleSalary > 9000 +--18. ѯͬһе +--19. ѯФΪԱϢ +--20. ѯԱϢһʾ(,ţ,,,,,,,,,,) +--case when end + +--ѯз 6.22--7.22 ԱϢ +select * from People where +(MONTH(PeopleBirth) = 6 and DAY(PeopleBirth) >=22) or +(MONTH(PeopleBirth) = 7 and DAY(PeopleBirth) <=22) + +--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(*) Ա,sum(PeopleSalary) Աܺ,convert(decimal(15,2),avg(PeopleSalary)) ƽ,max(PeopleSalary) ߹,min(PeopleSalary) ͹ from people p +inner join Department d on d.DepartmentId=p.DepartmentId +group by d.DepartmentId + +--4. ݲŷͳԱԱܺͣƽʣ߹ʺ͹ʣƽ10000 µIJͳƣ +--ҸƽʽС +select count(*) Ա,sum(PeopleSalary) Աܺ,convert(decimal(15,2),avg(PeopleSalary)) ƽ,max(PeopleSalary) ߹,min(PeopleSalary) ͹ from people p +inner join Department d on d.DepartmentId=p.DepartmentId +group by d.DepartmentId +having convert(decimal(15,2),avg(PeopleSalary))>=10000 +order by ƽ desc + +--5. ݲƣȻְλƣͳԱԱܺͣƽʣ߹ʺ͹ +select [Rank].RankName ְλ,count(*) Ա,sum(PeopleSalary) Աܺ,convert(decimal(15,2),avg(PeopleSalary)) ƽ,max(PeopleSalary) ߹, +min(PeopleSalary) ͹ from Department +inner join People on People.DepartmentId=Department.DepartmentId +inner join [Rank] on People.RankId=[Rank].RankId +group by [Rank].RankName + +--ѯԱϢһʾ(,ţ,,,,,,,,,,) +select PeopleName ,PeopleSex Ա,PeopleSalary ,PeoplePhone 绰,PEOPLEBIRTH , +case +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 '' +when year(PeopleBirth) % 12 = 0 then '' +else '' +end "Ф" +from People + +select PeopleName ,PeopleSex Ա,PeopleSalary ,PeoplePhone 绰,PEOPLEBIRTH , +case + 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 '' + when year(PeopleBirth) % 12 = 0 then '' + when year(PeopleBirth) % 12 = 1 then '' + when year(PeopleBirth) % 12 = 2 then '' + when year(PeopleBirth) % 12 = 3 then '' + ELSE '' +end Ф +from People \ No newline at end of file diff --git "a/\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" "b/\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" new file mode 100644 index 0000000..d318cf7 --- /dev/null +++ "b/\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" @@ -0,0 +1,111 @@ +**SQL中常用运算符** + +```sql +=:等于,比较是否相等及赋值 +!=:比较不等于 +>:比较大于 +<:比较小于 +>=:比较大于等于 +<=:比较小于等于 +IS NULL:比较为空 +IS NOT NULL:比较不为空 +in:比较是否在其中 +like:模糊查询 +BETWEEN...AND...:比较是否在两者之间 50-100 BETWEEN 50 AND 100 +and:逻辑与(两个条件同时成立表达式成立) +or:逻辑或(两个条件有一个成立表达式成立) +not:逻辑非(条件成立,表达式则不成立;条件不成立,表达式则成立) +``` +#### 模糊查询 + +模糊查询使用like关键字和通配符结合来实现,通配符具体含义如下: + +```sql +%:代表匹配0个字符、1个字符或多个字符。 +_:代表匹配有且只有1个字符。 +[]:代表匹配范围内 +[^]:代表匹配不在范围内 +``` +#### 聚合函数 + +SQL SERVER中聚合函数主要有: + +```sql +count:求数量 +max:求最大值 +min:求最小值 +sum:求和 +avg:求平均值 +``` +ROUND函数用法: + +```sql +round(num,len,[type]) +其中: +num表示需要处理的数字,len表示需要保留的长度,type处理类型(0是默认值代表四舍五入,非0代表直接截取) +select ROUND(123.45454,3) --123.45500 +select ROUND(123.45454,3,1) --123.45400 + +CONVERT()与CAST()函数: + +```sql +--1.保留小数 +convert(decimal(13,2),12.45454) +cast(12.45454 as decimal(13,2)) +--2.强制转换类型 +``` + + + +#### SQL中常用的时间函数 + +```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 +``` + +**时间格式控制字符串:** + +| 名称 | 日期单位 | 缩写 | +| ------------ | ----------- | --------- | +| 年 | year | yyyy 或yy | +| 季度 | quarter | qq,q | +| 月 | month | mm,m | +| 一年中第几天 | dayofyear | dy,y | +| 日 | day | dd,d | +| 一年中第几周 | week | wk,ww | +| 星期 | weekday | dw | +| 小时 | Hour | hh | +| 分钟 | minute | mi,n | +| 秒 | second | ss,s | +| 毫秒 | millisecond | ms | + +SQL语句执行顺序: + +```sql +(7) SELECT +(8) DISTINCT +(1) FROM +(3) JOIN +(2) ON +(4) WHERE +(5) GROUP BY +(6) HAVING +(9) ORDER BY +(10) LIMIT +``` + + + -- Gitee From 95f947e71d5fd2179c91858b47ee5d609d6eee3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE?= <3032059138@qq.com> Date: Sun, 18 Sep 2022 14:49:49 +0000 Subject: [PATCH 10/13] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20?= =?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 --- "\350\242\201\350\264\265\346\243\256./.keep" | 0 .../SQLQuery1.sql" | 299 ------------------ .../\347\254\224\350\256\260.txt" | 111 ------- 3 files changed, 410 deletions(-) delete mode 100644 "\350\242\201\350\264\265\346\243\256./.keep" delete mode 100644 "\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" delete mode 100644 "\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" diff --git "a/\350\242\201\350\264\265\346\243\256./.keep" "b/\350\242\201\350\264\265\346\243\256./.keep" deleted file mode 100644 index e69de29..0000000 diff --git "a/\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" "b/\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" deleted file mode 100644 index 0231695..0000000 --- "a/\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" +++ /dev/null @@ -1,299 +0,0 @@ -if exists(select * from sys.databases where name='DBTEST') -drop database DBTEST; -go - -create database DBTEST; -go - -use DBTEST; -go - ---ű -create table Department -( - --ű;int;primary key;identity(1,1)1ʼΪ1; - DepartmentId int primary key identity(1,1), - --;nvarchar(50)50ַ;not nullΪ; - DepartmentName nvarchar(50) not null, - --textı; - DepartmentRemark text -); -go - ---ְrankΪϵͳؼ֣˴ʹ[]Զ֣ϵͳؼ -create table [Rank] -( - RankId int primary key identity(1,1), - RankName nvarchar(50) not null, - RankRemark text -); -go - ---ԱϢ -create table People -( - PeopleId int primary key identity(1,1), - --references,ֶαԼ - DepartmentId int references Department(DepartmentId) not null, - RankId int references [Rank](RankId) not null, - PeopleName nvarchar(50) not null, - --defaultֶĬֵ; checkԹ涨ֵֶԼ; - PeopleSex nvarchar(1) default('') check(PeopleSex='' or PeopleSex='Ů') not null, - PeopleBirth datetime not null, - PeopleSalary decimal(12,2) check(PeopleSalary>= 1000 and PeopleSalary <= 100000) not null, - --uniqueΨһԼΪṩΨһԱ֤; - PeoplePhone nvarchar(20) unique not null, - PeopleAddress nvarchar(100), - --datetimesmalldatetimeԱʾʱͣgetdate()ڻȡϵͳǰʱ - PeopleAddTime smalldatetime default(getdate()) -); -go - - - -------------------------------ݲ------------------------------ ---ű -insert into Department values('','˾IJ'); -go - ---һԲ -insert into Department(DepartmentName,DepartmentRemark) -select 'г','ţIJ' union -select 'Ʒ','ǿյIJ' union -select 'ܾ','쵼IJ' ; -go - --------ְ -insert into [Rank](RankName,RankRemark) -values('','') -insert into [Rank](RankName,RankRemark) -values('м','߱') -insert into [Rank](RankName,RankRemark) -values('߼','߱Դȫ'); -go - ----------Ա -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(1,3,'','','1984-7-9',20000,'13554785452','ɶ',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(1,2,'','Ů','1987-7-9',15000,'13256854578','',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(1,1,'','','1988-8-8',12000,'13985745871','',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(2,1,'ŷ','','1990-8-8',8000,'13535987412','˲',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(2,3,'','','1989-4-8',9000,'13845789568','˲',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(3,3,'','','1995-4-8',9500,'13878562568','',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(3,2,'Ƹ','','1989-4-20',8500,'13335457412','人',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(3,1,'','Ů','1989-4-20',6500,'13437100050','人',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(2,2,'ܲ','','1987-12-20',25000,'13889562354','',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(2,3,'','','1981-11-11',9000,'13385299632','',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(2,1,'Τ','','1978-1-13',8000,'13478545263','Ϻ',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(1,1,'','','1998-12-12',7500,'13878523695','',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(1,3,'','','1968-11-22',9000,'13698545841','',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(3,3,'','','1988-1-22',11000,'13558745874','',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(3,2,'Ȩ','','1990-2-21',12000,'13698745214','',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(3,2,'','Ů','1995-2-21',13000,'13985478512','Ϻ',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(2,1,'С','Ů','1996-2-21',13500,'13778787874','',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(1,2,'','','1992-10-11',8000,'13987455214','人',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(2,3,'³','','1984-9-10',5500,'13254785965','ɶ',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(3,3,'','','1987-5-19',8500,'13352197364','ɶ',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(1,1,'½ѷ','','1996-5-19',7500,'13025457392','Ͼ',getdate()) - -insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, -PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) -values(1,2,'̫ʷ','','1983-6-1',7500,'13077778888','Ϻ',getdate()) - - ----ѯ -select * from Department -select * from [Rank] -select * from People - - ---1. ѯ - ---2. ָвѯԱн绰 -select PeopleName as ,Peoplesex as Ա from People; ---3. ָвѯ,ԶԱн绰 ---4. ѯ˾ԱڳУҪظݣ - ---ظУdistinct -select distinct peopleAddress from People; ---5. 蹤յ10%ѯԭʼʺ͵ĹʣʾԱнннвѯ -select top 3 PeopleName , PeopleSalary н, PeopleSalary*1.1 нн from People; - ---top ordey by - - -select * from People ---ָУԱн绰ѯԱΪŮԱϢ,Զ ---2. ѯнڵ10000 ԱϢ( ) ---3. ѯнڵ10000 ŮԱϢ() -select * from People where PeopleSalary>=10000 and PeopleSex='Ů' ---4. ʾ1980-1-1֮󣬶нڵ10000ŮԱϢ -select * from People where PeopleSalary>=10000 and PeopleSex='Ů' and PeopleBirth>'1995-1-1'; - ---5. ʾнڵ15000 Ա,нڵ8000ŮԱϢ -select * from People where (PeopleSalary>=8000 and peoplesex='Ů') or PeopleSalary>=15000 ---6. ѯн10000-20000 ֮ԱϢ( ) -select * from People where PeopleSalary between 10000 and 20000 ---7. ѯַڱϺԱϢ ---8. ѯԱϢ(ݹ򣬽): ascĬϣ: desc: -select * from People order by PeopleSalary desc - ---9. ʾеԱϢֵijȽе ---10. ѯߵ5˵Ϣ -select top 5 * from People order by PeopleSalary desc ---11. ѯߵ10%ԱϢ -select top 10 percent * from People order by PeopleSalary desc ---12. ѯַûдԱϢ ---Ϊղ=is null, is not null -select * from People where PeopleAddress is null; - ---13. ѯַѾдԱϢ ---14. ѯе80ԱϢ - ---15. ѯ30-40 ֮䣬ҹ15000-30000 ֮ԱϢ ---16. ѯз 6.22--7.22 ԱϢ -select * from people where -(MONTH(PeopleBirth)=6 and DAY(PeopleBirth)>=22) or -(MONTH(PeopleBirth)=7 and DAY(PeopleBirth)<=22) -; ---17. ѯʱƸߵ -select * from people where PeopleSalary > 9000 ---18. ѯͬһе ---19. ѯФΪԱϢ ---20. ѯԱϢһʾ(,ţ,,,,,,,,,,) ---case when end - ---ѯз 6.22--7.22 ԱϢ -select * from People where -(MONTH(PeopleBirth) = 6 and DAY(PeopleBirth) >=22) or -(MONTH(PeopleBirth) = 7 and DAY(PeopleBirth) <=22) - ---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(*) Ա,sum(PeopleSalary) Աܺ,convert(decimal(15,2),avg(PeopleSalary)) ƽ,max(PeopleSalary) ߹,min(PeopleSalary) ͹ from people p -inner join Department d on d.DepartmentId=p.DepartmentId -group by d.DepartmentId - ---4. ݲŷͳԱԱܺͣƽʣ߹ʺ͹ʣƽ10000 µIJͳƣ ---ҸƽʽС -select count(*) Ա,sum(PeopleSalary) Աܺ,convert(decimal(15,2),avg(PeopleSalary)) ƽ,max(PeopleSalary) ߹,min(PeopleSalary) ͹ from people p -inner join Department d on d.DepartmentId=p.DepartmentId -group by d.DepartmentId -having convert(decimal(15,2),avg(PeopleSalary))>=10000 -order by ƽ desc - ---5. ݲƣȻְλƣͳԱԱܺͣƽʣ߹ʺ͹ -select [Rank].RankName ְλ,count(*) Ա,sum(PeopleSalary) Աܺ,convert(decimal(15,2),avg(PeopleSalary)) ƽ,max(PeopleSalary) ߹, -min(PeopleSalary) ͹ from Department -inner join People on People.DepartmentId=Department.DepartmentId -inner join [Rank] on People.RankId=[Rank].RankId -group by [Rank].RankName - ---ѯԱϢһʾ(,ţ,,,,,,,,,,) -select PeopleName ,PeopleSex Ա,PeopleSalary ,PeoplePhone 绰,PEOPLEBIRTH , -case -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 '' -when year(PeopleBirth) % 12 = 0 then '' -else '' -end "Ф" -from People - -select PeopleName ,PeopleSex Ա,PeopleSalary ,PeoplePhone 绰,PEOPLEBIRTH , -case - 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 '' - when year(PeopleBirth) % 12 = 0 then '' - when year(PeopleBirth) % 12 = 1 then '' - when year(PeopleBirth) % 12 = 2 then '' - when year(PeopleBirth) % 12 = 3 then '' - ELSE '' -end Ф -from People \ No newline at end of file diff --git "a/\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" "b/\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" deleted file mode 100644 index d318cf7..0000000 --- "a/\350\242\201\350\264\265\346\243\256./\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" +++ /dev/null @@ -1,111 +0,0 @@ -**SQL中常用运算符** - -```sql -=:等于,比较是否相等及赋值 -!=:比较不等于 ->:比较大于 -<:比较小于 ->=:比较大于等于 -<=:比较小于等于 -IS NULL:比较为空 -IS NOT NULL:比较不为空 -in:比较是否在其中 -like:模糊查询 -BETWEEN...AND...:比较是否在两者之间 50-100 BETWEEN 50 AND 100 -and:逻辑与(两个条件同时成立表达式成立) -or:逻辑或(两个条件有一个成立表达式成立) -not:逻辑非(条件成立,表达式则不成立;条件不成立,表达式则成立) -``` -#### 模糊查询 - -模糊查询使用like关键字和通配符结合来实现,通配符具体含义如下: - -```sql -%:代表匹配0个字符、1个字符或多个字符。 -_:代表匹配有且只有1个字符。 -[]:代表匹配范围内 -[^]:代表匹配不在范围内 -``` -#### 聚合函数 - -SQL SERVER中聚合函数主要有: - -```sql -count:求数量 -max:求最大值 -min:求最小值 -sum:求和 -avg:求平均值 -``` -ROUND函数用法: - -```sql -round(num,len,[type]) -其中: -num表示需要处理的数字,len表示需要保留的长度,type处理类型(0是默认值代表四舍五入,非0代表直接截取) -select ROUND(123.45454,3) --123.45500 -select ROUND(123.45454,3,1) --123.45400 - -CONVERT()与CAST()函数: - -```sql ---1.保留小数 -convert(decimal(13,2),12.45454) -cast(12.45454 as decimal(13,2)) ---2.强制转换类型 -``` - - - -#### SQL中常用的时间函数 - -```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 -``` - -**时间格式控制字符串:** - -| 名称 | 日期单位 | 缩写 | -| ------------ | ----------- | --------- | -| 年 | year | yyyy 或yy | -| 季度 | quarter | qq,q | -| 月 | month | mm,m | -| 一年中第几天 | dayofyear | dy,y | -| 日 | day | dd,d | -| 一年中第几周 | week | wk,ww | -| 星期 | weekday | dw | -| 小时 | Hour | hh | -| 分钟 | minute | mi,n | -| 秒 | second | ss,s | -| 毫秒 | millisecond | ms | - -SQL语句执行顺序: - -```sql -(7) SELECT -(8) DISTINCT -(1) FROM -(3) JOIN -(2) ON -(4) WHERE -(5) GROUP BY -(6) HAVING -(9) ORDER BY -(10) LIMIT -``` - - - -- Gitee From 1ab370beb33b79e7b1b4e8a05bf2867f54694f15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE?= <3032059138@qq.com> Date: Sun, 18 Sep 2022 14:49:57 +0000 Subject: [PATCH 11/13] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20?= =?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 --- "\350\242\201\350\264\265\346\243\256/.keep" | 0 "\350\242\201\350\264\265\346\243\256/28.sql" | 52 ----------------- .../\347\254\224\350\256\260.txt" | 58 ------------------- 3 files changed, 110 deletions(-) delete mode 100644 "\350\242\201\350\264\265\346\243\256/.keep" delete mode 100644 "\350\242\201\350\264\265\346\243\256/28.sql" delete mode 100644 "\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" diff --git "a/\350\242\201\350\264\265\346\243\256/.keep" "b/\350\242\201\350\264\265\346\243\256/.keep" deleted file mode 100644 index e69de29..0000000 diff --git "a/\350\242\201\350\264\265\346\243\256/28.sql" "b/\350\242\201\350\264\265\346\243\256/28.sql" deleted file mode 100644 index 9f2be17..0000000 --- "a/\350\242\201\350\264\265\346\243\256/28.sql" +++ /dev/null @@ -1,52 +0,0 @@ ---判断数据库是否存在 -if exists (select * from sys.databases where name='DBTEXT') -drop database DBTEXT; ---创建数据库 -create database DBTEXT ---使用数据库 - - ---创建数据表 ---primary key 主键 ---identity 自增(标识符) ---default 默约束 ---check 检查 - ---部门信息表 -create table sectionInfo( - sectionID int primary key identity(2010,1), - sectionName varchar(10) not null -); - -INSERT into sectionInfo (sectionName) VALUES ('张三','李四','全网','暗属性出','认为对方','违法的'), ---员工信息表 -create table userInfo( - userNo int identity(1000,1) primary key not null, - userName varchar(10) not null check(len(userName)>4) unique, - userSex varchar(2) not null check(userSex='男' or userSex='女'), - userAge int not null check(userAge between 1 and 100), - userAddress varchar(50) default ('湖北'), - userSection int -); -Alter table sectionInfo add constraint FK_sectionInfo_ClassId foreign key(userSection) references asd(sectionID); -INSERT into userInfo (userName,userSex,userAge,userAddress,userSection) VALUES -('张三','男',19,'湖南',1), -('张嘶','女',20,'福建',2), -('张无','女',17,'广东',1), -('张六','男',19,'湖北',2), -('张气','男',17,'湖南',1) ---员工考勤表 -create table workInfo( - workId int identity(1,1) primary key not null, - userId int not null, - workTime datetime not null, - workDescription varchar (40) not null check(workDescription='迟到' or workDescription='早退' or workDescription='旷工' or workDescription='病假' or workDescription='事假') -); -Alter table workInfo add constraint FK_workInfo_ClassId foreign key(userId) references sectionInfo(userNo); - -INSERT into workInfo (workTime,workDescription) VALUES -('2021-09-05','早退'), -('2022-08-06','迟到'), -('2023-09-10','事假'), -('2020-09-20','旷工'), -('2021-10-05','病假') \ No newline at end of file diff --git "a/\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" "b/\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" deleted file mode 100644 index 3631665..0000000 --- "a/\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" +++ /dev/null @@ -1,58 +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) - - - - select * from StuInfo; \ No newline at end of file -- Gitee From f797da6eed476dc7b4d581464f09772a4ca79f2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE?= <3032059138@qq.com> Date: Sun, 18 Sep 2022 14:50:08 +0000 Subject: [PATCH 12/13] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E8=A2=81=E8=B4=B5?= =?UTF-8?q?=E6=A3=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\350\242\201\350\264\265\346\243\256/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\350\242\201\350\264\265\346\243\256/.keep" diff --git "a/\350\242\201\350\264\265\346\243\256/.keep" "b/\350\242\201\350\264\265\346\243\256/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 49b9b3c0f102b67e1d99e7ff7f0947111862a5c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE?= <3032059138@qq.com> Date: Sun, 18 Sep 2022 14:50:22 +0000 Subject: [PATCH 13/13] =?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> --- .../SQLQuery1.sql" | 299 ++++++++++++++++++ .../\347\254\224\350\256\260.txt" | 111 +++++++ 2 files changed, 410 insertions(+) create mode 100644 "\350\242\201\350\264\265\346\243\256/\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" create mode 100644 "\350\242\201\350\264\265\346\243\256/\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" diff --git "a/\350\242\201\350\264\265\346\243\256/\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" "b/\350\242\201\350\264\265\346\243\256/\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" new file mode 100644 index 0000000..0231695 --- /dev/null +++ "b/\350\242\201\350\264\265\346\243\256/\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" @@ -0,0 +1,299 @@ +if exists(select * from sys.databases where name='DBTEST') +drop database DBTEST; +go + +create database DBTEST; +go + +use DBTEST; +go + +--ű +create table Department +( + --ű;int;primary key;identity(1,1)1ʼΪ1; + DepartmentId int primary key identity(1,1), + --;nvarchar(50)50ַ;not nullΪ; + DepartmentName nvarchar(50) not null, + --textı; + DepartmentRemark text +); +go + +--ְrankΪϵͳؼ֣˴ʹ[]Զ֣ϵͳؼ +create table [Rank] +( + RankId int primary key identity(1,1), + RankName nvarchar(50) not null, + RankRemark text +); +go + +--ԱϢ +create table People +( + PeopleId int primary key identity(1,1), + --references,ֶαԼ + DepartmentId int references Department(DepartmentId) not null, + RankId int references [Rank](RankId) not null, + PeopleName nvarchar(50) not null, + --defaultֶĬֵ; checkԹ涨ֵֶԼ; + PeopleSex nvarchar(1) default('') check(PeopleSex='' or PeopleSex='Ů') not null, + PeopleBirth datetime not null, + PeopleSalary decimal(12,2) check(PeopleSalary>= 1000 and PeopleSalary <= 100000) not null, + --uniqueΨһԼΪṩΨһԱ֤; + PeoplePhone nvarchar(20) unique not null, + PeopleAddress nvarchar(100), + --datetimesmalldatetimeԱʾʱͣgetdate()ڻȡϵͳǰʱ + PeopleAddTime smalldatetime default(getdate()) +); +go + + + +------------------------------ݲ------------------------------ +--ű +insert into Department values('','˾IJ'); +go + +--һԲ +insert into Department(DepartmentName,DepartmentRemark) +select 'г','ţIJ' union +select 'Ʒ','ǿյIJ' union +select 'ܾ','쵼IJ' ; +go + +-------ְ +insert into [Rank](RankName,RankRemark) +values('','') +insert into [Rank](RankName,RankRemark) +values('м','߱') +insert into [Rank](RankName,RankRemark) +values('߼','߱Դȫ'); +go + +---------Ա +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,3,'','','1984-7-9',20000,'13554785452','ɶ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,2,'','Ů','1987-7-9',15000,'13256854578','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,1,'','','1988-8-8',12000,'13985745871','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,1,'ŷ','','1990-8-8',8000,'13535987412','˲',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,3,'','','1989-4-8',9000,'13845789568','˲',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,3,'','','1995-4-8',9500,'13878562568','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,2,'Ƹ','','1989-4-20',8500,'13335457412','人',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,1,'','Ů','1989-4-20',6500,'13437100050','人',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,2,'ܲ','','1987-12-20',25000,'13889562354','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,3,'','','1981-11-11',9000,'13385299632','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,1,'Τ','','1978-1-13',8000,'13478545263','Ϻ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,1,'','','1998-12-12',7500,'13878523695','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,3,'','','1968-11-22',9000,'13698545841','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,3,'','','1988-1-22',11000,'13558745874','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,2,'Ȩ','','1990-2-21',12000,'13698745214','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,2,'','Ů','1995-2-21',13000,'13985478512','Ϻ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,1,'С','Ů','1996-2-21',13500,'13778787874','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,2,'','','1992-10-11',8000,'13987455214','人',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,3,'³','','1984-9-10',5500,'13254785965','ɶ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,3,'','','1987-5-19',8500,'13352197364','ɶ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,1,'½ѷ','','1996-5-19',7500,'13025457392','Ͼ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,2,'̫ʷ','','1983-6-1',7500,'13077778888','Ϻ',getdate()) + + +---ѯ +select * from Department +select * from [Rank] +select * from People + + +--1. ѯ + +--2. ָвѯԱн绰 +select PeopleName as ,Peoplesex as Ա from People; +--3. ָвѯ,ԶԱн绰 +--4. ѯ˾ԱڳУҪظݣ + +--ظУdistinct +select distinct peopleAddress from People; +--5. 蹤յ10%ѯԭʼʺ͵ĹʣʾԱнннвѯ +select top 3 PeopleName , PeopleSalary н, PeopleSalary*1.1 нн from People; + +--top ordey by + + +select * from People +--ָУԱн绰ѯԱΪŮԱϢ,Զ +--2. ѯнڵ10000 ԱϢ( ) +--3. ѯнڵ10000 ŮԱϢ() +select * from People where PeopleSalary>=10000 and PeopleSex='Ů' +--4. ʾ1980-1-1֮󣬶нڵ10000ŮԱϢ +select * from People where PeopleSalary>=10000 and PeopleSex='Ů' and PeopleBirth>'1995-1-1'; + +--5. ʾнڵ15000 Ա,нڵ8000ŮԱϢ +select * from People where (PeopleSalary>=8000 and peoplesex='Ů') or PeopleSalary>=15000 +--6. ѯн10000-20000 ֮ԱϢ( ) +select * from People where PeopleSalary between 10000 and 20000 +--7. ѯַڱϺԱϢ +--8. ѯԱϢ(ݹ򣬽): ascĬϣ: desc: +select * from People order by PeopleSalary desc + +--9. ʾеԱϢֵijȽе +--10. ѯߵ5˵Ϣ +select top 5 * from People order by PeopleSalary desc +--11. ѯߵ10%ԱϢ +select top 10 percent * from People order by PeopleSalary desc +--12. ѯַûдԱϢ +--Ϊղ=is null, is not null +select * from People where PeopleAddress is null; + +--13. ѯַѾдԱϢ +--14. ѯе80ԱϢ + +--15. ѯ30-40 ֮䣬ҹ15000-30000 ֮ԱϢ +--16. ѯз 6.22--7.22 ԱϢ +select * from people where +(MONTH(PeopleBirth)=6 and DAY(PeopleBirth)>=22) or +(MONTH(PeopleBirth)=7 and DAY(PeopleBirth)<=22) +; +--17. ѯʱƸߵ +select * from people where PeopleSalary > 9000 +--18. ѯͬһе +--19. ѯФΪԱϢ +--20. ѯԱϢһʾ(,ţ,,,,,,,,,,) +--case when end + +--ѯз 6.22--7.22 ԱϢ +select * from People where +(MONTH(PeopleBirth) = 6 and DAY(PeopleBirth) >=22) or +(MONTH(PeopleBirth) = 7 and DAY(PeopleBirth) <=22) + +--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(*) Ա,sum(PeopleSalary) Աܺ,convert(decimal(15,2),avg(PeopleSalary)) ƽ,max(PeopleSalary) ߹,min(PeopleSalary) ͹ from people p +inner join Department d on d.DepartmentId=p.DepartmentId +group by d.DepartmentId + +--4. ݲŷͳԱԱܺͣƽʣ߹ʺ͹ʣƽ10000 µIJͳƣ +--ҸƽʽС +select count(*) Ա,sum(PeopleSalary) Աܺ,convert(decimal(15,2),avg(PeopleSalary)) ƽ,max(PeopleSalary) ߹,min(PeopleSalary) ͹ from people p +inner join Department d on d.DepartmentId=p.DepartmentId +group by d.DepartmentId +having convert(decimal(15,2),avg(PeopleSalary))>=10000 +order by ƽ desc + +--5. ݲƣȻְλƣͳԱԱܺͣƽʣ߹ʺ͹ +select [Rank].RankName ְλ,count(*) Ա,sum(PeopleSalary) Աܺ,convert(decimal(15,2),avg(PeopleSalary)) ƽ,max(PeopleSalary) ߹, +min(PeopleSalary) ͹ from Department +inner join People on People.DepartmentId=Department.DepartmentId +inner join [Rank] on People.RankId=[Rank].RankId +group by [Rank].RankName + +--ѯԱϢһʾ(,ţ,,,,,,,,,,) +select PeopleName ,PeopleSex Ա,PeopleSalary ,PeoplePhone 绰,PEOPLEBIRTH , +case +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 '' +when year(PeopleBirth) % 12 = 0 then '' +else '' +end "Ф" +from People + +select PeopleName ,PeopleSex Ա,PeopleSalary ,PeoplePhone 绰,PEOPLEBIRTH , +case + 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 '' + when year(PeopleBirth) % 12 = 0 then '' + when year(PeopleBirth) % 12 = 1 then '' + when year(PeopleBirth) % 12 = 2 then '' + when year(PeopleBirth) % 12 = 3 then '' + ELSE '' +end Ф +from People \ No newline at end of file diff --git "a/\350\242\201\350\264\265\346\243\256/\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" "b/\350\242\201\350\264\265\346\243\256/\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" new file mode 100644 index 0000000..d318cf7 --- /dev/null +++ "b/\350\242\201\350\264\265\346\243\256/\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" @@ -0,0 +1,111 @@ +**SQL中常用运算符** + +```sql +=:等于,比较是否相等及赋值 +!=:比较不等于 +>:比较大于 +<:比较小于 +>=:比较大于等于 +<=:比较小于等于 +IS NULL:比较为空 +IS NOT NULL:比较不为空 +in:比较是否在其中 +like:模糊查询 +BETWEEN...AND...:比较是否在两者之间 50-100 BETWEEN 50 AND 100 +and:逻辑与(两个条件同时成立表达式成立) +or:逻辑或(两个条件有一个成立表达式成立) +not:逻辑非(条件成立,表达式则不成立;条件不成立,表达式则成立) +``` +#### 模糊查询 + +模糊查询使用like关键字和通配符结合来实现,通配符具体含义如下: + +```sql +%:代表匹配0个字符、1个字符或多个字符。 +_:代表匹配有且只有1个字符。 +[]:代表匹配范围内 +[^]:代表匹配不在范围内 +``` +#### 聚合函数 + +SQL SERVER中聚合函数主要有: + +```sql +count:求数量 +max:求最大值 +min:求最小值 +sum:求和 +avg:求平均值 +``` +ROUND函数用法: + +```sql +round(num,len,[type]) +其中: +num表示需要处理的数字,len表示需要保留的长度,type处理类型(0是默认值代表四舍五入,非0代表直接截取) +select ROUND(123.45454,3) --123.45500 +select ROUND(123.45454,3,1) --123.45400 + +CONVERT()与CAST()函数: + +```sql +--1.保留小数 +convert(decimal(13,2),12.45454) +cast(12.45454 as decimal(13,2)) +--2.强制转换类型 +``` + + + +#### SQL中常用的时间函数 + +```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 +``` + +**时间格式控制字符串:** + +| 名称 | 日期单位 | 缩写 | +| ------------ | ----------- | --------- | +| 年 | year | yyyy 或yy | +| 季度 | quarter | qq,q | +| 月 | month | mm,m | +| 一年中第几天 | dayofyear | dy,y | +| 日 | day | dd,d | +| 一年中第几周 | week | wk,ww | +| 星期 | weekday | dw | +| 小时 | Hour | hh | +| 分钟 | minute | mi,n | +| 秒 | second | ss,s | +| 毫秒 | millisecond | ms | + +SQL语句执行顺序: + +```sql +(7) SELECT +(8) DISTINCT +(1) FROM +(3) JOIN +(2) ON +(4) WHERE +(5) GROUP BY +(6) HAVING +(9) ORDER BY +(10) LIMIT +``` + + + -- Gitee