From c79556822577f078ed3f93f16214c331c49da98d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80=E4=BB=81?= <1579614208@qq.com> Date: Sun, 18 Sep 2022 15:54:00 +0000 Subject: [PATCH 01/12] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\344\275\234\344\270\232/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "40\345\274\240\350\200\200\344\273\201/\344\275\234\344\270\232/.keep" diff --git "a/40\345\274\240\350\200\200\344\273\201/\344\275\234\344\270\232/.keep" "b/40\345\274\240\350\200\200\344\273\201/\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From de7461aad7c46c393af62223e4a6ba417449e3dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80=E4=BB=81?= <1579614208@qq.com> Date: Sun, 18 Sep 2022 15:54:41 +0000 Subject: [PATCH 02/12] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2040?= =?UTF-8?q?=E5=BC=A0=E8=80=80=E4=BB=81/=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\344\275\234\344\270\232/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 "40\345\274\240\350\200\200\344\273\201/\344\275\234\344\270\232/.keep" diff --git "a/40\345\274\240\350\200\200\344\273\201/\344\275\234\344\270\232/.keep" "b/40\345\274\240\350\200\200\344\273\201/\344\275\234\344\270\232/.keep" deleted file mode 100644 index e69de29..0000000 -- Gitee From ac1fc9f4e8924103d2781120eb5a495e5c19fa03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80=E4=BB=81?= <1579614208@qq.com> Date: Sun, 18 Sep 2022 15:57:44 +0000 Subject: [PATCH 03/12] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2040?= =?UTF-8?q?=E5=BC=A0=E8=80=80=E4=BB=81/0915=E4=BD=9C=E4=B8=9A.sql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0915\344\275\234\344\270\232.sql" | 48 ------------------- 1 file changed, 48 deletions(-) delete mode 100644 "40\345\274\240\350\200\200\344\273\201/0915\344\275\234\344\270\232.sql" diff --git "a/40\345\274\240\350\200\200\344\273\201/0915\344\275\234\344\270\232.sql" "b/40\345\274\240\350\200\200\344\273\201/0915\344\275\234\344\270\232.sql" deleted file mode 100644 index d04ca45..0000000 --- "a/40\345\274\240\350\200\200\344\273\201/0915\344\275\234\344\270\232.sql" +++ /dev/null @@ -1,48 +0,0 @@ ---创建数据库 DBTEST -create database DBTEST; ---使用数据库 -use dbtest; ---部门信息表 -create table sectioninfo( - sectionID int primary key identity(1,1), - sectionName varchar(10) not null -); ---员工信息表 -create table userinfo( - userNo int primary key identity(1,1) not null, - userName varchar(10) not null unique check(len(userName)>4), - userSex varchar(2) not null default('男') check(userSex='男' or userSex='女'), - userAge int not null check(userAge between 1 and 100), - userAddress varchar(50) default('湖北'), - userSection INT -); ---员工考勤表 -create table workinfo( - workId int primary key identity(1,1), - userId int references userinfo(userNo) not null , - workTime datetime not null, - workDescription varchar(40) not null check(workDescription in('迟到','早退','旷工','病假','事假')) -); -Alter table workInfo add constraint FK_workInfo_ClassId foreign key(userId) references userInfo(userNo); ---增加数据 -insert into sectionInfo(sectionName) values - ('开发部'), - ('运行部'), - ('人事部'), - ('策划部'), - ('管理部'); -select * from sectionInfo; -insert into userInfo(userName,userSex,userAge,userAddress,userSection) values - ('郑文源','男',8,'泉州',1), - ('唐皓颖','男',9,'贵阳',2), - ('袁贵森','女',25,'山西',3), - ('小白','男',38,'未知',4), - ('小黑','男',15,'未知',5); -select * from userInfo; -insert into workInfo(userId,workTime,workDescription) values - (1,'2022-01-05 15:25:46','迟到'), - (2,'2022-02-15 16:44:44','早退'), - (3,'2022-03-29 12:25:46','旷工'), - (4,'2022-04-25 08:45:46','病假'), - (5,'2022-06-18 19:25:46','事假'); -select * from workInfo; \ No newline at end of file -- Gitee From 3138785c24f2a1fb125e78e53cf1e619052db776 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80=E4=BB=81?= <1579614208@qq.com> Date: Sun, 18 Sep 2022 15:57:50 +0000 Subject: [PATCH 04/12] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2040?= =?UTF-8?q?=E5=BC=A0=E8=80=80=E4=BB=81/0918=E4=BD=9C=E4=B8=9A.sql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0918\344\275\234\344\270\232.sql" | 81 ------------------- 1 file changed, 81 deletions(-) delete mode 100644 "40\345\274\240\350\200\200\344\273\201/0918\344\275\234\344\270\232.sql" diff --git "a/40\345\274\240\350\200\200\344\273\201/0918\344\275\234\344\270\232.sql" "b/40\345\274\240\350\200\200\344\273\201/0918\344\275\234\344\270\232.sql" deleted file mode 100644 index ebf1aee..0000000 --- "a/40\345\274\240\350\200\200\344\273\201/0918\344\275\234\344\270\232.sql" +++ /dev/null @@ -1,81 +0,0 @@ ---查询出巨蟹 6.22--7.22 的员工信息 -select * from People -where datename(month,PeopleBirth) = 6 and day(PeopleBirth) between 22 and 30 -or datename(month,PeopleBirth) = 7 and day(PeopleBirth) between 1 and 22; - - ---查询所有员工信息,添加一列显示属相(鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪) -select *, case -when year(PeopleBirth)%12 = 0 -then '猴' -when year(PeopleBirth)%12 = 1 -then '鸡' -when year(PeopleBirth)%12 = 2 -then '狗' -when year(PeopleBirth)%12 = 3 -then '猪' -when year(PeopleBirth)%12 = 4 -then '鼠' -when year(PeopleBirth)%12 = 5 -then '牛' -when year(PeopleBirth)%12 = 6 -then '虎' -when year(PeopleBirth)%12 = 7 -then '兔' -when year(PeopleBirth)%12 = 8 -then '龙' -when year(PeopleBirth)%12 = 9 -then '蛇' -when year(PeopleBirth)%12 = 10 -then '马' -when year(PeopleBirth)%12 = 11 -then '羊' -end '生肖' -from People - - ---1. 查询出武汉地区所有的员工信息,要求显示部门名称以及员工的详细资料 -select p.*,d.DepartmentName from People p -inner join Department d -on p.DepartmentId = d.DepartmentId -where p.PeopleAddress = '武汉'; - - ---2. 查询出武汉地区所有的员工信息,要求显示部门名称,职级名称以及员工的详细资料 -select p.*,d.DepartmentName,r.RankName from People p -inner join Department d -on p.DepartmentId = d.DepartmentId -inner join Rank r on r.RankId = p.RankId -where p.PeopleAddress = '武汉'; - - ---3. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资。 -select count(D.DepartmentName)部门员工人数,sum(p.PeopleSalary)员工工资总和,convert(decimal(10,2), -avg(p.PeopleSalary))平均工资,max(p.PeopleSalary)最高工资,min(p.PeopleSalary)最低工资 -from People p -inner join Department d -on p.DepartmentId = d.DepartmentId -group by d.DepartmentName - - ---4. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资,平均工资在10000 以下的不参与统计, ---并且根据平均工资降序排列。 -select count(D.DepartmentName)部门员工人数,sum(p.PeopleSalary)员工工资总和,convert(decimal(10,2), -avg(p.PeopleSalary))平均工资,max(p.PeopleSalary)最高工资,min(p.PeopleSalary)最低工资 -from People p -inner join Department d -on p.DepartmentId = d.DepartmentId -group by d.DepartmentName -having avg(p.PeopleSalary) > 10000 -order by 平均工资 desc - - ---5. 根据部门名称,然后根据职位名称,分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资 -select d.DepartmentName,r.RankName,count(*)员工人数, -sum(p.PeopleSalary)员工工资总和,convert(decimal(10,2), -avg(p.PeopleSalary))平均工资,max(p.PeopleSalary)最高工资,min(p.PeopleSalary)最低工资 -from People p -inner join Department d -on p.DepartmentId = d.DepartmentId -inner join Rank r on r.RankId = p.RankId -group by d.DepartmentName,r.RankName \ No newline at end of file -- Gitee From 5fe9b753ccd4f2ec7c005ff18fe4004269a8d8d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80=E4=BB=81?= <1579614208@qq.com> Date: Sun, 18 Sep 2022 15:57:57 +0000 Subject: [PATCH 05/12] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2040?= =?UTF-8?q?=E5=BC=A0=E8=80=80=E4=BB=81/=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0916\347\254\224\350\256\260.sql" | 86 ------------------- ...2\350\241\250\347\272\246\346\235\237.sql" | 78 ----------------- 2 files changed, 164 deletions(-) delete mode 100644 "40\345\274\240\350\200\200\344\273\201/\347\254\224\350\256\260/0916\347\254\224\350\256\260.sql" delete mode 100644 "40\345\274\240\350\200\200\344\273\201/\347\254\224\350\256\260/2022-09-15-\345\273\272\345\272\223\345\273\272\350\241\250\347\272\246\346\235\237.sql" diff --git "a/40\345\274\240\350\200\200\344\273\201/\347\254\224\350\256\260/0916\347\254\224\350\256\260.sql" "b/40\345\274\240\350\200\200\344\273\201/\347\254\224\350\256\260/0916\347\254\224\350\256\260.sql" deleted file mode 100644 index d39053c..0000000 --- "a/40\345\274\240\350\200\200\344\273\201/\347\254\224\350\256\260/0916\347\254\224\350\256\260.sql" +++ /dev/null @@ -1,86 +0,0 @@ ---查询公司员工所在城市(不需要重复数据) ---distinct -select distinct PeopleAddress from People - ---查询所有员工信息(根据工资排序(order by),降序排列) -select * from People order by PeopleSalary desc - - ---查询1980年以后出生的员工信息 -select * from People where year(PeopleBirth)>=1980 - - ---查询出巨蟹 6.22--7.22 的员工信息 - ---查询出和赵云在同一个城市的人 - - -select * from People where PeopleAddress=(select peopleAddress from People where PeopleName='赵云') - ---查询出电话号码开头138的员工信息,第4位可能是7,可能8 ,最后一个号码是5 -select * from people -where PeoplePhone like '138[7-9]%5' --789 - ---查询出电话号码开头133的员工信息,第4位是2-5之间的数字 ,最后一个号码不是2和3 -select * from people -where PeoplePhone like '138[2-5]%[^23]' - - ---求出年龄比平均年龄高的人员信息 ---平均年龄:今年-出生年份 -select avg(year(getdate())-year(peoplebirth)) 平均年龄 from People - -select * from people where (year(getdate())-year(peoplebirth))>(select avg(year(getdate())-year(peoplebirth)) 平均年龄 from People) - ---平均工资 -select avg(PeopleSalary) 平均工资 from People - ---保留小数点后2位 ---round(小数,保留位数) -select round(avg(PeopleSalary),2) 平均工资 from People - ---convert 02154.13200 decimal(5,2):保留小数后2位,有效数字总共是5 - ---convert和cast 可以强制转换数据类型 -select convert(decimal(13,2),avg(PeopleSalary)) 平均工资 from People - ---cast -select cast(avg(PeopleSalary) as decimal(13,2)) 平均工资 from People - - ---根据员工所在地区分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资, ---要求筛选出员工人数至少在2人及以上的记录,并且1985年及以后出身的员工不参与统计。 ---count(*):空值也统计 count(字段):当前字段的空值不统计 -select count(*) 员工人数,sum(PeopleSalary) 工资总和, max(PeopleSalary) 最高工资,min(PeopleSalary) 最低工资 from People -where year(PeopleBirth)<1985 -group by PeopleAddress -having count(*)>=2 - - ---删除数据 -delete from People where PeopleName='蜗牛' - ---删除 ---drop 删除整张表 ---truncate删除整张表,表还在 - ---根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资,平均工资在10000 以下的不参与统计,并且根据平均工资降序排列。 -select count(*) 员工人数, sum(PeopleSalary) 员工工资总和,convert(decimal(15,2),avg(PeopleSalary)) 平均工资 from Department de -join People po on de.DepartmentId = po.DepartmentId -group by po.DepartmentId having avg(PeopleSalary)>=10000 -order by 平均工资 desc - -create table B( - cid int primary key, - cname varchar(20) -) - - -create table A( - sid int, - sname varchar(10), - cid int references B(cid) -) - -select * from A -left join B on A.cid=B.cid \ No newline at end of file diff --git "a/40\345\274\240\350\200\200\344\273\201/\347\254\224\350\256\260/2022-09-15-\345\273\272\345\272\223\345\273\272\350\241\250\347\272\246\346\235\237.sql" "b/40\345\274\240\350\200\200\344\273\201/\347\254\224\350\256\260/2022-09-15-\345\273\272\345\272\223\345\273\272\350\241\250\347\272\246\346\235\237.sql" deleted file mode 100644 index 526c95e..0000000 --- "a/40\345\274\240\350\200\200\344\273\201/\347\254\224\350\256\260/2022-09-15-\345\273\272\345\272\223\345\273\272\350\241\250\347\272\246\346\235\237.sql" +++ /dev/null @@ -1,78 +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; - - --字符串:char(5),varchar(5),nvarchar(5)之间区别是什么 - - - - --删除表:drop table 表名 - drop table StuInfo - - - --非空 - - --约束,自增(标识列) - - --外键 - --主键:默认唯一列 - --默认 - --唯一: - --check检查: - - --插入数据:insert into \ No newline at end of file -- Gitee From 6fe638dd610e03523e5dfc239dce59b79d4e8f15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80=E4=BB=81?= <1579614208@qq.com> Date: Sun, 18 Sep 2022 15:58:23 +0000 Subject: [PATCH 06/12] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\344\275\234\344\270\232/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\344\275\234\344\270\232/.keep" diff --git "a/\344\275\234\344\270\232/.keep" "b/\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From bd8ab96af0c2b2ae6cff9194837a1d4a42705d53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80=E4=BB=81?= <1579614208@qq.com> Date: Sun, 18 Sep 2022 15:58:48 +0000 Subject: [PATCH 07/12] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\344\275\234\344\270\232/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 "\344\275\234\344\270\232/.keep" diff --git "a/\344\275\234\344\270\232/.keep" "b/\344\275\234\344\270\232/.keep" deleted file mode 100644 index e69de29..0000000 -- Gitee From 2f3a58e348d739296b147f72e1c42f8c6ff03a80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80=E4=BB=81?= <1579614208@qq.com> Date: Sun, 18 Sep 2022 15:59:06 +0000 Subject: [PATCH 08/12] =?UTF-8?q?=E6=96=B0=E5=BB=BA=2040=E5=BC=A0=E8=80=80?= =?UTF-8?q?=E4=BB=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "40\345\274\240\350\200\200\344\273\201/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "40\345\274\240\350\200\200\344\273\201/.keep" diff --git "a/40\345\274\240\350\200\200\344\273\201/.keep" "b/40\345\274\240\350\200\200\344\273\201/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 17622ba416d7b88fcaf8bfa6d39a7c6a7d19e799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80=E4=BB=81?= <1579614208@qq.com> Date: Sun, 18 Sep 2022 15:59:22 +0000 Subject: [PATCH 09/12] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\344\275\234\344\270\232/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "40\345\274\240\350\200\200\344\273\201/\344\275\234\344\270\232/.keep" diff --git "a/40\345\274\240\350\200\200\344\273\201/\344\275\234\344\270\232/.keep" "b/40\345\274\240\350\200\200\344\273\201/\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 024d837698fbea430a4324a724ef67cb55b89468 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80=E4=BB=81?= <1579614208@qq.com> Date: Sun, 18 Sep 2022 15:59:34 +0000 Subject: [PATCH 10/12] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\347\254\224\350\256\260/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "40\345\274\240\350\200\200\344\273\201/\347\254\224\350\256\260/.keep" diff --git "a/40\345\274\240\350\200\200\344\273\201/\347\254\224\350\256\260/.keep" "b/40\345\274\240\350\200\200\344\273\201/\347\254\224\350\256\260/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 44038f192d6376ac6c86ecc578fe856906c433b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80=E4=BB=81?= <1579614208@qq.com> Date: Sun, 18 Sep 2022 16:01:13 +0000 Subject: [PATCH 11/12] =?UTF-8?q?=E5=BC=A0=E8=80=80=E4=BB=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 寮犺浠 <1579614208@qq.com> --- .../0915\344\275\234\344\270\232.sql" | 48 +++++++++++ .../0918\344\275\234\344\270\232.sql" | 81 +++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 "40\345\274\240\350\200\200\344\273\201/\344\275\234\344\270\232/0915\344\275\234\344\270\232.sql" create mode 100644 "40\345\274\240\350\200\200\344\273\201/\344\275\234\344\270\232/0918\344\275\234\344\270\232.sql" diff --git "a/40\345\274\240\350\200\200\344\273\201/\344\275\234\344\270\232/0915\344\275\234\344\270\232.sql" "b/40\345\274\240\350\200\200\344\273\201/\344\275\234\344\270\232/0915\344\275\234\344\270\232.sql" new file mode 100644 index 0000000..d04ca45 --- /dev/null +++ "b/40\345\274\240\350\200\200\344\273\201/\344\275\234\344\270\232/0915\344\275\234\344\270\232.sql" @@ -0,0 +1,48 @@ +--创建数据库 DBTEST +create database DBTEST; +--使用数据库 +use dbtest; +--部门信息表 +create table sectioninfo( + sectionID int primary key identity(1,1), + sectionName varchar(10) not null +); +--员工信息表 +create table userinfo( + userNo int primary key identity(1,1) not null, + userName varchar(10) not null unique check(len(userName)>4), + userSex varchar(2) not null default('男') check(userSex='男' or userSex='女'), + userAge int not null check(userAge between 1 and 100), + userAddress varchar(50) default('湖北'), + userSection INT +); +--员工考勤表 +create table workinfo( + workId int primary key identity(1,1), + userId int references userinfo(userNo) not null , + workTime datetime not null, + workDescription varchar(40) not null check(workDescription in('迟到','早退','旷工','病假','事假')) +); +Alter table workInfo add constraint FK_workInfo_ClassId foreign key(userId) references userInfo(userNo); +--增加数据 +insert into sectionInfo(sectionName) values + ('开发部'), + ('运行部'), + ('人事部'), + ('策划部'), + ('管理部'); +select * from sectionInfo; +insert into userInfo(userName,userSex,userAge,userAddress,userSection) values + ('郑文源','男',8,'泉州',1), + ('唐皓颖','男',9,'贵阳',2), + ('袁贵森','女',25,'山西',3), + ('小白','男',38,'未知',4), + ('小黑','男',15,'未知',5); +select * from userInfo; +insert into workInfo(userId,workTime,workDescription) values + (1,'2022-01-05 15:25:46','迟到'), + (2,'2022-02-15 16:44:44','早退'), + (3,'2022-03-29 12:25:46','旷工'), + (4,'2022-04-25 08:45:46','病假'), + (5,'2022-06-18 19:25:46','事假'); +select * from workInfo; \ No newline at end of file diff --git "a/40\345\274\240\350\200\200\344\273\201/\344\275\234\344\270\232/0918\344\275\234\344\270\232.sql" "b/40\345\274\240\350\200\200\344\273\201/\344\275\234\344\270\232/0918\344\275\234\344\270\232.sql" new file mode 100644 index 0000000..ebf1aee --- /dev/null +++ "b/40\345\274\240\350\200\200\344\273\201/\344\275\234\344\270\232/0918\344\275\234\344\270\232.sql" @@ -0,0 +1,81 @@ +--查询出巨蟹 6.22--7.22 的员工信息 +select * from People +where datename(month,PeopleBirth) = 6 and day(PeopleBirth) between 22 and 30 +or datename(month,PeopleBirth) = 7 and day(PeopleBirth) between 1 and 22; + + +--查询所有员工信息,添加一列显示属相(鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪) +select *, case +when year(PeopleBirth)%12 = 0 +then '猴' +when year(PeopleBirth)%12 = 1 +then '鸡' +when year(PeopleBirth)%12 = 2 +then '狗' +when year(PeopleBirth)%12 = 3 +then '猪' +when year(PeopleBirth)%12 = 4 +then '鼠' +when year(PeopleBirth)%12 = 5 +then '牛' +when year(PeopleBirth)%12 = 6 +then '虎' +when year(PeopleBirth)%12 = 7 +then '兔' +when year(PeopleBirth)%12 = 8 +then '龙' +when year(PeopleBirth)%12 = 9 +then '蛇' +when year(PeopleBirth)%12 = 10 +then '马' +when year(PeopleBirth)%12 = 11 +then '羊' +end '生肖' +from People + + +--1. 查询出武汉地区所有的员工信息,要求显示部门名称以及员工的详细资料 +select p.*,d.DepartmentName from People p +inner join Department d +on p.DepartmentId = d.DepartmentId +where p.PeopleAddress = '武汉'; + + +--2. 查询出武汉地区所有的员工信息,要求显示部门名称,职级名称以及员工的详细资料 +select p.*,d.DepartmentName,r.RankName from People p +inner join Department d +on p.DepartmentId = d.DepartmentId +inner join Rank r on r.RankId = p.RankId +where p.PeopleAddress = '武汉'; + + +--3. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资。 +select count(D.DepartmentName)部门员工人数,sum(p.PeopleSalary)员工工资总和,convert(decimal(10,2), +avg(p.PeopleSalary))平均工资,max(p.PeopleSalary)最高工资,min(p.PeopleSalary)最低工资 +from People p +inner join Department d +on p.DepartmentId = d.DepartmentId +group by d.DepartmentName + + +--4. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资,平均工资在10000 以下的不参与统计, +--并且根据平均工资降序排列。 +select count(D.DepartmentName)部门员工人数,sum(p.PeopleSalary)员工工资总和,convert(decimal(10,2), +avg(p.PeopleSalary))平均工资,max(p.PeopleSalary)最高工资,min(p.PeopleSalary)最低工资 +from People p +inner join Department d +on p.DepartmentId = d.DepartmentId +group by d.DepartmentName +having avg(p.PeopleSalary) > 10000 +order by 平均工资 desc + + +--5. 根据部门名称,然后根据职位名称,分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资 +select d.DepartmentName,r.RankName,count(*)员工人数, +sum(p.PeopleSalary)员工工资总和,convert(decimal(10,2), +avg(p.PeopleSalary))平均工资,max(p.PeopleSalary)最高工资,min(p.PeopleSalary)最低工资 +from People p +inner join Department d +on p.DepartmentId = d.DepartmentId +inner join Rank r on r.RankId = p.RankId +group by d.DepartmentName,r.RankName \ No newline at end of file -- Gitee From bfa176b84d3c09cb6810484ff11afce4db6a05ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=80=80=E4=BB=81?= <1579614208@qq.com> Date: Sun, 18 Sep 2022 16:01:50 +0000 Subject: [PATCH 12/12] =?UTF-8?q?=E5=BC=A0=E8=80=80=E4=BB=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 寮犺浠 <1579614208@qq.com> --- .../0916\347\254\224\350\256\260.sql" | 86 +++++++++++++++++++ ...2\350\241\250\347\272\246\346\235\237.sql" | 78 +++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 "40\345\274\240\350\200\200\344\273\201/\347\254\224\350\256\260/0916\347\254\224\350\256\260.sql" create mode 100644 "40\345\274\240\350\200\200\344\273\201/\347\254\224\350\256\260/2022-09-15-\345\273\272\345\272\223\345\273\272\350\241\250\347\272\246\346\235\237.sql" diff --git "a/40\345\274\240\350\200\200\344\273\201/\347\254\224\350\256\260/0916\347\254\224\350\256\260.sql" "b/40\345\274\240\350\200\200\344\273\201/\347\254\224\350\256\260/0916\347\254\224\350\256\260.sql" new file mode 100644 index 0000000..d39053c --- /dev/null +++ "b/40\345\274\240\350\200\200\344\273\201/\347\254\224\350\256\260/0916\347\254\224\350\256\260.sql" @@ -0,0 +1,86 @@ +--查询公司员工所在城市(不需要重复数据) +--distinct +select distinct PeopleAddress from People + +--查询所有员工信息(根据工资排序(order by),降序排列) +select * from People order by PeopleSalary desc + + +--查询1980年以后出生的员工信息 +select * from People where year(PeopleBirth)>=1980 + + +--查询出巨蟹 6.22--7.22 的员工信息 + +--查询出和赵云在同一个城市的人 + + +select * from People where PeopleAddress=(select peopleAddress from People where PeopleName='赵云') + +--查询出电话号码开头138的员工信息,第4位可能是7,可能8 ,最后一个号码是5 +select * from people +where PeoplePhone like '138[7-9]%5' --789 + +--查询出电话号码开头133的员工信息,第4位是2-5之间的数字 ,最后一个号码不是2和3 +select * from people +where PeoplePhone like '138[2-5]%[^23]' + + +--求出年龄比平均年龄高的人员信息 +--平均年龄:今年-出生年份 +select avg(year(getdate())-year(peoplebirth)) 平均年龄 from People + +select * from people where (year(getdate())-year(peoplebirth))>(select avg(year(getdate())-year(peoplebirth)) 平均年龄 from People) + +--平均工资 +select avg(PeopleSalary) 平均工资 from People + +--保留小数点后2位 +--round(小数,保留位数) +select round(avg(PeopleSalary),2) 平均工资 from People + +--convert 02154.13200 decimal(5,2):保留小数后2位,有效数字总共是5 + +--convert和cast 可以强制转换数据类型 +select convert(decimal(13,2),avg(PeopleSalary)) 平均工资 from People + +--cast +select cast(avg(PeopleSalary) as decimal(13,2)) 平均工资 from People + + +--根据员工所在地区分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资, +--要求筛选出员工人数至少在2人及以上的记录,并且1985年及以后出身的员工不参与统计。 +--count(*):空值也统计 count(字段):当前字段的空值不统计 +select count(*) 员工人数,sum(PeopleSalary) 工资总和, max(PeopleSalary) 最高工资,min(PeopleSalary) 最低工资 from People +where year(PeopleBirth)<1985 +group by PeopleAddress +having count(*)>=2 + + +--删除数据 +delete from People where PeopleName='蜗牛' + +--删除 +--drop 删除整张表 +--truncate删除整张表,表还在 + +--根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资,平均工资在10000 以下的不参与统计,并且根据平均工资降序排列。 +select count(*) 员工人数, sum(PeopleSalary) 员工工资总和,convert(decimal(15,2),avg(PeopleSalary)) 平均工资 from Department de +join People po on de.DepartmentId = po.DepartmentId +group by po.DepartmentId having avg(PeopleSalary)>=10000 +order by 平均工资 desc + +create table B( + cid int primary key, + cname varchar(20) +) + + +create table A( + sid int, + sname varchar(10), + cid int references B(cid) +) + +select * from A +left join B on A.cid=B.cid \ No newline at end of file diff --git "a/40\345\274\240\350\200\200\344\273\201/\347\254\224\350\256\260/2022-09-15-\345\273\272\345\272\223\345\273\272\350\241\250\347\272\246\346\235\237.sql" "b/40\345\274\240\350\200\200\344\273\201/\347\254\224\350\256\260/2022-09-15-\345\273\272\345\272\223\345\273\272\350\241\250\347\272\246\346\235\237.sql" new file mode 100644 index 0000000..526c95e --- /dev/null +++ "b/40\345\274\240\350\200\200\344\273\201/\347\254\224\350\256\260/2022-09-15-\345\273\272\345\272\223\345\273\272\350\241\250\347\272\246\346\235\237.sql" @@ -0,0 +1,78 @@ +--关系型数据库: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; + + --字符串:char(5),varchar(5),nvarchar(5)之间区别是什么 + + + + --删除表:drop table 表名 + drop table StuInfo + + + --非空 + + --约束,自增(标识列) + + --外键 + --主键:默认唯一列 + --默认 + --唯一: + --check检查: + + --插入数据:insert into \ No newline at end of file -- Gitee