From 6ac038fef148f2f5bc85e2d5b03b2524b26ac4f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E9=9B=85=E5=A8=9F?= <2673357576@qq.com> Date: Fri, 24 Mar 2023 09:22:14 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=88=91=E7=9A=84=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\344\275\234\344\270\232\344\270\200.sql" | 89 ++++++++++++++ ...4\344\275\234\344\270\232\344\272\214.sql" | 110 ++++++++++++++++++ 2 files changed, 199 insertions(+) create mode 100644 "23\345\220\264\351\233\205\345\250\237/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/\351\230\266\346\256\265\344\272\214\344\275\234\344\270\232\344\270\200.sql" create mode 100644 "23\345\220\264\351\233\205\345\250\237/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/\351\230\266\346\256\265\344\272\214\344\275\234\344\270\232\344\272\214.sql" diff --git "a/23\345\220\264\351\233\205\345\250\237/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/\351\230\266\346\256\265\344\272\214\344\275\234\344\270\232\344\270\200.sql" "b/23\345\220\264\351\233\205\345\250\237/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/\351\230\266\346\256\265\344\272\214\344\275\234\344\270\232\344\270\200.sql" new file mode 100644 index 0000000..a054c0b --- /dev/null +++ "b/23\345\220\264\351\233\205\345\250\237/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/\351\230\266\346\256\265\344\272\214\344\275\234\344\270\232\344\270\200.sql" @@ -0,0 +1,89 @@ +create database stuDB +go +use stuDB +create table stuInfo( +stuID int identity primary key,--学号 +stuName nvarchar(10),--姓名 +stuAge tinyint,--年龄 +stuSex tinyint check(stusex=0 or stusex=1),--性别 +time datetime --时间 +); +create table courseInfo( +courseID int identity primary key,--课程编号 +courseName nvarchar(20),--课程名称 +courseMarks tinyint --学分 +); +create table scoreInfo( +scoreID int identity primary key ,--分数编号 +stuID tinyint ,--学号 +courseID tinyint,--课程编号 +score tinyint--分数 +); +insert into stuInfo +values('Tom',19,1,Null), + ('Jack',20,0,Null), + ('Rose',21,1,Null), + ('Lulu',19,1,Null), + ('Lili',21,0,Null), + ('abc',20,1,'2007-01-01 01:11:36.590') +select * from stuInfo +insert into courseInfo +values('JavaBase',4), + ('HTML',2), + ('JavaScript',2), + ('SqlBase',2) +select * from courseInfo +insert into scoreInfo +values(1,1,80), + (1,2,85), + (1,4,50), + (2,1,75), + (2,3,45), + (2,4,75), + (3,1,45), + (4,1,95), + (4,2,75), + (4,3,90), + (4,4,45) +select * from scoreInfo + +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName,count(courseid)选修课程数量,avg(score)考试平均分 from scoreInfo +inner join stuInfo on scoreInfo.stuID=stuInfo.stuID +group by stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName,count(stuInfo.stuID) 学生个数,sum(score)考试的总分 from stuInfo +inner join scoreInfo on stuInfo.stuID=scoreInfo.stuID +inner join courseInfo on stuInfo.stuID=courseInfo.courseID +group by courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select distinct * from stuInfo as a +left join stuInfo as b +on a.stuAge=b.stuAge and a.stuSex=b.stuSex +where a.stuID!=b.stuID +--4.查询出学分一样的课程信息 +select * from courseInfo as a +left join courseInfo as b +on a.courseMarks=b.courseMarks +where a.courseID!=b.courseID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select distinct stuInfo.stuID,stuName,courseInfo.courseId,score from stuInfo +inner join scoreInfo on stuInfo.stuID=scoreInfo.stuID +join courseInfo on stuInfo.stuID=courseInfo.courseID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select distinct stuInfo.stuID,courseInfo.courseID,courseName ,courseMarks,score from stuInfo +inner join courseInfo on stuInfo.stuID=courseInfo.courseID +join scoreInfo on stuinfo.stuID=scoreInfo.stuID +--7.查询出没有参加考试的学生的学号和姓名 +select distinct score,stuInfo.stuID,stuName from stuInfo +left join scoreInfo on stuInfo.stuID=scoreInfo.stuID +where score is null +--8.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--9.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuID,avg(score)平均分,count(courseName)数量 from scoreInfo +inner join courseInfo on scoreInfo.stuID=courseInfo.courseID +where courseMarks>2 +group by stuID +having avg(score)>70 \ No newline at end of file diff --git "a/23\345\220\264\351\233\205\345\250\237/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/\351\230\266\346\256\265\344\272\214\344\275\234\344\270\232\344\272\214.sql" "b/23\345\220\264\351\233\205\345\250\237/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/\351\230\266\346\256\265\344\272\214\344\275\234\344\270\232\344\272\214.sql" new file mode 100644 index 0000000..45d5af3 --- /dev/null +++ "b/23\345\220\264\351\233\205\345\250\237/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/\351\230\266\346\256\265\344\272\214\344\275\234\344\270\232\344\272\214.sql" @@ -0,0 +1,110 @@ +create database internet +go +use internet +go +create table tbl_card( +ID nvarchar(30) primary key ,--卡号,主键 +passWord nvarchar(30),--密码 +balance int,--卡上的余额 +userName nvarchar(30)--该上网卡所属的用户名 +); +go +create table tbl_computer( +ID nvarchar(30) primary key,--机器编号,主键 +onUse int check(onUse=0 or onUse=1),--该机器是否正在使用,0为未使用,1为正在使用 +note int--该机器的说明 +); +go +create table tbl_record( +ID int primary key,--记录编号,主键 +cardId nvarchar(30) foreign key (cardId) references tbl_card(ID),--本次上网的卡号,外键 +ComputerId nvarchar(30) foreign key (ComputerId) references tbl_computer (ID), --本次上网记录所使用的机器号,外键 +beginTime nvarchar(50),--本次上网记录的开始时间 +endTime varchar(100),--本次上网记录的结束时间 +fee int --本次上网的费用 +); +insert into tbl_card +values('0023_ABC','555',98,'张军'), + ('0025_bbd','abe',300,'朱俊'), + ('0036_CCD','何柳',100,'何柳'), + ('0045_YGR','0045_YGR',58,'证验'), + ('0078_RJV','55885fg',600,'校庆'), + ('0089_EDE','zhang',134,'张峻') + +insert into tbl_computer +values('02',0,25555), + ('03',1,55555), + ('04',0,66666), + ('05',1,88888), + ('06',0,688878), + ('B01',0,558558) + +insert into tbl_record +values(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20'), + (34,'0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23'), + (45,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), + (46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-23 22:55:00','6'), + (47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), + (48,'0023_ABC','03','2007-01-06 22:55:00','2007-01-06 22:55:00','6'), + (55,'0023_ABC','03','2006-7-21 15:26:00','2006-7-21 22:55:00','50'), + (64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','3'), + (65,'0025_bbd','02','2006-07-28 18:00:00','2006-07-28 22:00:00','23'), + (98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23') + + + + +--请完成以下题目: +select * from tbl_card +select * from tbl_computer +select * from tbl_record + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select distinct cardId,userName,tbl_computer.ID,beginTime,endTime,fee from tbl_record +inner join tbl_card on tbl_card.ID=tbl_record.cardId +inner join tbl_computer on tbl_computer.ID=tbl_record.ComputerId +where userName='张军' order by fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select count(tbl_record.ComputerId) 上网次数 ,sum(fee) 消费总金额 from tbl_record +inner join tbl_computer on tbl_computer.ID=tbl_record.ComputerId +group by tbl_computer.ID + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select sum(fee) 消费总金额 from tbl_record +inner join tbl_computer on tbl_computer.ID=tbl_record.ComputerId +where onUse=1 + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select tbl_card.ID,userName from tbl_record +inner join tbl_card on tbl_card.ID=tbl_record.cardId +inner join tbl_computer on tbl_computer.ID=tbl_record.ComputerId +where tbl_card.ID not in(tbl_record.cardId) + +--5. 将密码与用户名一样的上网卡信息查询出来 +select a .* from tbl_card a +join tbl_card b on a.ID =b.ID +where a.passWord=b.userName +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 tbl_computer.ID 机器编号 ,count (tbl_computer.ID)使用次数 from tbl_computer +group by tbl_computer.ID +order by count (tbl_computer.ID) desc +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select tbl_card.ID 卡号,userName 用户名,tbl_computer.ID 机器号,fee 消费金额 from tbl_record +inner join tbl_card on tbl_card.ID=tbl_record.cardId +inner join tbl_computer on tbl_computer.ID=tbl_record.ComputerId +where tbl_record.cardId like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_card.ID 卡号,username 用户名,tbl_computer.ID 机器号,beginTime 开始时间,endTime 结束时间 from tbl_record +inner join tbl_card on tbl_card.ID=tbl_record.cardId +inner join tbl_computer on tbl_computer.ID=tbl_record.ComputerId +where datepart (weekday ,tbl_record.beginTime)in (1,7) +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_card.ID 卡号,username 用户名, tbl_computer.ID 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record +inner join tbl_card on tbl_card.ID=tbl_record.cardId +inner join tbl_computer on tbl_computer.ID=tbl_record.ComputerId + where datediff(hh,begintime,endtime)>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_card.ID 卡号,username 用户名, tbl_computer.ID 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record +inner join tbl_card on tbl_card.ID=tbl_record.cardId +inner join tbl_computer on tbl_computer.ID=tbl_record.ComputerId +order by fee desc \ No newline at end of file -- Gitee From ca0f41369adccc0a41a6ab46ec8cac4292199d48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E9=9B=85=E5=A8=9F?= <2673357576@qq.com> Date: Tue, 28 Mar 2023 11:17:47 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=88=91=E7=9A=84=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\346\225\260\347\273\203\344\271\240.sql" | 133 ++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 "23\345\220\264\351\233\205\345\250\237/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232 \346\227\245\346\234\237\345\207\275\346\225\260/\346\227\245\346\234\237\345\207\275\346\225\260\347\273\203\344\271\240.sql" diff --git "a/23\345\220\264\351\233\205\345\250\237/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232 \346\227\245\346\234\237\345\207\275\346\225\260/\346\227\245\346\234\237\345\207\275\346\225\260\347\273\203\344\271\240.sql" "b/23\345\220\264\351\233\205\345\250\237/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232 \346\227\245\346\234\237\345\207\275\346\225\260/\346\227\245\346\234\237\345\207\275\346\225\260\347\273\203\344\271\240.sql" new file mode 100644 index 0000000..ce5f4a4 --- /dev/null +++ "b/23\345\220\264\351\233\205\345\250\237/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232 \346\227\245\346\234\237\345\207\275\346\225\260/\346\227\245\346\234\237\345\207\275\346\225\260\347\273\203\344\271\240.sql" @@ -0,0 +1,133 @@ +create database stu +go +use stu +--班级信息表 +create table Class( +ClassId int identity not null primary key, +ClassName nvarchar(50) +); +--学生信息表 +create table Student( +StudentId int identity not null primary key, +StudentName nvarchar(50) not null, +StudentSex tinyint not null default 3, +StudentBirth date, +StudentAddress nvarchar(255) not null, +ClassId int +); +--插入班级信息 +insert into Class(ClassName) +values ('软件一班'),('软件二班'),('计算机应用技术班') +--插入学生信息 +insert into Student +values('刘正',1,'2000-01-01','广西省桂林市七星区空明西路10号鸾东小区',1), + ('黄贵',1,'2001-03-20','江西省南昌市青山湖区艾溪湖南路南150米广阳小区',1), + ('陈美',2,'2000-07-08','福建省龙岩市新罗区曹溪街道万达小区',1) +insert into Student +values('江文',1,'2000-08-10','安徽省合肥市庐阳区四里河路与潜山路交汇处万科城市之光',2), + ('钟琪女',2,'2001-03-21','湖南省长沙市雨花区红花坡社区',2) +insert into Student +values('曾小林',1,'1999-12-10','安徽省合肥市庐阳区四里河路与潜山路交汇处万科城市之光',3), + ('欧阳天天',2,'2000-04-05','湖北省武汉市洪山区友谊大道与二环线交汇处融侨悦府',3), + ('徐长卿',2,'2001-01-30','江苏省苏州市苏州工业园区独墅湖社区',3), + ('李逍遥',1,'1999-11-11','广东省广州市白云区金沙洲岛御洲三街恒大绿洲',3) +insert into Student +values('东方不败',3,'1999-12-11','河北省平定州西北四十余里的猩猩滩',0), + ('令狐冲',2,'2000-08-11','陕西省渭南市华阴市玉泉路南段',0) +--课程信息表 +create table Course( +CourseId int identity not null primary key, +ClassId int, +CourseName nvarchar(50) not null, +CoueseCredit tinyint not null default 0 +); + +insert into Student +values(1,'数据库高级应用',3), + (1,'javascript编程基础',3), + (1,'web前端程序设计基础',4), + (1,'动态网页设计.net基础',6) +insert into Student +values(2,'数据库高级应用',3), + (2,'javascript编程基础',3), + (2,'web前端程序设计基础',4), + (2,'动态网页设计.net基础',6) +insert into Student +values(3,'数据库高级应用',3), + (3,'javascript编程基础',3), + (3,'web前端程序设计基础',4), + (3,'动态网页设计.net基础',6) +--分数表 +create table Score( +Name nvarchar(50) not null, +CourseName nvarchar(50) not null, +Score int +) +insert into Score +values ('刘正','数据库高级应用',80), + ('刘正','javascript编程基础',78), + ('刘正','web前端程序设计基础',65), + ('刘正','动态网页设计.net基础',90), + ('黄贵','数据库高级应用',60), + ('黄贵','javascript编程基础',77), + ('黄贵','web前端程序设计基础',68), + ('黄贵','动态网页设计.net基础',88), + ('陈美','数据库高级应用',88), + ('陈美','javascript编程基础',45), + ('陈美','web前端程序设计基础',66), + ('陈美','动态网页设计.net基础',75), + ('江文','数据库高级应用',56), + ('江文','javascript编程基础',80), + ('江文','web前端程序设计基础',75), + ('江文','动态网页设计.net基础',66), + ('钟琪','数据库高级应用',88), + ('钟琪','javascript编程基础',79), + ('钟琪','web前端程序设计基础',72), + ('钟琪','动态网页设计.net基础',75), + ('曾小林','数据库高级应用',68), + ('曾小林','javascript编程基础',88), + ('曾小林','web前端程序设计基础',73), + ('曾小林','动态网页设计.net基础',63), + ('欧阳天天','数据库高级应用',68), + ('欧阳天天','javascript编程基础',88), + ('欧阳天天','web前端程序设计基础',73), + ('欧阳天天','动态网页设计.net基础',63), + ('徐长卿','数据库高级应用',68), + ('徐长卿','javascript编程基础',88), + ('徐长卿','web前端程序设计基础',73), + ('徐长卿','动态网页设计.net基础',63), + ('李逍遥','数据库高级应用',68), + ('李逍遥','javascript编程基础',88), + ('李逍遥','web前端程序设计基础',73), + ('李逍遥','动态网页设计.net基础',63) +delete from Score +where Name='李逍遥'and CourseName=' 数据库高级应用' + +select *from Student +update Student set StudentBirth='2000-04-06' where StudentId=7 +select *from Score +update Score set Score=61 where Name='徐长卿'and CourseName='javascript编程基础' + +----------------------- +--日期函数练习 +---------------------- +--1.获取当前系统时间 +select getdate()当前系统时间 +--2.查询学生的出生年份信息 +select year(StudentBirth)出生年份 from student +--3.查询学生信息:姓名,年龄(年龄用datediff函数进行计算) +select studentName,year(getdate())-year(StudentBirth)年龄 from Student +--4.查询本月生日的学生信息:学号,姓名,出生日期,年龄 +select * from student where month(studentBirth)=month(getdate()) +--5.查询下个月生日的学生信息 +select * from student where month(studentBirth)=month(getdate())+1 +--6.获取本月的第一天 +select dateadd(month,datediff(month,0,getdate()),0) 本月的第一天 +--7.获取本月的最后一天 +select dateadd(month,datediff(month,0,getdate())+1,0)-1 本月的最后一天 +--8.获取下个月的第一天 +select dateadd(month,datediff(month,0,getdate())+1,0)下个月的第一天 +--9.获取本周的 第一天 +select dateadd(week,datediff(week,0,getdate()),0)本周的第一天 +--10.获取本周的最后一天 +select dateadd(week,datediff(week,0,getdate())+1,0)-1 本周的最后一天 \ No newline at end of file -- Gitee