From 5e958174a7b8006491c0d51953ca5496fba97184 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=B6=9B?= <1986456126@qq.com> Date: Tue, 20 Sep 2022 14:22:45 +0000 Subject: [PATCH 1/9] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20?= =?UTF-8?q?=E6=9D=8E=E6=B6=9B=EF=BC=88=E7=AC=AC=E4=BA=8C=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.keep" | 0 .../DBTEST.sql" | 225 ------------------ ...0\344\275\234\344\270\232\357\274\211.sql" | 53 ----- .../\347\254\224\350\256\260.txt" | 141 ----------- 4 files changed, 419 deletions(-) delete mode 100644 "\346\235\216\346\266\233\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/.keep" delete mode 100644 "\346\235\216\346\266\233\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/DBTEST.sql" delete mode 100644 "\346\235\216\346\266\233\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/SQLQuery1\357\274\210\344\275\234\344\270\232\357\274\211.sql" delete mode 100644 "\346\235\216\346\266\233\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/\347\254\224\350\256\260.txt" diff --git "a/\346\235\216\346\266\233\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/.keep" "b/\346\235\216\346\266\233\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/.keep" deleted file mode 100644 index e69de29..0000000 diff --git "a/\346\235\216\346\266\233\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/DBTEST.sql" "b/\346\235\216\346\266\233\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/DBTEST.sql" deleted file mode 100644 index afff882..0000000 --- "a/\346\235\216\346\266\233\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/DBTEST.sql" +++ /dev/null @@ -1,225 +0,0 @@ -if exists(select * from sys.databases where name='DBTEST') -drop database DBTEST1; -go - -create database DBTEST1; -go - -use DBTEST1; -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), - --datetime和smalldatetime都可以表示时间类型,getdate()用于获取系统当前时间 - PeopleAddTime smalldatetime default(getdate()) -); -go - - - -------------------------------插入数据部分------------------------------ ---部门表插入数据 -insert into Department values('行政部','公司主管行政工作的部门'); -go - ---一次性插入多条数据 -insert into Department(DepartmentName,DepartmentRemark) -select '市场部','吹牛的部门' union -select '产品部','天马星空的部门' union -select '总经办','都是领导的部门' ; -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. 显示所有的员工信息,按照名字的长度进行倒序排列 ---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 diff --git "a/\346\235\216\346\266\233\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/SQLQuery1\357\274\210\344\275\234\344\270\232\357\274\211.sql" "b/\346\235\216\346\266\233\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/SQLQuery1\357\274\210\344\275\234\344\270\232\357\274\211.sql" deleted file mode 100644 index 588e098..0000000 --- "a/\346\235\216\346\266\233\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/SQLQuery1\357\274\210\344\275\234\344\270\232\357\274\211.sql" +++ /dev/null @@ -1,53 +0,0 @@ ---1. 查询出武汉地区所有的员工信息,要求显示部门名称以及员工的详细资料 -select * from People -inner join Department on People.DepartmentId = Department.DepartmentId -where PeopleAddress = '武汉'; - ---2. 查询出武汉地区所有的员工信息,要求显示部门名称,职级名称以及员工的详细资料 -select * from People -inner join Department on People.DepartmentId = Department.DepartmentId -inner join Rank on People.RankId = Rank.RankId -where PeopleAddress = '武汉'; - ---3. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资。 -select Department.DepartmentId,Department.DepartmentName,count(People.PeopleId) 员工人数,sum(PeopleSalary) -总工资,avg(PeopleSalary) 平均工资,max(PeopleSalary) 最高工资,min(PeopleSalary) 最低工资 from People -inner join Department on Department.DepartmentId =People.DepartmentId -group by Department.DepartmentId,Department.DepartmentName; - ---4. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资,平均工资在10000 以下的不参与统计,并且根据平均工资降序排列。 -select Department.DepartmentId,Department.DepartmentName,count(People.PeopleId) 员工人数,sum(PeopleSalary) -总工资,avg(PeopleSalary) 平均工资,max(PeopleSalary) 最高工资,min(PeopleSalary) 最低工资 from People -inner join Department on Department.DepartmentId =People.DepartmentId -group by Department.DepartmentId,Department.DepartmentName -having avg(PeopleSalary) >=10000 -order by avg(PeopleSalary) desc; - ---5. 根据部门名称,然后根据职位名称,分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资 -select Department.DepartmentId,Rank.RankId,count(People.PeopleId) 员工人数,sum(PeopleSalary) -总工资,convert(decimal(15,2),avg(PeopleSalary)) 平均工资,max(PeopleSalary) 最高工资,min(PeopleSalary) 最低工资 from People -inner join Rank on People.RankId = Rank.RankId -inner join Department on Department.DepartmentId = People.DepartmentId -group by Department.DepartmentId,Rank.RankId,Department.DepartmentName,Rank.RankName; - ---6.查询出巨蟹 6.22--7.22 的员工信息 -select * from People -where month(People.PeopleBirth) = 6 and day(People.PeopleBirth) >= 22 or month(People.PeopleBirth) = 7 and day(People.PeopleBirth) <=22 - ---7.查询所有员工信息,添加一列显示属相(鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪) -select *, -case year(PeopleBirth)%12 - when 4 then '鼠' - when 5 then '牛' - when 6 then '虎' - when 7 then '兔' - when 8 then '龙' - when 9 then '蛇' - when 10 then '马' - when 11 then '羊' - when 0 then '猴' - when 1 then '鸡' - when 2 then '狗' - when 3 then '猪' -end 生肖 -from People; \ No newline at end of file diff --git "a/\346\235\216\346\266\233\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/\347\254\224\350\256\260.txt" "b/\346\235\216\346\266\233\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/\347\254\224\350\256\260.txt" deleted file mode 100644 index 623da83..0000000 --- "a/\346\235\216\346\266\233\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/\347\254\224\350\256\260.txt" +++ /dev/null @@ -1,141 +0,0 @@ -# SQL SERVER - -## 1銆佸缓搴撳缓琛ㄥ嚱鏁 - -### 鏂板缓鏁版嵁搴 - -create database '鏁版嵁搴撳悕'; - -### 鍒犻櫎鏁版嵁搴 - -drop database if exists '鏁版嵁搴撳悕'; - -### 浣跨敤鏁版嵁搴 - -use 鏁版嵁搴撳悕; - -### 鏂板缓琛 - -create table '琛ㄥ悕' ( - - row_name 鏁版嵁绫诲瀷(闀垮害,灏忔暟鐐) unique primary key auto_increment comment '涓婚敭' - - row_name 鏁版嵁绫诲瀷(闀垮害,灏忔暟鐐) default 榛樿鍊 not null comment ''; - - foreign key(`row_name`) references `table_name`(`row_name`) - -### 鍒犻櫎琛 - -drop database if exists '琛ㄥ悕'; - -## 2銆佸鍒犳敼鏌 - -### 2.1銆佸熀鏈鍙 - -#### 鏌ヨ璁板綍 - -```sql -select * from `table_name` -``` - -#### 鍒犻櫎璁板綍 - -```sql -delete from `table_name`; -delete from `table_name` where `row_name = values; -``` - -#### 鏂板璁板綍 - -```sql -insert into `table_name` (`row_name1`,`row_name2`,...) values (value1,value2,...),(value1,value2,...); -``` - -### 2.2銆佹潯浠舵煡璇 - -#### 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涓瓧绗︺ -[]锛氫唬琛ㄥ尮閰嶈寖鍥村唴 -[^]锛氫唬琛ㄥ尮閰嶄笉鍦ㄨ寖鍥村唴 -``` - - - -## 3銆丼QL鍑芥暟 - -### case锛 - -CASE璇彞鏈変袱绉嶅舰寮忥細绗竴绉嶈瘎浼颁竴涓垨澶氫釜鏉′欢锛屽苟杩斿洖绗竴涓鍚堟潯浠剁殑缁撴灉銆 濡傛灉娌℃湁鏉′欢鏄鍚堢殑锛屽垯杩斿洖ELSE瀛愬彞閮ㄥ垎鐨勭粨鏋滐紝濡傛灉娌℃湁ELSE閮ㄥ垎锛屽垯杩斿洖NULL锛 - -```sql -CASE - WHEN condition1 THEN result1 - WHEN condition2 THEN result2 - WHEN conditionN THEN resultN - ELSE result -END; -``` - -绗簩绉岰ASE鍙ユ硶杩斿洖绗竴涓獀alue = compare_value姣旇緝缁撴灉涓虹湡鐨勭粨鏋溿 濡傛灉娌℃湁姣旇緝缁撴灉绗﹀悎锛屽垯杩斿洖ELSE鍚庣殑缁撴灉锛屽鏋滄病鏈塃LSE閮ㄥ垎锛屽垯杩斿洖NULL锛 - -```sql -CASE compare_value - WHEN condition1 THEN result1 - WHEN condition2 THEN result2 - WHEN conditionN THEN resultN - ELSE result -END; -``` - -### 鑱氬悎鍑芥暟 - -SQL SERVER涓仛鍚堝嚱鏁颁富瑕佹湁锛 - - count:姹傛暟閲 - max:姹傛渶澶у - min:姹傛渶灏忓 - sum:姹傚拰 - avg:姹傚钩鍧囧 - - - - - -## SQL鎵ц椤哄簭 - -```sql -(7) SELECT -(8) DISTINCT -(1) FROM -(3) JOIN -(2) ON -(4) WHERE -(5) GROUP BY -(6) HAVING -(9) ORDER BY -(10) LIMIT -``` \ No newline at end of file -- Gitee From 41dc561d7423ed4e43aa7eb92d42ced0ed5fe07f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=B6=9B?= <1986456126@qq.com> Date: Tue, 20 Sep 2022 14:23:47 +0000 Subject: [PATCH 2/9] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20?= =?UTF-8?q?=E6=9D=8E=E6=B6=9B/SQLQuery1.sql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\346\235\216\346\266\233/SQLQuery1.sql" | 40 ------------------------ 1 file changed, 40 deletions(-) delete mode 100644 "\346\235\216\346\266\233/SQLQuery1.sql" diff --git "a/\346\235\216\346\266\233/SQLQuery1.sql" "b/\346\235\216\346\266\233/SQLQuery1.sql" deleted file mode 100644 index ba261d7..0000000 --- "a/\346\235\216\346\266\233/SQLQuery1.sql" +++ /dev/null @@ -1,40 +0,0 @@ -CREATE DATABASE DBTEST; -USE dbtest -if exists (select * from sys.databases where name='sectionInfo') -drop table sectionInfo - -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) unique not null check(len(userName)>=4), - userSex varchar(2) not null check(userSex ='男' or userSex ='女'), - userAge int not null check(0 Date: Tue, 20 Sep 2022 14:23:53 +0000 Subject: [PATCH 3/9] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20?= =?UTF-8?q?=E6=9D=8E=E6=B6=9B/=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" | 25 ------------------- 1 file changed, 25 deletions(-) delete mode 100644 "\346\235\216\346\266\233/\347\254\224\350\256\260.txt" diff --git "a/\346\235\216\346\266\233/\347\254\224\350\256\260.txt" "b/\346\235\216\346\266\233/\347\254\224\350\256\260.txt" deleted file mode 100644 index 29bc7f7..0000000 --- "a/\346\235\216\346\266\233/\347\254\224\350\256\260.txt" +++ /dev/null @@ -1,25 +0,0 @@ -鍒涘缓鏁版嵁搴擄細 -create database 鏁版嵁搴撳悕 -database锛堜唬琛ㄦ暟鎹簱锛 - -if exists (select * from sys.databases where name='鏁版嵁搴撳悕') -drop锛堝垹闄わ級 database 鏁版嵁搴撳悕 - - -浣跨敤鏁版嵁搴 - use dbtest - -鍒涘缓琛 -create table 琛ㄥ悕 - -1.涓婚敭锛歱rimary key -2.鑷锛歩dentity(寮濮嬪,鑷鐨勫) -3.榛樿:default() -4.鍞竴锛歶nique -5.澶栭敭:references 涓昏〃鍚(鍒楀悕) - foreign key(瑕佸紩鐢ㄧ殑瀛楁) -6.鏂板鍒楋細alter table 琛ㄥ悕 add 鏂板鍒楀悕 绫诲瀷(int...) -7.闈炵┖:not null -8.妫鏌(闄愬埗鏉′欢):check (鍒楀悕 <,=,> '鏉′欢') - -鍒犻櫎琛:drop table 琛ㄥ悕 \ No newline at end of file -- Gitee From 587fd00cd91ea6cfaaac0ea9fd449b02023ed94c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=B6=9B?= <1986456126@qq.com> Date: Tue, 20 Sep 2022 14:24:54 +0000 Subject: [PATCH 4/9] =?UTF-8?q?=E6=96=B0=E5=BB=BA=202022-09-15=EF=BC=88?= =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C=E4=B8=9A=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\346\235\216\346\266\233/2022-09-15\357\274\210\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232\357\274\211/.keep" diff --git "a/\346\235\216\346\266\233/2022-09-15\357\274\210\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232\357\274\211/.keep" "b/\346\235\216\346\266\233/2022-09-15\357\274\210\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232\357\274\211/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 2efecc46874f4475a8dc4090a549f0d6c8c8fd34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=B6=9B?= <1986456126@qq.com> Date: Tue, 20 Sep 2022 14:27:48 +0000 Subject: [PATCH 5/9] =?UTF-8?q?2022-09-15=EF=BC=88=E7=AC=AC=E4=B8=80?= =?UTF-8?q?=E6=AC=A1=E4=BD=9C=E4=B8=9A=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 鏉庢稕 <1986456126@qq.com> --- .../SQLQuery1.sql" | 40 +++++++++++++++++++ .../\347\254\224\350\256\260.txt" | 25 ++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 "\346\235\216\346\266\233/2022-09-15\357\274\210\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232\357\274\211/SQLQuery1.sql" create mode 100644 "\346\235\216\346\266\233/2022-09-15\357\274\210\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232\357\274\211/\347\254\224\350\256\260.txt" diff --git "a/\346\235\216\346\266\233/2022-09-15\357\274\210\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232\357\274\211/SQLQuery1.sql" "b/\346\235\216\346\266\233/2022-09-15\357\274\210\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232\357\274\211/SQLQuery1.sql" new file mode 100644 index 0000000..ba261d7 --- /dev/null +++ "b/\346\235\216\346\266\233/2022-09-15\357\274\210\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232\357\274\211/SQLQuery1.sql" @@ -0,0 +1,40 @@ +CREATE DATABASE DBTEST; +USE dbtest +if exists (select * from sys.databases where name='sectionInfo') +drop table sectionInfo + +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) unique not null check(len(userName)>=4), + userSex varchar(2) not null check(userSex ='男' or userSex ='女'), + userAge int not null check(0 '鏉′欢') + +鍒犻櫎琛:drop table 琛ㄥ悕 \ No newline at end of file -- Gitee From fe48e522cb0ae15b6f273f62c398864e99c7cf60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=B6=9B?= <1986456126@qq.com> Date: Tue, 20 Sep 2022 14:28:18 +0000 Subject: [PATCH 6/9] =?UTF-8?q?=E6=96=B0=E5=BB=BA=202022-09-16=EF=BC=88?= =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E4=BD=9C=E4=B8=9A=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\346\235\216\346\266\233/2022-09-16\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/.keep" diff --git "a/\346\235\216\346\266\233/2022-09-16\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/.keep" "b/\346\235\216\346\266\233/2022-09-16\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From d698119598a2ecbee89fa1c322467df5cad24bac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=B6=9B?= <1986456126@qq.com> Date: Tue, 20 Sep 2022 14:28:52 +0000 Subject: [PATCH 7/9] =?UTF-8?q?2022-09-16=EF=BC=88=E7=AC=AC=E4=BA=8C?= =?UTF-8?q?=E6=AC=A1=E4=BD=9C=E4=B8=9A=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 鏉庢稕 <1986456126@qq.com> --- .../DBTEST.sql" | 225 ++++++++++++++++++ ...0\344\275\234\344\270\232\357\274\211.sql" | 53 +++++ .../\347\254\224\350\256\260.txt" | 141 +++++++++++ 3 files changed, 419 insertions(+) create mode 100644 "\346\235\216\346\266\233/2022-09-16\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/DBTEST.sql" create mode 100644 "\346\235\216\346\266\233/2022-09-16\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/SQLQuery1\357\274\210\344\275\234\344\270\232\357\274\211.sql" create mode 100644 "\346\235\216\346\266\233/2022-09-16\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/\347\254\224\350\256\260.txt" diff --git "a/\346\235\216\346\266\233/2022-09-16\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/DBTEST.sql" "b/\346\235\216\346\266\233/2022-09-16\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/DBTEST.sql" new file mode 100644 index 0000000..afff882 --- /dev/null +++ "b/\346\235\216\346\266\233/2022-09-16\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/DBTEST.sql" @@ -0,0 +1,225 @@ +if exists(select * from sys.databases where name='DBTEST') +drop database DBTEST1; +go + +create database DBTEST1; +go + +use DBTEST1; +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), + --datetime和smalldatetime都可以表示时间类型,getdate()用于获取系统当前时间 + PeopleAddTime smalldatetime default(getdate()) +); +go + + + +------------------------------插入数据部分------------------------------ +--部门表插入数据 +insert into Department values('行政部','公司主管行政工作的部门'); +go + +--一次性插入多条数据 +insert into Department(DepartmentName,DepartmentRemark) +select '市场部','吹牛的部门' union +select '产品部','天马星空的部门' union +select '总经办','都是领导的部门' ; +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. 显示所有的员工信息,按照名字的长度进行倒序排列 +--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 diff --git "a/\346\235\216\346\266\233/2022-09-16\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/SQLQuery1\357\274\210\344\275\234\344\270\232\357\274\211.sql" "b/\346\235\216\346\266\233/2022-09-16\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/SQLQuery1\357\274\210\344\275\234\344\270\232\357\274\211.sql" new file mode 100644 index 0000000..588e098 --- /dev/null +++ "b/\346\235\216\346\266\233/2022-09-16\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/SQLQuery1\357\274\210\344\275\234\344\270\232\357\274\211.sql" @@ -0,0 +1,53 @@ +--1. 查询出武汉地区所有的员工信息,要求显示部门名称以及员工的详细资料 +select * from People +inner join Department on People.DepartmentId = Department.DepartmentId +where PeopleAddress = '武汉'; + +--2. 查询出武汉地区所有的员工信息,要求显示部门名称,职级名称以及员工的详细资料 +select * from People +inner join Department on People.DepartmentId = Department.DepartmentId +inner join Rank on People.RankId = Rank.RankId +where PeopleAddress = '武汉'; + +--3. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资。 +select Department.DepartmentId,Department.DepartmentName,count(People.PeopleId) 员工人数,sum(PeopleSalary) +总工资,avg(PeopleSalary) 平均工资,max(PeopleSalary) 最高工资,min(PeopleSalary) 最低工资 from People +inner join Department on Department.DepartmentId =People.DepartmentId +group by Department.DepartmentId,Department.DepartmentName; + +--4. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资,平均工资在10000 以下的不参与统计,并且根据平均工资降序排列。 +select Department.DepartmentId,Department.DepartmentName,count(People.PeopleId) 员工人数,sum(PeopleSalary) +总工资,avg(PeopleSalary) 平均工资,max(PeopleSalary) 最高工资,min(PeopleSalary) 最低工资 from People +inner join Department on Department.DepartmentId =People.DepartmentId +group by Department.DepartmentId,Department.DepartmentName +having avg(PeopleSalary) >=10000 +order by avg(PeopleSalary) desc; + +--5. 根据部门名称,然后根据职位名称,分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资 +select Department.DepartmentId,Rank.RankId,count(People.PeopleId) 员工人数,sum(PeopleSalary) +总工资,convert(decimal(15,2),avg(PeopleSalary)) 平均工资,max(PeopleSalary) 最高工资,min(PeopleSalary) 最低工资 from People +inner join Rank on People.RankId = Rank.RankId +inner join Department on Department.DepartmentId = People.DepartmentId +group by Department.DepartmentId,Rank.RankId,Department.DepartmentName,Rank.RankName; + +--6.查询出巨蟹 6.22--7.22 的员工信息 +select * from People +where month(People.PeopleBirth) = 6 and day(People.PeopleBirth) >= 22 or month(People.PeopleBirth) = 7 and day(People.PeopleBirth) <=22 + +--7.查询所有员工信息,添加一列显示属相(鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪) +select *, +case year(PeopleBirth)%12 + when 4 then '鼠' + when 5 then '牛' + when 6 then '虎' + when 7 then '兔' + when 8 then '龙' + when 9 then '蛇' + when 10 then '马' + when 11 then '羊' + when 0 then '猴' + when 1 then '鸡' + when 2 then '狗' + when 3 then '猪' +end 生肖 +from People; \ No newline at end of file diff --git "a/\346\235\216\346\266\233/2022-09-16\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/\347\254\224\350\256\260.txt" "b/\346\235\216\346\266\233/2022-09-16\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/\347\254\224\350\256\260.txt" new file mode 100644 index 0000000..623da83 --- /dev/null +++ "b/\346\235\216\346\266\233/2022-09-16\357\274\210\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\211/\347\254\224\350\256\260.txt" @@ -0,0 +1,141 @@ +# SQL SERVER + +## 1銆佸缓搴撳缓琛ㄥ嚱鏁 + +### 鏂板缓鏁版嵁搴 + +create database '鏁版嵁搴撳悕'; + +### 鍒犻櫎鏁版嵁搴 + +drop database if exists '鏁版嵁搴撳悕'; + +### 浣跨敤鏁版嵁搴 + +use 鏁版嵁搴撳悕; + +### 鏂板缓琛 + +create table '琛ㄥ悕' ( + + row_name 鏁版嵁绫诲瀷(闀垮害,灏忔暟鐐) unique primary key auto_increment comment '涓婚敭' + + row_name 鏁版嵁绫诲瀷(闀垮害,灏忔暟鐐) default 榛樿鍊 not null comment ''; + + foreign key(`row_name`) references `table_name`(`row_name`) + +### 鍒犻櫎琛 + +drop database if exists '琛ㄥ悕'; + +## 2銆佸鍒犳敼鏌 + +### 2.1銆佸熀鏈鍙 + +#### 鏌ヨ璁板綍 + +```sql +select * from `table_name` +``` + +#### 鍒犻櫎璁板綍 + +```sql +delete from `table_name`; +delete from `table_name` where `row_name = values; +``` + +#### 鏂板璁板綍 + +```sql +insert into `table_name` (`row_name1`,`row_name2`,...) values (value1,value2,...),(value1,value2,...); +``` + +### 2.2銆佹潯浠舵煡璇 + +#### 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涓瓧绗︺ +[]锛氫唬琛ㄥ尮閰嶈寖鍥村唴 +[^]锛氫唬琛ㄥ尮閰嶄笉鍦ㄨ寖鍥村唴 +``` + + + +## 3銆丼QL鍑芥暟 + +### case锛 + +CASE璇彞鏈変袱绉嶅舰寮忥細绗竴绉嶈瘎浼颁竴涓垨澶氫釜鏉′欢锛屽苟杩斿洖绗竴涓鍚堟潯浠剁殑缁撴灉銆 濡傛灉娌℃湁鏉′欢鏄鍚堢殑锛屽垯杩斿洖ELSE瀛愬彞閮ㄥ垎鐨勭粨鏋滐紝濡傛灉娌℃湁ELSE閮ㄥ垎锛屽垯杩斿洖NULL锛 + +```sql +CASE + WHEN condition1 THEN result1 + WHEN condition2 THEN result2 + WHEN conditionN THEN resultN + ELSE result +END; +``` + +绗簩绉岰ASE鍙ユ硶杩斿洖绗竴涓獀alue = compare_value姣旇緝缁撴灉涓虹湡鐨勭粨鏋溿 濡傛灉娌℃湁姣旇緝缁撴灉绗﹀悎锛屽垯杩斿洖ELSE鍚庣殑缁撴灉锛屽鏋滄病鏈塃LSE閮ㄥ垎锛屽垯杩斿洖NULL锛 + +```sql +CASE compare_value + WHEN condition1 THEN result1 + WHEN condition2 THEN result2 + WHEN conditionN THEN resultN + ELSE result +END; +``` + +### 鑱氬悎鍑芥暟 + +SQL SERVER涓仛鍚堝嚱鏁颁富瑕佹湁锛 + + count:姹傛暟閲 + max:姹傛渶澶у + min:姹傛渶灏忓 + sum:姹傚拰 + avg:姹傚钩鍧囧 + + + + + +## SQL鎵ц椤哄簭 + +```sql +(7) SELECT +(8) DISTINCT +(1) FROM +(3) JOIN +(2) ON +(4) WHERE +(5) GROUP BY +(6) HAVING +(9) ORDER BY +(10) LIMIT +``` \ No newline at end of file -- Gitee From c01feb889eed74f27f4642cbb21c3298128d1585 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=B6=9B?= <1986456126@qq.com> Date: Tue, 20 Sep 2022 14:29:22 +0000 Subject: [PATCH 8/9] =?UTF-8?q?=E6=96=B0=E5=BB=BA=202022-09-20=EF=BC=88?= =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=AC=A1=E4=BD=9C=E4=B8=9A=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\346\235\216\346\266\233/2022-09-20\357\274\210\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\357\274\211/.keep" diff --git "a/\346\235\216\346\266\233/2022-09-20\357\274\210\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\357\274\211/.keep" "b/\346\235\216\346\266\233/2022-09-20\357\274\210\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\357\274\211/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 2d6156d91dd0b4b15d9eb3b402a42a57160f8f59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=B6=9B?= <1986456126@qq.com> Date: Tue, 20 Sep 2022 14:30:25 +0000 Subject: [PATCH 9/9] =?UTF-8?q?2022-09-20=EF=BC=88=E7=AC=AC=E4=B8=89?= =?UTF-8?q?=E6=AC=A1=E4=BD=9C=E4=B8=9A=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 鏉庢稕 <1986456126@qq.com> --- .../09-20.sql" | 49 ++++++++ .../BankTest.sql" | 107 ++++++++++++++++++ .../\347\254\224\350\256\260.txt" | 10 ++ 3 files changed, 166 insertions(+) create mode 100644 "\346\235\216\346\266\233/2022-09-20\357\274\210\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\357\274\211/09-20.sql" create mode 100644 "\346\235\216\346\266\233/2022-09-20\357\274\210\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\357\274\211/BankTest.sql" create mode 100644 "\346\235\216\346\266\233/2022-09-20\357\274\210\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\357\274\211/\347\254\224\350\256\260.txt" diff --git "a/\346\235\216\346\266\233/2022-09-20\357\274\210\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\357\274\211/09-20.sql" "b/\346\235\216\346\266\233/2022-09-20\357\274\210\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\357\274\211/09-20.sql" new file mode 100644 index 0000000..5e2fac3 --- /dev/null +++ "b/\346\235\216\346\266\233/2022-09-20\357\274\210\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\357\274\211/09-20.sql" @@ -0,0 +1,49 @@ +--1. 关羽的银行卡号为"6225547858741263",查询出余额比关羽多的银行卡信息,显示卡号,身份证,姓名,余额。 + +select CardNo,AccountCode,RealName,CardMoney from BankCard b +inner join AccountInfo a on a.AccountId=b.AccountId +where CardMoney>(select CardMoney from BankCard where CardNo='6225547858741263'); + + +--2. 从所有账户信息中查询出余额最高的交易明细(存钱取钱信息)。 + +select * from CardExchange +where CardNo=(select CardNo from BankCard where CardMoney=(select max(CardMoney)余额 from BankCard)); + + +--3. 查询有取款记录的银行卡及账户信息,显示卡号,身份证,姓名,余额。 + +select CardNo,AccountCode,RealName,CardMoney from BankCard b +inner join AccountInfo a on a.AccountId=b.AccountId +where CardNo = (select CardNoOut from CardTransfer ); + +--4. 查询出没有存款记录的银行卡及账户信息,显示卡号,身份证,姓名,余额。 + +select * from AccountInfo +where AccountId in (select AccountId from BankCard where CardNo in(select CardNo from CardExchange where MoneyInBank =0)); + + +--5. 关羽的银行卡号为"6225547858741263",查询当天是否有收到转账。 + +if exists(select * from CardTransfer where CardNoIn ='6225547858741263') +print '有收到转账' +else +print '没有收到转账'; + + +--6. 查询出交易次数(存款取款操作)最多的银行卡账户信息,显示:卡号,身份证,姓名,余额,交易次数。 + +select top 1 BankCard.CardNo 卡号,AccountCode 身份证,RealName 姓名,CardMoney 余额, +exchangeCount 交易次数 from BankCard +inner join AccountInfo on BankCard.AccountId = AccountInfo.AccountId +inner join +(select CardNo,COUNT(*) exchangeCount from CardExchange group by CardNo) CarcExchageTemp +on BankCard.CardNo = CarcExchageTemp.CardNo +order by exchangeCount desc; + + +--7. 查询出没有转账交易记录的银行卡账户信息,显示卡号,身份证,姓名,余额。 +select CardNo 卡号,AccountCode 身份证,RealName 姓名,CardMoney 余额 from BankCard b +inner join AccountInfo a on b.AccountId=a.AccountId +where b.CardNo not in (select CardNoIn from CardTransfer) and b.CardNo not in (select CardNoOut from CardTransfer); + diff --git "a/\346\235\216\346\266\233/2022-09-20\357\274\210\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\357\274\211/BankTest.sql" "b/\346\235\216\346\266\233/2022-09-20\357\274\210\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\357\274\211/BankTest.sql" new file mode 100644 index 0000000..e9dfd7a --- /dev/null +++ "b/\346\235\216\346\266\233/2022-09-20\357\274\210\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\357\274\211/BankTest.sql" @@ -0,0 +1,107 @@ +create database BankTest; +go +use banktest; +go +--账户信息表:存储个人信息 +create table AccountInfo +( + AccountId int primary key identity(1,1), --账户编号 + AccountCode varchar(20) not null, --身份证号码 + AccountPhone varchar(20) not null, --电话号码 + RealName varchar(20) not null, --真实姓名 + OpenTime smalldatetime not null, --开户时间 +) +--银行卡表:存储银行卡信息 +create table BankCard +( + CardNo varchar(30) primary key, --银行卡卡号 + AccountId int not null, --账户编号(与账户信息表形成主外键关系) + CardPwd varchar(30) not null, --银行卡密码 + CardMoney money not null, --银行卡余额 + CardState int not null,--1:正常,2:挂失,3:冻结,4:注销 + CardTime smalldatetime default(getdate()) --开卡时间 +) +--交易信息表(存储存钱和取钱的记录) +create table CardExchange +( + ExchangeId int primary key identity(1,1), --交易自动编号 + CardNo varchar(30) not null, --银行卡号(与银行卡表形成主外键关系) + MoneyInBank money not null, --存钱金额 + MoneyOutBank money not null, --取钱金额 + ExchangeTime smalldatetime not null, --交易时间 +) +--转账信息表(存储转账信息记录) +create table CardTransfer +( + TransferId int primary key identity(1,1),--转账自动编号 + CardNoOut varchar(30) not null, --转出银行卡号(与银行卡表形成主外键关系) + CardNoIn varchar(30) not null, --转入银行卡号(与银行卡表形成主外键关系) + TransferMoney money not null,--交易金额 + TransferTime smalldatetime not null, --交易时间 +) +--状态信息变化表(存储银行卡状态变化的记录,状态有1:正常,2:挂失,3:冻结,4:注销) +create table CardStateChange +( + StateId int primary key identity(1,1),--状态信息自动编号 + CardNo varchar(30) not null, --银行卡号(与银行卡表形成主外键关系) + OldState int not null, --银行卡原始状态 + NewState int not null, --银行卡新状态 + StateWhy varchar(200) not null, --状态变化原因 + StateTime smalldatetime not null, --记录产生时间 +) + + + +--为刘备,关羽,张飞三个人进行开户开卡的操作 +--刘备身份证:420107198905064135 +--关羽身份证:420107199507104133 +--张飞身份证:420107199602034138 +insert into AccountInfo(AccountCode,AccountPhone,RealName,OpenTime) +values('420107198905064135','13554785425','刘备',GETDATE()); +insert into BankCard(CardNo,AccountId,CardPwd,CardMoney,CardState) +values('6225125478544587',1,'123456',0,1); + +insert into AccountInfo(AccountCode,AccountPhone,RealName,OpenTime) +values('420107199507104133','13454788854','关羽',GETDATE()); +insert into BankCard(CardNo,AccountId,CardPwd,CardMoney,CardState) +values('6225547858741263',2,'123456',0,1); + +insert into AccountInfo(AccountCode,AccountPhone,RealName,OpenTime) +values('420107199602034138','13456896321','张飞',GETDATE()); +insert into BankCard(CardNo,AccountId,CardPwd,CardMoney,CardState) +values('6225547854125656',3,'123456',0,1); + +select * from AccountInfo +select * from BankCard + + + +--进行存钱操作,刘备存钱2000元,关羽存钱:8000元,张飞存钱:500000元 +select * from AccountInfo +update BankCard set CardMoney = CardMoney + 2000 where CardNo = '6225125478544587' +insert into CardExchange(CardNo,MoneyInBank,MoneyOutBank,ExchangeTime) +values('6225125478544587',2000,0,GETDATE()) + +update BankCard set CardMoney = CardMoney + 8000 where CardNo = '6225547858741263' + +insert into CardExchange(CardNo,MoneyInBank,MoneyOutBank,ExchangeTime) +values('6225547858741263',8000,0,GETDATE()) + +update BankCard set CardMoney = CardMoney + 500000 where CardNo = '6225547854125656' +insert into CardExchange(CardNo,MoneyInBank,MoneyOutBank,ExchangeTime) +values('6225547854125656',500000,0,GETDATE()) + +--转账:刘备给张飞转账1000元 +update BankCard set CardMoney = CardMoney -1000 where CardNo = '6225125478544587' +update BankCard set CardMoney = CardMoney + 1000 where CardNo = '6225547854125656' + +insert into CardTransfer(CardNoOut,CardNoIn,TransferMoney,TransferTime) +values('6225125478544587','6225547854125656',1000,GETDATE()) + + + +select * from AccountInfo --个人信息 +select * from BankCard --银行卡信息 +select * from CardStateChange --银行卡状态更改表 +select * from CardTransfer --转账表 +select * from CardExchange --交易记录 \ No newline at end of file diff --git "a/\346\235\216\346\266\233/2022-09-20\357\274\210\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\357\274\211/\347\254\224\350\256\260.txt" "b/\346\235\216\346\266\233/2022-09-20\357\274\210\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\357\274\211/\347\254\224\350\256\260.txt" new file mode 100644 index 0000000..b5d1b81 --- /dev/null +++ "b/\346\235\216\346\266\233/2022-09-20\357\274\210\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\357\274\211/\347\254\224\350\256\260.txt" @@ -0,0 +1,10 @@ +- ROW_NUMBER()鍑芥暟鐢熸垚鐨勬帓搴忔牴鎹帓搴忓瓙鍙ョ粰鍑**閫掑杩炵画鐨勫簭鍙** +- RANK()鍑芥暟鐢熸垚鐨勬帓搴忔牴鎹帓搴忓瓙鍙ョ粰鍑**閫掑鐨勫簭鍙凤紝浣嗘槸瀛樺湪骞跺垪骞朵笖璺崇┖** +- DENSE_RANK() 鍑芥暟鐢熸垚鐨勬帓搴忔牴鎹帓搴忓瓙鍙ョ粰鍑**閫掑鐨勫簭鍙凤紝浣嗘槸瀛樺湪骞跺垪涓嶈烦绌** + +```sql +ROW_NUMBER() over(order by 鍒楀悕 desc)銆 +-- select StuName 濮撳悕,ROW_NUMBER() over(order by score desc) +rank() over(order by 鍒楀悕 desc) +dense_rank() over(order by 鍒楀悕 desc) +``` \ No newline at end of file -- Gitee