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" new file mode 100644 index 0000000000000000000000000000000000000000..d04ca451afbbf139e3b6f816113eed184d9438fc --- /dev/null +++ "b/40\345\274\240\350\200\200\344\273\201/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/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" new file mode 100644 index 0000000000000000000000000000000000000000..ebf1aee7770fc08980360baa000a4dc69ad64bbc --- /dev/null +++ "b/40\345\274\240\350\200\200\344\273\201/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 diff --git "a/\347\254\224\350\256\260/0916\347\254\224\350\256\260.sql" "b/\347\254\224\350\256\260/0916\347\254\224\350\256\260.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d39053cc4eb2cf8775a93d07db07c59cb15bb608 --- /dev/null +++ "b/\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/\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/\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 0000000000000000000000000000000000000000..526c95eeea0bdab3b3c359d4b5cc675ed24bdedc --- /dev/null +++ "b/\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