diff --git "a/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/2022-09-16\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223.md.md" "b/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/2022-09-16\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223.md.md" new file mode 100644 index 0000000000000000000000000000000000000000..8df7d0710db9413685df3f0661bf1412323558f7 --- /dev/null +++ "b/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/2022-09-16\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223.md.md" @@ -0,0 +1,82 @@ +if exists (select * from sys.databases where name='DBTEST') +drop database DBTEST + +--创建数据库 +create database DBTEST +--使用数据库 +use DBTEST +--创建部门信息表 +create table sectionInfo( + --部门编号 + sectionID int identity(1,1) primary key, + --部门名称 + sectionName varchar(10) not null +); +--插入数据 +insert into sectionInfo(sectionName) values ('财政部') +insert into sectionInfo(sectionName) values ('法务部') +insert into sectionInfo(sectionName) values ('炫饭部') +insert into sectionInfo(sectionName) values ('睡觉部') +insert into sectionInfo(sectionName) values ('娱乐部') +--查询表 +select * from sectionInfo +--员工信息表 +create table userInfo( + --员工编号 + userNo int identity(1,1) primary key 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(userAge>1 and userAge<100), + --员工地址 + userAddress varchar(50) default('湖北'), + --员工部门 + userSection int references sectionInfo(sectionID) +); +--插入数据 +INSERT INTO userInfo + ( + userName, + userSex, + userAge, + userAddress, + userSection + ) + VALUES + ('吴哒里个哒','女',88, '广东省', 2), + ('吴跌里个跌','男',99,'山东省',5), + ('吴得里个得','男',99,'山东省',4), + ('吴哈里个哈','男',99,'山东省',2), + ('吴麻里个麻','男',99,'山东省',3) + +--查询表 +select * from userInfo +--考勤表 +create table workInfo( + --考勤编号 + workId int identity(1,1) primary key not null, + --考勤员工 + userId int references userInfo(userNo) not null, + --考勤时间 + workTime datetime not null, + --考勤说明 + workDescription varchar(40) not null check(workDescription='迟到' or workDescription='早退' or workDescription='旷工') +); +--插入数据 +INSERT INTO workInfo + ( + userId, + workTime, + workDescription + ) + VALUES + (1,GETDATE(), '迟到'), + (2,GETDATE(), '迟到'), + (3,GETDATE(), '迟到'), + (4,GETDATE(), '迟到'), + (4,GETDATE(), '迟到') + +--查询表 +select * from workInfo \ No newline at end of file diff --git "a/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/2022-09-18\346\237\245\350\257\242.md.md" "b/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/2022-09-18\346\237\245\350\257\242.md.md" new file mode 100644 index 0000000000000000000000000000000000000000..1bc4a923069e64afb8b166f3e0e16f94f4a7fcc8 --- /dev/null +++ "b/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/2022-09-18\346\237\245\350\257\242.md.md" @@ -0,0 +1,46 @@ +--1. 查询出武汉地区所有的员工信息,要求显示部门名称以及员工的详细资料 +select * from People P +inner join Department D on P.DepartmentId=D.DepartmentId +where PeopleAddress='武汉' +--2. 查询出武汉地区所有的员工信息,要求显示部门名称,职级名称以及员工的详细资料 +select * from People P +inner join Department D on P.DepartmentId=D.DepartmentId +inner join Rank R on P.RankId=R.RankId +where PeopleAddress='武汉' +--3. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资。 +select D.DepartmentName,count(D.DepartmentId)员工人数,SUM(P.PeopleSalary)员工工资总和,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 D.DepartmentName,COUNT(D.DepartmentId)员工人数,SUM(P.PeopleSalary)员工工资总和,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 AVG(P.PeopleSalary) desc +--5. 根据部门名称,然后根据职位名称,分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资 +select D.DepartmentName,R.RankName,count(D.DepartmentId)员工人数,SUM(P.PeopleSalary)员工工资总和,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 P.RankId=R.RankId +group by D.DepartmentName, R.RankName +--6.查询出巨蟹 6.22--7.22 的员工信息 +select * from People +where (MONTH(PeopleBirth)=6 and DAY(PeopleBirth)>=22) or (MONTH(PeopleBirth)=7 and DAY(PeopleBirth)<=22) +--7.查询所有员工信息,添加一列显示属相(鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪) +select *, +( + case + when YEAR(PeopleBirth)%12 =0 then '鼠' + when YEAR(PeopleBirth)%12 =1 then '牛' + when YEAR(PeopleBirth)%12 =2 then '虎' + when YEAR(PeopleBirth)%12 =3 then '兔' + when YEAR(PeopleBirth)%12 =4 then '龙' + when YEAR(PeopleBirth)%12 =5 then '蛇' + when YEAR(PeopleBirth)%12 =6 then '马' + when YEAR(PeopleBirth)%12 =7 then '羊' + when YEAR(PeopleBirth)%12 =8 then '猴' + when YEAR(PeopleBirth)%12 =9 then '鸡' + when YEAR(PeopleBirth)%12 =10 then '狗' + else '猪' + end +)属相 + from People \ No newline at end of file diff --git "a/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223.md.md" "b/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223.md.md" new file mode 100644 index 0000000000000000000000000000000000000000..04010e7a49a3fcd0bd384634d9562c5fd3b5c27b --- /dev/null +++ "b/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223.md.md" @@ -0,0 +1,60 @@ +``` +关系型数据库: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 diff --git "a/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/\346\237\245\350\257\2421.md" "b/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/\346\237\245\350\257\2421.md" new file mode 100644 index 0000000000000000000000000000000000000000..d54ca67cafef4a421deb630aa7bef4e7abf07aca --- /dev/null +++ "b/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/\346\237\245\350\257\2421.md" @@ -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/50\350\234\227\347\211\233/\347\254\224\350\256\260/2022-09-15-\347\240\201\344\272\221\346\217\220\344\272\244\347\254\224\350\256\260.md" "b/50\350\234\227\347\211\233/\347\254\224\350\256\260/2022-09-15-\347\240\201\344\272\221\346\217\220\344\272\244\347\254\224\350\256\260.md" deleted file mode 100644 index 35bdd145c85027a308e726da741a87dc05795a97..0000000000000000000000000000000000000000 --- "a/50\350\234\227\347\211\233/\347\254\224\350\256\260/2022-09-15-\347\240\201\344\272\221\346\217\220\344\272\244\347\254\224\350\256\260.md" +++ /dev/null @@ -1,29 +0,0 @@ -1.SQLServer 2014下载: - -https://blog.csdn.net/lililove2000/article/details/115673285 - -2.创建数据库 - -```sql -create database 数据库名 -``` - -3.创建数据表 - -```sql -create table 表名( -) -``` - - - -4.数据约束 - -5.插入数据 - -6.修改数据 - -7.删除数据 - -8.查询数据 - diff --git a/README.en.md b/README.en.md deleted file mode 100644 index ac31e94d7051efb27325dd6be9b67641b85572e6..0000000000000000000000000000000000000000 --- a/README.en.md +++ /dev/null @@ -1,36 +0,0 @@ -# SQL进阶 - -#### Description -11 - -#### Software Architecture -Software architecture description - -#### Installation - -1. xxxx -2. xxxx -3. xxxx - -#### Instructions - -1. xxxx -2. xxxx -3. xxxx - -#### Contribution - -1. Fork the repository -2. Create Feat_xxx branch -3. Commit your code -4. Create Pull Request - - -#### Gitee Feature - -1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md -2. Gitee blog [blog.gitee.com](https://blog.gitee.com) -3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore) -4. The most valuable open source project [GVP](https://gitee.com/gvp) -5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help) -6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) diff --git a/README.md b/README.md deleted file mode 100644 index 1c4b0e7c62868161068647925213862187205184..0000000000000000000000000000000000000000 --- a/README.md +++ /dev/null @@ -1,37 +0,0 @@ -# SQL进阶 - -#### 介绍 -11 - -#### 软件架构 -软件架构说明 - - -#### 安装教程 - -1. xxxx -2. xxxx -3. xxxx - -#### 使用说明 - -1. xxxx -2. xxxx -3. xxxx - -#### 参与贡献 - -1. Fork 本仓库 -2. 新建 Feat_xxx 分支 -3. 提交代码 -4. 新建 Pull Request - - -#### 特技 - -1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md -2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) -3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 -4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 -5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) -6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) diff --git a/advanced-sql b/advanced-sql new file mode 160000 index 0000000000000000000000000000000000000000..30887ad6cf06bbd5aaf0c641f256f68fd2ac9b2d --- /dev/null +++ b/advanced-sql @@ -0,0 +1 @@ +Subproject commit 30887ad6cf06bbd5aaf0c641f256f68fd2ac9b2d