diff --git "a/2021 03 30 \344\275\234\344\270\232/.keep" "b/2021 03 30 \344\275\234\344\270\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/2021 03 30 \344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery1.sql" "b/2021 03 30 \344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7716b9d78dc29220a396f0b6bfbe3817badcf2ef --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery1.sql" @@ -0,0 +1,84 @@ +use master +go +create database bbs +on +( + name='bbs', + filename='D:\bbs.mdf', + size=10, + maxsize=200, + filegrowth=20 +) +log on +( + name='bbs_log', + filename='D:\bbs_log.ldf ', + size=10, + maxsize=200, + filegrowth=20 +) +go +use bbs + +create table stuInfo +( + stuld int primary key identity not null, + stuname varchar(20) not null, + stuage varchar(20) not null, + stusex varchar(2) check (stusex in ('1','0'))not null, + time datetime default('null') +) +go +create table courseInfo +( + + courseid int primary key identity not null, + coursename varchar(20) not null, + coursemarks int not null +) +go +create table scoreInfo +( + + scoreid int primary key identity not null, + stuid int references stuInfo(stuld) not null, + courseld int references courseInfo(courseid) not null, + score int not null + + +) +go + + insert into stuInfo values ('tom','19','1',''),('jack','20','0',''),('rose','21','1',''),('lulu','19','1',''),('lili','21','0',''),('abc','20','1','2007-01-07') + + + 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 + + select stuid,stuname,count(courseld),avg(score) from scoreInfo inner join stuInfo on scoreInfo.stuid=stuInfo.stuld group by stuid,stuname + + select scoreInfo.courseld,coursename,COUNT(stuid),SUM(score) from scoreInfo inner join courseInfo on scoreInfo.courseld=courseInfo.courseid group by scoreInfo.courseld,coursename + + select a.* from stuInfo a , stuInfo b where a.stusex=b.stusex and a.stuage=b.stuage and a.stuld!=b.stuld + + select distinct a.courseid,a.coursename,a.coursemarks from courseInfo a,courseInfo b where a.coursemarks=b.coursemarks and a.courseid!=b.courseid + + select stuid,stuname,courseld,score from scoreInfo inner join stuInfo on stuInfo.stuld=scoreInfo.stuid group by stuid,stuname,courseld,score + + select stuid,courseInfo.courseid,coursename,coursemarks,score from scoreInfo inner join courseInfo on scoreInfo.courseld=courseInfo.courseid group by stuid,courseInfo.courseid,coursename,coursemarks,score + + select * from stuInfo left join scoreInfo on scoreInfo.stuid=stuInfo.stuld WHERE score IS NULL + +select * from stuInfo where datepart(weekday,time)=1 or datepart(weekday,time)=7 + + select * from stuInfo where stuname like '%a%' + + select stuid,count(courseld),AVG(score) from scoreInfo group by stuid having COUNT(courseld)>2 and AVG(score)>70 + diff --git "a/2021 03 30 \344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/\350\257\276\345\220\216\344\275\234\344\270\2321.sql" "b/2021 03 30 \344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/\350\257\276\345\220\216\344\275\234\344\270\2321.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c87659c70f569c3503995dfe354c1e6c1a22e70a --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/\350\257\276\345\220\216\344\275\234\344\270\2321.sql" @@ -0,0 +1,100 @@ + use master + go + create database Students04 + on + ( + name = 'Students04 ', + filename = 'D:\Students04.mdf', + size = 5MB, + maxsize = 50MB, + filegrowth = 10% + ) + log on + ( + name = 'Students04_log ', + filename = 'D:\Students04_log.mdf', + size = 5MB, + maxsize = 50MB, + filegrowth = 10% + ) + go + use Students04 + go + create table StuInfo + ( + StuId int primary key identity(1,1), + StuName varchar(10) not null, + StuAge int, + StuSex char(1) default(1) check(StuSex in(1,0)) not null, + Time datetime + ) + + create table CourseInfo + ( + CourseId int primary key identity(1,1), + CourseName varchar(20) not null, + CourseMarks int + ) + + create table ScoreInfo + ( + ScoreId int primary key identity(1,1), + StuId int references StuInfo(StuId), + CourseId int references CourseInfo(CourseId), + Score int + ) + + + insert into StuInfo values('Tom',19,1,NULL),('Jack',20,1,NULL), + ('Rose',21,1,NULL),('LUlu',19,1,NULL), + ('Lili',21,0,NULL),('abc',20,1,'2007-01-07 01:11:36.590') + + insert into CourseInfo values('JavaBase',4),('HTML',2), + ('JavaScript',2),('SqlBase',2) + + 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) + + --题目: + --1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 + select StuName 姓名,count(CourseId)课程数量,avg(Score)平均分 from ScoreInfo + inner join StuInfo on ScoreInfo.StuId = StuInfo.StuId + group by StuName + --2.查询出每门课程的选修的学生的个数和学生考试的总分 + select CourseName 课程,count(CourseInfo.CourseId),sum(Score)平均分 from CourseInfo + inner join ScoreInfo on CourseInfo.CourseId = ScoreInfo .CourseId + group by CourseName + --3.查询出性别一样并且年龄一样的学生的信息 + select A . * from StuInfo A inner join + (select StuAge,StuSex from StuInfo group by StuAge,StuSex having count(StuAge)>1 and count(StuSex) > 1) + B on A .StuAge = B .StuAge and A .StuSex = B .StuSex + --4.查询出学分一样的课程信息 + select A . CourseName, A .CourseMarks from CourseInfo A,CourseInfo B where A .CourseMarks = B .CourseMarks + and A .CourseId != B.CourseId group by A . CourseName, A .CourseMarks + --5.查询出参加了考试的学生的学号,姓名,课程号和分数 + select StuInfo.StuId 学号, StuName 姓名, ScoreInfo.CourseId 课程号,Score 分数 from ScoreInfo + inner join StuInfo on ScoreInfo.CourseId = StuInfo.StuId + inner join CourseInfo on ScoreInfo.StuId = CourseInfo.CourseId + --6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 + select StuInfo.StuId 学号, StuName 姓名, ScoreInfo.CourseId 课程号,CourseName 课程名,CourseMarks 课程学分,Score 分数 from ScoreInfo + inner join StuInfo on ScoreInfo.CourseId = StuInfo.StuId + inner join CourseInfo on ScoreInfo.StuId = CourseInfo.CourseId + --7.查询出没有参加考试的学生的学号和姓名 + select StuInfo.StuId 学号, StuName 姓名 from ScoreInfo + inner join StuInfo on ScoreInfo.StuId = StuInfo.StuId + where Score = '' + --8.查询出是周六周天来报到的学生 + select * from StuInfo where Time = '' and Time = '' + --9.查询出姓名中有字母a的学生的信息 + select * from StuInfo where StuName like '%a%' + --10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 + select ScoreInfo.StuId 学号,avg(Score)平均分,count(ScoreInfo.CourseId)课程数量 from ScoreInfo + inner join CourseInfo on ScoreInfo.StuId = CourseInfo.CourseId + inner join StuInfo on StuInfo.StuId = ScoreInfo.StuId + group by ScoreInfo.StuId + having avg(Score) > 70 and count(ScoreInfo.CourseId)>2 + + select * from StuInfo + select * from CourseInfo + select * from ScoreInfo diff --git "a/2021 03 30 \344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery1.sql" "b/2021 03 30 \344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8e4de673ecc5072b14f17ca05578cfb8bdff2243 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery1.sql" @@ -0,0 +1,89 @@ +use master +go +create database Students +on +( +name=' Students', +filename='D:\Students.mdf', +size=10, +maxsize=100, +filegrowth=10% +) +log on +( +name=' Students_log', +filename='D:\Students_log.ldf', +size=10, +maxsize=100, +filegrowth=10% + +) +go +use Students +go +create table stuInfo +( +stuID INT PRIMARY KEY IDENTITY(1,1), +stuName varchar(7) unique not null, +stuAge int , +stuSex int check(stuSex in(1,0)) not null, +time datetime +) +create table courseInfo +( +courseID int primary key identity(1,1), +courseName varchar(10) not null, +courseMarks int +) +create table scoreInfo +( +scoreID INT PRIMARY KEY IDENTITY(1,1), +stuID int references stuInfo(stuID) , +courseID int references courseInfo(courseID), +score int not null +) +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-07 11:36.590') +insert into courseInfo values('javabase',4),('html',2),('javascipt',2),('sqbase',2) +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) +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName 学生, count(C.courseID) 课程的数量,avg(score)平均分 from scoreInfo +inner join stuInfo S on scoreInfo.stuID=S.stuID +inner join courseInfo C ON scoreInfo.courseID=C.courseID +group by stuName ORDER BY stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +SELECT C.courseName 课程,count(scoreInfo.courseID) 学生的个数,sum(score)总分 FROM scoreInfo + inner join courseInfo C on scoreInfo.courseID=C.courseID GROUP BY C.courseName + +--3.查询出性别一样并且年龄一样的学生的信息 +SELECT *FROM stuInfo +select A.* from stuInfo A inner join +(SELECT stuAge, stuSex from stuInfo group by stuAge, stuSex having count(stuAge)>1 AND COUNT(stuSex)>1) B +ON A.stuAge=B.stuAge AND A.stuSex=B.stuSex + +--4.查询出学分一样的课程信息 +select A.courseName, A.courseMarks from courseInfo A , courseInfo B WHERE A.courseMarks=B.courseMarks AND A.courseID!=B.courseID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 + +SELECT S.stuID 学号,stuName 姓名,C.courseID 课程号,score 分数 FROM scoreInfo +INNER JOIN courseInfo C ON scoreInfo.courseID=C.courseID +INNER JOIN stuInfo S ON scoreInfo.stuID=S.stuID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +SELECT S.stuID 学号,stuName 姓名,C.courseID 课程号, courseName 课程名,courseMarks 课程学分,score 分数 FROM scoreInfo +INNER JOIN courseInfo C ON scoreInfo.courseID=C.courseID +INNER JOIN stuInfo S ON scoreInfo.stuID=S.stuID +--7.查询出没有参加考试的学生的学号和姓名 +select S.stuID 学号,stuName 姓名 from stuInfo S inner join scoreInfo ON S.stuID=scoreInfo.stuID where score='' +--8.查询出是周六周天来报到的学生 +select *from stuInfo where time='' +--9.查询出姓名中有字母a的学生的信息 +select *from stuInfo where stuName like '%a%' or stuName like '%a' or stuName like 'a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select S.stuID 学生的学号, COUNT(C.courseID) 选修课程的数量,AVG(score)考试平均分 from scoreInfo SI +inner join courseInfo C on SI.courseID=C.courseID +inner join stuInfo S on SI.stuID=S.stuID +group by S.stuID +having COUNT(C.courseID)>2 and AVG(score)>70 \ No newline at end of file diff --git "a/2021 03 30 \344\275\234\344\270\232/\345\210\230\346\257\205/SQLQuery2.sql" "b/2021 03 30 \344\275\234\344\270\232/\345\210\230\346\257\205/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4dc276109f1b5476735a11644163a1ca18c447a4 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\345\210\230\346\257\205/SQLQuery2.sql" @@ -0,0 +1,101 @@ +create database Student +on( +name=Student, +filename='D:\Student.mdf', +size=10mb, +maxsize=50mb, +filegrowth=10% +) +log on +( +name=Student_log, +filename='D:\Student_log.mdf', +size=10mb, +maxsize=50mb, +filegrowth=10% + +) + +create table stuInfo +( +stuID int not null primary key identity(1,1), +stuName nvarchar(10) not null, +stuAge int not null, +stuSex nchar check(stuSex='男' or stuSex ='女') , +time char(50) +) +insert into stuInfo values ('TOM',19,'男',NULL),('JACK',20,'女',NULL),('ROSE',21,'男',NULL),('LULU',19,'男',NULL),('LILI',21,'女',NULL),('ABC',20,'男','2007-01-07 01:11:36:590') +select * from stuInfo +create table courseInfo +( +courseID int primary key identity(1,1) not null, +sourseName nvarchar(10) not null, +courseMarks int + +) +insert into courseInfo values ('SQIBASE',4),('JAVASCRIPT',2),('HTML',2),('JAVABASE',2) +select * from courseInfo +create table scoreInfo +( +scoreID int primary key identity(1,1) not null, +stuID int references stuInfo(stuID) not null, +courseID int references courseInfo(courseID) not null, +score int not null +) +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 + +select * from scoreInfo +select * from stuInfo +select *from courseInfo + + +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuname 学生,count(*)课程数量,AVG(score) 平均分 from scoreinfo + inner join stuInfo on stuInfo.stuid=scoreInfo.stuID + inner join courseInfo on courseInfo. courseID=scoreinfo.courseID + group by stuname +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select soursename 课程,count(*)学生个数,SUM(score) 总分 from scoreInfo +inner join stuInfo on stuInfo.stuid=scoreInfo.stuID + inner join courseInfo on courseInfo. courseID=scoreinfo.courseID + group by sourseName +--3.查询出性别一样并且年龄一样的学生的信息 +select A.* from stuInfo A,stuInfo B +where A.stuAge=B.stuAge and A.stuSex=B.stuSex and A.stuID !=B.stuID +--4.查询出学分一样的课程信息 +select A.* from courseInfo A, courseInfo B +where A.courseMarks=B.courseMarks and A.courseID != B.courseID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuInfo.stuID,stuName,courseID,score from stuInfo +inner join scoreInfo on scoreInfo.stuID=stuInfo.stuID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuInfo.stuID,courseinfo.courseID,score,soursename,courseMarks from stuInfo +inner join scoreInfo on scoreInfo.stuID=stuInfo.stuID +inner join courseInfo on scoreInfo.courseID=courseInfo.courseID + +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuID,stuName from stuInfo full join scoreInfo on stuInfo.stuID =scoreInfo.stuID +where score is null +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuInfo.stuID 学号,avg(score) 考试平均分,count(*) 选修课程的数量 from stuInfo +inner join scoreInfo on scoreInfo.stuID=stuInfo.stuID +inner join courseInfo on scoreInfo.courseID=courseInfo.courseID +group by stuInfo.stuID +HAVING count(stuInfo.stuID +)>2 and avg(score)>70 + diff --git "a/2021 03 30 \344\275\234\344\270\232/\345\210\230\346\261\211\346\226\207/SQLQuery1.sql" "b/2021 03 30 \344\275\234\344\270\232/\345\210\230\346\261\211\346\226\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..184029e1967651147d6ee1932633b944fee25e27 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\345\210\230\346\261\211\346\226\207/SQLQuery1.sql" @@ -0,0 +1,66 @@ +create database Students +on +( +name='Students_data', +filename='D:\新建文件夹.mdf', +size=5mb, +filegrowth=10% +) +go +use Students +go +create table stuInfo +( +Stuid int not null, + Stuname char(20) not null, +stusex bit not null default('1')check(stusex=1 or stusex=0) , +stuage int not null, +time datetime +) +go +create table courseInfo +( +courseld int not null, +courseName char(20) not null, +coursemarks int not null, + +) +go +create table scoreInfo +( +scoreID int not null, +stuid int not null, +courseld int not null, +score int not null +) +go +select * from stuInfo +select * from courseInfo +select * from scoreInfo +insert into stuInfo(Stuid ,Stuname,stuage,stusex) values (1,'Tom',19, 1),(2,'Jack',20,0),(3,'Rose',20,0),(4,'Lulu',10,1),(5,'Lili',21,0),(6,'abc',20,1) + insert into courseInfo(courseld, courseName, coursemarks)values(1,'JavaBase',4),(2,'HTML',2),(3,'JavaScript',2),(4,'SqlBase',2) + insert into scoreInfo (scoreID,stuid,courseld ,score )values(1,1,1,80),(2,1,2,85),(3,1,4,50),(4,2,1,75),(5,2,3,45),(6,2,4,75),(7,3,1,45),(8,4,1,95),(9,4,2,75),(10,4,3,90),(11,4,4,45) + + + --题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select courseld,count(courseName)课程数量,avg(courseMarks)平均分 from courseInfo group by courseld +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select scoreInfo.courseld,coursename,COUNT(stuid),SUM(score) from scoreInfo inner join courseInfo on scoreInfo.courseld=courseInfo.courseld group by scoreInfo.courseld,coursename +--3.查询出性别一样并且年龄一样的学生的信息 +select a.* from stuInfo a , stuInfo b where a.stusex=b.stusex and a.stuage=b.stuage and a.stuid!=b.stuid +--4.查询出学分一样的课程信息 +select distinct a.courseld,a.coursename,a.coursemarks from courseInfo a,courseInfo b where a.coursemarks=b.coursemarks and a.courseld!=b.courseld +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuid,stuname,courseld,score from scoreInfo inner join stuInfo on stuInfo.stuid=scoreInfo.Stuid group by stuid,stuname,courseld,score + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 + select stuid,courseInfo.courseld,coursename,coursemarks,score from scoreInfo inner join courseInfo on scoreInfo.courseld=courseInfo.courseld group by stuid,courseInfo.courseld,coursename,coursemarks,score +--7.查询出没有参加考试的学生的学号和姓名 +select * from stuInfo left join scoreInfo on scoreInfo.stuid=stuInfo.stuId WHERE score IS NULL +--8.查询出是周六周天来报到的学生 +select * from stuInfo where datepart(weekday,time)=1 or datepart(weekday,time)=7 +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuname like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 + select stuid,count(courseld),AVG(score) from scoreInfo group by stuid having COUNT(courseld)>2 and AVG(score)>70 diff --git "a/2021 03 30 \344\275\234\344\270\232/\345\210\230\351\207\221\346\265\267/SQLQuery1.sql" "b/2021 03 30 \344\275\234\344\270\232/\345\210\230\351\207\221\346\265\267/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0c79b400f35b807ad8a78568b144678f7a978233 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\345\210\230\351\207\221\346\265\267/SQLQuery1.sql" @@ -0,0 +1,72 @@ +use master +go +create database Studen +on +( + name='Studen', + filename='D:\Studen.mdf', + size=10, + maxsize=200, + filegrowth=15% +) +log on +( + name='Studen_log', + filename='D:\Studen_log.ldf', + size=10, + maxsize=200, + filegrowth=15% +) +go +use Studen +go +create table stuInfo +( + stuID int primary key identity, + stuName varchar(20) not null, + stuAge varchar(10) , + stuSex int , + time datetime +) +create table courseInfo +( + courseID int primary key identity, + courseName varchar(20) not null, + courseMark varchar(10) +) +create table scoreInfo +( + scoreID int primary key identity, + stuID int , + courseID int , + score int +) +insert into stuInfo(stuName,stuAge,stuSex,time) values ('Tom','19',1,null),('Jack','20',0,null),('Rose','21',1,null),('Lulu','19',1,null),('L','21',0,null),('abc','20',1,getdate()) +select * from stuInfo +insert into courseInfo(courseName,courseMark) values ('JavaBase','4'),('HTML','2'),('JavaScript','2'),('SqlBase','2') +select * from courseInfo +insert into scoreInfo(stuID,courseID,score) 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 stuInfo +select * from courseInfo +select * from scoreInfo +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select ST.stuName 学生姓名,count(courseID) 课程数量,avg(score) 考试平均分 from stuInfo ST inner join scoreInfo SC on ST.stuID=SC.stuID group by SC.stuID,st.stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select CO.courseName 课程名称, count(SO.courseID) 选修的学生的个数 ,sum(score) 考试的总分 from courseInfo CO left join scoreInfo SO on CO.courseID=SO.courseID group by SO.courseID ,CO.courseName +--3.查询出性别一样并且年龄一样的学生的息信 +select * from stuInfo A , stuInfo B where A.stuAge=B.stuAge and A.stuSex=B.stuSex and A.stuID!=B.stuID +--4.查询出学分一样的课程信息 +select distinct * from courseInfo A inner join courseInfo B on A.courseMark=B.courseMark and A.courseName!=B.courseName +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select S.stuID 学号 ,stuName 姓名, C.courseID 课程号, score 分数 from stuInfo S ,courseInfo C ,scoreInfo SC where S.stuID=SC.stuID and C.courseID=SC.courseID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select S.stuID 学号 , C.courseID 课程号,courseName 课程名,courseMark 课程学分, score 分数 from stuInfo S ,courseInfo C ,scoreInfo SC where S.stuID=SC.stuID and C.courseID=SC.courseID +--7.查询出没有参加考试的学生的学号和姓名 + +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select S.stuID 学号, avg(score) 考试平均分 , count(SO.stuID) 选修课程的数量 from stuInfo S inner join scoreInfo SO on S.stuID=SO.stuID group by S.stuID +having avg(score)>70 and count(SO.stuID)>2 \ No newline at end of file diff --git "a/2021 03 30 \344\275\234\344\270\232/\345\217\266\347\234\237/SQLQuery1.sql" "b/2021 03 30 \344\275\234\344\270\232/\345\217\266\347\234\237/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0a4f6f5036ee5f382f7da928ebad81c0856725d3 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\345\217\266\347\234\237/SQLQuery1.sql" @@ -0,0 +1,128 @@ + +create database students +go + +use students +go + +create table stuInfo +( + stuId int identity primary key, + stuName varchar(10) not null , + stuAge char(2) not null, + stuSex char(1) not null check(stuSex = '1' or stuSex = '0') , + time datetime +) +go + +create table courseInfo +( + courseId int identity primary key, + courseName varchar(10) not null, + courseMarks int not null +) +go + +create table scoreInfo +( + scoreId int identity primary key , + stuId int references stuInfo(stuId), + courseId int references courseInfo(courseId), + score int +) +go + + +insert into stuInfo(stuName,stuAge,stuSex,time) +select 'Tom','19','1','' union +select 'Jack','20','0','' union +select 'Rose','21','1','' union +select 'Lulu','19','1','' union +select 'Lili','21','0','' union +select 'abc','20','1','2007-01-07 01:11:36.590' + +insert into courseInfo(courseName,courseMarks) +select 'JavaBase','4' union +select 'HTML','2' union +select 'JavaScript','2' union +select 'SqlBase','2' + +insert into scoreInfo(stuId,courseId,score) +select '1','1','80' union +select '1','2','85' union +select '1','4','50' union +select '2','1','75' union +select '2','3','45' union +select '2','4','75' union +select '3','1','45' union +select '4','1','95' union +select '4','2','75' union +select '4','3','90' union +select '4','4','45' + + + + +--1.查询出 每个学生 所选修的课程的数量和所选修的课程的考试的平均分 + +select si.stuName 学生,COUNT(sci.stuId) 选课数量,AVG(score) 考试平均分 from stuInfo si + inner join scoreInfo sci on si.stuId = sci.stuId + group by si.stuId,stuName + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 + +select courseName 课程名称,COUNT(si.courseId) 学生人数,SUM(score) 总分 from courseInfo ci + inner join scoreInfo si on ci.courseId = si.courseId + group by ci.courseId , courseName + +--3.查询出性别一样并且年龄一样的学生的信息 + +select * from stuInfo A,stuInfo B + where a.stuAge = b.stuAge and a.stuSex = b.stuSex and a.stuId != b.stuId + +--4.查询出学分一样的课程信息 + +select * from courseInfo ci where + ( + select COUNT(*) from courseInfo where + courseMarks = ci.courseMarks + )>1 + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 + +select si.stuId ,stuName ,scoreId ,score from stuInfo si + inner join scoreInfo sci on sci.stuId = si.stuId + group by si.stuId ,stuName ,scoreId ,score + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 + +select si.stuId 学号,ci.courseId 课程号,courseName 课程名,courseMarks 课程学分,score 分数 from scoreInfo sci + inner join courseInfo ci on ci.courseId = sci.courseId + inner join stuInfo si on si.stuId = sci.stuId + +select * from stuInfo +select * from courseInfo +select * from scoreInfo + +--7.查询出没有参加考试的学生的学号和姓名 + + select * from stuInfo si + left join scoreInfo sci on si.stuId=sci.stuId + where score IS NULL + +--8.查询出是周六周天来报到的学生 + +select * from stuInfo + where datepart(weekday,time)=1 or datepart(weekday,time)=7 + +--9.查询出姓名中有字母a的学生的信息 + +select * from stuInfo where stuName like '%a%' + +--10.查询出 选修了2门课程以上 的并且 考试平均分在70以上 的 学生的学号和考试平均分以及选修课程的数量 + +select si.stuId 学号,AVG(score) 考试平均分,COUNT(sci.stuId) 选课数量 from stuInfo si + inner join scoreInfo sci on si.stuId = sci.stuId + group by si.stuId + + diff --git "a/2021 03 30 \344\275\234\344\270\232/\345\217\266\351\231\210\350\276\211/SQLQuery2.sql" "b/2021 03 30 \344\275\234\344\270\232/\345\217\266\351\231\210\350\276\211/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..245c3f2a1be3d385beb62857851fca7843e7553b --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\345\217\266\351\231\210\350\276\211/SQLQuery2.sql" @@ -0,0 +1,127 @@ +create database students +go + +use students +go + +create table stuInfo +( + stuId int identity primary key, + stuName varchar(10) not null , + stuAge char(2) not null, + stuSex char(1) not null check(stuSex = '1' or stuSex = '0') , + time datetime +) +go + +create table courseInfo +( + courseId int identity primary key, + courseName varchar(10) not null, + courseMarks int not null +) +go + +create table scoreInfo +( + scoreId int identity primary key , + stuId int references stuInfo(stuId), + courseId int references courseInfo(courseId), + score int +) +go + + +insert into stuInfo(stuName,stuAge,stuSex,time) + select 'Tom','19','1','' union + select 'Jack','20','0','' union + select 'Rose','21','1','' union + select 'Lulu','19','1','' union + select 'Lili','21','0','' union + select 'abc','20','1','2007-01-07 01:11:36.590' + +insert into courseInfo(courseName,courseMarks) + select 'JavaBase','4' union + select 'HTML','2' union + select 'JavaScript','2' union + select 'SqlBase','2' + +insert into scoreInfo(stuId,courseId,score) + select '1','1','80' union + select '1','2','85' union + select '1','4','50' union + select '2','1','75' union + select '2','3','45' union + select '2','4','75' union + select '3','1','45' union + select '4','1','95' union + select '4','2','75' union + select '4','3','90' union + select '4','4','45' + + + + +--1.查询出 每个学生 所选修的课程的数量和所选修的课程的考试的平均分 + +select si.stuName 学生,COUNT(sci.stuId) 选课数量,AVG(score) 考试平均分 from stuInfo si + inner join scoreInfo sci on si.stuId = sci.stuId + group by si.stuId,stuName + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 + + select courseName 课程名称,COUNT(si.courseId) 学生人数,SUM(score) 总分 from courseInfo ci + inner join scoreInfo si on ci.courseId = si.courseId + group by ci.courseId , courseName + +--3.查询出性别一样并且年龄一样的学生的信息 + + select * from stuInfo A,stuInfo B + where a.stuAge = b.stuAge and a.stuSex = b.stuSex and a.stuId != b.stuId + +--4.查询出学分一样的课程信息 + + select * from courseInfo ci where + ( + select COUNT(*) from courseInfo where + courseMarks = ci.courseMarks + )>1 + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 + + select si.stuId ,stuName ,scoreId ,score from stuInfo si + inner join scoreInfo sci on sci.stuId = si.stuId + group by si.stuId ,stuName ,scoreId ,score + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 + + select si.stuId 学号,ci.courseId 课程号,courseName 课程名,courseMarks 课程学分,score 分数 from scoreInfo sci + inner join courseInfo ci on ci.courseId = sci.courseId + inner join stuInfo si on si.stuId = sci.stuId + + select * from stuInfo + select * from courseInfo + select * from scoreInfo + +--7.查询出没有参加考试的学生的学号和姓名 + + select * from stuInfo si + left join scoreInfo sci on si.stuId=sci.stuId + where score IS NULL + +--8.查询出是周六周天来报到的学生 + + select * from stuInfo + where datepart(weekday,time)=1 or datepart(weekday,time)=7 + +--9.查询出姓名中有字母a的学生的信息 + + select * from stuInfo where stuName like '%a%' + +--10.查询出 选修了2门课程以上 的并且 考试平均分在70以上 的 学生的学号和考试平均分以及选修课程的数量 + + select si.stuId 学号,AVG(score) 考试平均分,COUNT(sci.stuId) 选课数量 from stuInfo si + inner join scoreInfo sci on si.stuId = sci.stuId + group by si.stuId + + diff --git "a/2021 03 30 \344\275\234\344\270\232/\345\217\266\351\242\200/Student.sql" "b/2021 03 30 \344\275\234\344\270\232/\345\217\266\351\242\200/Student.sql" new file mode 100644 index 0000000000000000000000000000000000000000..930b821894feafe7129d8774a7c08ffb1546d3e9 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\345\217\266\351\242\200/Student.sql" @@ -0,0 +1,130 @@ +use master +go + +create database Student +on +( + name ='Student', + filename='D:\test\Student.mdf', + size=5, + maxsize=50, + filegrowth=10 +) +log on +( + name ='Student_log', + filename='D:\test\Student_log.ldf', + size=5, + maxsize=50, + filegrowth=10 +) +go + +use Student +go + +create table StuInfo +( + StuId int primary key identity , + StuName varchar(10) not null , + StuAge int , + StuSex int check (StuSex in (0,1)) , + Time datetime default(null) +) +go + +create table CourseInfo +( + CourseId int primary key identity , + CourseName varchar(10) not null , + CourseMarks int , +) +go + +create table ScoreInfo +( + ScoreId int primary key identity , + StuId int references StuInfo(StuId) , + CourseId int references CourseInfo(CourseId), + Score int +) +go + +insert StuInfo values +('Tom',19,1,''), +('Jack',20,0,''), +('Rose',21,1,''), +('Lulu',19,1,''), +('Lili',21,0,''), +('abc',20,1,'2007-01-07 01:11:36.590') +go + +insert CourseInfo values +('JavaBase',4), +('HTML',2), +('JavaScript',2), +('SqlBase',2) +go + +insert 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) +go + +select StuName as 姓名 ,count(*) as 课程数量,AVG(Score) as 平均分 from ScoreInfo +inner join StuInfo on StuInfo.StuId = ScoreInfo.StuId +group by ScoreInfo.StuId ,StuName +go + +select courseName as 课程, count( *) as 个数 ,sum(Score) as 总分 from ScoreInfo +inner join CourseInfo on CourseInfo.CourseId = ScoreInfo.CourseId +group by ScoreInfo.CourseId, courseName +go + +select A.StuId,A.StuName,A.StuAge,A.StuSex,A.Time from StuInfo A ,StuInfo B +where A.StuAge = B.StuAge and A.StuSex = B.StuSex and A.StuId != B.StuId +go + +select A.CourseId,A.CourseName,A.CourseMarks from CourseInfo A ,CourseInfo B +where A.CourseMarks = B.CourseMarks and A.CourseId != B.CourseId +go + +select A.StuId as 学号,StuName as 姓名, A.CourseId as 课程号,Score as 分数 from ScoreInfo A +inner join CourseInfo B on A.CourseId = B.CourseId +inner join StuInfo C on A.StuId = C.StuId +go + +select A.StuId as 学号,A.CourseId as 课程号,B.CourseName as 课程名,B.CourseMarks as 课程学分,Score as 分数 from ScoreInfo A +inner join CourseInfo B on A.CourseId = B.CourseId +inner join StuInfo C on A.StuId = C.StuId +go + +select StuId as 学号,StuName as 姓名 from StuInfo +except +select A.StuId as 学号,StuName as 姓名 from ScoreInfo A +inner join StuInfo C on A.StuId = C.StuId + +select stuName from StuInfo +where datepart(weekday,Time)=6 or datepart(weekday,Time)=7 +go + +select * from StuInfo +where StuName like '%a%' +go + +select * from ScoreInfo + +select A.StuId as 学号,AVG(Score) as 平均分,count(*) as 课程数量 from ScoreInfo A +inner join StuInfo C on A.StuId = C.StuId +group by A.StuId +having count(*) > 2 and AVG(Score) >70 +go \ No newline at end of file diff --git "a/2021 03 30 \344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery1.sql" "b/2021 03 30 \344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..be3928227ebe029f4c7e9305016fc59273a22e6d --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery1.sql" @@ -0,0 +1,113 @@ +use master +go +create database Students +on +( + name='Students', + filename='D:\新建文件夹6.mdf', + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) +log on +( + name='Students_log', + filename='D:\新建文件夹6_1og.ldf', + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) +go +use Students +go +create table stuInfo +( +stuID int primary key identity, +stuName varchar(10), +stuAge varchar(100), +stuSex varchar(5), +time datetime +) +go +use Students +go +create table courseInfo +( +courseID int, +courseName varchar(10), +courseMarks varchar(10) +) +go +use Students +go +create table scoreInfo +( +scoreID int, +stuID int, +courseID int, +score int +) +insert into stuInfo(stuName,stuAge,stuSex,time) values +('Tom',19,1,null), +('Jake',20,0,null), +('Rose',21,1,null), +('Lulu',19,1,null), +('Lili',21,0,null), +('abc',20,1,'2007-01-07') + +insert into courseInfo values +(1,'JavaBase',4), +(2,'HTML',2), +(3,'JavaScript',2), +(4,'SqlBsse',2) + +insert into scoreInfo values +(1,1,1,80), +(2,1,2,85), +(3,1,4,50), +(4,2,1,75), +(5,2,3,45), +(6,2,4,75), +(7,3,1,45), +(8,4,1,95), +(9,4,2,75), +(10,4,3,90), +(11,4,4,45) + + +--1.查询出 每个学生 所选修的课程的数量 和 所选修的课程 的 考试的平均分 +select T.stuName 学生,T.stuID,count(*)选修的课程的数量,avg(score)考试的平均分 from scoreInfo S inner join courseInfo C on S.courseID=C.courseID inner join stuInfo T on S.stuID=T.stuID group by stuName,T.stuID + +--2.查询出 每门课程 的 选修的学生的个数 和 学生考试的总分 +select C.courseName 课程,COUNT(C.courseID) 选修的学生的个数,sum(score)考试的总分 from scoreInfo S inner join courseInfo C on S.courseID=C.courseID inner join stuInfo T on S.stuID=T.stuID group by C.courseName, C.courseID + +--3.查询出性别一样并且年龄一样的学生的信息 +select distinct A.* from stuInfo A,stuInfo B where A.stuSex=B.stuSex and A.stuAge=B.stuAge and A.stuID!=B.stuID + +--4.查询出学分一样的课程信息 +select distinct A.* from courseInfo A,courseInfo B where A.courseMarks=B.courseMarks and A.courseID!=B.courseID + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select S.stuID 学号,T.stuName 姓名,S.courseID 课程号,S.score 分数 from scoreInfo S left join stuInfo T on S.stuID=T.stuID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select S.stuID 学号,S.courseID 课程号,C.courseName 课程名,S.score 分数 from scoreInfo S left join stuInfo T on S.stuID=T.stuID left join courseInfo C on S.courseID=C.courseID + +--7.查询出没有参加考试的学生的学号和姓名 +select stuID 学号,stuName 姓名 from stuInfo +except +select S.stuID 学号,stuName 姓名 from scoreInfo S inner join stuInfo T on S.stuID=T.stuID + +--8.查询出是周六周天来报到的学生 +select * from stuInfo where time>='2007-01-06' and time<='2007-01-07' + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' + +--10.查询出 选修了2门课程以上的 并且 考试平均分在70以上的 学生的学号 和 考试平均分 以及选修课程的数量 +select stuID 学号,avg(score) 考试平均分,count(*) 选修课程的数量 from scoreInfo group by stuID having count(*)>2 and avg(score)>=70 + + +select * from stuInfo +select * from courseInfo +select * from scoreInfo \ No newline at end of file diff --git "a/2021 03 30 \344\275\234\344\270\232/\345\220\264\346\230\237/1.sql" "b/2021 03 30 \344\275\234\344\270\232/\345\220\264\346\230\237/1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9f54c91fd695855c78c47840da7c6282eb84a69f --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\345\220\264\346\230\237/1.sql" @@ -0,0 +1,130 @@ +use master +go + +create database Student +on +( + name='Student', + filename='D:\Student.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='Student_log', + filename='D:\Student_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use Student +go + + +create table stuInfo +( + stuID int primary key identity(1,1), + stuName char(10), + stuAge int, + stuSex int check(stuSex=1 or stuSex=0), + time datetime, +) +go +create table courseInfo +( + courseID int primary key identity(1,1), + courseName char(20), + courseMarks int, +) +go + +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int +) +go + +insert into stuInfo (stuName,stuAge,stuSex,time) 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-07') + +select * from stuInfo + + +insert into courseInfo (courseName,courseMarks) values ('JavaBase',4), +('HTML',2), +('JavaScript',2), +('SqlBase',2) + +select * from courseInfo + +insert into scoreInfo (stuID,courseID,score) 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) + + + + +--1.查询出 每个学生 所 选修的课程的数量和 所选修的课程的 考试的平均分 +select stuName as 学生姓名,count(scoreInfo.stuID) as 选修的课程数 ,avg(score) as 考试的平均分 from scoreInfo +full join stuInfo on scoreInfo.stuID=stuInfo.stuID +group by stuName + + +--2.查询出 每门课程的 选修的学生的个数和 学生考试的总分 +select courseName,count(scoreInfo.courseID)选修的学生的个数,sum(score)学生考试的总分 from scoreInfo +inner join courseInfo on scoreInfo.courseID=courseInfo.courseID +group by courseName + + +--3.查询出性别一样并且年龄一样的学生的信息 +select A.* from stuInfo A,stuInfo B +where A.stuAge=B.stuAge and A.stuSex=B.stuSex and A.stuID !=B.stuID + +--4.查询出学分一样的课程信息 +select distinct B.* from courseInfo A,courseInfo B +where A.courseMarks=B.courseMarks and A.courseID !=B.courseID and A.courseName !=B.courseName + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuInfo.stuID,stuName,courseID,score from stuInfo +right join scoreInfo on scoreInfo.stuID=stuInfo.stuID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuName,scoreInfo.stuID,scoreInfo.courseID,courseName,courseMarks,score from scoreInfo +inner join courseInfo on scoreInfo.courseID=courseInfo.courseID +inner join stuInfo on stuInfo.stuID=scoreInfo.stuID + + + +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuID,stuName from stuInfo full join scoreInfo on stuInfo.stuID =scoreInfo.stuID +where score is null + + +--8.查询出是周六周天来报到的学生 +select * from stuInfo +where datepart(weekday,time)=1 or datepart(WEEKDAY,time)=7 + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' + + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuID as 学号,count(stuID)选修课程的数量,avg(score)考试平均分 from scoreInfo group by stuID having count(stuID)>2 and avg(score)>70 \ No newline at end of file diff --git "a/2021 03 30 \344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery1.sql" "b/2021 03 30 \344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..dd6aecd7a953aad005ebdc93538060e1bae7832f --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery1.sql" @@ -0,0 +1,152 @@ +use master +go + +create database student +on +( + name='student', + filename='E:\新建文件夹\作业\SQLstudent.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) + +log on +( + name='student_log', + filename='E:\新建文件夹\作业\SQLstudent_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +go + +use student +go + +create table stuInfo +( + stuID int primary key not null , + stuName varchar(10) not null, + stuAge int, + stuSex varchar(1) check(stuSex='0' or stuSex='1'), + stuTime datetime +) +go + +create table courseInfo +( + courseID int primary key identity(1,1) not null, + courseName nvarchar(10) not null, + courseMarks int +) +go + + +create table scoreInfo +( + scoreID int primary key identity(1,1) not null, + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int +) + + +insert into stuInfo(stuID,stuName,stuAge,stuSex,stuTime) +select '1','Tom','19','1',null union +select '2','Jack','20','0',null union +select '3','Rose','21','1',null union +select '4','Lulu','19','1',null union +select '5','Lili','21','0',null union +select '6','abc','20','1','2007-01-07 01:11:36.590' +go + + +insert into courseInfo(courseName,courseMarks) +select 'JavaBase','4' union +select 'HTML','2' union +select 'JavaScript','2' union +select 'SqlBase','2' +go + + + +insert into scoreInfo(stuID,courseID,score) +select '1','1','80'union +select '1','2','85'union +select '1','4','50'union +select '2','1','75'union +select '2','3','45'union +select '2','4','75'union +select '3','1','45'union +select '4','1','95'union +select '4','2','75'union +select '4','3','90'union +select '4','4','45' +go + + + + +select*from stuInfo +select*from courseInfo +select*from scoreInfo + + +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) + + +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select A.stuName 学生,COUNT(B.stuId) 选课数量,AVG(score) 考试平均分 from stuInfo A +inner join scoreInfo B on A.stuId = B.stuId +group by A.stuId,stuName + + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName as 课程, count( *) as 个数 ,sum(Score) as 总分 from scoreInfo +inner join courseInfo on courseInfo.courseId = scoreInfo.courseID +group by scoreInfo.courseID, courseName + + +--3.查询出性别一样并且年龄一样的学生的信息 +select*from stuInfo A,stuInfo B +where A.stuAge=B.StuAge and A.stuSex=B.StuSex and A.StuId != B.StuId + + +--4.查询出学分一样的课程信息 +select A.courseID,A.courseName,A.courseMarks from courseInfo A ,courseInfo B +where A.courseMarks = B.courseMarks and A.courseID != B.courseID + + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select A.stuID as 学号,stuName as 姓名, A.courseID as 课程号,score as 分数 from scoreInfo A +inner join courseInfo B on A.courseId = B.courseID +inner join stuInfo C on A.stuID = C.stuID + + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select C.stuId 学号,C.courseId 课程号,courseName 课程名,courseMarks 课程学分,score 分数 from scoreInfo B +inner join courseInfo C on C.courseId = B.courseId +inner join stuInfo A on A.stuId = B.stuId + + + +--7.查询出没有参加考试的学生的学号和姓名 +select * from stuInfo A +left join scoreInfo B on A.stuId=B.stuId +where score IS NULL + + +--8.查询出是周六周天来报到的学生 +select * from stuInfo +where datepart(weekday,stuTime)=1 or datepart(weekday,stuTime)=7 + + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' + + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select A.stuId 学号,AVG(score) 考试平均分,COUNT(B.stuId) 选课数量 from stuInfo A +inner join scoreInfo B on A.stuId = B.stuId +group by A.stuId diff --git "a/2021 03 30 \344\275\234\344\270\232/\345\224\220\345\207\241\350\276\211/SQLserver09.sql" "b/2021 03 30 \344\275\234\344\270\232/\345\224\220\345\207\241\350\276\211/SQLserver09.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2c2dd437c55e9ee622992913dfdf15a56395ac2d --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\345\224\220\345\207\241\350\276\211/SQLserver09.sql" @@ -0,0 +1,128 @@ + +create database students +go + +use students +go + +create table stuInfo +( + stuId int identity primary key, + stuName varchar(10) not null , + stuAge char(2) not null, + stuSex char(1) not null check(stuSex = '1' or stuSex = '0') , + time datetime +) +go + +create table courseInfo +( + courseId int identity primary key, + courseName varchar(10) not null, + courseMarks int not null +) +go + +create table scoreInfo +( + scoreId int identity primary key , + stuId int references stuInfo(stuId), + courseId int references courseInfo(courseId), + score int +) +go + + +insert into stuInfo(stuName,stuAge,stuSex,time) +select 'Tom','19','1','' union +select 'Jack','20','0','' union +select 'Rose','21','1','' union +select 'Lulu','19','1','' union +select 'Lili','21','0','' union +select 'abc','20','1','2007-01-07 01:11:36.590' + +insert into courseInfo(courseName,courseMarks) +select 'JavaBase','4' union +select 'HTML','2' union +select 'JavaScript','2' union +select 'SqlBase','2' + +insert into scoreInfo(stuId,courseId,score) +select '1','1','80' union +select '1','2','85' union +select '1','4','50' union +select '2','1','75' union +select '2','3','45' union +select '2','4','75' union +select '3','1','45' union +select '4','1','95' union +select '4','2','75' union +select '4','3','90' union +select '4','4','45' + + + + +--1.鏌ヨ鍑 姣忎釜瀛︾敓 鎵閫変慨鐨勮绋嬬殑鏁伴噺鍜屾墍閫変慨鐨勮绋嬬殑鑰冭瘯鐨勫钩鍧囧垎 + +select si.stuName 瀛︾敓,COUNT(sci.stuId) 閫夎鏁伴噺,AVG(score) 鑰冭瘯骞冲潎鍒 from stuInfo si + inner join scoreInfo sci on si.stuId = sci.stuId + group by si.stuId,stuName + +--2.鏌ヨ鍑烘瘡闂ㄨ绋嬬殑閫変慨鐨勫鐢熺殑涓暟鍜屽鐢熻冭瘯鐨勬诲垎 + +select courseName 璇剧▼鍚嶇О,COUNT(si.courseId) 瀛︾敓浜烘暟,SUM(score) 鎬诲垎 from courseInfo ci + inner join scoreInfo si on ci.courseId = si.courseId + group by ci.courseId , courseName + +--3.鏌ヨ鍑烘у埆涓鏍峰苟涓斿勾榫勪竴鏍风殑瀛︾敓鐨勪俊鎭 + +select * from stuInfo A,stuInfo B + where a.stuAge = b.stuAge and a.stuSex = b.stuSex and a.stuId != b.stuId + +--4.鏌ヨ鍑哄鍒嗕竴鏍风殑璇剧▼淇℃伅 + +select * from courseInfo ci where + ( + select COUNT(*) from courseInfo where + courseMarks = ci.courseMarks + )>1 + +--5.鏌ヨ鍑哄弬鍔犱簡鑰冭瘯鐨勫鐢熺殑瀛﹀彿锛屽鍚嶏紝璇剧▼鍙峰拰鍒嗘暟 + +select si.stuId ,stuName ,scoreId ,score from stuInfo si + inner join scoreInfo sci on sci.stuId = si.stuId + group by si.stuId ,stuName ,scoreId ,score + +--6.鏌ヨ鍑哄弬鍔犱簡鑰冭瘯鐨勫鐢熺殑瀛﹀彿锛岃绋嬪彿锛岃绋嬪悕锛岃绋嬪鍒嗗拰鍒嗘暟 + +select si.stuId 瀛﹀彿,ci.courseId 璇剧▼鍙,courseName 璇剧▼鍚,courseMarks 璇剧▼瀛﹀垎,score 鍒嗘暟 from scoreInfo sci + inner join courseInfo ci on ci.courseId = sci.courseId + inner join stuInfo si on si.stuId = sci.stuId + +select * from stuInfo +select * from courseInfo +select * from scoreInfo + +--7.鏌ヨ鍑烘病鏈夊弬鍔犺冭瘯鐨勫鐢熺殑瀛﹀彿鍜屽鍚 + + select * from stuInfo si + left join scoreInfo sci on si.stuId=sci.stuId + where score IS NULL + +--8.鏌ヨ鍑烘槸鍛ㄥ叚鍛ㄥぉ鏉ユ姤鍒扮殑瀛︾敓 + +select * from stuInfo + where datepart(weekday,time)=1 or datepart(weekday,time)=7 + +--9.鏌ヨ鍑哄鍚嶄腑鏈夊瓧姣峚鐨勫鐢熺殑淇℃伅 + +select * from stuInfo where stuName like '%a%' + +--10.鏌ヨ鍑 閫変慨浜2闂ㄨ绋嬩互涓 鐨勫苟涓 鑰冭瘯骞冲潎鍒嗗湪70浠ヤ笂 鐨 瀛︾敓鐨勫鍙峰拰鑰冭瘯骞冲潎鍒嗕互鍙婇変慨璇剧▼鐨勬暟閲 + +select si.stuId 瀛﹀彿,AVG(score) 鑰冭瘯骞冲潎鍒,COUNT(sci.stuId) 閫夎鏁伴噺 from stuInfo si + inner join scoreInfo sci on si.stuId = sci.stuId + group by si.stuId + + diff --git "a/2021 03 30 \344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/sql.sql" "b/2021 03 30 \344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/sql.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fc3bd6c586b033eaf8741a4f6792eae80e7241a8 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/sql.sql" @@ -0,0 +1,93 @@ +create database Student +on( +name=Student, +filename='D:\sql.mdf', +size=10mb, +maxsize=50mb, +filegrowth=10% +) +log on +( +name=sql_log, +filename='D:\sql_log.mdf', +size=10mb, +maxsize=50mb, +filegrowth=10% + +) + +create table stuInfo +( +stuID int not null primary key identity(1,1), +stuName nvarchar(10) not null, +stuAge int not null, +stuSex nchar check(stuSex='鐢' or stuSex ='濂') , +time char(50) +) +insert into stuInfo values ('TOM',19,'鐢',NULL),('JACK',20,'濂',NULL),('ROSE',21,'鐢',NULL),('LULU',19,'鐢',NULL),('LILI',21,'濂',NULL),('ABC',20,'鐢','2007-01-07 01:11:36:590') +select * from stuInfo +create table courseInfo +( +courseID int primary key identity(1,1) not null, +sourseName nvarchar(10) not null, +courseMarks int + +) +insert into courseInfo values ('SQIBASE',4),('JAVASCRIPT',2),('HTML',2),('JAVABASE',2) +select * from courseInfo +create table scoreInfo +( +scoreID int primary key identity(1,1) not null, +stuID int references stuInfo(stuID) not null, +courseID int references courseInfo(courseID) not null, +score int not null +) +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 + +select * from scoreInfo +select * from stuInfo +select *from courseInfo + + +--1.鏌ヨ鍑烘瘡涓鐢熸墍閫変慨鐨勮绋嬬殑鏁伴噺鍜屾墍閫変慨鐨勮绋嬬殑鑰冭瘯鐨勫钩鍧囧垎 +select stuname 瀛︾敓,count(*)璇剧▼鏁伴噺,AVG(score) 骞冲潎鍒 from scoreinfo + inner join stuInfo on stuInfo.stuid=scoreInfo.stuID + inner join courseInfo on courseInfo. courseID=scoreinfo.courseID + group by stuname +--2.鏌ヨ鍑烘瘡闂ㄨ绋嬬殑閫変慨鐨勫鐢熺殑涓暟鍜屽鐢熻冭瘯鐨勬诲垎 +select soursename 璇剧▼,count(*)瀛︾敓涓暟,SUM(score) 鎬诲垎 from scoreInfo +inner join stuInfo on stuInfo.stuid=scoreInfo.stuID + inner join courseInfo on courseInfo. courseID=scoreinfo.courseID + group by sourseName +--3.鏌ヨ鍑烘у埆涓鏍峰苟涓斿勾榫勪竴鏍风殑瀛︾敓鐨勪俊鎭 +select A.* from stuInfo A,stuInfo B +where A.stuAge=B.stuAge and A.stuSex=B.stuSex and A.stuID !=B.stuID +--4.鏌ヨ鍑哄鍒嗕竴鏍风殑璇剧▼淇℃伅 +select A.* from courseInfo A, courseInfo B +where A.courseMarks=B.courseMarks and A.courseID != B.courseID +--5.鏌ヨ鍑哄弬鍔犱簡鑰冭瘯鐨勫鐢熺殑瀛﹀彿锛屽鍚嶏紝璇剧▼鍙峰拰鍒嗘暟 +select stuInfo.stuID,stuName,courseID,score from stuInfo +inner join scoreInfo on scoreInfo.stuID=stuInfo.stuID +--6.鏌ヨ鍑哄弬鍔犱簡鑰冭瘯鐨勫鐢熺殑瀛﹀彿锛岃绋嬪彿锛岃绋嬪悕锛岃绋嬪鍒嗗拰鍒嗘暟 + + +--7.鏌ヨ鍑烘病鏈夊弬鍔犺冭瘯鐨勫鐢熺殑瀛﹀彿鍜屽鍚 + +--8.鏌ヨ鍑烘槸鍛ㄥ叚鍛ㄥぉ鏉ユ姤鍒扮殑瀛︾敓 + +--9.鏌ヨ鍑哄鍚嶄腑鏈夊瓧姣峚鐨勫鐢熺殑淇℃伅 + +--10.鏌ヨ鍑洪変慨浜2闂ㄨ绋嬩互涓婄殑骞朵笖鑰冭瘯骞冲潎鍒嗗湪70浠ヤ笂鐨勫鐢熺殑瀛﹀彿鍜岃冭瘯骞冲潎鍒嗕互鍙婇変慨璇剧▼鐨勬暟閲 +select stuInfo.stuID 瀛﹀彿,avg(score) 鑰冭瘯骞冲潎鍒,count(*) 閫変慨璇剧▼鐨勬暟閲 from stuInfo + diff --git "a/2021 03 30 \344\275\234\344\270\232/\345\274\240\345\256\217/SQLQuery3.sql" "b/2021 03 30 \344\275\234\344\270\232/\345\274\240\345\256\217/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..59d836b460d20e1f0a4b6c0cc286f1d86fd34a91 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\345\274\240\345\256\217/SQLQuery3.sql" @@ -0,0 +1,77 @@ +create database Student02 +on +( + name='Student02', + filename='D:\Student02.mdf', + size=20, + maxsize=300, + filegrowth=50 +) +log on +( + name='Student02_log', + filename='D:\Student02_log.ldf', + size=20, + maxsize=300, + filegrowth=50 +) +go +use Student02 +go +create table StudenInfo +( + stuID int identity(1,1) primary key, + stuName nvarchar(20) not null unique, + stuAge int null , + stuSex int check(stuSex in('1','0')) default('0'), + time datetime +) +insert into StudenInfo(stuName,stuAge,stuSex) values('Tom',19,1),('Jack',20,0),('Rose',21,1),('Lulu',19,1),('Lili',21,0) +insert into StudenInfo(stuName,stuAge,stuSex,time) values('abc',20,1,2007-01-07) + +create table courseInfo +( + courseID int identity(1,1) primary key, + courseName nvarchar(20) not null, + courseMarks int not null +) +insert into courseInfo(courseName,courseMarks) values ('JavaBase',4),('HTML',2),('Javascript',2),('SqlBase',2) + +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int references StudenInfo(stuID), + courseID int references courseInfo(courseID), + score int not null +) + +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 StudenInfo +select * from courseInfo +select * from scoreInfo +--1.查询出 每个学生 所选修的课程的数量和 所选修的课程的考试的平均分 +select stuName 姓名,count(distinct courseID)所选修的课程的数量和,avg(score)平均分 from scoreInfo SC inner join StudenInfo ST on SC.stuID=ST.stuID group by stuName + +--2.查询出 每门课程的选修的学生的个数 和 学生考试的总分 +select courseName 课程名,count(*)每门课程的选修的学生的个数,sum(score)学生考试的总分 from scoreInfo SC inner join courseInfo CO on SC.courseID=CO.courseID group by courseName + +--3.查询出性别一样并且年龄一样的学生的信息 +select A. * from StudenInfo A,StudenInfo B where A.stuSex = B.stuSex and A.stuAge=B.stuAge and A.stuName!=B.stuName + +--4.查询出学分一样的课程信息 +select A. * from scoreInfo A,scoreInfo B where A.score=B.score and A.stuID!=B.stuID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select SC. stuID,stuName,scoreID,score from scoreInfo SC inner join StudenInfo ST on SC.stuID=ST.stuID group by SC. stuID,stuName,scoreID,score +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select SC.scoreID,stuID,score,courseMarks,courseName from scoreInfo SC inner join courseInfo CO on SC.courseID=CO.courseID group by SC. scoreID,stuID,score,courseMarks,courseName +--7.查询出没有参加考试的学生的学号和姓名 +select * from StudenInfo ST left join scoreInfo SC on ST.stuID=SC.stuID where score is null +--8.查询出是周六周天来报到的学生 + select * from StudenInfo where datepart(WEEKDAY,time)=1 or datepart(WEEKDAY,time)=7 +--9.查询出姓名中有字母a的学生的信息 +select * from StudenInfo where stuName like '%a%' +--10.查询出 选修了2门课程以上的 并且考试平均分在70以上的学生的学号 和 考试平均分以及 选修课程的数量 +select ST. stuID,AVG(score),count(ST.stuID) from StudenInfo ST inner join scoreInfo SC on ST.stuID=SC.stuID group by ST.stuID having avg(score)>70 \ No newline at end of file diff --git "a/2021 03 30 \344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery1.sql" "b/2021 03 30 \344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fbdf6512bc43a6419bb0e0bde903bea52413d177 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery1.sql" @@ -0,0 +1,79 @@ +use master +go + +create database aaa +on( + name=aaa, + filename='D:\aaa.mdf', + size=10MB, + filegrowth=10% +) +log on( + name=aaa_log, + filename='D:\aaa_log.ldf', + size=10MB, + filegrowth=10% +) +go + +use aaa +go + +create table stuInfo +( + stuID int primary key identity(1,1), + stuName varchar(10) unique not null, + stuAge int not null, + stuSex int check(stuSex in('1','0')) not null, + stime datetime +) +create table courseInfo +( + courseId int primary key identity(1,1), + courseName varchar(10) unique not null, + courseMarks int not null, +) +create table scoreInfo +( + scoreId int identity(1,1), + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseId), + score int +) + +insert into stuInfo(stuName,stuAge,stuSex,stime) values('Tom','19','1',null),('Jack','20','0',null), +('Rose','21','1',null),('Lulu','19','1',null),('Lili','21','',null),('abc','20','1','2007-01-07 01:11:36.590') +insert into courseInfo(courseName,courseMarks) values('JacaBase','4'),('HTML','2'),('JavaScript','2'),('SqlBase','2') +insert into scoreInfo(stuID,courseID,score) values('1','1','80'),('1','2','85'),('1','4','50'),('2','1','75'), +('2','3','45'),('2','4','75'),('3','1','40'),('4','1','95'),('4','2','75'),('4','3','90'),('4','4','45') + +select * from stuInfo +select * from courseInfo +select * from scoreInfo + +select stuInfo.stuID,count(scoreInfo.courseID)数量,avg(score)平均分 from scoreInfo +inner join stuInfo on stuInfo.stuID=scoreInfo.courseId group by stuName,stuInfo.stuID + +select CourseName,count(courseInfo.CourseID) 数量,sum(score) 总分 from courseInfo +inner join scoreInfo on courseInfo.CourseID=scoreInfo.CourseID group by coursename + +select A.* from stuInfo A,stuInfo B +where A.StuSex = B.StuSex AND A.stuage=B.stuage AND a.StuId<>b.StuId + +select a.CourseName,a.coursemarks from courseInfo A,courseInfo B +WHERE A.coursemarks=B.coursemarks and a.CourseID !=b.CourseID group by a.CourseName,a.coursemarks + +select stuInfo.stuid,stuname,scoreInfo.CourseID,score From stuInfo +inner join scoreInfo on scoreInfo.StuID=stuInfo.StuId + +select scoreInfo.stuid,courseInfo.CourseID,CourseName,coursemarks,Score From courseInfo +inner join scoreInfo on scoreInfo.CourseID=courseInfo.CourseID + +SELECT stuInfo.StuId,stuName FROM stuInfo LEFT JOIN scoreInfo ON stuInfo.StuId=scoreInfo.StuID +except +SELECT stuInfo.StuId,stuName FROM stuInfo inner JOIN scoreInfo ON stuInfo.StuId=scoreInfo.StuID + +select StuId,stuName,stuAge,stuSex from stuInfo where StuName like '%a%' + +select scoreInfo.StuID,avg(Score)平均分,count(scoreInfo.Courseid)课程 from scoreInfo +inner join courseInfo on scoreInfo.courseId=courseInfo.courseId group by scoreInfo.StuID having count(scoreInfo.Courseid)>2 and avg(Score)>70 \ No newline at end of file diff --git "a/2021 03 30 \344\275\234\344\270\232/\345\276\220\351\252\217\351\271\217/xjp.sql" "b/2021 03 30 \344\275\234\344\270\232/\345\276\220\351\252\217\351\271\217/xjp.sql" new file mode 100644 index 0000000000000000000000000000000000000000..494601e9729b4892f6d03c10c9ff10ff830b31b3 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\345\276\220\351\252\217\351\271\217/xjp.sql" @@ -0,0 +1,86 @@ +use master +go +create database bbs +on +( + name='bbs', + filename='D:\bbs.mdf', + size=10, + maxsize=200, + filegrowth=20 +) +log on +( + name='bbs_log', + filename='D:\bbs_log.ldf ', + size=10, + maxsize=200, + filegrowth=20 +) +go +use bbs + +create table stuInfo +( + stuld int primary key identity not null, + stuname varchar(20) not null, + stuage varchar(20) not null, + stusex varchar(2) check (stusex in ('1','0'))not null, + time datetime default('null') +) +go +create table courseInfo +( + + courseid int primary key identity not null, + coursename varchar(20) not null, + coursemarks int not null +) +go +create table scoreInfo +( + + scoreid int primary key identity not null, + stuid int references stuInfo(stuld) not null, + courseld int references courseInfo(courseid) not null, + score int not null + + +) +go + + insert into stuInfo values ('tom','19','1',''),('jack','20','0',''),('rose','21','1',''),('lulu','19','1',''),('lili','21','0',''),('abc','20','1','2007-01-07') + + + 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 stuid,stuname,count(courseld),avg(score) from scoreInfo inner join stuInfo on scoreInfo.stuid=stuInfo.stuld group by stuid,stuname +--2.查询出每门课程的选修的学生的个数和学生考试的总分 + + select scoreInfo.courseld,coursename,COUNT(stuid),SUM(score) from scoreInfo inner join courseInfo on scoreInfo.courseld=courseInfo.courseid group by scoreInfo.courseld,coursename +--3.查询出性别一样并且年龄一样的学生的信息 +selecyA.* from bbUsers A. bbUsers B where A. uAge AND A.uSex=B.uSex and A.UID=B.UID +--4.查询出学分一样的课程信息 +select distinct a.courseid,a.coursename,a.coursemarks from courseInfo a,courseInfo b where a.coursemarks=b.coursemarks and a.courseid!=b.courseid + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 + +--7.查询出没有参加考试的学生的学号和姓名 + +--8.查询出是周六周天来报到的学生 +select * from stuInfo where datepart(weekday,time)=1 or datepart(weekday,time)=7 +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuname like'%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuid, countr(courseld),AVG(scorr)from scoreInfo group by stuid having COUNT(courseld)>2 and AVG(score)>70 \ No newline at end of file diff --git "a/2021 03 30 \344\275\234\344\270\232/\346\226\271\350\215\243\346\230\237/SQLQuery1.sql" "b/2021 03 30 \344\275\234\344\270\232/\346\226\271\350\215\243\346\230\237/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..461c44f4df04768c22eb6d0fde66d3129ce8188c --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\346\226\271\350\215\243\346\230\237/SQLQuery1.sql" @@ -0,0 +1,92 @@ + use master + go + create database student + on + ( + name=Student, + filename='D:\Student.mdf', + size=10MB, + maxsize=50MB, + filegrowth=10% + ) + log on + ( + name=Student_log, + filename='D:\Student_log.ldf', + size=10MB, + maxsize=50MB, + filegrowth=10% + ) + go + use student + go + create table stuInfo + ( + stuID int primary key identity(1,1), + stuName nvarchar(5), + stuAge int , + stuSex int check(stuSex=1or stuSex=0) not null, + time datetime + ) + go + create table courseInfo + ( + courseID int primary key identity(1,1), + courseName nvarchar(10), + courseMakes char(1) + ) + go + create table scoreInfo + ( + scoreID int primary key identity(1,1), + stuID int , + courseID int , + score int + ) + go + insert into stuInfo values ('Tom',19,1,null) + insert into stuInfo values ('Jack',20,0,null), + ('Rose',21,1,null),('Lulu',19,1,null),('Lil',21,0,null), + ('abc',20,1,'2007-01-07') + insert into courseInfo values ('JavaBase',4) + insert into courseInfo values ('HTML',2),('Javascript',2), + ('sqlBase',2) + insert into scoreInfo values (1,1,80) + + insert into scoreInfo values (1,2,85),(1,4,50),(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) + + --1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 + select stuName as 名字 ,COUNT(*) as 选修数量, AVG(score) from scoreInfo + inner join stuInfo on scoreInfo.stuID=stuInfo.stuID + group by stuName + select * from courseInfo + select * from stuInfo + select * from scoreInfo + --2.查询出每门课程的选修的学生的个数和学生考试的总分 + select courseName,COUNT(scoreInfo.courseID),SUM(score) from scoreInfo + inner join courseInfo on scoreInfo.courseID=courseInfo.courseID + group by courseName ,scoreInfo.courseID + +--3.查询出性别一样并且年龄一样的学生的信息 +select A.stuName,A.stuAge,A.stuSex from stuInfo A,stuInfo B where A.stuAge=B.stuAge and A.stuSex=B.stuSex and A.stuName!=B.stuName + +--4.查询出学分一样的课程信息 +select distinct A.* from courseInfo A,courseInfo B where A.courseMakes=B.courseMakes and A.courseID!=B.courseID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select st.stuID as 学号,st.stuName as 姓名,c.courseID as 课程号,s.score as 分数 from scoreInfo S +inner join courseInfo C on s.courseID=c.courseID +inner join stuInfo st on st.stuID=s.stuID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select st.stuID as 学号,s.courseID as 课程号,c.courseName as 课程名 ,c.courseMakes as 课程学分,s.score as 分数 from scoreInfo S +inner join courseInfo C on s.courseID=c.courseID +inner join stuInfo st on st.stuID=s.stuID +--7.查询出没有参加考试的学生的学号和姓名 + select * from stuInfo left join scoreInfo on scoreInfo.stuid=stuInfo.stuID WHERE score IS NULL +--8.查询出是周六周天来报到的学生 +select * from stuInfo where datepart(weekday,time)=1 or datepart(weekday,time)=7 +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuid,count(courseID),AVG(score) from scoreInfo group by stuid having COUNT(courseID)>2 and AVG(score)>70 diff --git "a/2021 03 30 \344\275\234\344\270\232/\346\226\275\346\261\237\345\263\260/SQLQuery1.sql" "b/2021 03 30 \344\275\234\344\270\232/\346\226\275\346\261\237\345\263\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d1143527d9c5f7dc73cacec3ceaa242690b3076e --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\346\226\275\346\261\237\345\263\260/SQLQuery1.sql" @@ -0,0 +1,83 @@ +use master +go +create database bbs +on +( + name='bbs', + filename='D:\bbs.mdf', + size=10, + maxsize=200, + filegrowth=20 +) +log on +( + name='bbs_log', + filename='D:\bbs_log.ldf ', + size=10, + maxsize=200, + filegrowth=20 +) +go +use bbs + +create table stuInfo +( + stuld int primary key identity not null, + stuname varchar(20) not null, + stuage varchar(20) not null, + stusex varchar(2) check (stusex in ('1','0'))not null, + time datetime default('null') +) +go +create table courseInfo +( + + courseid int primary key identity not null, + coursename varchar(20) not null, + coursemarks int not null +) +go +create table scoreInfo +( + + scoreid int primary key identity not null, + stuid int references stuInfo(stuld) not null, + courseld int references courseInfo(courseid) not null, + score int not null + + +) +go + + insert into stuInfo values ('tom','19','1',''),('jack','20','0',''),('rose','21','1',''),('lulu','19','1',''),('lili','21','0',''),('abc','20','1','2007-01-07') + + + 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 + + select stuid,stuname,count(courseld),avg(score) from scoreInfo inner join stuInfo on scoreInfo.stuid=stuInfo.stuld group by stuid,stuname + + select scoreInfo.courseld,coursename,COUNT(stuid),SUM(score) from scoreInfo inner join courseInfo on scoreInfo.courseld=courseInfo.courseid group by scoreInfo.courseld,coursename + + select a.* from stuInfo a , stuInfo b where a.stusex=b.stusex and a.stuage=b.stuage and a.stuld!=b.stuld + + select distinct a.courseid,a.coursename,a.coursemarks from courseInfo a,courseInfo b where a.coursemarks=b.coursemarks and a.courseid!=b.courseid + + select stuid,stuname,courseld,score from scoreInfo inner join stuInfo on stuInfo.stuld=scoreInfo.stuid group by stuid,stuname,courseld,score + + select stuid,courseInfo.courseid,coursename,coursemarks,score from scoreInfo inner join courseInfo on scoreInfo.courseld=courseInfo.courseid group by stuid,courseInfo.courseid,coursename,coursemarks,score + + select * from stuInfo left join scoreInfo on scoreInfo.stuid=stuInfo.stuld WHERE score IS NULL + +select * from stuInfo where datepart(weekday,time)=1 or datepart(weekday,time)=7 + + select * from stuInfo where stuname like '%a%' + + select stuid,count(courseld),AVG(score) from scoreInfo group by stuid having COUNT(courseld)>2 and AVG(score)>70 \ No newline at end of file diff --git "a/2021 03 30 \344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/Students.sql" "b/2021 03 30 \344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/Students.sql" new file mode 100644 index 0000000000000000000000000000000000000000..303d18115343b07507951b5a7cbaa58ed8bd82ee --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/Students.sql" @@ -0,0 +1,130 @@ +use master +go + +create database Student +on +( + name='Student', + filename='D:\Student.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='Student_log', + filename='D:\Student_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use Student +go + + +create table stuInfo +( + stuID int primary key identity(1,1), + stuName char(10), + stuAge int, + stuSex int check(stuSex=1 or stuSex=0), + time datetime, +) +go +create table courseInfo +( + courseID int primary key identity(1,1), + courseName char(20), + courseMarks int, +) +go + +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int +) +go + +insert into stuInfo (stuName,stuAge,stuSex,time) 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-07') + +select * from stuInfo + + +insert into courseInfo (courseName,courseMarks) values ('JavaBase',4), +('HTML',2), +('JavaScript',2), +('SqlBase',2) + +select * from courseInfo + +insert into scoreInfo (stuID,courseID,score) 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) + + + + +--1.查询出 每个学生 所 选修的课程的数量和 所选修的课程的 考试的平均分 +select stuName as 学生姓名,count(scoreInfo.stuID) as 选修的课程数 ,avg(score) as 考试的平均分 from scoreInfo +full join stuInfo on scoreInfo.stuID=stuInfo.stuID +group by stuName + + +--2.查询出 每门课程的 选修的学生的个数和 学生考试的总分 +select courseName,count(scoreInfo.courseID)选修的学生的个数,sum(score)学生考试的总分 from scoreInfo +inner join courseInfo on scoreInfo.courseID=courseInfo.courseID +group by courseName + + +--3.查询出性别一样并且年龄一样的学生的信息 +select A.* from stuInfo A,stuInfo B +where A.stuAge=B.stuAge and A.stuSex=B.stuSex and A.stuID !=B.stuID + +--4.查询出学分一样的课程信息 +select distinct B.* from courseInfo A,courseInfo B +where A.courseMarks=B.courseMarks and A.courseID !=B.courseID and A.courseName !=B.courseName + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuInfo.stuID,stuName,courseID,score from stuInfo +right join scoreInfo on scoreInfo.stuID=stuInfo.stuID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuName,scoreInfo.stuID,scoreInfo.courseID,courseName,courseMarks,score from scoreInfo +inner join courseInfo on scoreInfo.courseID=courseInfo.courseID +inner join stuInfo on stuInfo.stuID=scoreInfo.stuID + + + +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuID,stuName from stuInfo full join scoreInfo on stuInfo.stuID =scoreInfo.stuID +where score is null + + +--8.查询出是周六周天来报到的学生 +select * from stuInfo +where datepart(weekday,time)=1 or datepart(WEEKDAY,time)=7 + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' + + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuID as 学号,count(stuID)选修课程的数量,avg(score)考试平均分 from scoreInfo group by stuID having count(stuID)>2 and avg(score)>70 diff --git "a/2021 03 30 \344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/SQLQuery0330.sql" "b/2021 03 30 \344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/SQLQuery0330.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2a5e3ef14da6f431843bb69f5f054a13f206c893 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/SQLQuery0330.sql" @@ -0,0 +1,126 @@ +use master +go + +create database student +on +( + name='student', + filename='D:\student.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='student_log', + filename='D:\test\student_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +use student +go +create table stuInfo +( + stuID int identity not null, + stuName varchar(10) not null, + stuAge int not null, + stuSex varchar not null, + time datetime default(null) +) +create table courseInfo +( + courseID int identity not null, + courseName varchar(20) not null, + courseMarks varchar(10) not null +) +create table scoreInfo +( + scoreID int identity not null, + stuiD varchar(10) not null, + courseID varchar(10) not null, + score int not null +) +insert stuInfo values +('Tom' , 19 , 1 , ''), +('Jack' , 20 , 0 , ''), +('Rose' , 21 , 1 , ''), +('Lulu' , 19 , 1 , ''), +('Lili' , 21 , 0 , ''), +('abc' , 20 , 1 , '2007-01-07 01:11:36.590') +go + +insert courseInfo values +('JavaBase' , 4), +('HTML' , 2), +('JavaScipt' , 2), +('SqlBse' , 2) +go + +insert 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) +go + +select * from stuInfo +select * from courseInfo +select * from scoreInfo + +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName , COUNT(*) as 课程数量 , AVG(score) as 平均数 from scoreInfo +inner join stuInfo on stuInfo.stuID = scoreInfo.stuID +group by scoreInfo.stuID,stuName +go + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName , COUNT(*) as 个数 , SUM(score) as 总分 from scoreInfo +inner join courseInfo on courseInfo.courseID = scoreInfo.courseID +group by scoreInfo.courseID , courseName +go + +--3.查询出性别一样并且年龄一样的学生的信息 +select A . * from stuInfo A , stuInfo B where A.stuAge=B.stuAge AND A.stuSex=B.stuSex AND A.stuID !=B.stuID + +--4.查询出学分一样的课程信息 +select A.courseID,A.courseName,A.courseMarks from courseInfo A ,courseInfo B +where A.courseMarks = B.courseMarks and A.courseID != B.courseID +go + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select A.stuID as 学号,stuName as 姓名, A.courseID as 课程号,score as 分数 from scoreInfo A +inner join courseInfo B on A.courseID = B.courseID +inner join stuInfo C on A.stuID = C.stuID +go + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select A.stuID as 学号,A.courseID as 课程号,B.courseName as 课程名,B.courseMarks as 课程学分,score as 分数 from scoreInfo A +inner join courseInfo B on A.courseID = B.courseID +inner join stuInfo C on A.stuID = C.stuID +go + +--7.查询出没有参加考试的学生的学号和姓名 +select stuID as 学号,stuName as 姓名 from stuInfo +except +select A.stuID as 学号,stuName as 姓名 from scoreInfo A +inner join stuInfo C on A.stuID = C.stuID + +--8.查询出是周六周天来报到的学生 +select stuName from stuInfo +where datepart(weekday,Time)=6 or datepart(weekday,Time)=7 +go + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo +where stuName like '%a%' +go + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select A.stuID as 学号,AVG(score) as 平均分,COUNT(*) as 课程数量 from scoreInfo A +inner join stuInfo C on A.stuID = C.stuID +group by A.stuID +having COUNT(*) > 2 and AVG(score) >70 +go \ No newline at end of file diff --git "a/2021 03 30 \344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/\344\275\234\344\270\2321.sql" "b/2021 03 30 \344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/\344\275\234\344\270\2321.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d9031cf54764db3e4610c996ed4abf70bb9dcfcb --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/\344\275\234\344\270\2321.sql" @@ -0,0 +1,78 @@ +create database Student +on +( +name='Student', +filename='D:\Student.mdf', +size=5MB, +filegrowth=2MB, +maxsize=100MB +) +log on +( +name='Student-db', +filename='D:\Student-db.ldf', +size=5MB, +filegrowth=2MB, +maxsize=20MB +) +use student +go +create table Students +( +StuId int primary key identity(1,1) not null, +StuName nvarchar(20) not null, +stuage varchar(2) not null, +StuSex nvarchar(1) default('男')check(StuSex='男'or StuSex='女') not null, +time smalldatetime +) +create table Course +( +CourseID int primary key identity(1,1) not null, +CourseName nvarchar(50) not null, +coursemarks int not null +) +create table Score +( +ScoreID int identity(1,1), +StuID int references Students(StuId) not null, +CourseID int references Course(CourseID) not null, +Score int not null +) +insert into Students (StuName,stuage,StuSex,time) +select 'TOM',19,'男',null union +select 'Jack',20,'女',null union +select 'Rose',21,'男',null union +select 'Lulu',19,'男',null union +select 'Lili',21,'女',null union +select 'abc',20,'男','2007-01-07 01:11:36.590' +insert into Course (CourseName,coursemarks) values ('Javabase',4),('html',2),('javascript',2),('sqlbase',2) +insert into Score(StuID,CourseID,Score)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) +--有如图所示的三张表结构,学生信息表(Students),课程信息表(Course),分数信息表(Score) +select * from Students +select * from Course +select * from Score +--题目: +--1.查询出 每个学生 所选修的 课程的数量 和 所选修的课程的考试的平均分 +select StuName 学生姓名,count(Score.StuID) 所选课程数量,avg(Score) 平均分 from Students inner join Score on Students.StuId = Score.StuID group by StuName +--2.查询出每门课程的选修的学生的个数 和学生考试 的总分 +select CourseName,count(Score.CourseID) 选择该课程的学生个数,sum(score) 总分 from Course inner join Score on Course.CourseID=Score.CourseID group by coursename +--3.查询出性别一样并且年龄一样的学生的信息 +select A.* from Students A,Students B where A.StuSex = B.StuSex AND A.stuage=B.stuage AND a.StuId<>b.StuId +--4.查询出学分一样的课程信息 +select a.CourseID,a.CourseName,a.coursemarks from Course A,Course B WHERE A.coursemarks=B.coursemarks and a.CourseID !=b.CourseID group by a.CourseID,a.CourseName,a.coursemarks +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select Students.stuid,stuname,SCORE.CourseID,Score From Students inner join Score on Score.StuID=Students.StuId +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select Score.stuid,Course.CourseID,CourseName,coursemarks,Score From Course inner join Score on Score.CourseID=Course.CourseID +--7.查询出没有参加考试的学生的学号和姓名 +SELECT Students.StuId,StuName FROM Students LEFT JOIN Score ON Students.StuId=Score.StuID +except +SELECT Students.StuId,StuName FROM Students inner JOIN Score ON Students.StuId=Score.StuID +--8.查询出是周六周天来报到的学生 +select * from Students where datepart(weekday,time)=1 or datepart(weekday,time)=7 +--9.查询出姓名中有字母a的学生的信息 +select StuId,StuName,stuage,StuSex from Students where StuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select Score.StuID,avg(Score)平均成绩,count(Score.Courseid)选修课数量 from Score inner join Course on Score.courseId=Course.courseId group by Score.StuID +having count(Score.Courseid)>2 and avg(Score)>70 diff --git "a/2021 03 30 \344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery2.sql" "b/2021 03 30 \344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d4937efd0ae4d303eeb26b56779f36f6426ab7ff --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery2.sql" @@ -0,0 +1,90 @@ +use master +go + +create database dong +on +( +name='dong', +filename='D:\test\dong.mdf', +size=5mb, +maxsize=50mb, +filegrowth=10% +) +log on +( +name='dong_log', +filename='D:\test\dong_log.ldf', +size=5mb, +maxsize=50mb, +filegrowth=10% +) + +go +use dong + +go +create table stuInfo +( +stuID int primary key identity(1,1), +stuName nvarchar(20) not null, +stuAge int not null, +stuSex nchar(2) default('1') check(stuSex='1' or stuSex='0'), +time datetime +) + +go +create table courseInfo +( +courseID int primary key identity(1,1), +courseName nvarchar(20) not null, +courseMarks int + +) + +go +create table scoreInfo +( +scoreID int primary key identity(1,1), +stuID int references stuInfo(stuID), +courseID int references courseInfo(courseID), +score int +) + +go +insert into stuInfo (stuName,stuAge,stuSex) values ('Tom',19,1),('Jack',20,0),('Rose',21,0),('Lulu',19,1),('Lili',21,0),('abc',20,1) + +go +update stuInfo set time='2007-01-07 01:11:36.590' where stuID=6 + +go +insert into courseInfo (courseName,courseMarks) values ('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) + +go +insert into scoreInfo (stuID,courseID,score) 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) + +go +select * from stuInfo +select * from courseInfo +select * from scoreInfo + +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName 学生名字,count(scoreInfo.stuID) 选修课程数量,avg(score) 选修的课程平均分 from stuInfo inner join scoreInfo on scoreInfo.stuID = stuInfo.stuID group by stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName 课程名字,count(scoreInfo.courseID) 学生的个数,sum(score) 总分 from courseInfo inner join scoreInfo on courseInfo.courseID = scoreInfo.courseID group by courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select A. * from stuInfo A,stuInfo B where A.stuAge=B.stuAge and A.stuSex=B.stuSex and A.stuID!=B.stuID +--4.查询出学分一样的课程信息 +select A. * from courseInfo A,courseInfo B where A.courseMarks=B.courseMarks and A.courseID!=B.courseID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuInfo.stuID 学号,stuName 姓名,courseInfo.courseID 课程号,score 分数 from stuInfo inner join scoreInfo on scoreInfo.stuID=stuInfo.stuID inner join courseInfo on courseInfo.courseID=scoreInfo.courseID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuInfo.stuID 学号,courseInfo.courseID 课程号,courseName 课程名,courseMarks 课程学分,score 分数 from stuInfo inner join scoreInfo on scoreInfo.stuID=stuInfo.stuID inner join courseInfo on courseInfo.courseID=scoreInfo.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select stuID 学号,stuName 姓名 from stuInfo except select stuInfo.stuID 学号,stuName 姓名 from scoreInfo inner join stuInfo on scoreInfo.stuID=stuInfo.stuID group by stuInfo.stuID,stuName +--8.查询出是周六周天来报到的学生 +select * from stuInfo where time between '2007-01-05' and '2007-01-08' +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like'%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuInfo.stuID 学生ID,COUNT(*) 课程数量,AVG(score) 考试平均分数 from stuInfo inner join scoreInfo on scoreInfo.stuID=stuInfo.stuID group by stuInfo.stuID having AVG(score)>70 and COUNT(*)>2 \ No newline at end of file diff --git "a/2021 03 30 \344\275\234\344\270\232/\346\235\250\345\270\206/Student.sql" "b/2021 03 30 \344\275\234\344\270\232/\346\235\250\345\270\206/Student.sql" new file mode 100644 index 0000000000000000000000000000000000000000..930b821894feafe7129d8774a7c08ffb1546d3e9 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\346\235\250\345\270\206/Student.sql" @@ -0,0 +1,130 @@ +use master +go + +create database Student +on +( + name ='Student', + filename='D:\test\Student.mdf', + size=5, + maxsize=50, + filegrowth=10 +) +log on +( + name ='Student_log', + filename='D:\test\Student_log.ldf', + size=5, + maxsize=50, + filegrowth=10 +) +go + +use Student +go + +create table StuInfo +( + StuId int primary key identity , + StuName varchar(10) not null , + StuAge int , + StuSex int check (StuSex in (0,1)) , + Time datetime default(null) +) +go + +create table CourseInfo +( + CourseId int primary key identity , + CourseName varchar(10) not null , + CourseMarks int , +) +go + +create table ScoreInfo +( + ScoreId int primary key identity , + StuId int references StuInfo(StuId) , + CourseId int references CourseInfo(CourseId), + Score int +) +go + +insert StuInfo values +('Tom',19,1,''), +('Jack',20,0,''), +('Rose',21,1,''), +('Lulu',19,1,''), +('Lili',21,0,''), +('abc',20,1,'2007-01-07 01:11:36.590') +go + +insert CourseInfo values +('JavaBase',4), +('HTML',2), +('JavaScript',2), +('SqlBase',2) +go + +insert 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) +go + +select StuName as 姓名 ,count(*) as 课程数量,AVG(Score) as 平均分 from ScoreInfo +inner join StuInfo on StuInfo.StuId = ScoreInfo.StuId +group by ScoreInfo.StuId ,StuName +go + +select courseName as 课程, count( *) as 个数 ,sum(Score) as 总分 from ScoreInfo +inner join CourseInfo on CourseInfo.CourseId = ScoreInfo.CourseId +group by ScoreInfo.CourseId, courseName +go + +select A.StuId,A.StuName,A.StuAge,A.StuSex,A.Time from StuInfo A ,StuInfo B +where A.StuAge = B.StuAge and A.StuSex = B.StuSex and A.StuId != B.StuId +go + +select A.CourseId,A.CourseName,A.CourseMarks from CourseInfo A ,CourseInfo B +where A.CourseMarks = B.CourseMarks and A.CourseId != B.CourseId +go + +select A.StuId as 学号,StuName as 姓名, A.CourseId as 课程号,Score as 分数 from ScoreInfo A +inner join CourseInfo B on A.CourseId = B.CourseId +inner join StuInfo C on A.StuId = C.StuId +go + +select A.StuId as 学号,A.CourseId as 课程号,B.CourseName as 课程名,B.CourseMarks as 课程学分,Score as 分数 from ScoreInfo A +inner join CourseInfo B on A.CourseId = B.CourseId +inner join StuInfo C on A.StuId = C.StuId +go + +select StuId as 学号,StuName as 姓名 from StuInfo +except +select A.StuId as 学号,StuName as 姓名 from ScoreInfo A +inner join StuInfo C on A.StuId = C.StuId + +select stuName from StuInfo +where datepart(weekday,Time)=6 or datepart(weekday,Time)=7 +go + +select * from StuInfo +where StuName like '%a%' +go + +select * from ScoreInfo + +select A.StuId as 学号,AVG(Score) as 平均分,count(*) as 课程数量 from ScoreInfo A +inner join StuInfo C on A.StuId = C.StuId +group by A.StuId +having count(*) > 2 and AVG(Score) >70 +go \ No newline at end of file diff --git "a/2021 03 30 \344\275\234\344\270\232/\346\235\250\345\270\206/Tbl.sql" "b/2021 03 30 \344\275\234\344\270\232/\346\235\250\345\270\206/Tbl.sql" new file mode 100644 index 0000000000000000000000000000000000000000..565dc542b99d02f41fe032c115cb7c72a2a92e0d --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\346\235\250\345\270\206/Tbl.sql" @@ -0,0 +1,151 @@ +use master +go + +create database Tbl +on +( + name ='Tbl', + filename='D:\test\Tbl.mdf', + size=5, + maxsize=50, + filegrowth=10 +) +log on +( + name ='Tbl_log', + filename='D:\test\Tblt_log.ldf', + size=5, + maxsize=50, + filegrowth=10 +) +go + +use Tbl +go + +create table Tbl_card +( + CardId varchar(10) primary key , + PassWord varchar(10) not null , + Balance int , + UserName varchar(10) not null +) +go + +create table Tbl_computer +( + ComputerId varchar(10) primary key , + OnUse int check(OnUse in(0,1)) not null , + Note varchar(100) +) +go + +create table Tbl_record +( + RecordId int primary key , + CardId varchar(10) references Tbl_Card(CardId) , + ComputerId varchar(10) references Tbl_computer(ComputerId) , + BeginTime datetime , + EndTime datetime , + Fee int +) +go + +insert 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,'张峻') +go + +insert Tbl_computer values +('02',0,'25555'), +('03',1,'55555'), +('04',0,'66666'), +('05',1,'88888'), +('06',0,'688878'), +('B01',0,'558558') +go + +insert 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-22 22:55:00',50), +(48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00',6), +(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00',50), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',30), +(65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00',23), +(98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00',23) +go + +select +A.CardId as 卡号,UserName as 用户名,ComputerId as 机器编号, +BeginTime as 开始时间,EndTime as 结束时间,Fee as 消费金额 +from Tbl_record A +inner join Tbl_card B on A.CardId = B.CardId +where UserName = '张军' +order by Fee desc +go + +select ComputerId,count(ComputerId) as 上网次数,sum(Fee) as 总金额 from Tbl_record +group by ComputerId +go + +select A.CardId,sum(Fee) as 总金额 from Tbl_record A +inner join Tbl_card B on A.CardId = B.CardId +group by A.CardId +go + +select B.CardId,B.UserName as 总金额 from Tbl_record A +right join Tbl_card B on A.CardId = B.CardId +except +select A.CardId,UserName as 总金额 from Tbl_record A +inner join Tbl_card B on A.CardId = B.CardId +go + +select A.CardId,A.PassWord,A.Balance,A.UserName from Tbl_card A ,Tbl_card B +where A.PassWord = B.PassWord and A.PassWord = B.UserName +go + +select top 1 ComputerId as 机器号,count(RecordId) as 使用次数 from Tbl_record +group by ComputerId +order by count(RecordId) desc +go + +select A.CardId,UserName as 用户名,ComputerId as 卡号,Fee as 消费金额 from Tbl_record A +inner join Tbl_card B on A.CardId = B.CardId +where A.CardId like ('%ABC') +go + +select +A.CardId,UserName as 用户名,ComputerId as 机器号, +BeginTime as 开始时间,EndTime as 结束时间,Fee as 消费金额 +from Tbl_record A +inner join Tbl_card B on A.CardId = B.CardId +where datepart(weekday,BeginTime)=1 or datepart(weekday,BeginTime)=7 +go + +select +A.CardId,UserName as 用户名,ComputerId as 机器号, +BeginTime as 开始时间,EndTime as 结束时间,Fee as 消费金额 +from Tbl_record A +inner join Tbl_card B on A.CardId = B.CardId +where DATEDIFF(HH,BeginTime,EndTime) > 12 +go + +select +A.CardId,UserName as 用户名,ComputerId as 机器号, +BeginTime as 开始时间,EndTime as 结束时间,Fee as 消费金额 +from Tbl_record A +inner join Tbl_card B on A.CardId = B.CardId +except +select top 3 +A.CardId,UserName as 用户名,ComputerId as 机器号, +BeginTime as 开始时间,EndTime as 结束时间,Fee as 消费金额 +from Tbl_record A +inner join Tbl_card B on A.CardId = B.CardId +order by Fee desc diff --git "a/2021 03 30 \344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/\347\273\203\344\271\2401\344\275\234\344\270\232.sql" "b/2021 03 30 \344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/\347\273\203\344\271\2401\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..25fcbd24949186391579de85342f71d49d261725 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/\347\273\203\344\271\2401\344\275\234\344\270\232.sql" @@ -0,0 +1,74 @@ +create database Stu +on +( + name='Stu', + filename='E:\SQL课堂\Stu.mdf', + size=5, + maxsize=100, + filegrowth=10 +) +log on +( + name='Stu_log', + filename='E:\SQL课堂\Stu_log.ldf', + size=5, + maxsize=100, + filegrowth=10 +) +go +use Stu +go +create table stuInfo +( + stuID int not null primary key identity(1,1), + stuName char(10) not null, + stuAge int not null, + stuSex nchar(2) not null check(stuSex='男' or stuSex='女'), + time datetime +) +insert into stuInfo values('Tom','19','男',null),('Jack','20','女',null),('Rose','21','男',null),('Lulu','19','男',null),('Lili','21','女',null),('abc','20','男','2007-01-07 01:11:36.590') +create table courseInfo +( + courseID int not null primary key identity(1,1), + courseName char(10) not null, + courseMarks int not null +) +insert into courseInfo values('JavaBase','4'),('HTML','2'),('JavaScript','2'),('SqlBase','2') +create table scoreInfo +( + scoreID int not null primary key identity(1,1), + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int not null +) +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 stuInfo --学生信息表 +select * from courseInfo --课程信息表 +select * from scoreInfo --分数信息表 +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName 学生,SUM(courseMarks)课程的数量,AVG(score)考试的平均分 from scoreInfo inner join stuInfo on scoreInfo.stuID = stuInfo.stuID inner join courseInfo on scoreInfo.courseID = courseInfo.courseID group by stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName 课程,COUNT(scoreInfo.courseID)学生的个数,SUM(score)考试的总分 from scoreInfo inner join stuInfo on scoreInfo.stuID = stuInfo.stuID inner join courseInfo on scoreInfo.courseID = courseInfo.courseID group by courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select A. * from stuInfo A,stuInfo B where A.stuSex = B.stuSex and A.stuAge = B.stuAge and A.stuID != B.stuID +--4.查询出学分一样的课程信息 +select A. * from courseInfo A,courseInfo B where A.courseMarks = B.courseMarks and A.courseID != B.courseID +union +select * from courseInfo where courseID = 1 and courseID = 2 and courseID = 3 +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select scoreInfo.stuID 学号,stuName 姓名,scoreInfo.courseID 课程号,score 分数 from scoreInfo inner join stuInfo on scoreInfo.stuID = stuInfo.stuID inner join courseInfo on scoreInfo.courseID = courseInfo.courseID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select scoreInfo.stuID 学号,scoreInfo.courseID 课程号,courseMarks 课程学分,score 分数 from scoreInfo inner join stuInfo on scoreInfo.stuID = stuInfo.stuID inner join courseInfo on scoreInfo.courseID = courseInfo.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuID 没有参加考试的学生的学号,stuName 姓名 from stuInfo left join scoreInfo on scoreInfo.stuID = stuInfo.stuID where stuInfo.stuID > 0 +except +select stuInfo.stuID 没有参加考试的学生的学号,stuName 姓名 from stuInfo left join scoreInfo on scoreInfo.stuID = stuInfo.stuID where score > 0 +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuInfo.stuID 学生的学号,AVG(score)考试平均分在70以上,courseMarks 选修课程的数量 from scoreInfo inner join stuInfo on scoreInfo.stuID = stuInfo.stuID inner join courseInfo on scoreInfo.courseID = courseInfo.courseID group by stuInfo.stuID,courseMarks +having COUNT(stuInfo.stuID)>=2 and AVG(score)>=70 diff --git "a/2021 03 30 \344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/\347\273\203\344\271\2402.sql" "b/2021 03 30 \344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/\347\273\203\344\271\2402.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d27c879f570ef39aba6f0bcbb3fff3bee3032d8d --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/\347\273\203\344\271\2402.sql" @@ -0,0 +1,99 @@ +--首先创建网吧计费系统的数据库,表结构和数据如2.bmp所示,表的说明如下: +create database WB +on +( + name='WB', + filename='E:\SQL课堂\WB.mdf', + size=50, + maxsize=100, + filegrowth=10 +) +log on +( + name='WB_log', + filename='E:\SQL课堂\WB_log.ldf', + size=50, + maxsize=100, + filegrowth=10 +) +go +--表一为上网卡信息表(tbl_card) +--ID: 卡号,主键 +--PassWord:密码 +--Balance:卡上的余额 +--UserName:该上网卡所属的用户名 +use WB +go +create table tbl_card +( + ID char(10) not null primary key, + PassWord nchar(10) not null, + Balance int not null, + UserName nchar(5) not null +) +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','张峻') +--表2为网吧机器说明表(tbl_computer) +--ID:机器编号,主键 +--OnUse:该机器是否正在使用,0为未使用,1为正在使用 +--NOte:该机器的说明 +create table tbl_computer +( + ID char(10) not null primary key, + OnUse int not null check(OnUse='0' or OnUse='1'), + NOte text not null +) +insert into tbl_computer values ('02','0','25555'),('03','1','55555'),('04','0','66666'),('05','1','88888'),('06','0','688878'),('B01','0','558558') +--表3为上网记录表(tbl_record): +--ID:记录编号,主键 +--CardID:本次上网的卡号,外键 +--ComputerID:本次上网记录所使用的机器号,外键 +--BeginTime:本次上网记录的开始时间 +--EndTime:本次上网记录的结束时间 +--Fee:本次上网的费用 +create table tbl_record +( + ID int not null primary key, + CardID char(10) not null references tbl_card(ID), + ComputerID char(10) not null references tbl_computer(ID), + BeginTime datetime not null, + EndTime datetime not null, + Fee float not null +) +--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-22 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 15:26:00','2007-01-06 22:55:00','6'),('55','0023_ABC','03','2006-07-21 15:26:00','2006-07-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-12-28 18:00:00','2006-12-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 tbl_card.ID 显示卡号,UserName 用户名,tbl_computer.ID 机器编号,BeginTime 开始时间,EndTime 结束时间,Fee 消费金额 from tbl_record inner join tbl_card on tbl_record.CardID = tbl_card.ID inner join tbl_computer on tbl_record.ComputerID = tbl_computer.ID +where UserName='张军' order by Fee DESC + +--2. 查询出每台机器上的上网次数和消费的总金额 +select tbl_computer.ID 机器,COUNT(ComputerID)上网次数,SUM(Fee)总金额 from tbl_computer left join tbl_record on tbl_computer.ID = tbl_record.ComputerID group by tbl_computer.ID + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select tbl_record.CardID 上网卡,SUM(Fee)消费总金额 from tbl_record group by tbl_record.CardID + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select tbl_card.ID 上网卡号,UserName 用户名 from tbl_record right join tbl_card on tbl_record.CardID = tbl_card.ID +except +select tbl_card.ID 上网卡号,UserName 用户名 from tbl_record right join tbl_card on tbl_record.CardID = tbl_card.ID where Fee>0 + +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card where PassWord = UserName + +--6. 查询出使用次数最多的机器号和使用次数 + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + + + + diff --git "a/2021 03 30 \344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/SQLQuery1.sql" "b/2021 03 30 \344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8741b3268407d5c8ff9db0063cdb0636dcaa68f5 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/SQLQuery1.sql" @@ -0,0 +1,88 @@ +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) + +use master +go + +create database stuInfo +go +use stuInfo +go +create table StuUser +( + stuID int primary key identity, + stuName nvarchar(10) , + stuAge int , + stuSex int check(stuSex in('0','1')), + stuTime datetime null +) +create table Course +( + courseID int primary key identity, + courseName nvarchar(10), + courseMarks int +) +create table Score +( + scoreID int primary key identity , + stUID int references StuUser(stuID) , + courseID int references Course(courseID), + score int +) +go +insert into StuUser(stuName,stuAge,stuSex) values('Tom',19,1),('Jack',20,0),('Rose',21,1) +,('Lulu',19,1),('Lili',21,0) +insert into StuUser(stuName,stuAge,stuSex,stuTime) values('abc',20,1,'2007-01-07 01:11:36.590') + +insert into Course (courseName,courseMarks) +select 'JavaBase',4 union +select 'HTML',2 union +select 'JavaScripy',2 union +select 'SqlBase',2 + +insert into Score (stuID,courseID,score) +select 1,1,80 union +select 1,2,85 union +select 1,4,50 union +select 2,1,75 union +select 2,3,45 union +select 2,4,75 union +select 3,1,45 union +select 4,1,95 union +select 4,2,75 union +select 4,3,90 union +select 4,4,45 +--题目: + + +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName,count(Score.stuID),avg(score) from StuUser inner join Score on StuUser.stuID = score.stUID group by stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select Course.courseID,count(Course.courseID),sum(score) from Course +inner join Score on Course.courseID = Score.courseID group by Course.courseID +--3.查询出性别一样并且年龄一样的学生的信息 +select A.* from StuUser A inner join +(select stuAge,stuSex from StuUser group by stuAge,stuSex having count(stuAge)>1 and count(stuSex)>1) B +on A.stuAge = B.stuAge and A.stuSex =B.stuSex +--4.查询出学分一样的课程信息 +select A.courseName,A.courseMarks from Course A,Course B where +A.courseMarks = B.courseMarks and A.courseID != B.courseID group by A.courseMarks,A.courseName +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select S.stuName,S.stUID,S.stuName,C.courseID,score from Score C +left join StuUser S on C.stUID = S.stuID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuName,O.stUID,C.courseID,C.courseMarks,courseMarks,score from Score O +left join Course C on C.courseID = O.courseID +left join StuUser on O.stUID = StuUser.stuID +--7.查询出没有参加考试的学生的学号和姓名 +select stUID,stuName from StuUser group by stuID,stuName +except +select Score.stUID,stuName from Score inner join StuUser on Score.stUID = StuUser.stuID group by Score.stUID,stuName +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 +select * from StuUser where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stUID,avg(score),count(*) from Score group by stUID having count(*)>1 and avg(score)>70 +select * from Course +select * from Score +select * from StuUser \ No newline at end of file diff --git "a/2021 03 30 \344\275\234\344\270\232/\346\270\251\345\271\277\347\224\237/SQLQuery1.sql" "b/2021 03 30 \344\275\234\344\270\232/\346\270\251\345\271\277\347\224\237/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0a4f6f5036ee5f382f7da928ebad81c0856725d3 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\346\270\251\345\271\277\347\224\237/SQLQuery1.sql" @@ -0,0 +1,128 @@ + +create database students +go + +use students +go + +create table stuInfo +( + stuId int identity primary key, + stuName varchar(10) not null , + stuAge char(2) not null, + stuSex char(1) not null check(stuSex = '1' or stuSex = '0') , + time datetime +) +go + +create table courseInfo +( + courseId int identity primary key, + courseName varchar(10) not null, + courseMarks int not null +) +go + +create table scoreInfo +( + scoreId int identity primary key , + stuId int references stuInfo(stuId), + courseId int references courseInfo(courseId), + score int +) +go + + +insert into stuInfo(stuName,stuAge,stuSex,time) +select 'Tom','19','1','' union +select 'Jack','20','0','' union +select 'Rose','21','1','' union +select 'Lulu','19','1','' union +select 'Lili','21','0','' union +select 'abc','20','1','2007-01-07 01:11:36.590' + +insert into courseInfo(courseName,courseMarks) +select 'JavaBase','4' union +select 'HTML','2' union +select 'JavaScript','2' union +select 'SqlBase','2' + +insert into scoreInfo(stuId,courseId,score) +select '1','1','80' union +select '1','2','85' union +select '1','4','50' union +select '2','1','75' union +select '2','3','45' union +select '2','4','75' union +select '3','1','45' union +select '4','1','95' union +select '4','2','75' union +select '4','3','90' union +select '4','4','45' + + + + +--1.查询出 每个学生 所选修的课程的数量和所选修的课程的考试的平均分 + +select si.stuName 学生,COUNT(sci.stuId) 选课数量,AVG(score) 考试平均分 from stuInfo si + inner join scoreInfo sci on si.stuId = sci.stuId + group by si.stuId,stuName + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 + +select courseName 课程名称,COUNT(si.courseId) 学生人数,SUM(score) 总分 from courseInfo ci + inner join scoreInfo si on ci.courseId = si.courseId + group by ci.courseId , courseName + +--3.查询出性别一样并且年龄一样的学生的信息 + +select * from stuInfo A,stuInfo B + where a.stuAge = b.stuAge and a.stuSex = b.stuSex and a.stuId != b.stuId + +--4.查询出学分一样的课程信息 + +select * from courseInfo ci where + ( + select COUNT(*) from courseInfo where + courseMarks = ci.courseMarks + )>1 + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 + +select si.stuId ,stuName ,scoreId ,score from stuInfo si + inner join scoreInfo sci on sci.stuId = si.stuId + group by si.stuId ,stuName ,scoreId ,score + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 + +select si.stuId 学号,ci.courseId 课程号,courseName 课程名,courseMarks 课程学分,score 分数 from scoreInfo sci + inner join courseInfo ci on ci.courseId = sci.courseId + inner join stuInfo si on si.stuId = sci.stuId + +select * from stuInfo +select * from courseInfo +select * from scoreInfo + +--7.查询出没有参加考试的学生的学号和姓名 + + select * from stuInfo si + left join scoreInfo sci on si.stuId=sci.stuId + where score IS NULL + +--8.查询出是周六周天来报到的学生 + +select * from stuInfo + where datepart(weekday,time)=1 or datepart(weekday,time)=7 + +--9.查询出姓名中有字母a的学生的信息 + +select * from stuInfo where stuName like '%a%' + +--10.查询出 选修了2门课程以上 的并且 考试平均分在70以上 的 学生的学号和考试平均分以及选修课程的数量 + +select si.stuId 学号,AVG(score) 考试平均分,COUNT(sci.stuId) 选课数量 from stuInfo si + inner join scoreInfo sci on si.stuId = sci.stuId + group by si.stuId + + diff --git "a/2021 03 30 \344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/SQLQuery1.sql" "b/2021 03 30 \344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0eebfda2c28d8b25784e61ae32b25e3cb3b33e5c --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/SQLQuery1.sql" @@ -0,0 +1,129 @@ + +create database students +go + +use students +go + +create table stuInfo +( + stuId int identity primary key, + stuName varchar(10) not null , + stuAge char(2) not null, + stuSex char(1) not null check(stuSex = '1' or stuSex = '0') , + time datetime +) +go + +create table courseInfo +( + courseId int identity primary key, + courseName varchar(10) not null, + courseMarks int not null +) +go + +create table scoreInfo +( + scoreId int identity primary key , + stuId int references stuInfo(stuId), + courseId int references courseInfo(courseId), + score int +) +go + + +insert into stuInfo(stuName,stuAge,stuSex,time) +select 'Tom','19','1','' union +select 'Jack','20','0','' union +select 'Rose','21','1','' union +select 'Lulu','19','1','' union +select 'Lili','21','0','' union +select 'abc','20','1','2007-01-07 01:11:36.590' + +insert into courseInfo(courseName,courseMarks) +select 'JavaBase','4' union +select 'HTML','2' union +select 'JavaScript','2' union +select 'SqlBase','2' + +insert into scoreInfo(stuId,courseId,score) +select '1','1','80' union +select '1','2','85' union +select '1','4','50' union +select '2','1','75' union +select '2','3','45' union +select '2','4','75' union +select '3','1','45' union +select '4','1','95' union +select '4','2','75' union +select '4','3','90' union +select '4','4','45' + + + + +--1.查询出 每个学生 所选修的课程的数量和所选修的课程的考试的平均分 + +select si.stuName 学生,COUNT(sci.stuId) 选课数量,AVG(score) 考试平均分 from stuInfo si + inner join scoreInfo sci on si.stuId = sci.stuId + group by si.stuId,stuName + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 + +select courseName 课程名称,COUNT(si.courseId) 学生人数,SUM(score) 总分 from courseInfo ci + inner join scoreInfo si on ci.courseId = si.courseId + group by ci.courseId , courseName + +--3.查询出性别一样并且年龄一样的学生的信息 + +select * from stuInfo A,stuInfo B + where a.stuAge = b.stuAge and a.stuSex = b.stuSex and a.stuId != b.stuId + +--4.查询出学分一样的课程信息 + +select * from courseInfo ci where + ( + select COUNT(*) from courseInfo where + courseMarks = ci.courseMarks + )>1 + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 + +select si.stuId ,stuName ,scoreId ,score from stuInfo si + inner join scoreInfo sci on sci.stuId = si.stuId + group by si.stuId ,stuName ,scoreId ,score + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 + +select si.stuId 学号,ci.courseId 课程号,courseName 课程名,courseMarks 课程学分,score 分数 from scoreInfo sci + inner join courseInfo ci on ci.courseId = sci.courseId + inner join stuInfo si on si.stuId = sci.stuId + + + +--7.查询出没有参加考试的学生的学号和姓名 + + select * from stuInfo si + left join scoreInfo sci on si.stuId=sci.stuId + where score IS NULL + +--8.查询出是周六周天来报到的学生 + +select * from stuInfo + where datepart(weekday,time)=1 or datepart(weekday,time)=7 + +--9.查询出姓名中有字母a的学生的信息 + +select * from stuInfo where stuName like '%a%' + +--10.查询出 选修了2门课程以上 的并且 考试平均分在70以上 的 学生的学号和考试平均分以及选修课程的数量 + +select si.stuId 学号,AVG(score) 考试平均分,COUNT(sci.stuId) 选课数量 from stuInfo si + inner join scoreInfo sci on si.stuId = sci.stuId + group by si.stuId + + +select * from stuInfo +select * from courseInfo +select * from scoreInfo \ No newline at end of file diff --git "a/2021 03 30 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery3.sql" "b/2021 03 30 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c85238f0191932801892e6b5f9bfaf593a3e81be --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery3.sql" @@ -0,0 +1,104 @@ +create database dididada +on( + name='dididada', + filename='D:\dididada.mdf', + size=10, + maxsize=100, + filegrowth=10 + + +) +log on +( + name='dididada_log', + filename='D:\dididada_log.ldf', + size=10, + maxsize=100, + filegrowth=10 + + + +) +go +create table stuInfo( +stuID int identity(1,1) primary key, +stuName nvarchar(10)not null , +stuAge varchar(10) not null, +stuSex varchar(2) check(stuSex=1 or stuSex=0), +times datetime +) +go +create table courseInfo( +courseID int identity(1,1) primary key, +courseName nvarchar(10) not null, +courseMarks varchar(5) +) +go +create table scoreInfo( +scoreID int identity(1,1) primary key, +stuID int references stuInfo(stuID), +courseID int references courseInfo(courseID), +score int not null + +) +insert into stuInfo +select 'Tom',19,1,null union +select 'Jack',20,0 ,null union +select 'Rose',21,1,null union +select 'Lulu',19,1 ,null union +select 'Lili',21,0,null union +select 'abc',20,1 ,'2007-01-07 ' +insert into courseInfo +select 'JavaBase',4 union +select 'HTML',2 union +select 'JavaScript',2 union +select 'SqlBase',2 + +insert into scoreInfo +select 1,1 , 80union +select 1,2,85 union +select 1,4,50 union +select 2,1,75 union +select 2,3 ,45union +select 2,4 ,75union +select 3,1,45 union +select 4,1, 95union +select 4,2,75 union +select 4,3,90 union +select 4,4 ,45 + +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuID, courseName, courseMarks,avg(score) from courseInfo c inner join scoreInfo s on c.courseID=s.courseID group by courseName, courseMarks,score,stuID ORDER BY courseName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 + select scoreInfo.courseID,coursename,COUNT(stuid),SUM(score) from scoreInfo inner join courseInfo on scoreInfo.courseID=courseInfo.courseid group by scoreInfo.courseld,coursename + --3.查询出性别一样并且年龄一样的学生的信息 + select a.* from stuInfo a , stuInfo b where a.stusex=b.stusex and a.stuage=b.stuage and a.stuID!=b.stuID --4.查询出学分一样的课程信息 + select distinct a.courseid,a.coursename,a.coursemarks from courseInfo a,courseInfo b where a.coursemarks=b.coursemarks and a.courseid!=b.courseid + --5.查询出参加了考试的学生的学号,姓名,课程号和分数 + select stuInfo.stuID,stuname,courseID,score from scoreInfo inner join stuInfo on stuInfo.stuID=scoreInfo.stuid group by stuInfo.stuID,stuname,courseID,score + --6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 + select stuid,courseInfo.courseid,coursename,coursemarks,score from scoreInfo inner join courseInfo on scoreInfo.courseID=courseInfo.courseid group by stuid,courseInfo.courseid,coursename,coursemarks,score + --7.查询出没有参加考试的学生的学号和姓名 + select * from stuInfo left join scoreInfo on scoreInfo.stuid=stuInfo.stuID WHERE score IS NULL + --8.查询出是周六周天来报到的学生 +select * from stuInfo where datepart(weekday,times)=1 or datepart(weekday,times)=7 +--9.查询出姓名中有字母a的学生的信息 + select * from stuInfo where stuname like '%a%' + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 + select stuid,count(courseID),AVG(score) from scoreInfo group by stuid having COUNT(courseID)>2 and AVG(score)>70 + + + + + + + + + + + + + + + diff --git "a/2021 03 30 \344\275\234\344\270\232/\347\275\227\344\273\225\345\244\251\343\200\201/SQLQuery34.sql" "b/2021 03 30 \344\275\234\344\270\232/\347\275\227\344\273\225\345\244\251\343\200\201/SQLQuery34.sql" new file mode 100644 index 0000000000000000000000000000000000000000..efc9f840bb0344c0fd36afe54c2625295cbddded --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\347\275\227\344\273\225\345\244\251\343\200\201/SQLQuery34.sql" @@ -0,0 +1,92 @@ +create database Student +on( +name=Student, +filename='D:\sql.mdf', +size=10mb, +maxsize=50mb, +filegrowth=10% +) +log on +( +name=sql_log, +filename='D:\sql_log.mdf', +size=10mb, +maxsize=50mb, +filegrowth=10% + +) + +create table stuInfo +( +stuID int not null primary key identity(1,1), +stuName nvarchar(10) not null, +stuAge int not null, +stuSex nchar check(stuSex='男' or stuSex ='女') , +time char(50) +) +insert into stuInfo values ('TOM',19,'男',NULL),('JACK',20,'女',NULL),('ROSE',21,'男',NULL),('LULU',19,'男',NULL),('LILI',21,'女',NULL),('ABC',20,'男','2007-01-07 01:11:36:590') +select * from stuInfo +create table courseInfo +( +courseID int primary key identity(1,1) not null, +sourseName nvarchar(10) not null, +courseMarks int + +) +insert into courseInfo values ('SQIBASE',4),('JAVASCRIPT',2),('HTML',2),('JAVABASE',2) +select * from courseInfo +create table scoreInfo +( +scoreID int primary key identity(1,1) not null, +stuID int references stuInfo(stuID) not null, +courseID int references courseInfo(courseID) not null, +score int not null +) +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 + +select * from scoreInfo +select * from stuInfo +select *from courseInfo + + +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuname 学生,count(*)课程数量,AVG(score) 平均分 from scoreinfo + inner join stuInfo on stuInfo.stuid=scoreInfo.stuID + inner join courseInfo on courseInfo. courseID=scoreinfo.courseID + group by stuname +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select soursename 课程,count(*)学生个数,SUM(score) 总分 from scoreInfo +inner join stuInfo on stuInfo.stuid=scoreInfo.stuID + inner join courseInfo on courseInfo. courseID=scoreinfo.courseID + group by sourseName +--3.查询出性别一样并且年龄一样的学生的信息 +select A.* from stuInfo A,stuInfo B +where A.stuAge=B.stuAge and A.stuSex=B.stuSex and A.stuID !=B.stuID +--4.查询出学分一样的课程信息 +select A.* from courseInfo A, courseInfo B +where A.courseMarks=B.courseMarks and A.courseID != B.courseID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuInfo.stuID,stuName,courseID,score from stuInfo +inner join scoreInfo on scoreInfo.stuID=stuInfo.stuID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 + + +--7.查询出没有参加考试的学生的学号和姓名 + +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuInfo.stuID 学号,avg(score) 考试平均分,count(*) 选修课程的数量 from stuInfo \ No newline at end of file diff --git "a/2021 03 30 \344\275\234\344\270\232/\347\275\227\346\265\252\345\255\220/Students.sql" "b/2021 03 30 \344\275\234\344\270\232/\347\275\227\346\265\252\345\255\220/Students.sql" new file mode 100644 index 0000000000000000000000000000000000000000..303d18115343b07507951b5a7cbaa58ed8bd82ee --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\347\275\227\346\265\252\345\255\220/Students.sql" @@ -0,0 +1,130 @@ +use master +go + +create database Student +on +( + name='Student', + filename='D:\Student.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='Student_log', + filename='D:\Student_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use Student +go + + +create table stuInfo +( + stuID int primary key identity(1,1), + stuName char(10), + stuAge int, + stuSex int check(stuSex=1 or stuSex=0), + time datetime, +) +go +create table courseInfo +( + courseID int primary key identity(1,1), + courseName char(20), + courseMarks int, +) +go + +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int +) +go + +insert into stuInfo (stuName,stuAge,stuSex,time) 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-07') + +select * from stuInfo + + +insert into courseInfo (courseName,courseMarks) values ('JavaBase',4), +('HTML',2), +('JavaScript',2), +('SqlBase',2) + +select * from courseInfo + +insert into scoreInfo (stuID,courseID,score) 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) + + + + +--1.查询出 每个学生 所 选修的课程的数量和 所选修的课程的 考试的平均分 +select stuName as 学生姓名,count(scoreInfo.stuID) as 选修的课程数 ,avg(score) as 考试的平均分 from scoreInfo +full join stuInfo on scoreInfo.stuID=stuInfo.stuID +group by stuName + + +--2.查询出 每门课程的 选修的学生的个数和 学生考试的总分 +select courseName,count(scoreInfo.courseID)选修的学生的个数,sum(score)学生考试的总分 from scoreInfo +inner join courseInfo on scoreInfo.courseID=courseInfo.courseID +group by courseName + + +--3.查询出性别一样并且年龄一样的学生的信息 +select A.* from stuInfo A,stuInfo B +where A.stuAge=B.stuAge and A.stuSex=B.stuSex and A.stuID !=B.stuID + +--4.查询出学分一样的课程信息 +select distinct B.* from courseInfo A,courseInfo B +where A.courseMarks=B.courseMarks and A.courseID !=B.courseID and A.courseName !=B.courseName + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuInfo.stuID,stuName,courseID,score from stuInfo +right join scoreInfo on scoreInfo.stuID=stuInfo.stuID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuName,scoreInfo.stuID,scoreInfo.courseID,courseName,courseMarks,score from scoreInfo +inner join courseInfo on scoreInfo.courseID=courseInfo.courseID +inner join stuInfo on stuInfo.stuID=scoreInfo.stuID + + + +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuID,stuName from stuInfo full join scoreInfo on stuInfo.stuID =scoreInfo.stuID +where score is null + + +--8.查询出是周六周天来报到的学生 +select * from stuInfo +where datepart(weekday,time)=1 or datepart(WEEKDAY,time)=7 + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' + + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuID as 学号,count(stuID)选修课程的数量,avg(score)考试平均分 from scoreInfo group by stuID having count(stuID)>2 and avg(score)>70 diff --git "a/2021 03 30 \344\275\234\344\270\232/\350\221\243\344\270\226\351\276\231/SQLQuery1.sql" "b/2021 03 30 \344\275\234\344\270\232/\350\221\243\344\270\226\351\276\231/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0a4f6f5036ee5f382f7da928ebad81c0856725d3 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\350\221\243\344\270\226\351\276\231/SQLQuery1.sql" @@ -0,0 +1,128 @@ + +create database students +go + +use students +go + +create table stuInfo +( + stuId int identity primary key, + stuName varchar(10) not null , + stuAge char(2) not null, + stuSex char(1) not null check(stuSex = '1' or stuSex = '0') , + time datetime +) +go + +create table courseInfo +( + courseId int identity primary key, + courseName varchar(10) not null, + courseMarks int not null +) +go + +create table scoreInfo +( + scoreId int identity primary key , + stuId int references stuInfo(stuId), + courseId int references courseInfo(courseId), + score int +) +go + + +insert into stuInfo(stuName,stuAge,stuSex,time) +select 'Tom','19','1','' union +select 'Jack','20','0','' union +select 'Rose','21','1','' union +select 'Lulu','19','1','' union +select 'Lili','21','0','' union +select 'abc','20','1','2007-01-07 01:11:36.590' + +insert into courseInfo(courseName,courseMarks) +select 'JavaBase','4' union +select 'HTML','2' union +select 'JavaScript','2' union +select 'SqlBase','2' + +insert into scoreInfo(stuId,courseId,score) +select '1','1','80' union +select '1','2','85' union +select '1','4','50' union +select '2','1','75' union +select '2','3','45' union +select '2','4','75' union +select '3','1','45' union +select '4','1','95' union +select '4','2','75' union +select '4','3','90' union +select '4','4','45' + + + + +--1.查询出 每个学生 所选修的课程的数量和所选修的课程的考试的平均分 + +select si.stuName 学生,COUNT(sci.stuId) 选课数量,AVG(score) 考试平均分 from stuInfo si + inner join scoreInfo sci on si.stuId = sci.stuId + group by si.stuId,stuName + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 + +select courseName 课程名称,COUNT(si.courseId) 学生人数,SUM(score) 总分 from courseInfo ci + inner join scoreInfo si on ci.courseId = si.courseId + group by ci.courseId , courseName + +--3.查询出性别一样并且年龄一样的学生的信息 + +select * from stuInfo A,stuInfo B + where a.stuAge = b.stuAge and a.stuSex = b.stuSex and a.stuId != b.stuId + +--4.查询出学分一样的课程信息 + +select * from courseInfo ci where + ( + select COUNT(*) from courseInfo where + courseMarks = ci.courseMarks + )>1 + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 + +select si.stuId ,stuName ,scoreId ,score from stuInfo si + inner join scoreInfo sci on sci.stuId = si.stuId + group by si.stuId ,stuName ,scoreId ,score + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 + +select si.stuId 学号,ci.courseId 课程号,courseName 课程名,courseMarks 课程学分,score 分数 from scoreInfo sci + inner join courseInfo ci on ci.courseId = sci.courseId + inner join stuInfo si on si.stuId = sci.stuId + +select * from stuInfo +select * from courseInfo +select * from scoreInfo + +--7.查询出没有参加考试的学生的学号和姓名 + + select * from stuInfo si + left join scoreInfo sci on si.stuId=sci.stuId + where score IS NULL + +--8.查询出是周六周天来报到的学生 + +select * from stuInfo + where datepart(weekday,time)=1 or datepart(weekday,time)=7 + +--9.查询出姓名中有字母a的学生的信息 + +select * from stuInfo where stuName like '%a%' + +--10.查询出 选修了2门课程以上 的并且 考试平均分在70以上 的 学生的学号和考试平均分以及选修课程的数量 + +select si.stuId 学号,AVG(score) 考试平均分,COUNT(sci.stuId) 选课数量 from stuInfo si + inner join scoreInfo sci on si.stuId = sci.stuId + group by si.stuId + + diff --git "a/2021 03 30 \344\275\234\344\270\232/\350\221\243\344\270\226\351\276\231/SQLQuery3.sql" "b/2021 03 30 \344\275\234\344\270\232/\350\221\243\344\270\226\351\276\231/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..59d836b460d20e1f0a4b6c0cc286f1d86fd34a91 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\350\221\243\344\270\226\351\276\231/SQLQuery3.sql" @@ -0,0 +1,77 @@ +create database Student02 +on +( + name='Student02', + filename='D:\Student02.mdf', + size=20, + maxsize=300, + filegrowth=50 +) +log on +( + name='Student02_log', + filename='D:\Student02_log.ldf', + size=20, + maxsize=300, + filegrowth=50 +) +go +use Student02 +go +create table StudenInfo +( + stuID int identity(1,1) primary key, + stuName nvarchar(20) not null unique, + stuAge int null , + stuSex int check(stuSex in('1','0')) default('0'), + time datetime +) +insert into StudenInfo(stuName,stuAge,stuSex) values('Tom',19,1),('Jack',20,0),('Rose',21,1),('Lulu',19,1),('Lili',21,0) +insert into StudenInfo(stuName,stuAge,stuSex,time) values('abc',20,1,2007-01-07) + +create table courseInfo +( + courseID int identity(1,1) primary key, + courseName nvarchar(20) not null, + courseMarks int not null +) +insert into courseInfo(courseName,courseMarks) values ('JavaBase',4),('HTML',2),('Javascript',2),('SqlBase',2) + +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int references StudenInfo(stuID), + courseID int references courseInfo(courseID), + score int not null +) + +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 StudenInfo +select * from courseInfo +select * from scoreInfo +--1.查询出 每个学生 所选修的课程的数量和 所选修的课程的考试的平均分 +select stuName 姓名,count(distinct courseID)所选修的课程的数量和,avg(score)平均分 from scoreInfo SC inner join StudenInfo ST on SC.stuID=ST.stuID group by stuName + +--2.查询出 每门课程的选修的学生的个数 和 学生考试的总分 +select courseName 课程名,count(*)每门课程的选修的学生的个数,sum(score)学生考试的总分 from scoreInfo SC inner join courseInfo CO on SC.courseID=CO.courseID group by courseName + +--3.查询出性别一样并且年龄一样的学生的信息 +select A. * from StudenInfo A,StudenInfo B where A.stuSex = B.stuSex and A.stuAge=B.stuAge and A.stuName!=B.stuName + +--4.查询出学分一样的课程信息 +select A. * from scoreInfo A,scoreInfo B where A.score=B.score and A.stuID!=B.stuID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select SC. stuID,stuName,scoreID,score from scoreInfo SC inner join StudenInfo ST on SC.stuID=ST.stuID group by SC. stuID,stuName,scoreID,score +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select SC.scoreID,stuID,score,courseMarks,courseName from scoreInfo SC inner join courseInfo CO on SC.courseID=CO.courseID group by SC. scoreID,stuID,score,courseMarks,courseName +--7.查询出没有参加考试的学生的学号和姓名 +select * from StudenInfo ST left join scoreInfo SC on ST.stuID=SC.stuID where score is null +--8.查询出是周六周天来报到的学生 + select * from StudenInfo where datepart(WEEKDAY,time)=1 or datepart(WEEKDAY,time)=7 +--9.查询出姓名中有字母a的学生的信息 +select * from StudenInfo where stuName like '%a%' +--10.查询出 选修了2门课程以上的 并且考试平均分在70以上的学生的学号 和 考试平均分以及 选修课程的数量 +select ST. stuID,AVG(score),count(ST.stuID) from StudenInfo ST inner join scoreInfo SC on ST.stuID=SC.stuID group by ST.stuID having avg(score)>70 \ No newline at end of file diff --git "a/2021 03 30 \344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery1.sql" "b/2021 03 30 \344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7716b9d78dc29220a396f0b6bfbe3817badcf2ef --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery1.sql" @@ -0,0 +1,84 @@ +use master +go +create database bbs +on +( + name='bbs', + filename='D:\bbs.mdf', + size=10, + maxsize=200, + filegrowth=20 +) +log on +( + name='bbs_log', + filename='D:\bbs_log.ldf ', + size=10, + maxsize=200, + filegrowth=20 +) +go +use bbs + +create table stuInfo +( + stuld int primary key identity not null, + stuname varchar(20) not null, + stuage varchar(20) not null, + stusex varchar(2) check (stusex in ('1','0'))not null, + time datetime default('null') +) +go +create table courseInfo +( + + courseid int primary key identity not null, + coursename varchar(20) not null, + coursemarks int not null +) +go +create table scoreInfo +( + + scoreid int primary key identity not null, + stuid int references stuInfo(stuld) not null, + courseld int references courseInfo(courseid) not null, + score int not null + + +) +go + + insert into stuInfo values ('tom','19','1',''),('jack','20','0',''),('rose','21','1',''),('lulu','19','1',''),('lili','21','0',''),('abc','20','1','2007-01-07') + + + 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 + + select stuid,stuname,count(courseld),avg(score) from scoreInfo inner join stuInfo on scoreInfo.stuid=stuInfo.stuld group by stuid,stuname + + select scoreInfo.courseld,coursename,COUNT(stuid),SUM(score) from scoreInfo inner join courseInfo on scoreInfo.courseld=courseInfo.courseid group by scoreInfo.courseld,coursename + + select a.* from stuInfo a , stuInfo b where a.stusex=b.stusex and a.stuage=b.stuage and a.stuld!=b.stuld + + select distinct a.courseid,a.coursename,a.coursemarks from courseInfo a,courseInfo b where a.coursemarks=b.coursemarks and a.courseid!=b.courseid + + select stuid,stuname,courseld,score from scoreInfo inner join stuInfo on stuInfo.stuld=scoreInfo.stuid group by stuid,stuname,courseld,score + + select stuid,courseInfo.courseid,coursename,coursemarks,score from scoreInfo inner join courseInfo on scoreInfo.courseld=courseInfo.courseid group by stuid,courseInfo.courseid,coursename,coursemarks,score + + select * from stuInfo left join scoreInfo on scoreInfo.stuid=stuInfo.stuld WHERE score IS NULL + +select * from stuInfo where datepart(weekday,time)=1 or datepart(weekday,time)=7 + + select * from stuInfo where stuname like '%a%' + + select stuid,count(courseld),AVG(score) from scoreInfo group by stuid having COUNT(courseld)>2 and AVG(score)>70 + diff --git "a/2021 03 30 \344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery001.sql" "b/2021 03 30 \344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery001.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c294768a9a9517374e45283957ac5fa66fb60ef9 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery001.sql" @@ -0,0 +1,87 @@ +use master +go +create database BBC +on +( + name='BBC', + filename='D:\BBC.mdf', + size=10, + maxsize=300, + filegrowth=20% +) +log on +( + name='BBC_log', + filename='D:\BBC_log.mdf', + size=10, + maxsize=300, + filegrowth=20% +) +go +use BBC +go +create table tbl_card +( + id varchar(20) primary key, + passWord varchar(20) , + balance money , + userName varchar(50) +) + +create table tbl_computer +( + id int primary key, + onUse int , + note varchar(100) , +) + +create table tbl_record +( + id int primary key, + cardI varchar(20) references tbl_card(id) , + ComputerId int references tbl_computer(id) , + beginTime datetime , + endTime datetime , + fee money +) +insert into tbl_card(id,passWord,balance,userName) values ('0023_ABC','555',98,'张军'),('0025_bbd','abe',300,'朱俊'),('0036_CDD','何柳',100,'何柳'),('0045_YGR','0045_YGR',58,'验证'),('0078_RJV','55885fg',600,'校庆'),('0089_EDE','zhang',134,'张俊') + +insert into tbl_computer(id,onUse,note) values (02,0,'25555'),(03,1,'55555'),(04,0,'66666'),(05,1,'88888'),(06,0,'688878'),(01,0,'558558') + +insert into tbl_record(id,cardI,ComputerId,beginTime,endTime,fee) values (23,'0078_RJV',01,'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-22 22:55:00',6),(47,'0023_ABC',03,'2006-12-23 15:26:00','2006-12-23 22:55:00',23), +(48,'0023_ABC',03,'2007-01-06 15:26:00','2007-01-06 22:55:00',6),(55,'0023_ABC',03,'2006-07-21 15:26:00','2007-07-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-12-28 18:00:00','2006-12-28 22:00:00',23),(98,'0025_bbd',02,'2006-12-26 18:00:00','2006-12-26 22:00:00',23) + + + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 + select C.id 卡号, userName 用户名, CP.id 机器编号 , beginTime 开始时间, endTime 结束时间 ,fee 消费金额 from tbl_card C,tbl_computer CP ,tbl_record R + where C.id=R.cardI and CP.id=R.ComputerId and userName='张军' order by fee DESC +--2. 查询出每台机器上的上网次数和消费的总金额 +select CP.Id 机器编号, count(ComputerId) 上网次数, sum(fee) 消费总金额 from tbl_computer CP left join tbl_record R on R.ComputerId =CP.id group by CP.Id +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardI 网卡号 , sum(fee) 消费总金额 from tbl_record group by cardI +--4. 查询出从未消费过的上网卡的卡号和用户名 +select C.id 卡号, userName 用户名 from tbl_card C left join tbl_record R on C.id=R.cardI where fee is null +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card where passWord= userName +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 使用次数最多的机器号, count(ComputerId) 使用次数 from tbl_record group by ComputerId order by count(ComputerId) DESC +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select C.id 卡号, userName 用户名, R.ComputerId 上网机器编号 ,fee 消费金额 from tbl_card C left join tbl_record R on C.id = R.cardI +where C.id like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select R.cardI 卡号, userName 用户名, ComputerId 机器号, beginTime 开始时间, endTime 结束时间 from tbl_record R left join tbl_card C on +R.cardI=C.id where datename(DW,beginTime)='星期六' or datename(DW,beginTime)='星期天' +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select R.cardI 卡号, userName 用户名, ComputerId 机器号, beginTime 开始时间 ,endTime 结束时间 ,fee 消费金额 from tbl_record R left join tbl_card C +on R.cardI= C.id where datediff(HH,beginTime,endTime)>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardI 卡号, userName 用户名, ComputerId 机器号, beginTime 开始时间,endTime 结束时间 , fee 消费金额 from tbl_record left join tbl_card C on +cardI=C.id order by fee DESC offset 3 rows fetch next 7 rows only + + +select * from tbl_card +select * from tbl_computer +select * from tbl_record diff --git "a/2021 03 30 \344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery1.sql" "b/2021 03 30 \344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0c79b400f35b807ad8a78568b144678f7a978233 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery1.sql" @@ -0,0 +1,72 @@ +use master +go +create database Studen +on +( + name='Studen', + filename='D:\Studen.mdf', + size=10, + maxsize=200, + filegrowth=15% +) +log on +( + name='Studen_log', + filename='D:\Studen_log.ldf', + size=10, + maxsize=200, + filegrowth=15% +) +go +use Studen +go +create table stuInfo +( + stuID int primary key identity, + stuName varchar(20) not null, + stuAge varchar(10) , + stuSex int , + time datetime +) +create table courseInfo +( + courseID int primary key identity, + courseName varchar(20) not null, + courseMark varchar(10) +) +create table scoreInfo +( + scoreID int primary key identity, + stuID int , + courseID int , + score int +) +insert into stuInfo(stuName,stuAge,stuSex,time) values ('Tom','19',1,null),('Jack','20',0,null),('Rose','21',1,null),('Lulu','19',1,null),('L','21',0,null),('abc','20',1,getdate()) +select * from stuInfo +insert into courseInfo(courseName,courseMark) values ('JavaBase','4'),('HTML','2'),('JavaScript','2'),('SqlBase','2') +select * from courseInfo +insert into scoreInfo(stuID,courseID,score) 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 stuInfo +select * from courseInfo +select * from scoreInfo +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select ST.stuName 学生姓名,count(courseID) 课程数量,avg(score) 考试平均分 from stuInfo ST inner join scoreInfo SC on ST.stuID=SC.stuID group by SC.stuID,st.stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select CO.courseName 课程名称, count(SO.courseID) 选修的学生的个数 ,sum(score) 考试的总分 from courseInfo CO left join scoreInfo SO on CO.courseID=SO.courseID group by SO.courseID ,CO.courseName +--3.查询出性别一样并且年龄一样的学生的息信 +select * from stuInfo A , stuInfo B where A.stuAge=B.stuAge and A.stuSex=B.stuSex and A.stuID!=B.stuID +--4.查询出学分一样的课程信息 +select distinct * from courseInfo A inner join courseInfo B on A.courseMark=B.courseMark and A.courseName!=B.courseName +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select S.stuID 学号 ,stuName 姓名, C.courseID 课程号, score 分数 from stuInfo S ,courseInfo C ,scoreInfo SC where S.stuID=SC.stuID and C.courseID=SC.courseID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select S.stuID 学号 , C.courseID 课程号,courseName 课程名,courseMark 课程学分, score 分数 from stuInfo S ,courseInfo C ,scoreInfo SC where S.stuID=SC.stuID and C.courseID=SC.courseID +--7.查询出没有参加考试的学生的学号和姓名 + +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select S.stuID 学号, avg(score) 考试平均分 , count(SO.stuID) 选修课程的数量 from stuInfo S inner join scoreInfo SO on S.stuID=SO.stuID group by S.stuID +having avg(score)>70 and count(SO.stuID)>2 \ No newline at end of file diff --git "a/2021 03 30 \344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/SQLQuery1.sql" "b/2021 03 30 \344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c27a29ccf76da21058f353086063b35b3a3f0962 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/SQLQuery1.sql" @@ -0,0 +1,92 @@ +create database student +on +( + name='student', + filename='D:\student.mdf', + size=10, + maxsize=200, + filegrowth=20 +) +log on +( + name='student_log', + filename='D:\student_log.ldf', + size=10, + maxsize=200, + filegrowth=20 +) +go +use student +go + +create table stuInfo +( + stuId int identity(1,1) primary key not null , + stuName nvarchar(10) not null, + stuSex varchar(2) not null , + stuAge int not null, + time datetime +) +create table courseInfo +( + courseId int identity(1,1) primary key not null, + courseName nvarchar(10), + courseMarks int not null +) +create table scoreInfo +( + scoreId int identity(1,1) primary key, + stuId int references stuInfo(stuId), + courseId int references courseInfo(courseId), + score int +) +insert into stuInfo(stuName,stuAge,stuSex,time) +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-07 01:11:36.590') + +insert into courseInfo(courseName,courseMarks) +values('JavtBase','4'),('HTML','2'),('JavtScript','2'),('SqlBase','2') + +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 stuInfo +select * from courseInfo +select * from scoreInfo + +--1.查询出每个学生 所选修的课程的数量 和所选修的课程的考试的平均分 +select stuName 学生,count(courseId)选修的课程的数量,avg(score)考试的平均分 from stuInfo SU inner join scoreInfo SR on SU.stuId=SR.stuId group by stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName 课程,count(stuId)选修的学生的个数,sum(score) 学生考试的总分 from courseInfo CI INNER JOIN scoreInfo SR on CI.courseId=SR.courseId group by courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select A. * from stuInfo A , stuInfo B WHERE A.stuAge=B.stuAge and A.stuSex=B.stuSex AND A.stuId!=B.stuId + +--4.查询出学分一样的课程信息 +select A. * from courseInfo A, courseInfo B where A.courseMarks=B.courseMarks AND A.courseName!=B.courseName + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select SU.stuId 学号,stuName 姓名,courseId 课程号,score 分数 from stuInfo SU inner join scoreInfo SR on SU.stuId=SR.stuId + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select A.stuId,A.stuName,B.courseName,C.score FROM stuInfo A,courseInfo B,scoreInfo C WHERE A.stuId=C.stuId AND B.courseId=C.courseId + +--7.查询出没有参加考试的学生的学号和姓名 +select StuId 学号,StuName 姓名 from StuInfo +except +select A.StuId 学号,StuName 姓名 from ScoreInfo A +inner join StuInfo C on A.StuId = C.StuId +--8.查询出是周六周天来报到的学生 +select stuName from StuInfo where datepart(weekday,Time)=1 or datepart(weekday,Time)=7 + +--9.查询出姓名中有字母a的学生的信息 +select * from StuInfo where StuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select A.StuId 学号,AVG(Score) 平均分,count(*) 课程数量 from ScoreInfo A +inner join StuInfo C on A.StuId = C.StuId +group by A.StuId +having count(*) > 2 and AVG(Score) >70 + + + + + + diff --git "a/2021 03 30 \344\275\234\344\270\232/\350\256\270\350\207\252\346\246\225/SQLQuery2.sql" "b/2021 03 30 \344\275\234\344\270\232/\350\256\270\350\207\252\346\246\225/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a52b85a88753431dd65c3d32a8a31055e35da759 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\350\256\270\350\207\252\346\246\225/SQLQuery2.sql" @@ -0,0 +1,71 @@ + create database student +on +( + name='student', + filename='D:\student.mdf', + size=20, + maxsize=300, + filegrowth=50 +) +log on +( + name='student_log', + filename='D:\student_log.ldf', + size=20, + maxsize=300, + filegrowth=50 +) +create table stuInfo +( + stuID int primary key identity(1,1), + stuNmae nvarchar(20), + stuAge char(10), + stuSex varchar(1) , + time datetime null +) +create table courseInfo +( + courseID int primary key identity(1,1), + courseName nvarchar(20) not null , + courseMarks int +) +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int foreign key references stuInfo(stuID) not null, + courseID int foreign key references courseInfo(courseID), + score int not null check(score>=0 and score<=100) +) +insert into stuInfo(stuNmae,stuAge,stuSex,time)values('TOM',19,'1',''),('Jack',20,'0',''), +('Rose',21,'1',''),('Lulu',19,'1',''),('Lili',21,'0',''),('abc',20,'1','2007-01-07 01:11:36.590') +insert into courseInfo(courseName,courseMarks)values('JavaBase', 4),('HTML', 2),('JavaScript', 2),('SqlBase', 4) +insert into scoreInfo(stuID,courseID,score)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) + + + +select *from courseInfo +select *from scoreInfo +select *from stuInfo + +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select courseID,count(courseName),avg(courseMarks) from courseInfo group by courseID + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName,count(courseName),sum(courseMarks) from courseInfo group by courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select A * from StudenInfo A,StudenInfo B where A.stuAge=B.stuAge and A.stuAge and A.stuID! =B.stuID +--4.查询出学分一样的课程信息 +select courseName,count(courseName),sum(courseMarks) from courseInfo group by courseName +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select A * from StudenInfo A,StudenInfo B where A.stuAge=B.stuAge and A.stuAge and A.stuID! =B.stuID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select courseID,count(courseName),avg(courseMarks) from courseInfo group by courseID +--7.查询出没有参加考试的学生的学号和姓名 +select courseID,count(courseName),avg(courseMarks) from courseInfo group by courseID +--8.查询出是周六周天来报到的学生 +select courseName,count(courseName),sum(courseMarks) from courseInfo group by courseName +--9.查询出姓名中有字母a的学生的信息 +select A * from StudenInfo A,StudenInfo B where A.stuAge=B.stuAge and A.stuAge and A.stuID! =B.stuID +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select courseID,count(courseName),avg(courseMarks) from courseInfo group by courseID diff --git "a/2021 03 30 \344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/SQLQuery2.sql" "b/2021 03 30 \344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4dc276109f1b5476735a11644163a1ca18c447a4 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/SQLQuery2.sql" @@ -0,0 +1,101 @@ +create database Student +on( +name=Student, +filename='D:\Student.mdf', +size=10mb, +maxsize=50mb, +filegrowth=10% +) +log on +( +name=Student_log, +filename='D:\Student_log.mdf', +size=10mb, +maxsize=50mb, +filegrowth=10% + +) + +create table stuInfo +( +stuID int not null primary key identity(1,1), +stuName nvarchar(10) not null, +stuAge int not null, +stuSex nchar check(stuSex='男' or stuSex ='女') , +time char(50) +) +insert into stuInfo values ('TOM',19,'男',NULL),('JACK',20,'女',NULL),('ROSE',21,'男',NULL),('LULU',19,'男',NULL),('LILI',21,'女',NULL),('ABC',20,'男','2007-01-07 01:11:36:590') +select * from stuInfo +create table courseInfo +( +courseID int primary key identity(1,1) not null, +sourseName nvarchar(10) not null, +courseMarks int + +) +insert into courseInfo values ('SQIBASE',4),('JAVASCRIPT',2),('HTML',2),('JAVABASE',2) +select * from courseInfo +create table scoreInfo +( +scoreID int primary key identity(1,1) not null, +stuID int references stuInfo(stuID) not null, +courseID int references courseInfo(courseID) not null, +score int not null +) +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 + +select * from scoreInfo +select * from stuInfo +select *from courseInfo + + +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuname 学生,count(*)课程数量,AVG(score) 平均分 from scoreinfo + inner join stuInfo on stuInfo.stuid=scoreInfo.stuID + inner join courseInfo on courseInfo. courseID=scoreinfo.courseID + group by stuname +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select soursename 课程,count(*)学生个数,SUM(score) 总分 from scoreInfo +inner join stuInfo on stuInfo.stuid=scoreInfo.stuID + inner join courseInfo on courseInfo. courseID=scoreinfo.courseID + group by sourseName +--3.查询出性别一样并且年龄一样的学生的信息 +select A.* from stuInfo A,stuInfo B +where A.stuAge=B.stuAge and A.stuSex=B.stuSex and A.stuID !=B.stuID +--4.查询出学分一样的课程信息 +select A.* from courseInfo A, courseInfo B +where A.courseMarks=B.courseMarks and A.courseID != B.courseID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuInfo.stuID,stuName,courseID,score from stuInfo +inner join scoreInfo on scoreInfo.stuID=stuInfo.stuID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuInfo.stuID,courseinfo.courseID,score,soursename,courseMarks from stuInfo +inner join scoreInfo on scoreInfo.stuID=stuInfo.stuID +inner join courseInfo on scoreInfo.courseID=courseInfo.courseID + +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuID,stuName from stuInfo full join scoreInfo on stuInfo.stuID =scoreInfo.stuID +where score is null +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuInfo.stuID 学号,avg(score) 考试平均分,count(*) 选修课程的数量 from stuInfo +inner join scoreInfo on scoreInfo.stuID=stuInfo.stuID +inner join courseInfo on scoreInfo.courseID=courseInfo.courseID +group by stuInfo.stuID +HAVING count(stuInfo.stuID +)>2 and avg(score)>70 + diff --git "a/2021 03 30 \344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/SQLQuery1.sql" "b/2021 03 30 \344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..62ba57f66f25e97a7454c4d6ca92db1a29873e58 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/SQLQuery1.sql" @@ -0,0 +1,101 @@ +use master +go + +create database students +go + +use students +go + +create table stuInfo +( + stuID int primary key identity, + stuName varchar(20), + stuAge int, + stuSex varchar(2), + time datetime +) +go + +create table courseInfo +( + courseID int primary key identity, + courseName varchar(20), + courseMarks int +) +go + +create table scoreInfo +( + scoreID int primary key identity, + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int +) +go + +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-07 01:11:36') +go + +insert into courseInfo values('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) +go + +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) +go + + +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select SC.courseID,count(SC.courseID)课程的数量,avg(score)平均分 +from stuInfo ST inner join scoreInfo SC on ST.stuID=SC.stuID +inner join courseInfo CI on CI.courseID=SC.courseID +group by SC.courseID,courseMarks + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select CI.courseID,sum(score)总分 +from scoreInfo SI inner join courseInfo CI on SI.courseID=CI.courseID +group by CI.courseID + +--3.查询出性别一样并且年龄一样的学生的信息 +select * from stuInfo A +where(select count(*) from stuInfo where stuSex=A.stuSex and stuAge=A.stuAge)>1 + +--4.查询出学分一样的课程信息 +select A.* from courseInfo A +where(select count(*) from courseInfo where courseMarks=A.courseMarks)>1 + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select ST.stuID,stuName,courseID,score +from scoreInfo SC inner join stuInfo ST on ST.stuID=SC.stuID +group by ST.stuID,stuName,courseID,score + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select ST.stuID,CI.courseID,courseName,courseMarks,SI.score +from courseInfo CI inner join scoreInfo SI on CI.courseID=SI.courseID +inner join stuInfo ST on SI.stuID=ST.stuID + +--7.查询出没有参加考试的学生的学号和姓名 +select ST.stuID,stuName +from stuInfo ST left join scoreInfo SI on ST.stuID=SI.stuID +except +select SI.stuID,stuName +from stuInfo A inner join scoreInfo SI on A.stuID=SI.stuID + +--8.查询出是周六周天来报到的学生 +select * from stuInfo where datepart(weekday,time+@@datefirst-1) in (6,7) + +--9.查询出姓名中有字母a的学生的信息 +select * +from stuInfo ST full join scoreInfo SI on ST.stuID=SI.stuID +where stuName like '%a%' + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select SI.stuID,avg(score) 平均分,count(courseID) 选修课程的数量 +from scoreInfo SI inner join stuInfo ST on ST.stuID=SI.stuID +group by SI.stuID +having count(courseID)>2 and avg(score)>70 + + +select * from stuInfo +select * from courseInfo +select * from scoreInfo \ No newline at end of file diff --git "a/2021 03 30 \344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery7.sql" "b/2021 03 30 \344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery7.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0eff723fa26da8bd0fdd405e57fa3f2ee5bca6fb --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery7.sql" @@ -0,0 +1,83 @@ +use master +go +create database Student +on +( +name='Student', +filename='D:\SQLcunchu\Student.mdf', +size=5mb, +maxsize=100mb, +filegrowth=10% +) +log on +( +name='Student_log', +filename='D:\SQLcunchu\Student_log.ldf', +size=5mb, +maxsize=100mb, +filegrowth=10% +) +go +use Student +go +create table stuInfo +( +stuID int primary key identity(1,1), +stuName nvarchar(20) , +stuAge char(150) , +stuSex char(1) check(stuSex in (1,0)), +time datetime +) +create table courseInfo +( +courseID int primary key identity(1,1), +courseName nvarchar(10), +courseMarks char(100) check(courseMarks>=0 and courseMarks<=10) +) +create table scoreInfo +( +scoreID int primary key identity(1,1), +stuID int references stuInfo(stuID), +courseID int references courseInfo(courseID), +score int check(score>=0 and score<=100) +) +insert into stuInfo(stuName,stuAge,stuSex,time)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-07 01:11:36.590') +insert into courseInfo(courseName,courseMarks)values +('JavaBase',4 ), +('HTML',2), +('JavaScript',2), +('SqlBase',2) +insert into scoreInfo(stuID,courseID,score)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 stuInfo +select *from courseInfo +select *from scoreInfo +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName 学生名字,count(*)课程数量,Avg(score)平均分 from courseInfo,scoreInfo,stuInfo where courseInfo.courseID=scoreInfo.courseID group by stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName 课程名称,count(*)学生个数,sum(score)学生考试总分 from courseInfo inner join scoreInfo on courseInfo.courseID=scoreInfo.courseID group by courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select A.*from stuInfo A inner join (select stuAge,stuSex from stuInfo group by stuAge,stuSex having count(stuAge)>1 and count(stuSex)>1) B on A.stuAge=B.stuAge and A.stuSex=B.stuSex +--4.查询出学分一样的课程信息 +select A.courseName,A.courseMarks from courseInfo A,courseInfo B where A.courseMarks=B.courseMarks and A.courseID !=B.courseID group by A.courseMarks,A.courseName +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuInfo.stuID 学生学号,stuName 学生姓名,courseID 课程编号,score 分数 from stuInfo right join scoreInfo on stuInfo.stuID = scoreInfo.stuID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuName 学生的姓名,scoreInfo.stuID 学生的学号,scoreInfo.courseID 课程号,courseName 课程名,courseMarks 课程学分,score 分数 from scoreInfo +inner join courseInfo on scoreInfo.courseID=courseInfo.courseID +inner join stuInfo on stuInfo.stuID=scoreInfo.stuID +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuID 学生学号,stuName 学生姓名 from stuInfo full join scoreInfo on stuInfo.stuID =scoreInfo.stuID where score is null +--8.查询出是周六周天来报到的学生 +select stuName 学生姓名 from stuInfo where time=7 and time=6 +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuID 学号,count(stuID)选修课程数,avg(score)考试平均分 from scoreInfo group by stuID having count(stuID)>2 and avg(score)>70 diff --git "a/2021 03 30 \344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/0330.sql" "b/2021 03 30 \344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/0330.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3e0af2900e5e3c0cb7a906c3a0283f6be9ea8ba0 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/0330.sql" @@ -0,0 +1,125 @@ +use master +go +create database Student +on +( + name='Student', + filename='D:\Student.mdf', + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) +log on +( + name='Student_log', + filename='D:\Student_log.ldf', + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) +go +use Student +go + +create table stuInfo +( + stuID int primary key identity(1,1), + stuName nvarchar(20), + stuAge varchar(20), + StuSex char(2) default('1') check(StuSex in('1','0')), + time datetime +) + +create table courseInfo +( + courseID int primary key identity(1,1), + courseName varchar(20), + courseMarks varchar(20) +) + +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int +) + +insert into stuInfo(stuName,stuAge,StuSex) values('Tom',19,1), +('Jack',20,0), +('Rose',21,1), +('Lulu',19,1), +('Lili',21,0), +('abc',20,1) +update stuInfo set time='2007-01-07 01:11:36.590' where stuID=6 +--'2007-01-07 01:11:36.590' + +insert into courseInfo values ('JavaBase',4), +('HTML',2), +('JavaScript',2), +('SqlBase',2) + +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 stuInfo +select * from courseInfo +select * from scoreInfo + +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) + +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 + +select SI.stuName 学生姓名,COUNT(*) 课程的数量, AVG(score) 课程的考试的平均分 from scoreInfo SC inner join stuInfo SI on SC.stuID=SI.stuID +inner join courseInfo CI on CI.courseID=SC.courseID group by SI.stuName + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 + +select CI.courseName 课程名称,COUNT(*) 选修的学生的个数, SUM(score)学生考试的总分 from scoreInfo SC inner join stuInfo SI on SC.stuID=SI.stuID +inner join courseInfo CI on CI.courseID=SC.courseID group by CI.courseName + +--3.查询出性别一样并且年龄一样的学生的信息 + +select A. * from stuInfo A ,stuInfo B where A.stuAge=B.stuAge AND A.StuSex=B.StuSex AND A.stuID != B.stuID + +--4.查询出学分一样的课程信息 + +select distinct A. * from courseInfo A ,courseInfo B where A.courseMarks=B.courseMarks AND A.courseID != B.courseID + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 + +select SI.stuID 学号,stuName 姓名,CI.courseID 课程号,score 分数 from scoreInfo SC inner join stuInfo SI on SC.stuID=SI.stuID +inner join courseInfo CI on CI.courseID=SC.courseID group by SI.stuID,stuName,CI.courseID,score + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 + +select SI.stuID 学号,CI.courseID 课程号,courseName 课程名,courseMarks 课程学分,score 分数 from scoreInfo SC inner join stuInfo SI on SC.stuID=SI.stuID +inner join courseInfo CI on CI.courseID=SC.courseID group by SI.stuID,CI.courseID,courseName,courseMarks,score + +--7.查询出没有参加考试的学生的学号和姓名 +select stuID 学号,stuName 姓名 from stuInfo +except +select SI.stuID 学号,stuName 姓名 from scoreInfo SC inner join stuInfo SI on SC.stuID=SI.stuID group by SI.stuID,stuName + +--8.查询出是周六周天来报到的学生 + +select * from stuInfo where time between '2007-01-05' and '2007-01-08' + +--9.查询出姓名中有字母a的学生的信息 + +select * from stuInfo where stuName like '%a%' + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 + +select SI.stuID 学号,AVG(score) 考试平均分,COUNT(*)选修课程的数量 from scoreInfo SC inner join stuInfo SI on SC.stuID=SI.stuID +inner join courseInfo CI on CI.courseID=SC.courseID group by SI.stuID having count(*)>2 and avg(score)>=70 diff --git "a/2021 03 30 \344\275\234\344\270\232/\351\237\246\345\244\251\347\277\224/SQLQuery1.sql" "b/2021 03 30 \344\275\234\344\270\232/\351\237\246\345\244\251\347\277\224/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0a4f6f5036ee5f382f7da928ebad81c0856725d3 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\351\237\246\345\244\251\347\277\224/SQLQuery1.sql" @@ -0,0 +1,128 @@ + +create database students +go + +use students +go + +create table stuInfo +( + stuId int identity primary key, + stuName varchar(10) not null , + stuAge char(2) not null, + stuSex char(1) not null check(stuSex = '1' or stuSex = '0') , + time datetime +) +go + +create table courseInfo +( + courseId int identity primary key, + courseName varchar(10) not null, + courseMarks int not null +) +go + +create table scoreInfo +( + scoreId int identity primary key , + stuId int references stuInfo(stuId), + courseId int references courseInfo(courseId), + score int +) +go + + +insert into stuInfo(stuName,stuAge,stuSex,time) +select 'Tom','19','1','' union +select 'Jack','20','0','' union +select 'Rose','21','1','' union +select 'Lulu','19','1','' union +select 'Lili','21','0','' union +select 'abc','20','1','2007-01-07 01:11:36.590' + +insert into courseInfo(courseName,courseMarks) +select 'JavaBase','4' union +select 'HTML','2' union +select 'JavaScript','2' union +select 'SqlBase','2' + +insert into scoreInfo(stuId,courseId,score) +select '1','1','80' union +select '1','2','85' union +select '1','4','50' union +select '2','1','75' union +select '2','3','45' union +select '2','4','75' union +select '3','1','45' union +select '4','1','95' union +select '4','2','75' union +select '4','3','90' union +select '4','4','45' + + + + +--1.查询出 每个学生 所选修的课程的数量和所选修的课程的考试的平均分 + +select si.stuName 学生,COUNT(sci.stuId) 选课数量,AVG(score) 考试平均分 from stuInfo si + inner join scoreInfo sci on si.stuId = sci.stuId + group by si.stuId,stuName + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 + +select courseName 课程名称,COUNT(si.courseId) 学生人数,SUM(score) 总分 from courseInfo ci + inner join scoreInfo si on ci.courseId = si.courseId + group by ci.courseId , courseName + +--3.查询出性别一样并且年龄一样的学生的信息 + +select * from stuInfo A,stuInfo B + where a.stuAge = b.stuAge and a.stuSex = b.stuSex and a.stuId != b.stuId + +--4.查询出学分一样的课程信息 + +select * from courseInfo ci where + ( + select COUNT(*) from courseInfo where + courseMarks = ci.courseMarks + )>1 + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 + +select si.stuId ,stuName ,scoreId ,score from stuInfo si + inner join scoreInfo sci on sci.stuId = si.stuId + group by si.stuId ,stuName ,scoreId ,score + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 + +select si.stuId 学号,ci.courseId 课程号,courseName 课程名,courseMarks 课程学分,score 分数 from scoreInfo sci + inner join courseInfo ci on ci.courseId = sci.courseId + inner join stuInfo si on si.stuId = sci.stuId + +select * from stuInfo +select * from courseInfo +select * from scoreInfo + +--7.查询出没有参加考试的学生的学号和姓名 + + select * from stuInfo si + left join scoreInfo sci on si.stuId=sci.stuId + where score IS NULL + +--8.查询出是周六周天来报到的学生 + +select * from stuInfo + where datepart(weekday,time)=1 or datepart(weekday,time)=7 + +--9.查询出姓名中有字母a的学生的信息 + +select * from stuInfo where stuName like '%a%' + +--10.查询出 选修了2门课程以上 的并且 考试平均分在70以上 的 学生的学号和考试平均分以及选修课程的数量 + +select si.stuId 学号,AVG(score) 考试平均分,COUNT(sci.stuId) 选课数量 from stuInfo si + inner join scoreInfo sci on si.stuId = sci.stuId + group by si.stuId + + diff --git "a/2021 03 30 \344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery1.sql" "b/2021 03 30 \344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6efbcad3bfd0d852826f73bf24ad6fec0de269d8 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery1.sql" @@ -0,0 +1,81 @@ +use master +go +create database lnl +on +( + name='list', + filename='D:\lnl.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='list_log', + filename='D:\lnl_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +use lnl +go +create table Suddny +( +stuid int identity(1,1) primary key not null, +stuName varchar(5) not null, +stuAge int not null, +stuSex int not null, +time datetime +) +go +create table Abcde +( +courseld int identity(1,1) primary key not null, +courseName varchar(11) not null, +courseMarks int not null +) +go +create table Fsese +( +scoreid int identity(1,1) not null, +stuid int references Suddny(stuid) not null, +courseid int references Abcde(courseld) not null, +score int not null +) +go +insert into Suddny(stuName,stuAge,stuSex,time) 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-07 01:11:36.590') +select * from Suddny + +insert into Abcde(courseName,courseMarks) values +('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) +select * from Abcde + +insert into Fsese(stuid,courseid,score) 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 Fsese +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName 学生名字,count(Fsese.stuID) 选修课程数量,avg(score) 选修的课程平均分 from Suddny inner join Fsese on Suddny.stuID = Fsese.stuID group by stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName 课程名字,count(Fsese.courseID) 学生的个数,sum(score) 总分 from Abcde inner join Fsese on Abcde.courseld = Fsese.courseID group by courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select A. * from Suddny A,Suddny B where A.stuAge=B.stuAge and A.stuSex=B.stuSex and A.stuID!=B.stuID +--4.查询出学分一样的课程信息 +select A. * from Abcde A,Abcde B where A.courseMarks=B.courseMarks and A.courseld!=B.courseld +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select Suddny.stuID 学号,stuName 姓名,Abcde.courseld 课程号,score 分数 from Suddny inner join Fsese on Fsese.stuID=Suddny.stuID inner join Abcde on Abcde.courseld=Fsese.courseID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select Suddny.stuID 学号,Abcde.courseld 课程号,courseName 课程名,courseMarks 课程学分,score 分数 from Suddny inner join Fsese on Fsese.stuID=Suddny.stuID inner join Abcde on Abcde.courseld=Fsese.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select stuID 学号,stuName 姓名 from Suddny except select Suddny.stuID 学号,stuName 姓名 from Fsese inner join Suddny on Fsese.stuID=Suddny.stuID group by Suddny.stuID,stuName +--8.查询出是周六周天来报到的学生 +select * from Suddny where time between '2007-01-05' and '2007-01-08' +--9.查询出姓名中有字母a的学生的信息 +select * from Suddny where stuName like'%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select Suddny.stuID 学生ID,COUNT(*) 课程数量,AVG(score) 考试平均分数 from Suddny inner join Fsese on Fsese.stuID=Suddny.stuID group by Suddny.stuID having AVG(score)>70 and COUNT(*)>2 + diff --git "a/2021 03 30 \344\275\234\344\270\232/\351\253\230\345\245\225/Students.sql" "b/2021 03 30 \344\275\234\344\270\232/\351\253\230\345\245\225/Students.sql" new file mode 100644 index 0000000000000000000000000000000000000000..303d18115343b07507951b5a7cbaa58ed8bd82ee --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\351\253\230\345\245\225/Students.sql" @@ -0,0 +1,130 @@ +use master +go + +create database Student +on +( + name='Student', + filename='D:\Student.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='Student_log', + filename='D:\Student_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use Student +go + + +create table stuInfo +( + stuID int primary key identity(1,1), + stuName char(10), + stuAge int, + stuSex int check(stuSex=1 or stuSex=0), + time datetime, +) +go +create table courseInfo +( + courseID int primary key identity(1,1), + courseName char(20), + courseMarks int, +) +go + +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int +) +go + +insert into stuInfo (stuName,stuAge,stuSex,time) 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-07') + +select * from stuInfo + + +insert into courseInfo (courseName,courseMarks) values ('JavaBase',4), +('HTML',2), +('JavaScript',2), +('SqlBase',2) + +select * from courseInfo + +insert into scoreInfo (stuID,courseID,score) 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) + + + + +--1.查询出 每个学生 所 选修的课程的数量和 所选修的课程的 考试的平均分 +select stuName as 学生姓名,count(scoreInfo.stuID) as 选修的课程数 ,avg(score) as 考试的平均分 from scoreInfo +full join stuInfo on scoreInfo.stuID=stuInfo.stuID +group by stuName + + +--2.查询出 每门课程的 选修的学生的个数和 学生考试的总分 +select courseName,count(scoreInfo.courseID)选修的学生的个数,sum(score)学生考试的总分 from scoreInfo +inner join courseInfo on scoreInfo.courseID=courseInfo.courseID +group by courseName + + +--3.查询出性别一样并且年龄一样的学生的信息 +select A.* from stuInfo A,stuInfo B +where A.stuAge=B.stuAge and A.stuSex=B.stuSex and A.stuID !=B.stuID + +--4.查询出学分一样的课程信息 +select distinct B.* from courseInfo A,courseInfo B +where A.courseMarks=B.courseMarks and A.courseID !=B.courseID and A.courseName !=B.courseName + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuInfo.stuID,stuName,courseID,score from stuInfo +right join scoreInfo on scoreInfo.stuID=stuInfo.stuID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuName,scoreInfo.stuID,scoreInfo.courseID,courseName,courseMarks,score from scoreInfo +inner join courseInfo on scoreInfo.courseID=courseInfo.courseID +inner join stuInfo on stuInfo.stuID=scoreInfo.stuID + + + +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuID,stuName from stuInfo full join scoreInfo on stuInfo.stuID =scoreInfo.stuID +where score is null + + +--8.查询出是周六周天来报到的学生 +select * from stuInfo +where datepart(weekday,time)=1 or datepart(WEEKDAY,time)=7 + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' + + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuID as 学号,count(stuID)选修课程的数量,avg(score)考试平均分 from scoreInfo group by stuID having count(stuID)>2 and avg(score)>70 diff --git "a/2021 03 30 \344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery7.sql" "b/2021 03 30 \344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery7.sql" new file mode 100644 index 0000000000000000000000000000000000000000..467088810c78440ff8484063c76313db0c1a9a71 --- /dev/null +++ "b/2021 03 30 \344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery7.sql" @@ -0,0 +1,99 @@ +use master +go +create database Student +on +( + name='Student', + filename='D:\Student.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='Student_log', + filename='D:\Student_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use Student +go + +create table stuInfo +( + StuId int primary key identity(1,1), + StuName nvarchar(20) not null, + StuAge int, + StuSex nvarchar(1) default(1) check(StuSex=1 or StuSex=0), + time datetime +) +insert into stuInfo(StuName,StuAge,StuSex,time) values +('TOP',19,1,null), +('jack',20,0,null), +('Rose',21,1,null), +('Lulu',19,1,null), +('Lili',21,0,null), +('abc',20,1,'2007-01-07') + +select *from stuInfo + +create table courseInfo +( + CourseId int primary key identity(1,1), + courseName nvarchar(20) unique not null, + courseMarks nvarchar(10) +) + +insert into courseInfo (courseName,courseMarks) values ('javabase',4),('HTML',2),('JAVAscript',2),('SQL',2) + +select *from courseInfo + +create table Score +( + ScoreId int primary key identity(1,1), + StuId int references stuInfo(StuId), + CourseId int references courseInfo(CourseId), + Score int not null +) +insert into Score(StuId,CourseId,Score) values (1,1,80), +(1,2,85),(1,4,50),(2,1,75),(2,3,45),(2,4,75),(1,1,80),(3,1,45),(4,1,95),(4,2,75), +(4,3,90),(4,4,45) +select *from Score + +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) +select *from stuInfo +select *from courseInfo +select *from Score +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 + +select StuName,count(*) 数量,avg (Score)平均分 from Score + inner join stuInfo on stuInfo.StuId=Score.StuId group by StuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select StuName,count(*)个数,sum(Score)总分 from Score + inner join stuInfo on stuInfo.StuId=Score.StuId group by StuName +--3.查询出性别一样并且年龄一样的学生的信息 +select * from stuInfo A, stuInfo b where A.StuAge=B.StuAge AND A.StuSex=B.StuSex AND A.StuId !=B.StuId +--4.查询出学分一样的课程信息 +select * from courseInfo A, courseInfo b where A.courseMarks=B.courseMarks AND A.CourseId !=B.CourseId +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuInfo.StuId 学号,StuName 姓名,courseInfo.CourseId 课程号,Score 分数 from Score inner join courseInfo on Score.CourseId=courseInfo.CourseId + inner join stuInfo on stuInfo.StuId=Score.StuId group by stuInfo.StuId,courseInfo.CourseId,StuName,Score +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuInfo.StuId 学号,StuName 姓名,courseInfo.CourseId 课程号,courseMarks 课程学分 ,Score 分数 from Score inner join courseInfo on Score.CourseId=courseInfo.CourseId + inner join stuInfo on stuInfo.StuId=Score.StuId group by stuInfo.StuId,courseInfo.CourseId,StuName,courseMarks,Score +--7.查询出没有参加考试的学生的学号和姓名 +select StuId 学号,StuName 姓名 from stuInfo where StuId=StuId +except +select Score.StuId,StuName from stuInfo ,Score where Score.StuId =stuInfo.StuId group by +Score.StuId,StuName +--8.查询出是周六周天来报到的学生 +select * from stuInfo where datepart(weekday,Time)=6 or datepart(weekday,Time)=7 +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where StuName like 'a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 + +select StuId 学号,avg(Score)平均分,count (*)数量 from courseInfo,Score where courseInfo.CourseId>2 group by StuId having avg(Score)>70 \ No newline at end of file diff --git a/2021 03 31/.keep b/2021 03 31/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/2021 03 31/\345\205\260\351\246\250\345\204\277/\350\257\276\345\220\216\344\275\234\344\270\2322.sql" "b/2021 03 31/\345\205\260\351\246\250\345\204\277/\350\257\276\345\220\216\344\275\234\344\270\2322.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c53c8fc61ff88b79f7deff30fe393f1520b9d9ff --- /dev/null +++ "b/2021 03 31/\345\205\260\351\246\250\345\204\277/\350\257\276\345\220\216\344\275\234\344\270\2322.sql" @@ -0,0 +1,107 @@ +use master +go +create database WB +on +( +name = 'WB', +filename = 'D:\ WB.maf', +size = 5MB, +maxsize = 50MB, +filegrowth = 10% +) +log on +( +name = 'WB_log', +filename = 'D:\ WB_log.maf', +size = 5MB, +maxsize = 50MB, +filegrowth = 10% +) +go +use WB +go + +create table Tbl_card +( +cardId varchar(20) primary key not null, +passWord varchar(20) unique not null, +balance money not null, +userName varchar(10) +) + +create table Tbl_computer +( +ComputerId varchar(10) primary key not null, +onUse int check(onUse in(0,1)), +note text +) + +create table Tbl_record +( +record int primary key not null, +cardId varchar(20) references Tbl_card(cardId) not null, +ComputerId varchar(10) references Tbl_computer(ComputerId), +beginTime datetime, +endTime datetime, +fee money +) + +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-25 22:55:00',50), + (46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-25 22:55:00',6), + (47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-25 22:55:00',50), + (48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00',6), + (55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-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-12-28 18:00:00','2006-12-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 Tbl_card.cardId 卡号,userName 用户名,ComputerId 机器编号, beginTime 开始时间,endTime 结束时间,fee 消费金额 from Tbl_record + inner join Tbl_card on Tbl_record.cardId = Tbl_card.cardId + where userName = '张军' + order by fee DESC + --2. 查询出每台机器上的上网次数和消费的总金额 + select ComputerId,count(cardId),sum(fee)消费总金额 from Tbl_record + group by ComputerId + --3. 查询出所有已经使用过的上网卡的消费总金额 + select cardId ,sum(fee) 消费总金额 from Tbl_record + group by cardId + --4. 查询出从未消费过的上网卡的卡号和用户名 + select Tbl_record.cardId , userName from Tbl_record + right join Tbl_card on Tbl_record.cardId = Tbl_card.cardId + where fee is null + --5. 将密码与用户名一样的上网卡信息查询出来 + select * from Tbl_card where passWord = userName + --6. 查询出使用次数最多的机器号和使用次数 + select TOP 1 ComputerId,count(cardId) from Tbl_record + group by ComputerId + order by count(cardId) DESC + --7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + select Tbl_record.cardId,userName,ComputerId,fee from Tbl_card + inner join Tbl_record on Tbl_card.cardId = Tbl_record.cardId + where Tbl_record.cardId like '%ABC' + --8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select Tbl_card.cardId,userName,ComputerId,beginTime ,endTime ,fee from Tbl_record + inner join Tbl_card on Tbl_record.cardId = Tbl_card.cardId + where datename(DW,beginTime )= '星期六' and datename(DW,beginTime )= '星期日' + --9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select Tbl_card.cardId,userName,ComputerId,beginTime ,endTime ,fee from Tbl_record + inner join Tbl_card on Tbl_record.cardId = Tbl_card.cardId + where datediff(HH,beginTime,endTime) > 12 + --10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select top 3 Tbl_card.cardId,userName,ComputerId,beginTime ,endTime ,fee from Tbl_record + inner join Tbl_card on Tbl_record.cardId = Tbl_card.cardId + order by fee DESC diff --git "a/2021 03 31/\345\210\230\346\200\235\347\233\210/SQLQuery1.sql" "b/2021 03 31/\345\210\230\346\200\235\347\233\210/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..200d07859deb1836f05a82d877c76d35d1fbd95b --- /dev/null +++ "b/2021 03 31/\345\210\230\346\200\235\347\233\210/SQLQuery1.sql" @@ -0,0 +1,85 @@ +use master +go +create database WanBa +on +( +name='WanBa', +filename='D:\WanBa.mdf', +size=10, +maxsize=100, +filegrowth=10% +) +log on +( +name='WanBa_log', +filename='D:\WanBa_log.ldf', +size=10, +maxsize=100, +filegrowth=10% +) +go +use WanBa +go +create table tbl_card +( +id varchar(10) primary key , +password varchar(10) , +balance int , +userName nvarchar(5) +) +create table tbl_computer +( +id char(5) primary key, +onUse int check(onUse in(0,1)), +note text +) +create table tbl_record +( +id int primary key , +cardId varchar(10) references tbl_card(id), +ComputerId char(5) references tbl_computer(id), +beginTime datetime , +endTime datetime, +fee int +) +insert into tbl_card values('0023_ABC','555',98,'张军'),('0025_bbd','abe',300,'朱俊'), + ('0036_DDD','何柳',100,'何柳'),('0045_YGR','0045_YGR',58,'证验'), + ('0078_RIV','55885g',600,'校庆'),('0089_EDE','zhang',134,'张峻') +insert into tbl_computer values('02',0,'25555'),('03',1,'55555'), + ('04',0,'66666'),('05',1,'88888'), + ('06',0,'688879'),('B01',0,'558558') +insert into tbl_record values (23,'0078_RIV','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-22 15: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 15:26:00','2007-01-06 15:55:00',6), + (55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00',50), + (64,'0045_YGR','04','2006-12-24 15:26:00','2006-12-23 22:55:00',3), + (65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00',23), + (98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00',23) +-- 请完成以下题目: +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +SELECT cardId ,ComputerId,userName,beginTime,endTime,fee FROM tbl_record +inner join tbl_card on tbl_record.cardId=tbl_card.id +where userName='张军'order by fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId, count(id)上网次数,sum(fee)消费的总金额 from tbl_record group by ComputerId +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardId,sum(fee)消费总金额 from tbl_record group by cardId +--4. 查询出从未消费过的上网卡的卡号和用户名 +select tbl_card.id,userName from tbl_record right join tbl_card on tbl_record.cardId=tbl_card.id where fee is null +--5. 将密码与用户名一样的上网卡信息查询出来 +select passWord,userName from tbl_card where userName=passWord +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId,count(ComputerId) from tbl_record group by ComputerId order by count(ComputerId) desc +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +SELECT tbl_card.id,userName,ComputerId,fee FROM tbl_card inner join tbl_record on tbl_card.id=tbl_record.cardId where tbl_card.id='%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +SELECT TOP 3 cardId,userName,ComputerId ,beginTime,endTime,fee FROM tbl_record INNER JOIN tbl_card ON tbl_record.cardId=tbl_card.id where beginTime='' and beginTime='' +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +SELECT cardId,userName,ComputerId ,beginTime,endTime,fee FROM tbl_record INNER JOIN tbl_card ON tbl_record.cardId=tbl_card.id WHERE endTime-beginTime>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +SELECT TOP 3 cardId,userName,ComputerId ,beginTime,endTime,fee FROM tbl_record INNER JOIN tbl_card ON tbl_record.cardId=tbl_card.id ORDER BY fee desc + + \ No newline at end of file diff --git "a/2021 03 31/\345\210\230\346\257\205/SQLQuery1.sql" "b/2021 03 31/\345\210\230\346\257\205/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..72084a5b46a8988819f3a48e2c033b03bc445826 --- /dev/null +++ "b/2021 03 31/\345\210\230\346\257\205/SQLQuery1.sql" @@ -0,0 +1,88 @@ +create database tbl +on +( name='tbl', + filename='D:\tbl.mdf' + + +) +log on +( name='tbl_log', + filename='D:\tbl_log.ldf' + + + +) +go +use tbl +create table tbl_card +( + id varchar(20) primary key not null, + passWord nvarchar(20) not null, + balance int not null , + userName nvarchar(20) not null + + +) +create table tbl_computer +( + id varchar(20) primary key not null , + onUse varchar(2) check(onUse in ('0','1')) not null, + note nvarchar(20) + + + + ) + create table tbl_record + ( + id varchar(20) primary key not null, + cardId varchar(20) references tbl_card(id), + ComputerId varchar(20) references tbl_computer(id), + beginTime datetime , + endTime datetime, + 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','张峻') + select * from tbl_card + insert into tbl_computer values('02','0','25555'),('03','1','55555'),('04','0','66666'),('05','1','88888'),('06','0','688878'),('B01','0','558558') + select * from tbl_computer + 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-22 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 15:26:00','2007-01-06 22:55:00','6'), + ('55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), + ('64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','30'), + ('65','0025_bbd','02','2006-12-28 18:00:00','2006-12-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_record + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where userName='张军' order by fee desc + + select ComputerId,COUNT(cardId),SUM(fee) from tbl_record group by ComputerId + + select cardId,SUM(fee) from tbl_record left join tbl_card on tbl_record.cardId=tbl_card.id group by cardId + + select tbl_card.id,userName from tbl_card left join tbl_record on tbl_record.cardId=tbl_card.id where fee is null + + select a.* from tbl_card a,tbl_card b where a.passWord=b.userName + + select top 1 ComputerId,count(id) from tbl_record group by ComputerId order by count(id)desc + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where cardId like '%_ABC' + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where datepart(weekday,endTime)=1 or datepart(weekday,endTime)=7 + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where datediff(HH,beginTime,endTime) >12 + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id + except + select top 3 cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id order by fee desc + + + + diff --git "a/2021 03 31/\345\210\230\346\261\211\346\226\207/SQLQuery1.sql" "b/2021 03 31/\345\210\230\346\261\211\346\226\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8cb026f7077d908f385d9b6a603c701a1bc322c3 --- /dev/null +++ "b/2021 03 31/\345\210\230\346\261\211\346\226\207/SQLQuery1.sql" @@ -0,0 +1,123 @@ +use master +go + +create database WB +on +( +name='WB', +filename='D:\test\WB.mdf', +size=10mb, +maxsize=50mb, +filegrowth=10% +) +log on +( +name='WB_log', +filename='D:\test\WB_log.ldf', +size=10mb, +maxsize=50mb, +filegrowth=10% +) + +go +use WB + +go +create table tbl_card +( +id nvarchar(20) primary key, +passWord varchar(20), +balance money, +userName nvarchar(20) +) + +go +create table tbl_computer +( +id varchar(20) primary key, +onUse varchar(10) check(onUse=1 or onUse=0), +note text +) + +go +create table tbl_record +( +id int primary key, +cardId nvarchar(20) references tbl_card(id), +ComputerId varchar(20) references tbl_computer(id), +beginTime datetime, +endTime datetime, +fee money +) + +go +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,'校庆'), +('008_EDE','zhang',134,'张峻') + +go +insert into tbl_computer values +('02',0,'25555'), +('03',1,'55555'), +('04',0,'66666'), +('05',1,'88888'), +('06',0,'688878'), +('B01',0,'558558') + +go +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-22 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 15:26:00','2007-01-06 22:55:00',6), +(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 15:26:00',50), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',3), +(65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-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 userName 用户名,tbl_card.id 卡号,tbl_record.ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_card inner join tbl_record on tbl_card.id = tbl_record.cardId where userName='张军' order by tbl_record.fee desc + +--2. 查询出每台机器上的上网次数和消费的总金额 +select tbl_computer.id 机器编号, COUNT(ComputerId) 上网次数, SUM(fee) 消费总金额 from tbl_computer left join tbl_record on tbl_record.ComputerId =tbl_computer.id group by tbl_computer.Id + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select tbl_card.id 网卡号,SUM(fee) 消费总金额 from tbl_card inner join tbl_record on tbl_card.id = tbl_record.cardId group by tbl_card.id + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select id 网卡号,userName 用户名 from tbl_card +except +select tbl_record.cardId 网卡号,userName 用户名 from tbl_record inner join tbl_card on tbl_card.id = tbl_record.cardId + +select tbl_card.id 卡号, userName 用户名,fee 消费金额 from tbl_card left join tbl_record tbl_record on tbl_card.id=tbl_record.cardId where fee is null + +--5. 将密码与用户名一样的上网卡信息查询出来 +select A. * from tbl_card A,tbl_card B where A.passWord=B.userName + +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 机器号,COUNT(cardId) 使用次数 from tbl_record group by ComputerId order by COUNT(cardId) desc + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select tbl_card.id 卡号,userName 用户名,ComputerId 机器号,fee 消费金额 from tbl_card inner join tbl_record on tbl_card.id=tbl_record.cardId where tbl_card.id like'%ABC%' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId 网卡号,userName 用户名,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where datename(DW,beginTime)='星期六' or datename(DW,beginTime)='星期天' + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_card.id 卡号,userName 用户名,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_card inner join tbl_record on tbl_card.id = tbl_record.cardId where datediff(hour,endTime,beginTime)>12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 tbl_card.id 卡号,userName 用户名,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record +left join tbl_card on tbl_card.id = tbl_record.cardId +order by fee desc \ No newline at end of file diff --git "a/2021 03 31/\345\217\266\347\234\237/SQLQuery1.sql" "b/2021 03 31/\345\217\266\347\234\237/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0a0b71c05be1588b253219ae9b29241b49871841 --- /dev/null +++ "b/2021 03 31/\345\217\266\347\234\237/SQLQuery1.sql" @@ -0,0 +1,115 @@ +use master +go +create database BBC +on +( + name='BBC', + filename='D:\BBC.mdf', + size=10, + maxsize=300, + filegrowth=20% +) +log on +( + name='BBC_log', + filename='D:\BBC_log.mdf', + size=10, + maxsize=300, + filegrowth=20% +) +go +use BBC +go +create table tbl_card +( + id varchar(20) primary key, + passWord varchar(20) , + balance money , + userName varchar(50) +) + +create table tbl_computer +( + id int primary key, + onUse int , + note varchar(100) , +) + +create table tbl_record +( + id int primary key, + cardI varchar(20) references tbl_card(id) , + ComputerId int references tbl_computer(id) , + beginTime datetime , + endTime datetime , + fee money +) +insert into tbl_card(id,passWord,balance,userName) values ('0023_ABC','555',98,'张军'),('0025_bbd','abe',300,'朱俊'),('0036_CDD','何柳',100,'何柳'),('0045_YGR','0045_YGR',58,'验证'),('0078_RJV','55885fg',600,'校庆'),('0089_EDE','zhang',134,'张俊') + +insert into tbl_computer(id,onUse,note) values (02,0,'25555'),(03,1,'55555'),(04,0,'66666'),(05,1,'88888'),(06,0,'688878'),(01,0,'558558') + +insert into tbl_record(id,cardI,ComputerId,beginTime,endTime,fee) values (23,'0078_RJV',01,'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-22 22:55:00',6),(47,'0023_ABC',03,'2006-12-23 15:26:00','2006-12-23 22:55:00',23), +(48,'0023_ABC',03,'2007-01-06 15:26:00','2007-01-06 22:55:00',6),(55,'0023_ABC',03,'2006-07-21 15:26:00','2007-07-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-12-28 18:00:00','2006-12-28 22:00:00',23),(98,'0025_bbd',02,'2006-12-26 18:00:00','2006-12-26 22:00:00',23) + + + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 + + select C.id 卡号, userName 用户名, CP.id 机器编号 , beginTime 开始时间, endTime 结束时间 ,fee 消费金额 from tbl_card C,tbl_computer CP ,tbl_record R + where C.id=R.cardI and CP.id=R.ComputerId and userName='张军' + order by fee DESC + +--2. 查询出每台机器上的上网次数和消费的总金额 + +select CP.Id 机器编号, count(ComputerId) 上网次数, sum(fee) 消费总金额 from tbl_computer CP + left join tbl_record R on R.ComputerId =CP.id + group by CP.Id + +--3. 查询出所有已经使用过的上网卡的消费总金额 + +select cardI 网卡号 , sum(fee) 消费总金额 + from tbl_record + group by cardI + +--4. 查询出从未消费过的上网卡的卡号和用户名 + +select C.id 卡号, userName 用户名 from tbl_card C + left join tbl_record R on C.id=R.cardI + where fee is null + +--5. 将密码与用户名一样的上网卡信息查询出来 + +select * from tbl_card + where passWord= userName + +--6. 查询出使用次数最多的机器号和使用次数 + +select top 1 ComputerId 使用次数最多的机器号, count(ComputerId) 使用次数 from tbl_record + group by ComputerId + order by count(ComputerId) DESC + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + +select C.id 卡号, userName 用户名, R.ComputerId 上网机器编号 ,fee 消费金额 from tbl_card C + left join tbl_record R on C.id = R.cardI + where C.id like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select R.cardI 卡号, userName 用户名, ComputerId 机器号, beginTime 开始时间, endTime 结束时间 from tbl_record R + left join tbl_card C on R.cardI=C.id + where datename(DW,beginTime)='星期六' or datename(DW,beginTime)='星期天' + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select R.cardI 卡号, userName 用户名, ComputerId 机器号, beginTime 开始时间 ,endTime 结束时间 ,fee 消费金额 from tbl_record R + left join tbl_card C on R.cardI= C.id + where datediff(HH,beginTime,endTime)>12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select top 7 cardI 卡号, userName 用户名, ComputerId 机器号, beginTime 开始时间,endTime 结束时间 , fee 消费金额 from tbl_record + left join tbl_card C on cardI=C.id + order by fee diff --git "a/2021 03 31/\345\217\266\351\231\210\350\276\211/SQLQuery1.sql" "b/2021 03 31/\345\217\266\351\231\210\350\276\211/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0a0b71c05be1588b253219ae9b29241b49871841 --- /dev/null +++ "b/2021 03 31/\345\217\266\351\231\210\350\276\211/SQLQuery1.sql" @@ -0,0 +1,115 @@ +use master +go +create database BBC +on +( + name='BBC', + filename='D:\BBC.mdf', + size=10, + maxsize=300, + filegrowth=20% +) +log on +( + name='BBC_log', + filename='D:\BBC_log.mdf', + size=10, + maxsize=300, + filegrowth=20% +) +go +use BBC +go +create table tbl_card +( + id varchar(20) primary key, + passWord varchar(20) , + balance money , + userName varchar(50) +) + +create table tbl_computer +( + id int primary key, + onUse int , + note varchar(100) , +) + +create table tbl_record +( + id int primary key, + cardI varchar(20) references tbl_card(id) , + ComputerId int references tbl_computer(id) , + beginTime datetime , + endTime datetime , + fee money +) +insert into tbl_card(id,passWord,balance,userName) values ('0023_ABC','555',98,'张军'),('0025_bbd','abe',300,'朱俊'),('0036_CDD','何柳',100,'何柳'),('0045_YGR','0045_YGR',58,'验证'),('0078_RJV','55885fg',600,'校庆'),('0089_EDE','zhang',134,'张俊') + +insert into tbl_computer(id,onUse,note) values (02,0,'25555'),(03,1,'55555'),(04,0,'66666'),(05,1,'88888'),(06,0,'688878'),(01,0,'558558') + +insert into tbl_record(id,cardI,ComputerId,beginTime,endTime,fee) values (23,'0078_RJV',01,'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-22 22:55:00',6),(47,'0023_ABC',03,'2006-12-23 15:26:00','2006-12-23 22:55:00',23), +(48,'0023_ABC',03,'2007-01-06 15:26:00','2007-01-06 22:55:00',6),(55,'0023_ABC',03,'2006-07-21 15:26:00','2007-07-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-12-28 18:00:00','2006-12-28 22:00:00',23),(98,'0025_bbd',02,'2006-12-26 18:00:00','2006-12-26 22:00:00',23) + + + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 + + select C.id 卡号, userName 用户名, CP.id 机器编号 , beginTime 开始时间, endTime 结束时间 ,fee 消费金额 from tbl_card C,tbl_computer CP ,tbl_record R + where C.id=R.cardI and CP.id=R.ComputerId and userName='张军' + order by fee DESC + +--2. 查询出每台机器上的上网次数和消费的总金额 + +select CP.Id 机器编号, count(ComputerId) 上网次数, sum(fee) 消费总金额 from tbl_computer CP + left join tbl_record R on R.ComputerId =CP.id + group by CP.Id + +--3. 查询出所有已经使用过的上网卡的消费总金额 + +select cardI 网卡号 , sum(fee) 消费总金额 + from tbl_record + group by cardI + +--4. 查询出从未消费过的上网卡的卡号和用户名 + +select C.id 卡号, userName 用户名 from tbl_card C + left join tbl_record R on C.id=R.cardI + where fee is null + +--5. 将密码与用户名一样的上网卡信息查询出来 + +select * from tbl_card + where passWord= userName + +--6. 查询出使用次数最多的机器号和使用次数 + +select top 1 ComputerId 使用次数最多的机器号, count(ComputerId) 使用次数 from tbl_record + group by ComputerId + order by count(ComputerId) DESC + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + +select C.id 卡号, userName 用户名, R.ComputerId 上网机器编号 ,fee 消费金额 from tbl_card C + left join tbl_record R on C.id = R.cardI + where C.id like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select R.cardI 卡号, userName 用户名, ComputerId 机器号, beginTime 开始时间, endTime 结束时间 from tbl_record R + left join tbl_card C on R.cardI=C.id + where datename(DW,beginTime)='星期六' or datename(DW,beginTime)='星期天' + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select R.cardI 卡号, userName 用户名, ComputerId 机器号, beginTime 开始时间 ,endTime 结束时间 ,fee 消费金额 from tbl_record R + left join tbl_card C on R.cardI= C.id + where datediff(HH,beginTime,endTime)>12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select top 7 cardI 卡号, userName 用户名, ComputerId 机器号, beginTime 开始时间,endTime 结束时间 , fee 消费金额 from tbl_record + left join tbl_card C on cardI=C.id + order by fee diff --git "a/2021 03 31/\345\217\266\351\242\200/SQLQuery1.sql" "b/2021 03 31/\345\217\266\351\242\200/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e4def8ca8bac36c23f8c8c6f8501ed2745a86cd1 --- /dev/null +++ "b/2021 03 31/\345\217\266\351\242\200/SQLQuery1.sql" @@ -0,0 +1,110 @@ +use master +go +create database WB +on +( +name = 'WB', +filename = 'D:\ WB.maf', +size = 5MB, +maxsize = 50MB, +filegrowth = 10% +) +log on +( +name = 'WB_log', +filename = 'D:\ WB_log.maf', +size = 5MB, +maxsize = 50MB, +filegrowth = 10% +) +go +use WB +go + +create table Tbl_card +( +cardId varchar(20) primary key not null, +passWord varchar(20) unique not null, +balance money not null, +userName varchar(10) +) + +create table Tbl_computer +( +ComputerId varchar(10) primary key not null, +onUse int check(onUse in(0,1)), +note text +) + +create table Tbl_record +( +record int primary key not null, +cardId varchar(20) references Tbl_card(cardId) not null, +ComputerId varchar(10) references Tbl_computer(ComputerId), +beginTime datetime, +endTime datetime, +fee money +) + +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-25 22:55:00',50), + (46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-25 22:55:00',6), + (47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-25 22:55:00',50), + (48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00',6), + (55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-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-12-28 18:00:00','2006-12-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 Tbl_card.cardId 卡号, userName 用户名,ComputerId 机器编号, beginTime 开始时间,endTime 结束时间, fee 消费金额 from Tbl_record +inner join Tbl_card on Tbl_record.cardId = Tbl_card.cardId +where userName= '张军' +order by fee DESC + +--2. 查询出每台机器上的上网次数和消费的总金额 +select computerid ,count(cardid),sum(fee) 消费总金额 from Tbl_record +group by ComputerId +select * from Tbl_card +select * from Tbl_computer +select * from Tbl_record +--3. 查询出所有已经使用过的上网卡的消费总金额 + select cardId ,sum(fee) 消费总金额 from Tbl_record + group by cardId + --4. 查询出从未消费过的上网卡的卡号和用户名 + select Tbl_record.cardId , userName from Tbl_record + right join Tbl_card on Tbl_record.cardId = Tbl_card.cardId + where fee is null + --5. 将密码与用户名一样的上网卡信息查询出来 + select * from Tbl_card where passWord = userName + --6. 查询出使用次数最多的机器号和使用次数 + select TOP 1 ComputerId,count(cardId) from Tbl_record + group by ComputerId + order by count(cardId) DESC + --7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + select Tbl_record.cardId,userName,ComputerId,fee from Tbl_card + inner join Tbl_record on Tbl_card.cardId = Tbl_record.cardId + where Tbl_record.cardId like '%ABC' + --8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select Tbl_card.cardId,userName,ComputerId,beginTime ,endTime ,fee from Tbl_record + inner join Tbl_card on Tbl_record.cardId = Tbl_card.cardId + where datename(DW,beginTime )= '星期六' and datename(DW,beginTime )= '星期日' + --9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select Tbl_card.cardId,userName,ComputerId,beginTime ,endTime ,fee from Tbl_record + inner join Tbl_card on Tbl_record.cardId = Tbl_card.cardId + where datediff(HH,beginTime,endTime) > 12 + --10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select top 3 Tbl_card.cardId,userName,ComputerId,beginTime ,endTime ,fee from Tbl_record + inner join Tbl_card on Tbl_record.cardId = Tbl_card.cardId + order by fee DESC \ No newline at end of file diff --git "a/2021 03 31/\345\220\264\344\276\235\346\266\265/SQLQuery2.sql" "b/2021 03 31/\345\220\264\344\276\235\346\266\265/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c79a25451007cc76b14a83ecedffecf427e85267 --- /dev/null +++ "b/2021 03 31/\345\220\264\344\276\235\346\266\265/SQLQuery2.sql" @@ -0,0 +1,120 @@ + +use master +go +create database WangBa +on +( + name='WangBa', + filename='D:\新建文件夹3.mdf', + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) +log on +( + name='WangBa_log', + filename='D:\新建文件夹3_1og.ldf', + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) +go +use WangBa +go +create table tbl_card +( +id varchar(20) primary key, +passWord varchar(20), +balance money, +userName varchar(20) +) +go +use WangBa +go +create table tbl_computer +( +id varchar(20) primary key, +onUse int check(onUse=0 or onUse=1), +note text +) +go +use WangBa +go +create table tbl_record +( +id varchar(20) primary key, +cardId varchar(20) references tbl_card(id), +ComputerId varchar(20) references tbl_computer(id), +beginTime datetime, +endTime datetime, +fee money +) +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-7-15 19:00:00','2007-7-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-25 22:55:00',50), +('46','0023_ABC','03','2006-12-23 15:26:00','2006-12-25 22:55:00',6), +('47','0023_ABC','03','2006-12-23 15:26:00','2006-12-25 22:55:00',50), +('48','0023_ABC','03','2006-12-23 15:26:00','2006-12-25 22:55:00',6), +('55','0023_ABC','03','2006-12-23 15:26:00','2006-12-25 22:55:00',50), +('64','0045_YGR','04','2006-12-24 18:00:00','2006-12-25 22:00:00',3), +('65','0025_bbd','02','2006-12-28 18:00:00','2006-12-25 22:00:00',23), +('98','0025_bbd','02','2006-12-26 18:00:00','2006-12-25 22:00:00',23) + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select C.id 卡号,C.userName 用户名,D.id 机器编号,L.beginTime 开始时间,L.endTime 结束时间,L.fee 消费金额 from tbl_record L inner join tbl_card C on L.cardId=C.id inner join tbl_computer D on L.ComputerId=D.id +where C.userName='张军' order by L.fee DESC + +--2. 查询出 每台机器 上的 上网次数 和 消费的总金额 +select D.id 机器,count(*)上网次数,sum(fee)消费的总金额 from tbl_computer D left join tbl_record L on D.id=L.ComputerId group by D.id + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardId 卡号,sum(fee) 消费总金额 from tbl_record group by cardId + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select id 卡号,userName 用户名 from tbl_card +except +select C.id 卡号,C.userName 用户名 from tbl_record L inner join tbl_card C on C.id=L.cardId + +--5. 将密码与用户名一样的上网卡信息查询出来 +select A.passWord 密码,A.userName 用户名 from tbl_card A,tbl_card B where A.passWord=B.userName + +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 L.ComputerId 机器号, count(*)使用次数 from tbl_record L inner join tbl_computer D on L.ComputerId=D.id group by ComputerId order by count(*) DESC + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select * from tbl_card C left join tbl_record L on C.id=L.cardId where C.id like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select C.id 卡号,C.userName 用户名,D.id 机器号,L.beginTime 开始时间,L.endTime 结束时间,fee 消费金额 from tbl_record L inner join tbl_card C on L.cardId=C.id inner join tbl_computer D on L.ComputerId=D.id +where datename(dw,beginTime)='星期六'or datename(dw,beginTime)='星期日' + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select C.id 卡号,C.userName 用户名,D.id 机器号,L.beginTime 开始时间,L.endTime 结束时间,L.fee 消费金额 from tbl_record L inner join tbl_card C on L.cardId=C.id inner join tbl_computer D on L.ComputerId=D.id +where datediff(hh,beginTime,endTime)>12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select C.id 卡号,C.userName 用户名,D.id 机器号,L.beginTime 开始时间,L.endTime 结束时间,L.fee 消费金额 from tbl_record L inner join tbl_card C on L.cardId=C.id inner join tbl_computer D on L.ComputerId=D.id +except +select C.id 卡号,C.userName 用户名,D.id 机器号,L.beginTime 开始时间,L.endTime 结束时间,L.fee 消费金额 from tbl_record L inner join tbl_card C on L.cardId=C.id inner join tbl_computer D on L.ComputerId=D.id +where fee=(select max(fee) from tbl_record) + +select * from tbl_card +select * from tbl_computer +select * from tbl_record \ No newline at end of file diff --git "a/2021 03 31/\345\220\264\346\230\237/SQLQuery1.sql" "b/2021 03 31/\345\220\264\346\230\237/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0a0b71c05be1588b253219ae9b29241b49871841 --- /dev/null +++ "b/2021 03 31/\345\220\264\346\230\237/SQLQuery1.sql" @@ -0,0 +1,115 @@ +use master +go +create database BBC +on +( + name='BBC', + filename='D:\BBC.mdf', + size=10, + maxsize=300, + filegrowth=20% +) +log on +( + name='BBC_log', + filename='D:\BBC_log.mdf', + size=10, + maxsize=300, + filegrowth=20% +) +go +use BBC +go +create table tbl_card +( + id varchar(20) primary key, + passWord varchar(20) , + balance money , + userName varchar(50) +) + +create table tbl_computer +( + id int primary key, + onUse int , + note varchar(100) , +) + +create table tbl_record +( + id int primary key, + cardI varchar(20) references tbl_card(id) , + ComputerId int references tbl_computer(id) , + beginTime datetime , + endTime datetime , + fee money +) +insert into tbl_card(id,passWord,balance,userName) values ('0023_ABC','555',98,'张军'),('0025_bbd','abe',300,'朱俊'),('0036_CDD','何柳',100,'何柳'),('0045_YGR','0045_YGR',58,'验证'),('0078_RJV','55885fg',600,'校庆'),('0089_EDE','zhang',134,'张俊') + +insert into tbl_computer(id,onUse,note) values (02,0,'25555'),(03,1,'55555'),(04,0,'66666'),(05,1,'88888'),(06,0,'688878'),(01,0,'558558') + +insert into tbl_record(id,cardI,ComputerId,beginTime,endTime,fee) values (23,'0078_RJV',01,'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-22 22:55:00',6),(47,'0023_ABC',03,'2006-12-23 15:26:00','2006-12-23 22:55:00',23), +(48,'0023_ABC',03,'2007-01-06 15:26:00','2007-01-06 22:55:00',6),(55,'0023_ABC',03,'2006-07-21 15:26:00','2007-07-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-12-28 18:00:00','2006-12-28 22:00:00',23),(98,'0025_bbd',02,'2006-12-26 18:00:00','2006-12-26 22:00:00',23) + + + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 + + select C.id 卡号, userName 用户名, CP.id 机器编号 , beginTime 开始时间, endTime 结束时间 ,fee 消费金额 from tbl_card C,tbl_computer CP ,tbl_record R + where C.id=R.cardI and CP.id=R.ComputerId and userName='张军' + order by fee DESC + +--2. 查询出每台机器上的上网次数和消费的总金额 + +select CP.Id 机器编号, count(ComputerId) 上网次数, sum(fee) 消费总金额 from tbl_computer CP + left join tbl_record R on R.ComputerId =CP.id + group by CP.Id + +--3. 查询出所有已经使用过的上网卡的消费总金额 + +select cardI 网卡号 , sum(fee) 消费总金额 + from tbl_record + group by cardI + +--4. 查询出从未消费过的上网卡的卡号和用户名 + +select C.id 卡号, userName 用户名 from tbl_card C + left join tbl_record R on C.id=R.cardI + where fee is null + +--5. 将密码与用户名一样的上网卡信息查询出来 + +select * from tbl_card + where passWord= userName + +--6. 查询出使用次数最多的机器号和使用次数 + +select top 1 ComputerId 使用次数最多的机器号, count(ComputerId) 使用次数 from tbl_record + group by ComputerId + order by count(ComputerId) DESC + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + +select C.id 卡号, userName 用户名, R.ComputerId 上网机器编号 ,fee 消费金额 from tbl_card C + left join tbl_record R on C.id = R.cardI + where C.id like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select R.cardI 卡号, userName 用户名, ComputerId 机器号, beginTime 开始时间, endTime 结束时间 from tbl_record R + left join tbl_card C on R.cardI=C.id + where datename(DW,beginTime)='星期六' or datename(DW,beginTime)='星期天' + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select R.cardI 卡号, userName 用户名, ComputerId 机器号, beginTime 开始时间 ,endTime 结束时间 ,fee 消费金额 from tbl_record R + left join tbl_card C on R.cardI= C.id + where datediff(HH,beginTime,endTime)>12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select top 7 cardI 卡号, userName 用户名, ComputerId 机器号, beginTime 开始时间,endTime 结束时间 , fee 消费金额 from tbl_record + left join tbl_card C on cardI=C.id + order by fee diff --git "a/2021 03 31/\345\221\250\346\230\237\345\256\207/SQLQuery1.sql" "b/2021 03 31/\345\221\250\346\230\237\345\256\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..47f3bc312858fc099899acabfa1524a65b7ff5b1 --- /dev/null +++ "b/2021 03 31/\345\221\250\346\230\237\345\256\207/SQLQuery1.sql" @@ -0,0 +1,135 @@ +use master +go + +create database InternetBar +on +( + name='InternetBar', + filename='D:\InternetBar.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) + +log on +( + name='InternetBar_log', + filename='D:\InternetBar_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +go + +use InternetBar +go + +create table tbl_card +( + ID varchar(20) primary key, + PassWord varchar(10), + Balance int, + UseName varchar(5) +) + + + +create table tbl_computer +( + ID varchar(5) primary key, + OnUse varchar(1) check(OnUse=0 or OnUse=1) not null, + Note nvarchar(20) +) + + +create table tbl_record +( + ID int primary key, + cardId varchar(20) references tbl_card(ID), + ComputerId varchar(5) references tbl_computer(ID), + beginTime datetime, + endTime datetime, + fee money +) + + + +insert into tbl_card(ID,PassWord,Balance,UseName) +select'0023_ABC','555','98','张军' union +select'0025_bbd','abe','300','朱俊' union +select'0036_CCD','何柳','100','何柳' union +select'0045_YGR','0045_YGR','58','验证' union +select'0078_RJV','55885fg','600','校庆' union +select'0089_EDE','zhang','1234','张峻' + + + +insert into tbl_computer(ID,OnUse,Note) +select'02','0','25555' union +select'03','1','55555' union +select'04','0','66666' union +select'05','1','88888' union +select'06','0','688878' union +select'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-22 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 15:26:00','2007-01-06 22:55:00','6'), + ('55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), + ('64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','30'), + ('65','0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23'), + ('98','0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23') + + + + --1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 + select A.cardId as 卡号 , UseName as 用户名 , ComputerId as 机器编号 , beginTime as 开始时间 , endTime as 结束时间 , fee as 和消费金额 +from tbl_record A inner join tbl_card B on A.cardID = A.cardId +where UseName = '张军' order by fee desc + + + --2. 查询出每台机器上的上网次数和消费的总金额 + select ComputerId , COUNT(ComputerId) , SUM(fee) from tbl_record group by ComputerId + + + --3. 查询出所有已经使用过的上网卡的消费总金额 + select A.cardId , SUM(fee) from tbl_record A +inner join tbl_card B on A.cardID=B.cardId +group by A.cardID + + + --4. 查询出从未消费过的上网卡的卡号和用户名 + + + + --5. 将密码与用户名一样的上网卡信息查询出来 +select A.ID , A.passWord , A.balance ,A.UseName from tbl_card A , tbl_card B +where A.passWord = B.UseName and A.ID = B.ID + + + --6. 查询出使用次数最多的机器号和使用次数 + select top 1 ComputerId , COUNT(cardId) from tbl_record +group by ComputerId +order by COUNT(cardId) desc + + + --7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + select A.cardId , UseName , ComputerId , fee from tbl_record A +inner join tbl_card B on A.cardId = B.ID +where A.cardId like('%ABC') + + + --8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + + + --9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 7 cardId 卡号, UseName 用户名, ComputerId 机器号, beginTime 开始时间,endTime 结束时间 , fee 消费金额 from tbl_record +left join tbl_card C on cardId=C.ID +order by fee \ No newline at end of file diff --git "a/2021 03 31/\345\224\220\345\207\241\350\276\211/SQLserver10.sql" "b/2021 03 31/\345\224\220\345\207\241\350\276\211/SQLserver10.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1936fc1841c2919265978408cd515199b1ce217d --- /dev/null +++ "b/2021 03 31/\345\224\220\345\207\241\350\276\211/SQLserver10.sql" @@ -0,0 +1,84 @@ +create database tbl +on +( name='tbl', + filename='D:\tbl.mdf' + + +) +log on +( name='tbl_log', + filename='D:\tbl_log.ldf' + + + +) +go +use tbl +create table tbl_card +( + id varchar(20) primary key not null, + passWord nvarchar(20) not null, + balance int not null , + userName nvarchar(20) not null + + +) +create table tbl_computer +( + id varchar(20) primary key not null , + onUse varchar(2) check(onUse in ('0','1')) not null, + note nvarchar(20) + + + + ) + create table tbl_record + ( + id varchar(20) primary key not null, + cardId varchar(20) references tbl_card(id), + ComputerId varchar(20) references tbl_computer(id), + beginTime datetime , + endTime datetime, + 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','锠熿棊') + select * from tbl_card + insert into tbl_computer values('02','0','25555'),('03','1','55555'),('04','0','66666'),('05','1','88888'),('06','0','688878'),('B01','0','558558') + select * from tbl_computer + 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-22 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 15:26:00','2007-01-06 22:55:00','6'), + ('55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), + ('64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','30'), + ('65','0025_bbd','02','2006-12-28 18:00:00','2006-12-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_record + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where userName='锠熿棅' order by fee desc + + select ComputerId,COUNT(cardId),SUM(fee) from tbl_record group by ComputerId + + select cardId,SUM(fee) from tbl_record left join tbl_card on tbl_record.cardId=tbl_card.id group by cardId + + select tbl_card.id,userName from tbl_card left join tbl_record on tbl_record.cardId=tbl_card.id where fee is null + + select a.* from tbl_card a,tbl_card b where a.passWord=b.userName + + select top 1 ComputerId,count(id) from tbl_record group by ComputerId order by count(id)desc + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where cardId like '%_ABC' + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where datepart(weekday,endTime)=1 or datepart(weekday,endTime)=7 + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where datediff(HH,beginTime,endTime) >12 + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id + except + select top 3 cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id order by fee desc \ No newline at end of file diff --git "a/2021 03 31/\345\225\206\350\265\242\346\227\255/s.sql" "b/2021 03 31/\345\225\206\350\265\242\346\227\255/s.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a8d706aa2fda7c7d250fc179f84282e02731f2ec --- /dev/null +++ "b/2021 03 31/\345\225\206\350\265\242\346\227\255/s.sql" @@ -0,0 +1,93 @@ +use master +go + +create database s +on +( + name='s.mdf', + filename='D:\s.mdf', + maxsize =50, + size=20, + filegrowth=10% +) + log on + ( + name='s_log.ldf', + filename='D:\s_log.ldf', + size=20, + maxsize=50, + filegrowth=10% + + ) + go + use wang + go +create table tbl_card +( +ID varchar(10) primary key , +passWord char(15), +balance char(15), +userName nvarchar(10) +) +go +create table tbl_computer +( +ID varchar(10) primary key, +onUse int check(onUse=0 or onUse=1), +note nvarchar(50) +) +go +create table tbl_record +( +ID int primary key, +cardId varchar(10) references tbl_card(ID), +ComputerId varchar(10) references tbl_computer(ID), +beginTime datetime, +endTime datetime, +fee money +) +insert into tbl_card values ('0023_ABC','555','98','张军') +insert into tbl_card values ('0025_bbd','abe','300','朱骏'),('0036_CCD','何柳','100','何柳'), +('0045_YGR','0045_YGR','58','验证'),('0078_RJV','5588fg','600','校庆'),('0089_EDE','zhang','134','张俊') +insert into tbl_computer values ('02',0,'25555') +insert into tbl_computer values ('03',1,'55555'),('04',0,'66666'),('05',1,'888888'),('06',0,'6888878'),('01',0,'558558') + + +insert Tbl_record values +(23,'0078_RJV','01','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-22 22:55:00',50), +(48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00',6), +(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00',50), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',30), +(65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00',23), +(98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00',23) +go +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号 +select * from tbl_card +select * from tbl_record +--用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select C.ID,S.userName,C.ID,C.ComputerId,C.beginTime,C.endTime,C.fee from tbl_card S inner join tbl_record C on S.ID=C.cardID WHERE S.userName LIKE '张军' +--2. 查询出每台机器上的上网次数和消费的总金额 +select S.computerID,SUM(S.fee),COUNT(*) from tbl_record S inner join tbl_record C ON S.ID=C.ID group by S.computerID +--3. 查询出所有已经使用过的上网卡的消费总金额 +select S.cardId,SUM(S.fee),COUNT(*) from tbl_record S inner join tbl_record C ON S.ID=C.ID group by S.cardId +--4. 查询出从未消费过的上网卡的卡号和用户名 +select S.cardId,C.userName from tbl_record S right join tbl_card C on S.cardId=C.ID where S.cardId IS NULL +--5. 将密码与用户名一样的上网卡信息查询出来 +select userName,PassWord,B.* from tbl_card A LEFT join tbl_record B ON A.ID=B.cardId WHERE passWord = userName +--6. 查询出使用次数最多的机器号和使用次数 + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + + + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + + diff --git "a/2021 03 31/\345\274\240\345\256\217/SQLQuery13.sql" "b/2021 03 31/\345\274\240\345\256\217/SQLQuery13.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5acff686a752ee479b460e224ab723d885310da1 --- /dev/null +++ "b/2021 03 31/\345\274\240\345\256\217/SQLQuery13.sql" @@ -0,0 +1,101 @@ +create database WB +on +( + name='WB', + filename='D:\WB.mdf', + size=20, + maxsize=300, + filegrowth=50 +) +log on +( + name='WB_log', + filename='D:\WB_log.ldf', + size=20, + maxsize=300, + filegrowth=50 +) +GO +use WB +go + +create table tbl_card +( + id nvarchar(20) primary key , + password nvarchar(20) not null, + balance money, + userName nvarchar(10) not null +) + +insert into tbl_card (id,password,balance,userName) values('0023_ABC','555','98','张军'),('0025_bbd','abc','300','朱俊'),('0036_CCD','何柳','100','何柳'),('0045_YGR','0045_YGR','58','证验'),('0078_RJV','55885fg','600','校庆'),('0089_EDE','zhang','134','张俊') + +select * from tbl_card + +create table tbl_computer +( + id nvarchar(20) primary key , + onUse nvarchar(1) check(onUse='0' or onUse='1'), + note int +) + +insert into tbl_computer (id,onUse,note) values('02','0',25555), +('03','1',55555), +('04','0',66666), +('05','1',88888), +('06','0',688878), +('B01','0',558558) + +select * from tbl_computer + +create table tbl_record +( + id nvarchar(20) primary key , + cardId nvarchar(20) references tbl_card(id), + ComputerId nvarchar(20)references tbl_computer(id), + beginTime datetime, + endTime datetime, + fee money +) + +select * from tbl_card +select * from tbl_computer +select * from tbl_record + +insert into tbl_record (id,cardId,ComputerId,beginTime,endTime,fee) values +('23','0078_RJV','B01','2007-07-15','2007-07-15','20'), +('34','0025_bbd','02','2006-12-25','2006-12-25','23'), +('45','0023_ABC','03','2006-12-23','2006-12-23','50'), +('46','0023_ABC','03','2006-12-22','2006-12-22','6'), +('47','0023_ABC','03','2006-12-23','2006-12-23','50'), +('48','0023_ABC','03','2007-01-06','2007-01-06','6'), +('55','0023_ABC','03','2006-07-21','2006-07-21','50'), + ('64','0045_YGR','04','2006-12-24','2006-12-24','3'), +('65','0025_bbd','02','2006-12-28','2006-12-28','23'), +('98','0025_bbd','02','2006-12-26','2006-12-26','23') + +select * from tbl_card +select * from tbl_computer +select * from tbl_record + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select CA.id 卡号,ComputerId 机器编号,userName 用户名,beginTime 开始时间,endTime 结束时间,fee 费用 from tbl_record RE +inner join tbl_card CA on RE.id=CA.id +where userName='张军' order by fee DESC +--2. 查询出 每台机器上的上网次数 和 消费的总金额 +select ComputerId,count(ComputerId)上网次数,sum(fee)总金额 from tbl_record group by ComputerId +--3. 查询出所有已经使用过的上网卡的消费总金额 +select ComputerId,sum(fee)总金额 from tbl_record group by ComputerId +--4. 查询出从未消费过的上网卡的卡号和用户名 +select CA.id,userName ,fee from tbl_record RE left join tbl_card CA on RE.id=CA.id where fee is null +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card A,tbl_card B where A.userName=B.password +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId,count(ComputerId) 次数 from tbl_record group by ComputerId order by count(ComputerId) DESC +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select CA.id,userName,ComputerId,fee from tbl_card CA inner join tbl_record RE on CA.id=RE.id where CA.id like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select * from tbl_record where datepart(WEEKDAY,beginTime)=1 or datepart(WEEKDAY,beginTime)=7 +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select * from tbl_record where is (beginTime - endTime)>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金 +select TOP 7 * from tbl_record order by fee ASC \ No newline at end of file diff --git "a/2021 03 31/\345\274\240\351\221\253/SQLQuery2.sql" "b/2021 03 31/\345\274\240\351\221\253/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fa60dd516cddc227f3c57f2cc3e14c4e870a53d4 --- /dev/null +++ "b/2021 03 31/\345\274\240\351\221\253/SQLQuery2.sql" @@ -0,0 +1,105 @@ +use master +go + +create database Tbl +go + +use Tbl +go + +create table Tbl_card +( + CardId varchar(10) primary key , + PassWord varchar(10) not null , + Balance int , + UserName varchar(10) not null +) +go + +create table Tbl_computer +( + ComputerId varchar(10) primary key , + OnUse int check(OnUse in(0,1)) not null , + Note varchar(100) +) +go + +create table Tbl_record +( + RecordId int primary key , + CardId varchar(10) references Tbl_Card(CardId) , + ComputerId varchar(10) references Tbl_computer(ComputerId) , + BeginTime datetime , + EndTime datetime , + Fee int +) +go + +insert 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,'张峻') +go + +insert Tbl_computer values +('02',0,'25555'), +('03',1,'55555'), +('04',0,'66666'), +('05',1,'88888'), +('06',0,'688878'), +('B01',0,'558558') +go + +insert 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-22 22:55:00',50), +(48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00',6), +(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00',50), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',30), +(65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00',23), +(98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00',23) +go + +select * from Tbl_card +select * from Tbl_computer +select * from Tbl_record + +select Tbl_record.CardId,userName,ComputerId,beginTime,endTime,fee from tbl_record +inner join tbl_card on Tbl_card.CardId=Tbl_record.CardId where UserName='张军'ORDER BY fee DESC + +select CardId,count(ComputerId)次数,sum(Fee)总消费 from Tbl_record +group by ComputerId,CardId + +select Tbl_computer.ComputerId,sum(Fee)总消费 from tbl_record +inner join Tbl_computer on Tbl_computer.ComputerId=tbl_record.ComputerId group by Tbl_computer.ComputerId + +select tbl_card.CardId,userName from tbl_card +left join tbl_record on tbl_record.cardId=tbl_card.CardId where fee is null + +select PassWord,UserName from tbl_card where PassWord=UserName + +select top 1 computerID,count(computerID)次数 from Tbl_record +group by ComputerId order by count(computerID)desc + +select Tbl_card.CardId,UserName,ComputerId,Fee from Tbl_card +inner join Tbl_record on Tbl_record.CardId=Tbl_card.CardId +where Tbl_card.cardId like '%_ABC' + +select Tbl_card.CardId,UserName,ComputerId,BeginTime,EndTime,Fee from Tbl_card +inner join Tbl_record on Tbl_record.CardId=Tbl_card.CardId +where datepart(weekday,endTime)=1 or datepart(weekday,endTime)=7 + +select Tbl_card.CardId,UserName,ComputerId,BeginTime,EndTime,Fee from Tbl_card +inner join Tbl_record on Tbl_record.CardId=Tbl_card.CardId +where datediff(HH,beginTime,endTime) >12 + +select Tbl_card.CardId,UserName,ComputerId,BeginTime,EndTime,Fee from Tbl_card +inner join Tbl_record on Tbl_record.CardId=Tbl_card.CardId +except +select top 3 Tbl_card.CardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.cardId order by fee desc \ No newline at end of file diff --git "a/2021 03 31/\345\276\220\351\252\217\351\271\217/\345\276\220\351\252\217\351\271\217/SQLQuery1.sql" "b/2021 03 31/\345\276\220\351\252\217\351\271\217/\345\276\220\351\252\217\351\271\217/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cbc8a66c0b6b2d4836b7cb63c9340944872da044 --- /dev/null +++ "b/2021 03 31/\345\276\220\351\252\217\351\271\217/\345\276\220\351\252\217\351\271\217/SQLQuery1.sql" @@ -0,0 +1,129 @@ +锘縞reate database tbl +on +( name='tbl', + filename='D:\tbl.mdf' + + +) +log on +( name='tbl_log', + filename='D:\tbl_log.ldf' + + + +) +go +use tbl +create table tbl_card +( + id varchar(20) primary key not null, + passWord nvarchar(20) not null, + balance int not null , + userName nvarchar(20) not null + + +) +create table tbl_computer +( + id varchar(20) primary key not null , + onUse varchar(2) check(onUse in ('0','1')) not null, + note nvarchar(20) + + + + ) + create table tbl_record + ( + id varchar(20) primary key not null, + cardId varchar(20) references tbl_card(id), + ComputerId varchar(20) references tbl_computer(id), + beginTime datetime , + endTime datetime, + 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','锠熿棊') + select * from tbl_card + insert into tbl_computer values('02','0','25555'), + ('03','1','55555'), + ('04','0','66666'), + ('05','1','88888'), + ('06','0','688878'), + ('B01','0','558558') + select * from tbl_computer + 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-22 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 15:26:00','2007-01-06 22:55:00','6'), + ('55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), + ('64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','30'), + ('65','0025_bbd','02','2006-12-28 18:00:00','2006-12-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_record + + --1. 鏌ヨ鍑虹敤鎴峰悕涓'寮犲啗'鐨勪笂缃戝崱鐨勪笂缃戣褰曪紝瑕佹眰鏄剧ず鍗″彿锛岀敤鎴峰悕锛屾満鍣ㄧ紪鍙枫佸紑濮嬫椂闂淬佺粨鏉熸椂闂达紝鍜屾秷璐归噾棰濓紝骞舵寜娑堣垂閲戦闄嶅簭鎺掑垪 + select C.id 鍗″彿, userName 鐢ㄦ埛鍚, CP.id 鏈哄櫒缂栧彿 , beginTime 寮濮嬫椂闂, endTime 缁撴潫鏃堕棿 ,fee 娑堣垂閲戦 + from tbl_card C,tbl_computer CP ,tbl_record R + where C.id=R.cardId and CP.id=R.ComputerId and userName='寮犲啗' + order by fee DESC + +--2. 鏌ヨ鍑烘瘡鍙版満鍣ㄤ笂鐨勪笂缃戞鏁板拰娑堣垂鐨勬婚噾棰 + select CP.Id 鏈哄櫒缂栧彿, count(ComputerId) 涓婄綉娆℃暟, sum(fee) 娑堣垂鎬婚噾棰 from tbl_computer CP + left join tbl_record R on R.ComputerId =CP.id + group by CP.Id +--3. 鏌ヨ鍑烘墍鏈夊凡缁忎娇鐢ㄨ繃鐨勪笂缃戝崱鐨勬秷璐规婚噾棰 + select cardId 缃戝崱鍙,sum(fee) 娑堣垂鎬婚噾棰 + from tbl_record + group by cardId + --4. 鏌ヨ鍑轰粠鏈秷璐硅繃鐨勪笂缃戝崱鐨勫崱鍙峰拰鐢ㄦ埛鍚 + +select C.id 鍗″彿, userName 鐢ㄦ埛鍚 from tbl_card C + left join tbl_record R on C.id=R.cardId + where fee is null + +--5. 灏嗗瘑鐮佷笌鐢ㄦ埛鍚嶄竴鏍风殑涓婄綉鍗′俊鎭煡璇㈠嚭鏉 + + select * from tbl_card + where passWord= userName + +--6. 鏌ヨ鍑轰娇鐢ㄦ鏁版渶澶氱殑鏈哄櫒鍙峰拰浣跨敤娆℃暟 + + select top 1 ComputerId 浣跨敤娆℃暟鏈澶氱殑鏈哄櫒鍙, count(ComputerId) 浣跨敤娆℃暟 from tbl_record + group by ComputerId + order by count(ComputerId) DESC + +--7. 鏌ヨ鍑哄崱鍙锋槸浠'ABC'缁撳熬鐨勫崱鍙凤紝鐢ㄦ埛鍚嶏紝涓婄綉鐨勬満鍣ㄥ彿鍜屾秷璐归噾棰 + + select C.id 鍗″彿, userName 鐢ㄦ埛鍚, R.ComputerId 涓婄綉鏈哄櫒缂栧彿 ,fee 娑堣垂閲戦 from tbl_card C + left join tbl_record R on C.id = R.cardId + where C.id like '%ABC' + +--8. 鏌ヨ鍑烘槸鍛ㄥ叚銆佸懆澶╀笂缃戠殑璁板綍锛岃姹傛樉绀轰笂缃戠殑鍗″彿锛岀敤鎴峰悕锛屾満鍣ㄥ彿锛屽紑濮嬫椂闂淬佺粨鏉熸椂闂村拰娑堣垂閲戦 + + select R.cardId 鍗″彿, userName 鐢ㄦ埛鍚, ComputerId 鏈哄櫒鍙, beginTime 寮濮嬫椂闂, endTime 缁撴潫鏃堕棿 from tbl_record R + left join tbl_card C on R.cardId=C.id + where datename(DW,beginTime)='鏄熸湡鍏' or datename(DW,beginTime)='鏄熸湡澶' + +--9. 鏌ヨ鎴愪竴娆′笂缃戞椂闂磋秴杩12灏忔椂鐨勭殑涓婄綉璁板綍锛岃姹傛樉绀轰笂缃戠殑鍗″彿锛岀敤鎴峰悕锛屾満鍣ㄥ彿锛屽紑濮嬫椂闂淬佺粨鏉熸椂闂村拰娑堣垂閲戦 + + select R.cardId 鍗″彿, userName 鐢ㄦ埛鍚, ComputerId 鏈哄櫒鍙, beginTime 寮濮嬫椂闂 ,endTime 缁撴潫鏃堕棿 ,fee 娑堣垂閲戦 from tbl_record R + left join tbl_card C on R.cardId= C.id + where datediff(HH,beginTime,endTime)>12 + +--10. 鏌ヨ闄ゆ秷璐归噾棰濇帓鍒楀墠涓夊悕(鏈楂)鐨勪笂缃戣褰曪紝瑕佹眰鏄剧ず涓婄綉鐨勫崱鍙凤紝鐢ㄦ埛鍚嶏紝鏈哄櫒鍙凤紝寮濮嬫椂闂淬佺粨鏉熸椂闂村拰娑堣垂閲戦 + + select top 7 cardId 鍗″彿, userName 鐢ㄦ埛鍚, ComputerId 鏈哄櫒鍙, beginTime 寮濮嬫椂闂,endTime 缁撴潫鏃堕棿 , fee 娑堣垂閲戦 from tbl_record + left join tbl_card C on cardId=C.id + order by fee \ No newline at end of file diff --git "a/2021 03 31/\346\226\271\350\215\243\346\230\237/SQLQuery1.sql" "b/2021 03 31/\346\226\271\350\215\243\346\230\237/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..72084a5b46a8988819f3a48e2c033b03bc445826 --- /dev/null +++ "b/2021 03 31/\346\226\271\350\215\243\346\230\237/SQLQuery1.sql" @@ -0,0 +1,88 @@ +create database tbl +on +( name='tbl', + filename='D:\tbl.mdf' + + +) +log on +( name='tbl_log', + filename='D:\tbl_log.ldf' + + + +) +go +use tbl +create table tbl_card +( + id varchar(20) primary key not null, + passWord nvarchar(20) not null, + balance int not null , + userName nvarchar(20) not null + + +) +create table tbl_computer +( + id varchar(20) primary key not null , + onUse varchar(2) check(onUse in ('0','1')) not null, + note nvarchar(20) + + + + ) + create table tbl_record + ( + id varchar(20) primary key not null, + cardId varchar(20) references tbl_card(id), + ComputerId varchar(20) references tbl_computer(id), + beginTime datetime , + endTime datetime, + 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','张峻') + select * from tbl_card + insert into tbl_computer values('02','0','25555'),('03','1','55555'),('04','0','66666'),('05','1','88888'),('06','0','688878'),('B01','0','558558') + select * from tbl_computer + 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-22 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 15:26:00','2007-01-06 22:55:00','6'), + ('55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), + ('64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','30'), + ('65','0025_bbd','02','2006-12-28 18:00:00','2006-12-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_record + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where userName='张军' order by fee desc + + select ComputerId,COUNT(cardId),SUM(fee) from tbl_record group by ComputerId + + select cardId,SUM(fee) from tbl_record left join tbl_card on tbl_record.cardId=tbl_card.id group by cardId + + select tbl_card.id,userName from tbl_card left join tbl_record on tbl_record.cardId=tbl_card.id where fee is null + + select a.* from tbl_card a,tbl_card b where a.passWord=b.userName + + select top 1 ComputerId,count(id) from tbl_record group by ComputerId order by count(id)desc + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where cardId like '%_ABC' + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where datepart(weekday,endTime)=1 or datepart(weekday,endTime)=7 + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where datediff(HH,beginTime,endTime) >12 + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id + except + select top 3 cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id order by fee desc + + + + diff --git "a/2021 03 31/\346\226\275\346\261\237\345\263\260/SQLQuery1.sql" "b/2021 03 31/\346\226\275\346\261\237\345\263\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8cb026f7077d908f385d9b6a603c701a1bc322c3 --- /dev/null +++ "b/2021 03 31/\346\226\275\346\261\237\345\263\260/SQLQuery1.sql" @@ -0,0 +1,123 @@ +use master +go + +create database WB +on +( +name='WB', +filename='D:\test\WB.mdf', +size=10mb, +maxsize=50mb, +filegrowth=10% +) +log on +( +name='WB_log', +filename='D:\test\WB_log.ldf', +size=10mb, +maxsize=50mb, +filegrowth=10% +) + +go +use WB + +go +create table tbl_card +( +id nvarchar(20) primary key, +passWord varchar(20), +balance money, +userName nvarchar(20) +) + +go +create table tbl_computer +( +id varchar(20) primary key, +onUse varchar(10) check(onUse=1 or onUse=0), +note text +) + +go +create table tbl_record +( +id int primary key, +cardId nvarchar(20) references tbl_card(id), +ComputerId varchar(20) references tbl_computer(id), +beginTime datetime, +endTime datetime, +fee money +) + +go +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,'校庆'), +('008_EDE','zhang',134,'张峻') + +go +insert into tbl_computer values +('02',0,'25555'), +('03',1,'55555'), +('04',0,'66666'), +('05',1,'88888'), +('06',0,'688878'), +('B01',0,'558558') + +go +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-22 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 15:26:00','2007-01-06 22:55:00',6), +(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 15:26:00',50), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',3), +(65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-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 userName 用户名,tbl_card.id 卡号,tbl_record.ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_card inner join tbl_record on tbl_card.id = tbl_record.cardId where userName='张军' order by tbl_record.fee desc + +--2. 查询出每台机器上的上网次数和消费的总金额 +select tbl_computer.id 机器编号, COUNT(ComputerId) 上网次数, SUM(fee) 消费总金额 from tbl_computer left join tbl_record on tbl_record.ComputerId =tbl_computer.id group by tbl_computer.Id + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select tbl_card.id 网卡号,SUM(fee) 消费总金额 from tbl_card inner join tbl_record on tbl_card.id = tbl_record.cardId group by tbl_card.id + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select id 网卡号,userName 用户名 from tbl_card +except +select tbl_record.cardId 网卡号,userName 用户名 from tbl_record inner join tbl_card on tbl_card.id = tbl_record.cardId + +select tbl_card.id 卡号, userName 用户名,fee 消费金额 from tbl_card left join tbl_record tbl_record on tbl_card.id=tbl_record.cardId where fee is null + +--5. 将密码与用户名一样的上网卡信息查询出来 +select A. * from tbl_card A,tbl_card B where A.passWord=B.userName + +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 机器号,COUNT(cardId) 使用次数 from tbl_record group by ComputerId order by COUNT(cardId) desc + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select tbl_card.id 卡号,userName 用户名,ComputerId 机器号,fee 消费金额 from tbl_card inner join tbl_record on tbl_card.id=tbl_record.cardId where tbl_card.id like'%ABC%' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId 网卡号,userName 用户名,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where datename(DW,beginTime)='星期六' or datename(DW,beginTime)='星期天' + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_card.id 卡号,userName 用户名,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_card inner join tbl_record on tbl_card.id = tbl_record.cardId where datediff(hour,endTime,beginTime)>12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 tbl_card.id 卡号,userName 用户名,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record +left join tbl_card on tbl_card.id = tbl_record.cardId +order by fee desc \ No newline at end of file diff --git "a/2021 03 31/\346\235\216\345\255\220\346\272\220/SQLQuery0331.sql" "b/2021 03 31/\346\235\216\345\255\220\346\272\220/SQLQuery0331.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fd6ccd3a3c5e93e47c97abff06c38158a8889908 --- /dev/null +++ "b/2021 03 31/\346\235\216\345\255\220\346\272\220/SQLQuery0331.sql" @@ -0,0 +1,130 @@ +use master +go + +create database bbs +on +( + name='bbs', + filename='D:\bbs.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='bbs_log', + filename='D:\bbs_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +use bbs +go +create table tbl_card +( + cardID varchar(10) primary key, + passWord varchar(20), + balance int , + userName varchar(10) +) +create table tbl_computer +( + computerID varchar(10) primary key, + onUse varchar check(onUse='0' or onUse='1'), + note int +) +create table tbl_record +( + recordID int primary key, + cardId varchar(10), + ComputerId varchar(10), + beginTime datetime, + endTime datetime, + fee int +) + + +insert 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 , '张峻') +go +insert tbl_computer values +('02' , 0 , 25555),('03' , 1 , 55555), +('04' , 0 , 66666),('05' , 1 , 88888), +('06' , 0 , 688878),('B01' , 0 , 558558) +go +insert tbl_record values +(23 , '0078_RJV' , 'B01' , '2007-07-15 19:00:00' , '2007-07-15 21: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-22 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 15:26:00' , '2007-01-06 22:55:00' , 6), +(55 , '0023_ABC' , '03' , '2006-07-21 15:26:00' , '2006-07-21 22:55:00' , 50), +(64 , '0045-Y' , '04' , '2006-12-24 18:00:00' , '2006-12-24 22:00:00' , 3), +(65 , '0025_bbd' , '02' , '2006-12-28 18:00:00' , '2006-12-28 22:00:00' , 23), +(98 , '0025_bbd' , '02' , '2006-12-26 18:00:00' , '2006-12-26 22:00:00' , 23) +go + + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select A.cardID as 卡号 , userName as 用户名 , ComputerId as 机器编号 , beginTime as 开始时间 , endTime as 结束时间 , fee as 和消费金额 +from tbl_record A inner join tbl_card B on A.cardID = B.cardID +where userName = '张军' order by fee desc +go + +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId , COUNT(ComputerId) , SUM(fee) from tbl_record group by ComputerId +go + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select A.cardID , SUM(fee) from tbl_record A +inner join tbl_card B on A.cardID=B.cardID +group by A.cardID +go + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select B.cardID , B.userName from tbl_record A +right join tbl_card B on A.cardID = B.cardID +except +select A.cardID , userName from tbl_record A +inner join tbl_card B on A.cardID = B.cardID +go + +--5. 将密码与用户名一样的上网卡信息查询出来 +select A.cardID , A.passWord , A.balance ,A.userName from tbl_card A , tbl_card B +where A.passWord = B.userName and A.cardID = B.cardID +go + +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId , COUNT(recordID) from tbl_record +group by ComputerId +order by COUNT(recordID) desc +go + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select A.cardId , userName , ComputerId , fee from tbl_record A +inner join tbl_card B on A.cardId = B.cardId +where A.cardId like('%ABC') +go + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select A.cardId , ComputerId , beginTime , endTime , fee from tbl_record A +inner join tbl_card B on A.cardId = B.cardId +where datepart(weekday,beginTime)=1 or datepart(weekday,beginTime)=7 +go + +--9. 查询出一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select A.cardId , ComputerId , beginTime , endTime , fee from tbl_record A +inner join tbl_card B on A.cardId = B.cardId +where DATEDIFF(HH,beginTime,endTime)>12 +go + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 7 cardId 卡号, userName 用户名, ComputerId 机器号, beginTime 开始时间,endTime 结束时间 , fee 消费金额 from tbl_record + left join tbl_card C on cardId=C.cardID + order by fee \ No newline at end of file diff --git "a/2021 03 31/\346\235\216\346\230\214\345\256\235/SQLQuery6.sql" "b/2021 03 31/\346\235\216\346\230\214\345\256\235/SQLQuery6.sql" new file mode 100644 index 0000000000000000000000000000000000000000..69918723c3d3075fc695eef23269009f69d6ecce --- /dev/null +++ "b/2021 03 31/\346\235\216\346\230\214\345\256\235/SQLQuery6.sql" @@ -0,0 +1,106 @@ +create database lzl +on primary +( + name = 'lzl', + size = 5, + maxsize = 500, + filename = 'D:\新建文件夹\.mdf', + filegrowth = 10% +) +log on +( + name = 'lzl_log', + size = 5, + maxsize = 500, + filename = 'D:\新建文件夹\.ldf', + filegrowth = 10% +) +use lzl +go +create table tbl_card +( + id varchar(10) primary key, + password varchar(10) not null, + balance money check(balance>0), + username varchar(10) +) +go +create table tbl_computer +( + id varchar(5) primary key, + onsure varchar(1) check(onsure=0 or onsure=1), + note varchar(10) +) +go +create table tbl_record +( + id varchar(5) primary key, + cardId varchar(10) references tbl_card(id), + ComputerId varchar(5) references tbl_computer(id), + beginTime smalldatetime, + endTime smalldatetime, + fee money +) +--表一为上网卡信息表(tbl_card) +--id: 卡号,主键 +--passWord:密码 +--balance:卡上的余额 +--userName:该上网卡所属的用户名 +insert into tbl_card(id,password,balance,userName) values ('0023_ABC','555',98,'张军'),('0025_CCD','abc',300,'朱柳'),('0036_CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'证言'),('0078_RJV','55885fg',600,'校庆'),('0089_EDE','zhang',134,'校庆') +--表2为网吧机器说明表(tbl_computer) +--id:机器编号,主键 +--onUse:该机器是否正在使用,0为未使用,1为正在使用 +--note:该机器的说明 +insert into tbl_computer(id,onsure,note) values('02',0,25555),('03',1,55555),('04',0,66666),('05',1,88888),('06',0,688878),('B01',0,558558) +--表3为上网记录表(tbl_record): +--id:记录编号,主键 +--cardId:本次上网的卡号,外键 +--ComputerId:本次上网记录所使用的机器号,外键 +--beginTime:本次上网记录的开始时间 +--endTime:本次上网记录的结束时间 +--fee:本次上网的费用 +insert into tbl_record(id,cardId,ComputerId,beginTime,endTime,fee) +select 23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00',20 union +select 34,'0025_CCD','02','2006-12-25 18:00:00','2006-12-25 22:00:00',23 union +select 45,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50 union +select 46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00',6 union +select 47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50 union +select 48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00',6 union +select 55,'0023_ABC','03','2006-07-21 15:26:00','2006-7-21 22:55:00',50 union +select 64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 18:00:00',300 union +select 65,'0025_CCD','02','2006-12-28 18:00:00','2006-12-28 18:00:00',23 union +select 98,'0025_CCD','02','2006-12-26 18:00:00','2006-12-26 22:00:00',23 +insert into tbl_record(id,cardId,ComputerId,beginTime,endTime,fee) values (100,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-18 21:00:00',20) +select * from tbl_card +select * from tbl_record +select * from tbl_computer +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select a.id 卡号,a.username 用户名, b.ComputerId 机器编号,b.beginTime 开始时间,b.endTime 结束时间,fee 消费金额 from tbl_card A inner join tbl_record B +on a.id=b.cardId where username='张军' order by fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select b.ComputerId,count(b.ComputerId) 上网总次数, sum(fee) 消费总金额 from tbl_computer a inner join tbl_record b on a.id=b.ComputerId group by b.ComputerId +--3. 查询出所有已经使用过的上网卡的消费总金额) +select cardId 卡号, count(cardId) 该卡使用次数, sum(fee) 消费总金额 from tbl_record group by cardId +--4. 查询出从未消费过的上网卡的卡号和用户名 +select id 卡号,userName 用户名 from tbl_card +except +select cardId 卡号,userName from tbl_record a inner join tbl_card b on a.cardId=b.id +--5. 将密码与用户名一样的上网卡信息查询出来 +select a.* from tbl_card a,tbl_card b where a.password=b.username +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 cardId,count(cardId) 使用次数 from tbl_record group by cardId order by count(cardId) desc +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select a.id,a.username 用户名, b.ComputerId 上网机器号, fee from tbl_card a inner join tbl_record b on a.id=b.cardId where a.id like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select a.id 卡号,a.username 用户名,b.ComputerId 机器号,b.beginTime,b.endTime,b.fee 消费金额 from tbl_card a inner join tbl_record b on a.id=b.cardId +where datename(DW,beginTime)='星期六' or datename(DW,beginTime)='星期日' +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select a.id,a.username,b.beginTime,b.endTime,fee 消费金额 from tbl_card a inner join tbl_record b on a.id=b.cardId where datediff(hour,beginTime,endTime)>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 a.id 上网卡号,a.username 用户名,b.ComputerId 机器号, b.beginTime 开始时间 ,b.endTime 结束时间,fee 消费金额 from tbl_card a inner join tbl_record b +on a.id=b.cardId order by fee desc + + + + diff --git "a/2021 03 31/\346\235\250\344\270\260\350\261\252/SQLQuery1.sql" "b/2021 03 31/\346\235\250\344\270\260\350\261\252/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..55bcb5982051b09439156a3083fff14a1ab49ed5 --- /dev/null +++ "b/2021 03 31/\346\235\250\344\270\260\350\261\252/SQLQuery1.sql" @@ -0,0 +1,123 @@ +use master +go + +create database WB +on +( +name='WB', +filename='D:\test\WB.mdf', +size=10mb, +maxsize=50mb, +filegrowth=10% +) +log on +( +name='WB_log', +filename='D:\test\WB_log.ldf', +size=10mb, +maxsize=50mb, +filegrowth=10% +) + +go +use WB + +go +create table tbl_card +( +id nvarchar(20) primary key, +passWord varchar(20), +balance money, +userName nvarchar(20) +) + +go +create table tbl_computer +( +id varchar(20) primary key, +onUse varchar(10) check(onUse=1 or onUse=0), +note text +) + +go +create table tbl_record +( +id int primary key, +cardId nvarchar(20) references tbl_card(id), +ComputerId varchar(20) references tbl_computer(id), +beginTime datetime, +endTime datetime, +fee money +) + +go +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,'校庆'), +('008_EDE','zhang',134,'张峻') + +go +insert into tbl_computer values +('02',0,'25555'), +('03',1,'55555'), +('04',0,'66666'), +('05',1,'88888'), +('06',0,'688878'), +('B01',0,'558558') + +go +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-22 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 15:26:00','2007-01-06 22:55:00',6), +(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 15:26:00',50), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',3), +(65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-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 userName 用户名,tbl_card.id 卡号,tbl_record.ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_card inner join tbl_record on tbl_card.id = tbl_record.cardId where userName='张军' order by tbl_record.fee desc + +--2. 查询出每台机器上的上网次数和消费的总金额 +select tbl_computer.id 机器编号, COUNT(ComputerId) 上网次数, SUM(fee) 消费总金额 from tbl_computer left join tbl_record on tbl_record.ComputerId =tbl_computer.id group by tbl_computer.Id + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select tbl_card.id 网卡号,SUM(fee) 消费总金额 from tbl_card inner join tbl_record on tbl_card.id = tbl_record.cardId group by tbl_card.id + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select id 网卡号,userName 用户名 from tbl_card +except +select tbl_record.cardId 网卡号,userName 用户名 from tbl_record inner join tbl_card on tbl_card.id = tbl_record.cardId + +select tbl_card.id 卡号, userName 用户名,fee 消费金额 from tbl_card left join tbl_record tbl_record on tbl_card.id=tbl_record.cardId where fee is null + +--5. 将密码与用户名一样的上网卡信息查询出来 +select A. * from tbl_card A,tbl_card B where A.passWord=B.userName + +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 机器号,COUNT(cardId) 使用次数 from tbl_record group by ComputerId order by COUNT(cardId) desc + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select tbl_card.id 卡号,userName 用户名,ComputerId 机器号,fee 消费金额 from tbl_card inner join tbl_record on tbl_card.id=tbl_record.cardId where tbl_card.id like'%ABC%' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId 网卡号,userName 用户名,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where datename(DW,beginTime)='星期六' or datename(DW,beginTime)='星期天' + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_card.id 卡号,userName 用户名,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_card inner join tbl_record on tbl_card.id = tbl_record.cardId where datediff(hour,endTime,beginTime)>12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 tbl_card.id 卡号,userName 用户名,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record +left join tbl_card on tbl_card.id = tbl_record.cardId +order by fee desc diff --git "a/2021 03 31/\346\235\250\345\270\206/Tbl.sql" "b/2021 03 31/\346\235\250\345\270\206/Tbl.sql" new file mode 100644 index 0000000000000000000000000000000000000000..565dc542b99d02f41fe032c115cb7c72a2a92e0d --- /dev/null +++ "b/2021 03 31/\346\235\250\345\270\206/Tbl.sql" @@ -0,0 +1,151 @@ +use master +go + +create database Tbl +on +( + name ='Tbl', + filename='D:\test\Tbl.mdf', + size=5, + maxsize=50, + filegrowth=10 +) +log on +( + name ='Tbl_log', + filename='D:\test\Tblt_log.ldf', + size=5, + maxsize=50, + filegrowth=10 +) +go + +use Tbl +go + +create table Tbl_card +( + CardId varchar(10) primary key , + PassWord varchar(10) not null , + Balance int , + UserName varchar(10) not null +) +go + +create table Tbl_computer +( + ComputerId varchar(10) primary key , + OnUse int check(OnUse in(0,1)) not null , + Note varchar(100) +) +go + +create table Tbl_record +( + RecordId int primary key , + CardId varchar(10) references Tbl_Card(CardId) , + ComputerId varchar(10) references Tbl_computer(ComputerId) , + BeginTime datetime , + EndTime datetime , + Fee int +) +go + +insert 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,'张峻') +go + +insert Tbl_computer values +('02',0,'25555'), +('03',1,'55555'), +('04',0,'66666'), +('05',1,'88888'), +('06',0,'688878'), +('B01',0,'558558') +go + +insert 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-22 22:55:00',50), +(48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00',6), +(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00',50), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',30), +(65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00',23), +(98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00',23) +go + +select +A.CardId as 卡号,UserName as 用户名,ComputerId as 机器编号, +BeginTime as 开始时间,EndTime as 结束时间,Fee as 消费金额 +from Tbl_record A +inner join Tbl_card B on A.CardId = B.CardId +where UserName = '张军' +order by Fee desc +go + +select ComputerId,count(ComputerId) as 上网次数,sum(Fee) as 总金额 from Tbl_record +group by ComputerId +go + +select A.CardId,sum(Fee) as 总金额 from Tbl_record A +inner join Tbl_card B on A.CardId = B.CardId +group by A.CardId +go + +select B.CardId,B.UserName as 总金额 from Tbl_record A +right join Tbl_card B on A.CardId = B.CardId +except +select A.CardId,UserName as 总金额 from Tbl_record A +inner join Tbl_card B on A.CardId = B.CardId +go + +select A.CardId,A.PassWord,A.Balance,A.UserName from Tbl_card A ,Tbl_card B +where A.PassWord = B.PassWord and A.PassWord = B.UserName +go + +select top 1 ComputerId as 机器号,count(RecordId) as 使用次数 from Tbl_record +group by ComputerId +order by count(RecordId) desc +go + +select A.CardId,UserName as 用户名,ComputerId as 卡号,Fee as 消费金额 from Tbl_record A +inner join Tbl_card B on A.CardId = B.CardId +where A.CardId like ('%ABC') +go + +select +A.CardId,UserName as 用户名,ComputerId as 机器号, +BeginTime as 开始时间,EndTime as 结束时间,Fee as 消费金额 +from Tbl_record A +inner join Tbl_card B on A.CardId = B.CardId +where datepart(weekday,BeginTime)=1 or datepart(weekday,BeginTime)=7 +go + +select +A.CardId,UserName as 用户名,ComputerId as 机器号, +BeginTime as 开始时间,EndTime as 结束时间,Fee as 消费金额 +from Tbl_record A +inner join Tbl_card B on A.CardId = B.CardId +where DATEDIFF(HH,BeginTime,EndTime) > 12 +go + +select +A.CardId,UserName as 用户名,ComputerId as 机器号, +BeginTime as 开始时间,EndTime as 结束时间,Fee as 消费金额 +from Tbl_record A +inner join Tbl_card B on A.CardId = B.CardId +except +select top 3 +A.CardId,UserName as 用户名,ComputerId as 机器号, +BeginTime as 开始时间,EndTime as 结束时间,Fee as 消费金额 +from Tbl_record A +inner join Tbl_card B on A.CardId = B.CardId +order by Fee desc diff --git "a/2021 03 31/\346\236\227\344\275\263\345\205\203/SQLQuery3.sql" "b/2021 03 31/\346\236\227\344\275\263\345\205\203/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e2d1dbf9e545a3e3578d2c9c517d47912e3b25fa --- /dev/null +++ "b/2021 03 31/\346\236\227\344\275\263\345\205\203/SQLQuery3.sql" @@ -0,0 +1,111 @@ +--首先创建网吧计费系统的数据库,表结构和数据如2.bmp所示,表的说明如下: +create database WB +on +( + name='WB', + filename='D:\WB.mdf', + size=50, + maxsize=100, + filegrowth=10 +) +log on +( + name='WB_log', + filename='D:\WB_log.ldf', + size=50, + maxsize=100, + filegrowth=10 +) +go +--表一为上网卡信息表(tbl_card) +--ID: 卡号,主键 +--PassWord:密码 +--Balance:卡上的余额 +--UserName:该上网卡所属的用户名 +use WB +go +create table tbl_card +( + ID char(10) not null primary key, + PassWord nchar(10) not null, + Balance int not null, + UserName nchar(5) not null +) +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','张峻') +--表2为网吧机器说明表(tbl_computer) +--ID:机器编号,主键 +--OnUse:该机器是否正在使用,0为未使用,1为正在使用 +--NOte:该机器的说明 +create table tbl_computer +( + ID char(10) not null primary key, + OnUse int not null check(OnUse='0' or OnUse='1'), + NOte text not null +) +insert into tbl_computer values ('02','0','25555'),('03','1','55555'),('04','0','66666'),('05','1','88888'),('06','0','688878'),('B01','0','558558') +--表3为上网记录表(tbl_record): +--ID:记录编号,主键 +--CardID:本次上网的卡号,外键 +--ComputerID:本次上网记录所使用的机器号,外键 +--BeginTime:本次上网记录的开始时间 +--EndTime:本次上网记录的结束时间 +--Fee:本次上网的费用 +create table tbl_record +( + ID int not null primary key, + CardID char(10) not null references tbl_card(ID), + ComputerID char(10) not null references tbl_computer(ID), + BeginTime datetime not null, + EndTime datetime not null, + Fee float not null +) +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-22 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 15:26:00','2007-01-06 22:55:00','6'),('55','0023_ABC','03','2006-07-21 15:26:00','2006-07-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-12-28 18:00:00','2006-12-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 tbl_card.ID 显示卡号,UserName 用户名,tbl_computer.ID 机器编号,BeginTime 开始时间,EndTime 结束时间,Fee 消费金额 from tbl_record inner join tbl_card on tbl_record.CardID = tbl_card.ID inner join tbl_computer on tbl_record.ComputerID = tbl_computer.ID +where UserName='张军' order by Fee DESC + +--2. 查询出每台机器上的上网次数和消费的总金额 +select tbl_computer.ID 机器,COUNT(ComputerID)上网次数,SUM(Fee)总金额 from tbl_computer left join tbl_record on tbl_computer.ID = tbl_record.ComputerID group by tbl_computer.ID + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select tbl_record.CardID 上网卡,SUM(Fee)消费总金额 from tbl_record group by tbl_record.CardID + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select tbl_card.ID 上网卡号,UserName 用户名 from tbl_record right join tbl_card on tbl_record.CardID = tbl_card.ID +except +select tbl_card.ID 上网卡号,UserName 用户名 from tbl_record right join tbl_card on tbl_record.CardID = tbl_card.ID where Fee>0 + +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card where PassWord = UserName + +--6. 查询出使用次数最多的机器号和使用次数 +select Top 1 tbl_computer.ID 机器号,COUNT(ComputerID)使用次数 from tbl_record inner join tbl_computer on tbl_record.ComputerID = tbl_computer.ID group by tbl_computer.ID +order by COUNT(ComputerID) DESC + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select tbl_card.ID 卡号,UserName 用户名,tbl_computer.ID 机器号,Fee 消费金额 from tbl_record inner join tbl_card on tbl_record.CardID = tbl_card.ID inner join tbl_computer on tbl_record.ComputerID = tbl_computer.ID +where tbl_card.ID like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_card.ID 卡号,UserName 用户名,ComputerID 机器号,BeginTime 开始时间,EndTime 结束时间,Fee 消费金额 from tbl_record inner join tbl_card on tbl_record.CardID = tbl_card.ID +where DATENAME(DW,BeginTime)='星期六' or DATENAME(DW,BeginTime)='星期日' + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_card.ID 卡号,UserName 用户名,ComputerID 机器号,BeginTime 开始时间,EndTime 结束时间,Fee 消费金额 from tbl_record inner join tbl_card on tbl_record.CardID = tbl_card.ID +where DATEDIFF(HH,BeginTime,EndTime)>5 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_card.ID 卡号,UserName 用户名,ComputerID 机器号,BeginTime 开始时间,EndTime 结束时间,Fee 消费金额 from tbl_record inner join tbl_card on tbl_record.CardID = tbl_card.ID group by tbl_card.ID,UserName,ComputerID,BeginTime,EndTime,Fee +except +select Top 3 tbl_card.ID 卡号,UserName 用户名,ComputerID 机器号,BeginTime 开始时间,EndTime 结束时间,Fee 消费金额 from tbl_record inner join tbl_card on tbl_record.CardID = tbl_card.ID group by tbl_card.ID,UserName,ComputerID,BeginTime,EndTime,Fee +order by Fee DESC + + + + diff --git "a/2021 03 31/\346\270\251\345\271\277\347\224\237/SQLQuery3.sql" "b/2021 03 31/\346\270\251\345\271\277\347\224\237/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..142386da34dcba90ddb39bc55f7f74bf1adf456b --- /dev/null +++ "b/2021 03 31/\346\270\251\345\271\277\347\224\237/SQLQuery3.sql" @@ -0,0 +1,80 @@ +create database A +go +create table stuinfo +( + stuID int primary key identity, + stuName varchar(20), + stuAge int, + stuSex int, + Time datetime +) +create table courseinfo +( + courseID int primary key identity, + courseName varchar(20), + courseMarks int +) +create table scoreinfo +( + scoreID int primary key identity, + stuID int references stuinfo(stuID), + courseID int references courseinfo(courseID), + score int +) + + +insert into stuinfo(stuName,stuAge,stuSex,time) +select 'Tom',19,1,NULL union +select 'Jack',20,0,NULL union +select 'Rose',21,1,NULL union +select 'Lulu',19,1,NULL union +select 'Lili',21,0,NULL union +select 'abc',20,1,'2007-01-07' + + +insert into courseinfo(courseName,courseMarks) +select 'JavaBase',4 union +select 'HTML',2 union +select 'JavaScipt',2 union +select 'SqlBase',2 + +insert into scoreinfo(stuID,courseID,score) +select 1,1,80 union +select 1,2,85 union +select 1,4,50 union +select 2,1,75 union +select 2,3,45 union +select 2,4,75 union +select 3,1,45 union +select 4,1,95 union +select 4,2,75 union +select 4,3,90 union +select 4,4,45 + +select * from stuinfo +select * from courseinfo +select * from scoreinfo +--avg(score) +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName,count(*),avg(score) from stuinfo a inner join scoreinfo b on a.stuID=b.stuID inner join courseinfo c on a.stuID=c.courseID +group by stuName + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName,count(*) 个数,SUM(score) 总分 from courseinfo a inner join scoreinfo b on a.courseID=b.courseID group by courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select * from stuinfo a,stuinfo b where a.stuAge=b.stuAge and a.stuSex=b.stuSex and a.stuID!=b.stuID +--4.查询出学分一样的课程信息 +select * from courseinfo a,courseinfo b where a.courseMarks=b.courseMarks and a.courseID!=b.courseID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select a.stuID,stuName,b.courseID,score from stuinfo a inner join scoreinfo b on a.stuID=b.stuID inner join courseinfo c on a.stuID=c.courseID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select a.stuID,b.courseID,courseName,courseMarks,score from stuinfo a inner join scoreinfo b on a.stuID=b.stuID inner join courseinfo c on a.stuID=c.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select a.stuID,stuName from stuinfo a left join scoreinfo b on a.stuID=b.stuID left join courseinfo c on a.stuID=c.courseID + +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 diff --git "a/2021 03 31/\347\206\212\346\226\207\351\221\253/SQLQuery1.sql" "b/2021 03 31/\347\206\212\346\226\207\351\221\253/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0a0b71c05be1588b253219ae9b29241b49871841 --- /dev/null +++ "b/2021 03 31/\347\206\212\346\226\207\351\221\253/SQLQuery1.sql" @@ -0,0 +1,115 @@ +use master +go +create database BBC +on +( + name='BBC', + filename='D:\BBC.mdf', + size=10, + maxsize=300, + filegrowth=20% +) +log on +( + name='BBC_log', + filename='D:\BBC_log.mdf', + size=10, + maxsize=300, + filegrowth=20% +) +go +use BBC +go +create table tbl_card +( + id varchar(20) primary key, + passWord varchar(20) , + balance money , + userName varchar(50) +) + +create table tbl_computer +( + id int primary key, + onUse int , + note varchar(100) , +) + +create table tbl_record +( + id int primary key, + cardI varchar(20) references tbl_card(id) , + ComputerId int references tbl_computer(id) , + beginTime datetime , + endTime datetime , + fee money +) +insert into tbl_card(id,passWord,balance,userName) values ('0023_ABC','555',98,'张军'),('0025_bbd','abe',300,'朱俊'),('0036_CDD','何柳',100,'何柳'),('0045_YGR','0045_YGR',58,'验证'),('0078_RJV','55885fg',600,'校庆'),('0089_EDE','zhang',134,'张俊') + +insert into tbl_computer(id,onUse,note) values (02,0,'25555'),(03,1,'55555'),(04,0,'66666'),(05,1,'88888'),(06,0,'688878'),(01,0,'558558') + +insert into tbl_record(id,cardI,ComputerId,beginTime,endTime,fee) values (23,'0078_RJV',01,'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-22 22:55:00',6),(47,'0023_ABC',03,'2006-12-23 15:26:00','2006-12-23 22:55:00',23), +(48,'0023_ABC',03,'2007-01-06 15:26:00','2007-01-06 22:55:00',6),(55,'0023_ABC',03,'2006-07-21 15:26:00','2007-07-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-12-28 18:00:00','2006-12-28 22:00:00',23),(98,'0025_bbd',02,'2006-12-26 18:00:00','2006-12-26 22:00:00',23) + + + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 + + select C.id 卡号, userName 用户名, CP.id 机器编号 , beginTime 开始时间, endTime 结束时间 ,fee 消费金额 from tbl_card C,tbl_computer CP ,tbl_record R + where C.id=R.cardI and CP.id=R.ComputerId and userName='张军' + order by fee DESC + +--2. 查询出每台机器上的上网次数和消费的总金额 + +select CP.Id 机器编号, count(ComputerId) 上网次数, sum(fee) 消费总金额 from tbl_computer CP + left join tbl_record R on R.ComputerId =CP.id + group by CP.Id + +--3. 查询出所有已经使用过的上网卡的消费总金额 + +select cardI 网卡号 , sum(fee) 消费总金额 + from tbl_record + group by cardI + +--4. 查询出从未消费过的上网卡的卡号和用户名 + +select C.id 卡号, userName 用户名 from tbl_card C + left join tbl_record R on C.id=R.cardI + where fee is null + +--5. 将密码与用户名一样的上网卡信息查询出来 + +select * from tbl_card + where passWord= userName + +--6. 查询出使用次数最多的机器号和使用次数 + +select top 1 ComputerId 使用次数最多的机器号, count(ComputerId) 使用次数 from tbl_record + group by ComputerId + order by count(ComputerId) DESC + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + +select C.id 卡号, userName 用户名, R.ComputerId 上网机器编号 ,fee 消费金额 from tbl_card C + left join tbl_record R on C.id = R.cardI + where C.id like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select R.cardI 卡号, userName 用户名, ComputerId 机器号, beginTime 开始时间, endTime 结束时间 from tbl_record R + left join tbl_card C on R.cardI=C.id + where datename(DW,beginTime)='星期六' or datename(DW,beginTime)='星期天' + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select R.cardI 卡号, userName 用户名, ComputerId 机器号, beginTime 开始时间 ,endTime 结束时间 ,fee 消费金额 from tbl_record R + left join tbl_card C on R.cardI= C.id + where datediff(HH,beginTime,endTime)>12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select top 7 cardI 卡号, userName 用户名, ComputerId 机器号, beginTime 开始时间,endTime 结束时间 , fee 消费金额 from tbl_record + left join tbl_card C on cardI=C.id + order by fee diff --git "a/2021 03 31/\347\216\213\351\200\270\351\243\236/SQLQuery9.sql" "b/2021 03 31/\347\216\213\351\200\270\351\243\236/SQLQuery9.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cf1912baad3490fbea8d1ed5a7ffd7e8b8223ec9 --- /dev/null +++ "b/2021 03 31/\347\216\213\351\200\270\351\243\236/SQLQuery9.sql" @@ -0,0 +1,95 @@ +use master +go + +create database wang +on +( + name='wang.mdf', + filename='D:\wang.mdf', + maxsize =50, + size=20, + filegrowth=10% +) + log on + ( + name='wang_log.ldf', + filename='D:\wang_log.ldf', + size=20, + maxsize=50, + filegrowth=10% + + ) + go + use wang + go +create table tbl_card +( +ID varchar(10) primary key , +passWord char(15), +balance char(15), +userName nvarchar(10) +) +go +create table tbl_computer +( +ID varchar(10) primary key, +onUse int check(onUse=0 or onUse=1), +note nvarchar(50) +) +go +create table tbl_record +( +ID int primary key, +cardId varchar(10) references tbl_card(ID), +ComputerId varchar(10) references tbl_computer(ID), +beginTime datetime, +endTime datetime, +fee money +) +insert into tbl_card values ('0023_ABC','555','98','张军') +insert into tbl_card values ('0025_bbd','abe','300','朱骏'),('0036_CCD','何柳','100','何柳'), +('0045_YGR','0045_YGR','58','验证'),('0078_RJV','5588fg','600','校庆'),('0089_EDE','zhang','134','张俊') +insert into tbl_computer values ('02',0,'25555') +insert into tbl_computer values ('03',1,'55555'),('04',0,'66666'),('05',1,'888888'),('06',0,'6888878'),('01',0,'558558') + + +insert Tbl_record values +(23,'0078_RJV','01','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-22 22:55:00',50), +(48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00',6), +(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00',50), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',30), +(65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00',23), +(98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00',23) +go +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号 +select * from tbl_card +select * from tbl_record +--用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select C.ID,S.userName,C.ID,C.ComputerId,C.beginTime,C.endTime,C.fee from tbl_card S inner join tbl_record C on S.ID=C.cardID WHERE S.userName LIKE '张军' +--2. 查询出每台机器上的上网次数和消费的总金额 +select S.computerID,SUM(S.fee),COUNT(*) from tbl_record S inner join tbl_record C ON S.ID=C.ID group by S.computerID +--3. 查询出所有已经使用过的上网卡的消费总金额 +select S.cardId,SUM(S.fee),COUNT(*) from tbl_record S inner join tbl_record C ON S.ID=C.ID group by S.cardId +--4. 查询出从未消费过的上网卡的卡号和用户名 +select S.cardId,C.userName from tbl_record S right join tbl_card C on S.cardId=C.ID where S.cardId IS NULL +--5. 将密码与用户名一样的上网卡信息查询出来 +select userName,PassWord,B.* from tbl_card A LEFT join tbl_record B ON A.ID=B.cardId WHERE passWord = userName +--6. 查询出使用次数最多的机器号和使用次数 +SELECT top 1 ComputerId ,count(*) from tbl_record A left join tbl_computer B on A.cardId=B.ID GROUP BY A.ID,ComputerId +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +SELECT A.cardId,B.userName,A.ComputerId,A.fee FROM tbl_record A inner join tbl_card B on A.cardId = B.ID where cardId LIKE '%ABC%' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where datediff(HH,beginTime,endTime) >12 +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id + except + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + + + select top 3 cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id order by fee desc diff --git "a/2021 03 31/\347\275\227\346\265\252\345\255\220/SQLQuery1.sql" "b/2021 03 31/\347\275\227\346\265\252\345\255\220/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6fba7a072f2822c2ce2503c5f64fb6f01bea6c68 --- /dev/null +++ "b/2021 03 31/\347\275\227\346\265\252\345\255\220/SQLQuery1.sql" @@ -0,0 +1,88 @@ +create database tbl +on +( name='tbl', + filename='D:\tbl.mdf' + + +) +log on +( name='tbl_log', + filename='D:\tbl_log.ldf' + + + +) +go +use tbl +create table tbl_card +( + id varchar(20) primary key not null, + passWord nvarchar(20) not null, + balance int not null , + userName nvarchar(20) not null + + +) +create table tbl_computer +( + id varchar(20) primary key not null , + onUse varchar(2) check(onUse in ('0','1')) not null, + note nvarchar(20) + + + + ) + create table tbl_record + ( + id varchar(20) primary key not null, + cardId varchar(20) references tbl_card(id), + ComputerId varchar(20) references tbl_computer(id), + beginTime datetime , + endTime datetime, + 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','张峻') + select * from tbl_card + insert into tbl_computer values('02','0','25555'),('03','1','55555'),('04','0','66666'),('05','1','88888'),('06','0','688878'),('B01','0','558558') + select * from tbl_computer + 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-22 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 15:26:00','2007-01-06 22:55:00','6'), + ('55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), + ('64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','30'), + ('65','0025_bbd','02','2006-12-28 18:00:00','2006-12-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_record + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where userName='张军' order by fee desc + + select ComputerId,COUNT(cardId),SUM(fee) from tbl_record group by ComputerId + + select cardId,SUM(fee) from tbl_record left join tbl_card on tbl_record.cardId=tbl_card.id group by cardId + + select tbl_card.id,userName from tbl_card left join tbl_record on tbl_record.cardId=tbl_card.id where fee is null + + select a.* from tbl_card a,tbl_card b where a.passWord=b.userName + + select top 1 ComputerId,count(id) from tbl_record group by ComputerId order by count(id)desc + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where cardId like '%_ABC' + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where datepart(weekday,endTime)=1 or datepart(weekday,endTime)=7 + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where datediff(HH,beginTime,endTime) >12 + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id + except + select top 3 cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id order by fee desc + + + + diff --git "a/2021 03 31/\350\221\243\344\270\226\351\276\231/SQLQuery1.sql" "b/2021 03 31/\350\221\243\344\270\226\351\276\231/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0a0b71c05be1588b253219ae9b29241b49871841 --- /dev/null +++ "b/2021 03 31/\350\221\243\344\270\226\351\276\231/SQLQuery1.sql" @@ -0,0 +1,115 @@ +use master +go +create database BBC +on +( + name='BBC', + filename='D:\BBC.mdf', + size=10, + maxsize=300, + filegrowth=20% +) +log on +( + name='BBC_log', + filename='D:\BBC_log.mdf', + size=10, + maxsize=300, + filegrowth=20% +) +go +use BBC +go +create table tbl_card +( + id varchar(20) primary key, + passWord varchar(20) , + balance money , + userName varchar(50) +) + +create table tbl_computer +( + id int primary key, + onUse int , + note varchar(100) , +) + +create table tbl_record +( + id int primary key, + cardI varchar(20) references tbl_card(id) , + ComputerId int references tbl_computer(id) , + beginTime datetime , + endTime datetime , + fee money +) +insert into tbl_card(id,passWord,balance,userName) values ('0023_ABC','555',98,'张军'),('0025_bbd','abe',300,'朱俊'),('0036_CDD','何柳',100,'何柳'),('0045_YGR','0045_YGR',58,'验证'),('0078_RJV','55885fg',600,'校庆'),('0089_EDE','zhang',134,'张俊') + +insert into tbl_computer(id,onUse,note) values (02,0,'25555'),(03,1,'55555'),(04,0,'66666'),(05,1,'88888'),(06,0,'688878'),(01,0,'558558') + +insert into tbl_record(id,cardI,ComputerId,beginTime,endTime,fee) values (23,'0078_RJV',01,'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-22 22:55:00',6),(47,'0023_ABC',03,'2006-12-23 15:26:00','2006-12-23 22:55:00',23), +(48,'0023_ABC',03,'2007-01-06 15:26:00','2007-01-06 22:55:00',6),(55,'0023_ABC',03,'2006-07-21 15:26:00','2007-07-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-12-28 18:00:00','2006-12-28 22:00:00',23),(98,'0025_bbd',02,'2006-12-26 18:00:00','2006-12-26 22:00:00',23) + + + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 + + select C.id 卡号, userName 用户名, CP.id 机器编号 , beginTime 开始时间, endTime 结束时间 ,fee 消费金额 from tbl_card C,tbl_computer CP ,tbl_record R + where C.id=R.cardI and CP.id=R.ComputerId and userName='张军' + order by fee DESC + +--2. 查询出每台机器上的上网次数和消费的总金额 + +select CP.Id 机器编号, count(ComputerId) 上网次数, sum(fee) 消费总金额 from tbl_computer CP + left join tbl_record R on R.ComputerId =CP.id + group by CP.Id + +--3. 查询出所有已经使用过的上网卡的消费总金额 + +select cardI 网卡号 , sum(fee) 消费总金额 + from tbl_record + group by cardI + +--4. 查询出从未消费过的上网卡的卡号和用户名 + +select C.id 卡号, userName 用户名 from tbl_card C + left join tbl_record R on C.id=R.cardI + where fee is null + +--5. 将密码与用户名一样的上网卡信息查询出来 + +select * from tbl_card + where passWord= userName + +--6. 查询出使用次数最多的机器号和使用次数 + +select top 1 ComputerId 使用次数最多的机器号, count(ComputerId) 使用次数 from tbl_record + group by ComputerId + order by count(ComputerId) DESC + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + +select C.id 卡号, userName 用户名, R.ComputerId 上网机器编号 ,fee 消费金额 from tbl_card C + left join tbl_record R on C.id = R.cardI + where C.id like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select R.cardI 卡号, userName 用户名, ComputerId 机器号, beginTime 开始时间, endTime 结束时间 from tbl_record R + left join tbl_card C on R.cardI=C.id + where datename(DW,beginTime)='星期六' or datename(DW,beginTime)='星期天' + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select R.cardI 卡号, userName 用户名, ComputerId 机器号, beginTime 开始时间 ,endTime 结束时间 ,fee 消费金额 from tbl_record R + left join tbl_card C on R.cardI= C.id + where datediff(HH,beginTime,endTime)>12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select top 7 cardI 卡号, userName 用户名, ComputerId 机器号, beginTime 开始时间,endTime 结束时间 , fee 消费金额 from tbl_record + left join tbl_card C on cardI=C.id + order by fee diff --git "a/2021 03 31/\350\224\241\344\270\234\347\224\237/SQLQuery1.sql" "b/2021 03 31/\350\224\241\344\270\234\347\224\237/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..72084a5b46a8988819f3a48e2c033b03bc445826 --- /dev/null +++ "b/2021 03 31/\350\224\241\344\270\234\347\224\237/SQLQuery1.sql" @@ -0,0 +1,88 @@ +create database tbl +on +( name='tbl', + filename='D:\tbl.mdf' + + +) +log on +( name='tbl_log', + filename='D:\tbl_log.ldf' + + + +) +go +use tbl +create table tbl_card +( + id varchar(20) primary key not null, + passWord nvarchar(20) not null, + balance int not null , + userName nvarchar(20) not null + + +) +create table tbl_computer +( + id varchar(20) primary key not null , + onUse varchar(2) check(onUse in ('0','1')) not null, + note nvarchar(20) + + + + ) + create table tbl_record + ( + id varchar(20) primary key not null, + cardId varchar(20) references tbl_card(id), + ComputerId varchar(20) references tbl_computer(id), + beginTime datetime , + endTime datetime, + 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','张峻') + select * from tbl_card + insert into tbl_computer values('02','0','25555'),('03','1','55555'),('04','0','66666'),('05','1','88888'),('06','0','688878'),('B01','0','558558') + select * from tbl_computer + 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-22 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 15:26:00','2007-01-06 22:55:00','6'), + ('55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), + ('64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','30'), + ('65','0025_bbd','02','2006-12-28 18:00:00','2006-12-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_record + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where userName='张军' order by fee desc + + select ComputerId,COUNT(cardId),SUM(fee) from tbl_record group by ComputerId + + select cardId,SUM(fee) from tbl_record left join tbl_card on tbl_record.cardId=tbl_card.id group by cardId + + select tbl_card.id,userName from tbl_card left join tbl_record on tbl_record.cardId=tbl_card.id where fee is null + + select a.* from tbl_card a,tbl_card b where a.passWord=b.userName + + select top 1 ComputerId,count(id) from tbl_record group by ComputerId order by count(id)desc + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where cardId like '%_ABC' + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where datepart(weekday,endTime)=1 or datepart(weekday,endTime)=7 + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where datediff(HH,beginTime,endTime) >12 + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id + except + select top 3 cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id order by fee desc + + + + diff --git "a/2021 03 31/\350\256\270\345\237\271\346\250\237/SQLQuery1.sql" "b/2021 03 31/\350\256\270\345\237\271\346\250\237/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..72084a5b46a8988819f3a48e2c033b03bc445826 --- /dev/null +++ "b/2021 03 31/\350\256\270\345\237\271\346\250\237/SQLQuery1.sql" @@ -0,0 +1,88 @@ +create database tbl +on +( name='tbl', + filename='D:\tbl.mdf' + + +) +log on +( name='tbl_log', + filename='D:\tbl_log.ldf' + + + +) +go +use tbl +create table tbl_card +( + id varchar(20) primary key not null, + passWord nvarchar(20) not null, + balance int not null , + userName nvarchar(20) not null + + +) +create table tbl_computer +( + id varchar(20) primary key not null , + onUse varchar(2) check(onUse in ('0','1')) not null, + note nvarchar(20) + + + + ) + create table tbl_record + ( + id varchar(20) primary key not null, + cardId varchar(20) references tbl_card(id), + ComputerId varchar(20) references tbl_computer(id), + beginTime datetime , + endTime datetime, + 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','张峻') + select * from tbl_card + insert into tbl_computer values('02','0','25555'),('03','1','55555'),('04','0','66666'),('05','1','88888'),('06','0','688878'),('B01','0','558558') + select * from tbl_computer + 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-22 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 15:26:00','2007-01-06 22:55:00','6'), + ('55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), + ('64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','30'), + ('65','0025_bbd','02','2006-12-28 18:00:00','2006-12-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_record + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where userName='张军' order by fee desc + + select ComputerId,COUNT(cardId),SUM(fee) from tbl_record group by ComputerId + + select cardId,SUM(fee) from tbl_record left join tbl_card on tbl_record.cardId=tbl_card.id group by cardId + + select tbl_card.id,userName from tbl_card left join tbl_record on tbl_record.cardId=tbl_card.id where fee is null + + select a.* from tbl_card a,tbl_card b where a.passWord=b.userName + + select top 1 ComputerId,count(id) from tbl_record group by ComputerId order by count(id)desc + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where cardId like '%_ABC' + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where datepart(weekday,endTime)=1 or datepart(weekday,endTime)=7 + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where datediff(HH,beginTime,endTime) >12 + + select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id + except + select top 3 cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id order by fee desc + + + + diff --git "a/2021 03 31/\350\256\270\350\207\252\346\246\225/SQLQuery1.sql" "b/2021 03 31/\350\256\270\350\207\252\346\246\225/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3bf36dc659ae0fef576aca25dad0317ce1cec3e9 --- /dev/null +++ "b/2021 03 31/\350\256\270\350\207\252\346\246\225/SQLQuery1.sql" @@ -0,0 +1,87 @@ +create database tbl +on( +name='tbl', +FILENAME='D:\tbl.mdf', +SIZE=5MB, +MAXSIZE=100MB, +filegrowth=10% + +) +log on +( +name='tbl_log', +FILENAME='D:\tbl_log.ldf', +SIZE=5MB, +MAXSIZE=100MB, +filegrowth=10% +) +go +use tbl + create table tbl_card + ( + cardid nvarchar(20) primary key , + passWord nvarchar(20), + balance nvarchar(20), + userName nvarchar(20) + ) + create table tbl_computer + ( + computerid nvarchar(20) primary key , + onUse nvarchar check(onUse=0 or onUse=1), + note text, + ) + create table tbl_record + ( + id nvarchar(20) primary key , + cardId nvarchar(20) references tbl_card(cardid), + ComputerId nvarchar(20) references tbl_computer(computerid), + beginTime datetime, + endTime datetime, + fee money + ) + + insert into tbl_card (cardid,passWord,balance,userName) 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 (computerid,onUse,note) values('02','0','25555'),('03','1','55555'), + ('04','0','666666'),('05','1','888888'),('06','0','688878'),('B01','0','558558') + insert into tbl_record (id,cardid,computerid,beginTime,endTime,fee) values('23','0078_RJV','B01','2007-7-15 19:00:00','2007-7-15 21:00','20'), + ('34','0025_bbd','02','2006-12-25 18:00','2006-12-25 22: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-22 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','2006-01-06 15:26:00','2006-01-06 22:55:00','6'), + ('55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), + ('64','0045_YGR','04','2006-12-24 20:00:00','2006-12-24 22:00:00','3') + ,('65','0025_bbd','02','2006-12-28 20:00:00','2006-12-28 22:00:00','23'), + ('98','0025_bbd','02','2006-12-26 20:00:00','2006-12-26 22:00:00','23') + +--请完成以下题目: +select * from tbl_card +select * from tbl_computer +select * from tbl_record + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select tbl_card.cardid,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.cardid where userName='张军' group by tbl_card.cardid,userName,ComputerId,beginTime,endTime,fee order by fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId, count (*) 次数,sum(fee) 总金额 from tbl_record group by ComputerId +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardId,SUM(fee) 总金额 from tbl_record inner join tbl_computer on tbl_record.ComputerId=tbl_computer.ComputerId where onUse=1 group by cardId +--4. 查询出从未消费过的上网卡的卡号和用户名 +select cardId,SUM(fee) 总金额 from tbl_record inner join tbl_computer on tbl_record.ComputerId=tbl_computer.ComputerId where onUse=0 group by cardId +--5. 将密码与用户名一样的上网卡信息查询出来 + +select A.* from tbl_card A,tbl_card B where A.cardid=B.cardid AND A.passWord=B.userName +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 机械号,count (*) 使用次数 from tbl_record group by ComputerId order by 使用次数 desc +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select tbl_record.cardid 卡号,userName 用户名,ComputerId 机械号,fee 消费金额 from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.cardid where tbl_card.cardid like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select * from tbl_record +where datepart (WEEKDAY,beginTime)=1 or DATEPART (WEEKDAY,endTime)=7 +UNION +select tbl_record.cardid 卡号,userName 用户名,ComputerId 机械号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.cardid +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_record.cardid 卡号,userName 用户名,ComputerId 机械号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.cardid WHERE endTime-beginTime>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 tbl_record.cardid 卡号,userName 用户名,ComputerId 机械号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.cardid order by fee desc diff --git "a/2021 03 31/\350\265\265\345\230\211\351\252\217/SQLQuery1.sql" "b/2021 03 31/\350\265\265\345\230\211\351\252\217/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5e8b272a2c43f18c65ef50a50189a32b7e64722d --- /dev/null +++ "b/2021 03 31/\350\265\265\345\230\211\351\252\217/SQLQuery1.sql" @@ -0,0 +1,131 @@ +--首先创建网吧计费系统的数据库, +--表结构和数据如2.bmp所示,表的说明如下: +create database Wanba +on( +name=Wanba, +filename='D:\Wanba.mdf', +size=10mb, +maxsize=50mb, +filegrowth=10% +) +log on( +name=Wanba_log, +filename='D:\Wanba_log.mdf', +size=10mb, +maxsize=50mb, +filegrowth=10% +) +go +--表一为上网卡信息表( +create table tbl_card +( +id varchar(30) primary key not null, + --卡号,主键 +passWord char(30) not null, +--密码 +balance int not null, +--卡上的余额 +userName varchar(30) not null +--该上网卡所属的用户名 +) +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,'张峻') +select * from tbl_card +--表2为网吧机器说明表( +create table tbl_computer +( +id varchar primary key not null, +--机器编号,主键 +onUse int check(onuse='1' or onuse='0') not null, +--该机器是否正在使用,0为未使用,1为正在使用 +note char(50) not null +--该机器的说明 +) +insert into tbl_computer values + (02,0,25555),(03,1,55555), + (04,0,66666),(05,1,88888), + (06,0,688878),(01,0,558558) + select * from tbl_computer +--表3为上网记录表( +create table tbl_record +( +id int primary key not null, +--记录编号,主键 +cardId varchar(30) references tbl_card(id) not null, +--本次上网的卡号,外键 +ComputerId varchar references tbl_computer(id) not null, +--本次上网记录所使用的机器号,外键 +beginTime char(30) not null, +--本次上网记录的开始时间 +endTime char(30) not null, +--本次上网记录的结束时间 +fee int not null +--本次上网的费用 +) +INSERT into tbl_record values +(23,'0078_RJV',01,'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-22 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 15:26:00','2007-01-06 22:55:00',6), +(55,'0023_ABC',03,'2006-07-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-12-28 18:00:00','2006-12-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_record + + +select * from tbl_card +select * from tbl_computer +select * from tbl_record +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select tbl_card.id 卡号,username,tbl_computer.id,begintime,endtime,fee from tbl_record + inner join tbl_card on tbl_record.CardID = tbl_card.ID + inner join tbl_computer on tbl_record.ComputerID = tbl_computer.ID +where UserName='张军' order by Fee DESC +--2. 查询出每台机器上的上网次数和消费的总金额 +select tbl_computer.id,count(*)上网次数,sum(fee) 消费的总金额 from tbl_record + inner join tbl_card on tbl_record.CardID = tbl_card.ID + inner join tbl_computer on tbl_record.ComputerID = tbl_computer.ID + group by tbl_computer.id +--3. 查询出所有已经使用过的上网卡的消费总金额 +select tbl_card.id,sum(fee) 消费总金额 from tbl_record +inner join tbl_card on tbl_record.cardId = tbl_card.id +group by tbl_card.id +--4. 查询出从未消费过的上网卡的卡号和用户名 +select tbl_card.id,username from tbl_card +full join tbl_record on tbl_record.cardId =tbl_card.id +where fee is null +--5. 将密码与用户名一样的上网卡信息查询出来 +select A.* from tbl_card A,tbl_card B +where A.passWord=A.userName and A.id != B.id + +select *FROM tbl_card where passWord = userName +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 tbl_computer.id,count(*) from tbl_record +inner join tbl_computer on tbl_record.ComputerId=tbl_computer.id +group by tbl_computer.id order by count(*) desc +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select tbl_card.id ,username,tbl_computer.id,fee from tbl_record +inner join tbl_card on tbl_record.cardId=tbl_card.id +inner join tbl_computer on tbl_record.ComputerId=tbl_computer.id +where tbl_card.id like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_card.id,username,tbl_computer.id,begintime,endtime,fee from tbl_record +inner join tbl_card on tbl_record.cardId=tbl_card.id +inner join tbl_computer on tbl_record.ComputerId=tbl_computer.id +where DATENAME(DW,BeginTime)='星期六' or DATENAME(DW,BeginTime)='星期日' +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_card.id,username,tbl_computer.id,begintime,endtime,fee from tbl_record +inner join tbl_card on tbl_record.cardId=tbl_card.id +inner join tbl_computer on tbl_record.ComputerId=tbl_computer.id +where DATEDIFF(HH,BeginTime,EndTime)>5 +--10. 查询出消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 tbl_card.id,username,tbl_computer.id,begintime,endtime,fee from tbl_record +inner join tbl_card on tbl_record.cardId=tbl_card.id +inner join tbl_computer on tbl_record.ComputerId=tbl_computer.id +order by fee desc + diff --git "a/2021 03 31/\351\203\221\347\276\216\345\251\267/SQLQuery7.sql" "b/2021 03 31/\351\203\221\347\276\216\345\251\267/SQLQuery7.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a4887a9c5ba969a0bf16f7d3763cf936f18af124 --- /dev/null +++ "b/2021 03 31/\351\203\221\347\276\216\345\251\267/SQLQuery7.sql" @@ -0,0 +1,102 @@ +use master +go +create database WB +on +( +name='WB', +filename='D:\SQLcunchu\WB.dmf', +size=5mb, +maxsize=100mb, +filegrowth=10% +) +log on +( +name='WB_log', +filename='D:\SQLcunchu\WB_log.lmf', +size=5mb, +maxsize=100mb, +filegrowth=10% +) +go +use WB +go +create table tbl_card +( +id varchar(20) primary key, +passWord varchar(11), +balance money, +userName nvarchar(20) +) +create table tbl_computer +( +id varchar(10) primary key, +onUse char check(onUse in (1,0)), +note ntext +) +create table tbl_record +( +id int primary key, +cardId varchar(20) references tbl_card(id), +ComputerId varchar(10), +begintime datetime, +endtime datetime, +free money +) +insert into tbl_card values +('0025_AAA','555','77','张军'), +('0023_ABC','555',98,'张军'), +('0025_bbd','abe',300,'朱俊'), +('0036_CCD','何柳',100,'何柳'), +('0045_VGR','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,'68878'), +('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-22 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 15:26:00','2007-01-06 22:55:00','6'), +(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), +(64,'0045_VGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','38'), +(65,'0025_bbd','04','2006-12-28 18:00:00','2006-12-28 22:00:00','23'), +(98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-28 22:00:00','23') +select*from tbl_card +select*from tbl_computer +select*from tbl_record +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select cardId 卡号,userName 用户姓名,tbl_record.id 机器编号,begintime 开始时间,endtime 结束时间,free 消费金额 from tbl_card +right join tbl_record on tbl_card.id = tbl_record.cardId +where userName='张军' group by cardId, userName,tbl_record.id,begintime,endtime,free order by free desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select tbl_computer.id 机器名称,count(*)上网次数,sum(free)消费总金额 from tbl_computer left join tbl_record on tbl_computer.id=tbl_record.ComputerId group by tbl_computer.id +--3. 查询出所有已经使用过的上网卡的消费总金额 +select sum(free)消费总金额 from tbl_record left join tbl_card on tbl_record.cardId = tbl_card.id where cardId is not null +--4. 查询出从未消费过的上网卡的卡号和用户名 +select tbl_card.id 卡号,userName 用户名 from tbl_record right join tbl_card on tbl_record.cardId = tbl_card.id where cardId is null +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card A inner join +(select passWord,userName from tbl_card group by passWord,userName having count(passWord)>1 and count(userName)>1) B on A.userName=B.userName and A.passWord=B.passWord +--6. 查询出使用次数最多的机器号和使用次数 +SELECT TOP 1 A.ComputerId ,count( * ) AS count from tbl_record A INNER join tbl_record B on A.ID=B.ID GROUP BY A.ComputerId ORDER BY COUNT(*) desc +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select cardId 卡号,userName 用户名,ComputerId 机器号,free 消费金额 from tbl_card A inner join tbl_record B on A.id=B.cardId where cardId like '[0-9][0-9][0-9][0-9]_ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId 卡号,userName 用户名,begintime 开始时间,endtime 结束时间,free 消费金额 from tbl_card right join tbl_record on tbl_card.id=tbl_record.cardId +where datepart(weekday,endtime)=1 or datepart(WEEKDAY,endtime)=7 +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select * from tbl_record where datediff(HH,begintime,endtime)>=12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 *from tbl_record order by free desc +select *from tbl_record where free =(select MAX(free) from tbl_record) + + + + diff --git "a/2021 03 31/\351\203\255\347\220\252\346\236\253/0331.sql" "b/2021 03 31/\351\203\255\347\220\252\346\236\253/0331.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2b51ce3cb0c051918bf490ac181d14bcf32d8631 --- /dev/null +++ "b/2021 03 31/\351\203\255\347\220\252\346\236\253/0331.sql" @@ -0,0 +1,133 @@ +use master +go +create database Wangba +on +( + name='Wangba', + filename='D:\Wangba.mdf', + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) +log on +( + name='Wangba_log', + filename='D:\Wangba_log.ldf', + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) +go +use Wangba +go + +create table tbl_card +( + ID varchar(20) Primary key, + PassWord varchar(20), + Balance money, + UseName nvarchar(20) +) + +create table tbl_computer +( + ID varchar(20) primary key, + OnUse char(2) check(OnUse in (1,0)), + Note int +) + +create table tbl_record +( + ID varchar(20) primary key, + cardId varchar(20) references tbl_card(ID), + ComputerId varchar(20) references tbl_computer(ID), + beginTime datetime, + endTime datetime, + fee money +) + +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-22 22:55:00',6), +(47,'0023_ABC','03','2006-07-15 15:26:00','2006-07-15 22:55:00',50), +(48,'0023_ABC','03','2007-07-15 15:26:00','2007-07-15 22:55:00',6), +(55,'0023_ABC','03','2006-07-15 15:26:00','2006-07-15 22:55:00',50), +(64,'0045_YGR','04','2006-07-15 18:00:00','2006-07-15 22:00:00',30), +(65,'0025_bbd','02','2006-07-15 18:00:00','2006-07-15 22:00:00',23), +(98,'0025_bbd','02','2006-07-15 18:00:00','2006-07-15 22:00:00',23) + +select * from tbl_card +select * from tbl_computer +select * from tbl_record + +--请完成以下题目: +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额, +--并按消费金额降序排列 + +select TD.ID 卡号,UseName 用户名,TR.ID 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record TR inner join tbl_card TD on TR.cardId=TD.ID +inner join tbl_computer TC on TR.ComputerId=TC.ID where UseName='张军' group by TD.ID ,UseName, TR.ID ,beginTime ,endTime ,fee order by fee DESC + +--2. 查询出每台机器上的上网次数和消费的总金额 + +select ComputerId 机器编号,COUNT(*) 上网次数,SUM(fee) 消费的总金额 from tbl_record group by ComputerId + +--3. 查询出所有已经使用过的上网卡的消费总金额 + +select TD.ID 卡号,SUM(fee) 消费总金额 from tbl_record TR inner join tbl_card TD on TR.cardId=TD.ID group by TD.ID + + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select ID 卡号,UseName 用户名 from tbl_card +except +select cardId 卡号,UseName 用户名 from tbl_record TR inner join tbl_card TD on TR.cardId=TD.ID +--5. 将密码与用户名一样的上网卡信息查询出来 + +select A.* from tbl_card A,tbl_card B where A.PassWord=B.PassWord AND A.UseName=B.UseName + +--6. 查询出使用次数最多的机器号和使用次数 + +select top 1 ComputerId 机器号 , COUNT(*) 使用次数 from tbl_record group by ComputerId order by COUNT(*) DESC +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + +select TD.ID 卡号,UseName 用户名,ComputerId 机器号,fee 消费金额 from tbl_record TR inner join tbl_card TD on TR.cardId=TD.ID + where TD.ID like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select TD.ID 卡号,UseName 用户名,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record TR inner join tbl_card TD on TR.cardId=TD.ID +where datename(DW,beginTime)='星期六' or datename(DW,beginTime)='星期日' +select datename(DW,'2007-07-15 19:00:00.000') + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select TD.ID 卡号,UseName 用户名,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record TR inner join tbl_card TD on TR.cardId=TD.ID +where datediff(HOUR,endTime,endTime)>12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select TD.ID 卡号,UseName 用户名,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from tbl_record TR inner join tbl_card TD on TR.cardId=TD.ID +inner join tbl_computer TC ON TC.ID=TR.ComputerId +except +select TD.ID 卡号,UseName 用户名,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from tbl_record TR inner join tbl_card TD on TR.cardId=TD.ID +inner join tbl_computer TC ON TC.ID=TR.ComputerId +where fee=(select MAX(fee) from tbl_record) + + + diff --git "a/2021 03 31/\351\237\246\345\244\251\347\277\224/SQLQuery1.sql" "b/2021 03 31/\351\237\246\345\244\251\347\277\224/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0a0b71c05be1588b253219ae9b29241b49871841 --- /dev/null +++ "b/2021 03 31/\351\237\246\345\244\251\347\277\224/SQLQuery1.sql" @@ -0,0 +1,115 @@ +use master +go +create database BBC +on +( + name='BBC', + filename='D:\BBC.mdf', + size=10, + maxsize=300, + filegrowth=20% +) +log on +( + name='BBC_log', + filename='D:\BBC_log.mdf', + size=10, + maxsize=300, + filegrowth=20% +) +go +use BBC +go +create table tbl_card +( + id varchar(20) primary key, + passWord varchar(20) , + balance money , + userName varchar(50) +) + +create table tbl_computer +( + id int primary key, + onUse int , + note varchar(100) , +) + +create table tbl_record +( + id int primary key, + cardI varchar(20) references tbl_card(id) , + ComputerId int references tbl_computer(id) , + beginTime datetime , + endTime datetime , + fee money +) +insert into tbl_card(id,passWord,balance,userName) values ('0023_ABC','555',98,'张军'),('0025_bbd','abe',300,'朱俊'),('0036_CDD','何柳',100,'何柳'),('0045_YGR','0045_YGR',58,'验证'),('0078_RJV','55885fg',600,'校庆'),('0089_EDE','zhang',134,'张俊') + +insert into tbl_computer(id,onUse,note) values (02,0,'25555'),(03,1,'55555'),(04,0,'66666'),(05,1,'88888'),(06,0,'688878'),(01,0,'558558') + +insert into tbl_record(id,cardI,ComputerId,beginTime,endTime,fee) values (23,'0078_RJV',01,'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-22 22:55:00',6),(47,'0023_ABC',03,'2006-12-23 15:26:00','2006-12-23 22:55:00',23), +(48,'0023_ABC',03,'2007-01-06 15:26:00','2007-01-06 22:55:00',6),(55,'0023_ABC',03,'2006-07-21 15:26:00','2007-07-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-12-28 18:00:00','2006-12-28 22:00:00',23),(98,'0025_bbd',02,'2006-12-26 18:00:00','2006-12-26 22:00:00',23) + + + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 + + select C.id 卡号, userName 用户名, CP.id 机器编号 , beginTime 开始时间, endTime 结束时间 ,fee 消费金额 from tbl_card C,tbl_computer CP ,tbl_record R + where C.id=R.cardI and CP.id=R.ComputerId and userName='张军' + order by fee DESC + +--2. 查询出每台机器上的上网次数和消费的总金额 + +select CP.Id 机器编号, count(ComputerId) 上网次数, sum(fee) 消费总金额 from tbl_computer CP + left join tbl_record R on R.ComputerId =CP.id + group by CP.Id + +--3. 查询出所有已经使用过的上网卡的消费总金额 + +select cardI 网卡号 , sum(fee) 消费总金额 + from tbl_record + group by cardI + +--4. 查询出从未消费过的上网卡的卡号和用户名 + +select C.id 卡号, userName 用户名 from tbl_card C + left join tbl_record R on C.id=R.cardI + where fee is null + +--5. 将密码与用户名一样的上网卡信息查询出来 + +select * from tbl_card + where passWord= userName + +--6. 查询出使用次数最多的机器号和使用次数 + +select top 1 ComputerId 使用次数最多的机器号, count(ComputerId) 使用次数 from tbl_record + group by ComputerId + order by count(ComputerId) DESC + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + +select C.id 卡号, userName 用户名, R.ComputerId 上网机器编号 ,fee 消费金额 from tbl_card C + left join tbl_record R on C.id = R.cardI + where C.id like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select R.cardI 卡号, userName 用户名, ComputerId 机器号, beginTime 开始时间, endTime 结束时间 from tbl_record R + left join tbl_card C on R.cardI=C.id + where datename(DW,beginTime)='星期六' or datename(DW,beginTime)='星期天' + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select R.cardI 卡号, userName 用户名, ComputerId 机器号, beginTime 开始时间 ,endTime 结束时间 ,fee 消费金额 from tbl_record R + left join tbl_card C on R.cardI= C.id + where datediff(HH,beginTime,endTime)>12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select top 7 cardI 卡号, userName 用户名, ComputerId 机器号, beginTime 开始时间,endTime 结束时间 , fee 消费金额 from tbl_record + left join tbl_card C on cardI=C.id + order by fee diff --git "a/2021 03 31/\351\273\204\345\277\240\347\243\212/SQLQuery15.sql" "b/2021 03 31/\351\273\204\345\277\240\347\243\212/SQLQuery15.sql" new file mode 100644 index 0000000000000000000000000000000000000000..196b283b408fb815fef694f0df00b4a107ad6d4a --- /dev/null +++ "b/2021 03 31/\351\273\204\345\277\240\347\243\212/SQLQuery15.sql" @@ -0,0 +1,97 @@ +use master +go +create database WB +on( +name='WB', +FILENAME='D:\WB.LDF', +SIZE=5MB, +MAXSIZE=100MB, +filegrowth=10% + +) +log on ( +name='WB_log', +FILENAME='D:\WB.LDF_log', +SIZE=5MB, +MAXSIZE=100MB, +filegrowth=10% + + +) +go +use WB +GO + create table tbl_card + ( + cardid nvarchar(20) primary key , + passWord nvarchar(20), + balance nvarchar(20), + userName nvarchar(20) + ) + create table tbl_computer + ( + computerid nvarchar(20) primary key , + onUse nvarchar check(onUse=0 or onUse=1), + note text, + ) + create table tbl_record + ( + id nvarchar(20) primary key , + cardId nvarchar(20) references tbl_card(cardid), + ComputerId nvarchar(20) references tbl_computer(computerid), + beginTime datetime, + endTime datetime, + fee money + ) + + insert into tbl_card (cardid,passWord,balance,userName) 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 (computerid,onUse,note) values('02','0','25555'),('03','1','55555'), + ('04','0','666666'),('05','1','888888'),('06','0','688878'),('B01','0','558558') + insert into tbl_record (id,cardid,computerid,beginTime,endTime,fee) values('23','0078_RJV','B01','2007-7-15 19:00:00','2007-7-15 21:00','20'), + ('34','0025_bbd','02','2006-12-25 18:00','2006-12-25 22: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-22 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','2006-01-06 15:26:00','2006-01-06 22:55:00','6'), + ('55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), + ('64','0045_YGR','04','2006-12-24 20:00:00','2006-12-24 22:00:00','3') + ,('65','0025_bbd','02','2006-12-28 20:00:00','2006-12-28 22:00:00','23'), + ('98','0025_bbd','02','2006-12-26 20:00:00','2006-12-26 22:00:00','23') + + +--请完成以下题目: +select * from tbl_card +select * from tbl_computer +select * from tbl_record + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select tbl_card.cardid,userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.cardid group by tbl_card.cardid,userName,ComputerId,beginTime,endTime,fee order by fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId, count (*) 次数,sum(fee) 总金额 from tbl_record group by ComputerId +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardId,SUM(fee) 总金额 from tbl_record inner join tbl_computer on tbl_record.ComputerId=tbl_computer.ComputerId where onUse=1 group by cardId +--4. 查询出从未消费过的上网卡的卡号和用户名 +select cardId,SUM(fee) 总金额 from tbl_record inner join tbl_computer on tbl_record.ComputerId=tbl_computer.ComputerId where onUse=0 group by cardId +--5. 将密码与用户名一样的上网卡信息查询出来 + +select A.* from tbl_card A,tbl_card B where A.cardid=B.cardid AND A.passWord=B.userName +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 机械号,count (*) 使用次数 from tbl_record group by ComputerId order by 使用次数 desc +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select tbl_record.cardid 卡号,userName 用户名,ComputerId 机械号,fee 消费金额 from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.cardid where tbl_card.cardid like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select * from tbl_record +where datepart (WEEKDAY,beginTime)=1 or DATEPART (WEEKDAY,endTime)=7 +UNION +select tbl_record.cardid 卡号,userName 用户名,ComputerId 机械号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.cardid +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_record.cardid 卡号,userName 用户名,ComputerId 机械号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.cardid WHERE endTime-beginTime>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 tbl_record.cardid 卡号,userName 用户名,ComputerId 机械号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.cardid order by fee desc + + + + + \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/.keep" "b/2021 04 02 \344\275\234\344\270\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/2021 04 02 \344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ba7fd89937d3d5c45406b9b859ae454a9fe8c9e4 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery1.sql" @@ -0,0 +1,55 @@ +create database GoodsDB +on +( + + name='GoodsDB', + filename='D:\GoodsDB.mdf' +) +log on +( + + name='GoodsDB_log', + filename='D:\GoodsDB_log.ldf' + + +) +go +use GoodsDB +create table GoodsType +( + TypeID int not null primary key identity, + TypeName nvarchar(20) not null + +) +go +create table GoodsInfo +( + GoodsID int not null primary key identity, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) + + +) + +insert into GoodsType values ('服装内衣'),('鞋包配饰'),('手机数码') +select * from GoodsType +insert into GoodsInfo values + ('提花小西装',' 红色', '菲曼琪', '300', '1'), + ('百搭短裤' ,'绿色' ,'哥弟' ,'100' ,'1'), + ('无袖背心', '白色', '阿依莲', '700', '1'), + ('低帮休闲鞋' ,'红色' ,'菲曼琪', '900' ,'2'), + ('中跟单鞋', '绿色', '哥弟', '400', '2'), + ('平底鞋', '白色' ,'阿依莲' ,'200',' 2'), + ('迷你照相机', '红色' ,'尼康', '500', '3'), + ('硬盘 ','黑色', '希捷', '600' ,'3'), + ('显卡' ,'黑色', '技嘉', '800', '3') + SELECT * from GoodsInfo + + SELECT top 1 GoodsName as 商品名称,GoodsColor as 商品颜色,GoodsMoney as 商品价格 from GoodsInfo order by GoodsMoney desc + + select TypeID,MAX(GoodsMoney) as 最高价格,min(GoodsMoney) as 最低价格,avg(GoodsMoney) as 平均价格 from GoodsInfo group by TypeID + + select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>300 and GoodsMoney<600 diff --git "a/2021 04 02 \344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery2.sql" "b/2021 04 02 \344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2d86defe64c7a8db1f89e4b942c464480cfbb4aa --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery2.sql" @@ -0,0 +1,103 @@ +use ClassicDb +go + + +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore + +select D.* from +(select A.StudentId a,A.CourseId b,A.Score c,B.StudentId d,B.CourseId e,B.Score f from +(select * from StudentCourseScore where CourseId = 1) as A , +(select * from StudentCourseScore where CourseId = 2) as B +where A.Score > B.Score and A.StudentId = B.StudentId) as C +inner join StudentInfo D on C.a = D.StudentCode + + + + +select StudentId,count(CourseId) from StudentCourseScore +where CourseId = 1 or CourseId = 2 +group by StudentId +having count(CourseId) = 2 + + +select * from (select * from StudentCourseScore where CourseId = 1) as A +right join (select * from StudentCourseScore where CourseId = 2) as B on A.StudentId = B.StudentId + + + +select * from (select * from StudentCourseScore where CourseId = 1) as A +left join (select * from StudentCourseScore where CourseId = 2) as B on A.StudentId = B.StudentId +where A.CourseId is null + + +select StudentId as 学生编号,StudentName as 学生姓名,avg(Score) as 平均成绩 from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +group by StudentId,StudentName +having avg(Score) >= 60 + + +select B.* from StudentCourseScore A +left join StudentInfo B on A.StudentId = B.StudentCode + + +select +StudentCode as 学生编号,StudentName as 学生姓名, +count(CourseId) as 选课总数,sum(Score) as 总成绩 +from StudentCourseScore A +right join StudentInfo B on A.StudentId = B.StudentCode +group by StudentCode,StudentName +order by StudentCode + + + +select StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +left join StudentInfo B on A.StudentId = B.StudentCode +group by StudentCode,StudentName,Birthday,Sex,ClassId + + + +select count(*) as 姓老师的数量 from Teachers +group by TeacherName +having TeacherName like ('李%') + + + + +select B.* from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +where A.CourseId = 1 + + + +select StudentId,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +group by StudentId,StudentName,Birthday,Sex,ClassId +having count(CourseId)<3 + + + +select StudentId,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 1 or CourseId = 2 or CourseId = 3 +group by StudentId,StudentName,Birthday,Sex,ClassId + + + +select StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 1 or CourseId = 2 or CourseId = 3 +group by StudentCode,StudentName,Birthday,Sex,ClassId +having count(CourseId) = 3 + + + +select StudentName from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +where A.CourseId != 1 +group by StudentCode,StudentName + diff --git "a/2021 04 02 \344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d05691a686eee14c51b30bd90a7e1483e943d833 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery3.sql" @@ -0,0 +1,42 @@ + +CREATE database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5, + maxsize=50, + filegrowth=1 +) +log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5, + maxsize=50, + filegrowth=1 +) +go +use HOUSE_DB +go +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name varchar(50) not null unique +) +create table HOUSE +( +house_id int primary key identity(1,1), +house_name varchar(50) not null , +house_price float not null default(0), +type_id int references HOUSE_TYPE(type_id) +) +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅' ) +insert into HOUSE values('小红屋',500,'1'),('经济屋',1000,'2'),('别墅屋',5000,'3') +select * from HOUSE_TYPE +select * from HOUSE +select * from HOUSE_TYPE where type_name like '%型%' +select * from HOUSE order by house_price desc +select house_name 房屋名称,type_name 房屋类型名称 from HOUSE A INNER JOIN HOUSE_TYPE B ON A.type_id=B.type_id +select top 1 house_price 租金,house_name 房屋名称 from HOUSE order by house_price desc + diff --git "a/2021 04 02 \344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery4.sql" "b/2021 04 02 \344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3c3ef987b4e8418896f3d20f18c72ae8249f6000 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery4.sql" @@ -0,0 +1,70 @@ +use master +go + +create database StarManagerDB +on +( + name='StarManagerDB', + filename='D:\test\StarManagerDB.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='StarManagerDB_log', + filename='D:\test\StarManagerDB_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +go + +use StarManagerDB +go + +create table StarType +( + T_NO int primary key identity not null , + T_NAME nvarchar(20) , +) +go + +create table StarInfo +( + S_NO int primary key identity not null , + S_NAME nvarchar(20) not null , + S_AGE int not null , + S_HOBBY nvarchar(20) , + S_NATIVE nvarchar(20) default('中国大陆') , + S_T_NO int references StarType(T_NO) +) +go + +insert StarType values +('体育明星'), +('IT明星'), +('相声演员') +go + +insert StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) +go + +select top 3 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo +order by S_AGE desc +go + +select S_T_NO as 类型,count(*) as 人数,avg(S_Age) as 平均年龄 from StarInfo +group by S_T_NO +go + +select top 1 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo +where S_T_NO = 1 +order by S_AGE desc + diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/50\351\242\230\357\274\210\345\244\215\344\271\240\357\274\211.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/50\351\242\230\357\274\210\345\244\215\344\271\240\357\274\211.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2ba2db6caa26e014a19208a634670637c05862dc --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/50\351\242\230\357\274\210\345\244\215\344\271\240\357\274\211.sql" @@ -0,0 +1,74 @@ + -- 练习题目: + + select * from StudentInfo + select * from Teachers + select * from CourseInfo + select * from StudentCourseScore + -- 1.查询"数学 "课程比" 语文 "课程成绩高的 学生的信息 及 课程分数 + select * from StudentCourseScore A,StudentCourseScore B where A.StudentId=B.StudentId AND A.CourseId=2 and B.CourseId=1 and A.Score>B.Score + + -- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 + select * from StudentCourseScore A,StudentCourseScore B where A.StudentId=B.StudentId AND A.CourseId=2 and B.CourseId=1 + + select * from (select StudentId ,CourseId from StudentCourseScore WHERE CourseId=1) A , + (SELECT StudentId,CourseId from StudentCourseScore where CourseId=2 ) B + where A.StudentId=B.StudentId + -- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) + select * from (select StudentId ,CourseId from StudentCourseScore where CourseId=1) A left join + (select StudentId,CourseId from StudentCourseScore where CourseId=2 ) B on A.StudentId=B.StudentId + -- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 + select * from (select StudentId ,CourseId from StudentCourseScore where CourseId=1) A left join + (select StudentId ,CourseId from StudentCourseScore where CourseId=2)B ON A.StudentId=B.StudentId + where B.CourseId is null and B.StudentId is null + + -- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 + select StudentId,StudentName,avg(Score)平均成绩 from StudentCourseScore + inner join StudentInfo on StudentCourseScore.StudentId = StudentInfo.Id + group by StudentId,StudentName + having avg(Score) >=60 + + -- 3.查询在成绩表存在成绩的学生信息 + select distinct StudentInfo. * from StudentCourseScore + inner join StudentInfo on StudentCourseScore.StudentId = StudentInfo.Id + + -- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + select StudentInfo.Id,StudentName,count(CourseId)选课总数,sum(Score)总成绩 from StudentCourseScore + inner join StudentInfo on StudentCourseScore.StudentId = StudentInfo.Id + group by StudentName,StudentInfo.Id + + -- 4.1 查有成绩的学生信息 + select distinct StudentInfo. * from StudentCourseScore + inner join StudentInfo on StudentCourseScore.StudentId = StudentInfo.Id + + -- 5.查询「李」姓老师的数量 + select count(TeacherName)李姓老师的数量 from Teachers where TeacherName like '李%' + + -- 6.查询学过「张三」老师授课的同学的信息 + select distinct StudentInfo . * from StudentCourseScore + inner join CourseInfo on StudentCourseScore.CourseId = CourseInfo.TeacherId + inner join StudentInfo on StudentInfo.Id=StudentCourseScore.StudentId + where TeacherId = 1 + + -- 7.查询没有学全所有课程的同学的信息 + select count(courseId) , StudentInfo. * from StudentCourseScore + inner join StudentInfo on StudentCourseScore.StudentId = StudentInfo.Id + group by StudentInfo.Id ,StudentCode,StudentName,Birthday,Sex,ClassId + having count(courseId)<3 + + -- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + select count(courseId), StudentInfo. * from StudentCourseScore inner join StudentInfo on StudentCourseScore.StudentId = StudentInfo.Id + where StudentInfo.Id!=01 + group by StudentInfo.Id ,StudentCode,StudentName,Birthday,Sex,ClassId + having count(courseId)=2 or count(courseId)=1 or count(courseId)=3 + + -- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + select count(courseId), StudentInfo. * from StudentInfo inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId + where StudentInfo.Id!=01 + group by StudentInfo.Id ,StudentCode,StudentName,Birthday,Sex,ClassId + having count(courseId)=3 + + -- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + select StudentName from StudentCourseScore + full join StudentInfo on StudentCourseScore.StudentId = StudentInfo.Id + full join CourseInfo on StudentCourseScore.CourseId = CourseInfo.Id + where Score is null \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/\345\244\215\344\271\2401.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/\345\244\215\344\271\2401.sql" new file mode 100644 index 0000000000000000000000000000000000000000..18da62aa4e637d20d8d8e06920a16265e07c2cd5 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/\345\244\215\344\271\2401.sql" @@ -0,0 +1,84 @@ +use master +go +create database GoodsDB +on +( +name = 'GoodsDB', +filename = 'D:\GoodsDB.mdf', +size = 5MB, +maxsize = 50MB, +filegrowth = 10% +) +log on +( +name = 'GoodsDB_log', +filename = 'D:\GoodsDB_log.ldf', +size = 5MB, +maxsize = 50MB, +filegrowth = 10% +) +go + +use GoodsDB +go + +create table GoodsType +( +TypeID int primary key identity(1,1) not null, +TypeName nvarchar(20) not null +) + +create table GoodsInfo +( +GoodsID int primary key identity(1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int references GoodsType(TypeID) +) + + + --使用插入语句为两张表添加数据 + --商品类型表(GoodsType) + --商品类型编号 商品类型名称 + --1 服装内衣 + --2 鞋包配饰 + --3 手机数码 + insert into GoodsType values ('服装内衣'),('鞋包配饰'),('手机数码') + + --商品信息表(GoodsType) + --商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 + --1 提花小西装 红色 菲曼琪 300 1 + --2 百搭短裤 绿色 哥弟 100 1 + --3 无袖背心 白色 阿依莲 700 1 + --4 低帮休闲鞋 红色 菲曼琪 900 2 + --5 中跟单鞋 绿色 哥弟 400 2 + --6 平底鞋 白色 阿依莲 200 2 + --7 迷你照相机 红色 尼康 500 3 + --8 硬盘 黑色 希捷 600 3 + --9 显卡 黑色 技嘉 800 3 + insert into GoodsInfo values ('提花小西装', '红色', '菲曼琪', 300 ,1), + ( '百搭短裤', '绿色', '哥弟', 100 ,1), + ('无袖背心', '白色', '阿依莲', 700 ,1), + ('低帮休闲鞋', '红色', '菲曼琪', 900 ,2), + ('中跟单鞋', '绿色', '哥弟', 400 ,2), + ('平底鞋', '白色', '阿依莲', 200 ,2), + ('迷你照相机', '红色', '尼康', 500 ,3), + ('硬盘', '黑色', '希捷', 600 ,3), + ('显卡', '黑色', '技嘉', 800 ,3) + + + --3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + select TOP 1 GoodsName 商品名称,GoodsMoney 商品价格,GoodsColor 商品颜色 from GoodsInfo + order by GoodsMoney DESC + --4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + select TypeID 商品类型编号,max(GoodsMoney)最高价格,min(GoodsMoney)最低价格,avg(GoodsMoney)平均价格 from GoodsInfo + group by TypeID + --5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + select * from GoodsInfo + where GoodsColor = '红色 ' and GoodsMoney >=300 and GoodsMoney <=600 + + select * from GoodsType + select * from GoodsInfo + diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/\345\244\215\344\271\2402.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/\345\244\215\344\271\2402.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b8519b5e8274bb7953c8573f2bc27a3f4a72d2f3 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/\345\244\215\344\271\2402.sql" @@ -0,0 +1,67 @@ +use master +go +create database Houses +on +( +name = 'Houses', +filename = 'D:\ Houses.mdf', +size = 5MB, +maxsize = 50MB, +filegrowth = 10% +) +log on +( +name = ' Houses_log', +filename = 'D:\ Houses_log.ldf', +size = 5MB, +maxsize = 50MB, +filegrowth = 10% +) +go + +use Houses +go + +create table HOUSE_TYPE +( +[type_id] int primary key identity not null, +[type_name] nvarchar(50) unique not null +) + +create table HOUSE +( +house_id int primary key identity not null, +house_name varchar(50) not null, +house_price float default(0) not null, +type_id int references HOUSE_TYPE([type_id]) +) + + + --3、添加表数据 + --HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 + --HOUSE表中添加至少3条数据,不能全都为同一类型 + insert into HOUSE_TYPE values ('小户型'),('经济型'),('别墅') + + insert into HOUSE values ('学区房',3000,1),('地铁房',2000,2),('碧桂园',6000,3) + --4、添加查询 + --查询所有房屋信息 + SELECT * FROM HOUSE_TYPE + --使用模糊查询包含”型“字的房屋类型信息 + select * FROM HOUSE_TYPE where [type_name] like '%型' + --查询出房屋的名称和租金,并且按照租金降序排序 + select house_name,house_price FROM HOUSE + order by house_price DESC + --使用连接查询,查询信息,显示房屋名称和房屋类型名称 + select [type_name],house_name from HOUSE + inner join HOUSE_TYPE on HOUSE.type_id = HOUSE_TYPE.type_id + --查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 + select top 1 house_name,house_price from HOUSE + order by house_price DESC + + select house_name,house_price from HOUSE where house_price = + (select max(house_price) from HOUSE ) + + + + SELECT * FROM HOUSE_TYPE + SELECT * FROM HOUSE diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/\345\244\215\344\271\2403.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/\345\244\215\344\271\2403.sql" new file mode 100644 index 0000000000000000000000000000000000000000..086df7ea30cf8999a93099aca1ee9ff7025a8cc9 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/\345\244\215\344\271\2403.sql" @@ -0,0 +1,77 @@ +use master +go +create database StarManagerDB +on +( +name = 'StarManagerDB', +filename = 'D:\ StarManagerDB.mdf', +size = 5MB, +maxsize = 50MB, +filegrowth = 10% +) +log on +( +name = ' StarManagerDB_log', +filename = 'D:\ StarManagerDB_log.ldf', +size = 5MB, +maxsize = 50MB, +filegrowth = 10% +) +go + +use StarManagerDB +go + +create table StarType +( +T_NO int primary key identity(1,1) not null, +T_NAME nvarchar(20) +) + +create table StarInfo +( +S_NO int primary key identity(1,1) not null, +S_NAME nvarchar(20) not null , +S_AGE int not null, +S_HOBBY nvarchar(20) not null , +S_NATIVE nvarchar(20) not null , +S_T_NO int references StarType(T_NO) +) + + + --2、使用插入语句为两张表添加数据 + + --明星类型表(StarType) + --明星类型编号 明星类型 + --1 体育明星 + --2 IT明星 + --3 相声演员 + insert into StarType values ('体育明星'),('IT明星'),('相声演员') + + --明星表(StarInfo) + --明星编号 姓名 年龄 特技 籍贯 明星类型编号 + --1 梅西 30 射门 阿根廷 1 + --2 科比 35 过人 美国 1 + --3 蔡景现 40 敲代码 中国 2 + --4 马斯克 36 造火箭 外星人 2 + --5 郭德纲 50 相声 中国 3 + --6 黄铮 41 拼多多 中国 2 + insert into StarInfo values ('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1), + ('蔡景现',40,'敲代码','中国',2),('马斯克',36,'造火箭','外星人',2), + ('郭德纲',50,'相声','中国',3),('黄铮',41,'拼多多','中国',2) + + --3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 + select top 3 S_NAME 姓名,S_AGE 年龄,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo + order by S_AGE DESC + --4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 + select S_T_NO 明星类型编号,count(S_NO )明星人数,avg(S_AGE)平均年龄 from StarInfo + group by S_T_NO + having count(S_NO )>2 + --5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 + select top 1 T_NAME 体育明星,S_AGE 年龄,S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo + inner join StarType on StarInfo.S_T_NO = StarType.T_NO + where T_NAME = '体育明星' + order by S_AGE DESC + + select * from StarType + select * from StarInfo \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7fa71bf16f8bdd4af762fae4f78591271af8c6ef --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery1.sql" @@ -0,0 +1,46 @@ +use master +go +create database GoodsDB +on( +name='GoodsDB', +filename='D:\GoodsDB.mdf', +size=10, +maxsize=100, +filegrowth=10% +) +log on +( +name='GoodsDB_log', +filename='D:\GoodsDB_log.ldf', +size=10, +maxsize=100, +filegrowth=10% +) +go +use GoodsDB +go +create table GoodsType +( +TypeID int primary key identity(1,1) not null, +TypeName nvarchar(20) not null, +) +create table GoodsInfo +( +GoodsID int primary key identity(1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) not null, + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) +insert into GoodsType values('服装内衣'),(' 鞋包配饰'),('手机数码') +insert into GoodsInfo values( '提花小西装',' 红色',' 菲曼琪', 300 ,1),('百搭短裤' ,'绿色' ,'哥弟', 100 ,1), +('无袖背心','白色', '阿依莲', 700, 1),('低帮休闲鞋','红色','菲曼琪', 900, 2),('中跟单鞋', '绿色', '哥弟', 400 ,2), +('平底鞋', '白色', '阿依莲', 200, 2),('迷你照相机', '红色', '尼康', 500, 3),('硬盘', '黑色', '希捷' ,600, 3), +( '显卡', '黑色', '技嘉', 800 ,3) +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 名字,GoodsColor 颜色,GoodsMoney 价格 from GoodsInfo +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select GoodsID, max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney)平均价格 from GoodsInfo group by GoodsID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..67c4ad796249265bbc6faa88b1d3db029ab11d11 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery3.sql" @@ -0,0 +1,54 @@ +use master +go +create database HOUSE_DB +on +( +name='HOUSE_DB', +filename='D:\HOUSE_DB.mdf', +size=5, +maxsize=50, +filegrowth=20% +) +log on +( +name='HOUSE_DB_log', +filename='D:\HOUSE_DB_log.ldf', +size=5, +maxsize=50, +filegrowth=20% +) +go +use HOUSE_DB +go +create table HOUSE_TYPE +( +type_id int not null primary key identity, +type_name varchar(50) unique not null , + +) +create table HOUSE +( +house_id int primary key identity not null, +house_name varchar(50) not null, +house_price float not null default(0), +type_id int not null references HOUSE_TYPE(type_id) +) +--3、添加表数据 + +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') +--HOUSE表中添加至少3条数据,不能全都为同一类型 +insert into HOUSE values('a房',2000,3),('b房',1500,2),('c房',1200,1) +--4、添加查询 +--查询所有房屋信息 +select*from HOUSE_TYPE +select*from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name ,house_price from HOUSE order by house_price DESC +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name ,type_name from HOUSE INNER JOIN HOUSE_TYPE ON HOUSE.type_id=HOUSE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +SELECT TOP 1 house_name,MAX(house_price) FROM HOUSE GROUP BY house_name + diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery4.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c4791da7e49cd850ea995d8eb641ed8b268f7104 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery4.sql" @@ -0,0 +1,46 @@ +USE MASTER +GO +CREATE DATABASE StarManagerDB + +on( +name='StarManagerDB', +filename='D:\StarManagerDB.mdf', +size=10, +maxsize=100, +filegrowth=10% +) +log on +( +name='StarManagerDB_log', +filename='D:\StarManagerDB_log.ldf', +size=10, +maxsize=100, +filegrowth=10% +) +GO +USE StarManagerDB +GO +CREATE TABLE StarType +( +T_NO INT NOT NULL PRIMARY KEY IDENTITY(1,1), +T_NAME nvarchar(20) +) +CREATE TABLE StarInfo +( +S_NO INT NOT NULL PRIMARY KEY IDENTITY(1,1), +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20), +S_NATIVE nvarchar(20) default('中国大陆'), +S_T_NO int references StarType(T_NO) +) +insert into StarType values('体育明星'),('IT明星'),('相声演员') +insert into StarInfo values('梅西',30, '射门', '阿根廷',1),('科比',35,'过人', '美国', 1), +('蔡景现',40,'敲代码','中国',2),('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国', 3),('黄铮', 41, '拼多多','中国',2) +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME,S_HOBBY 特技, S_NATIVE 明星籍贯 from StarInfo order by S_AGE DESC +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +SELECT T_NO 明星类型编号,COUNT(S_NO) 明星人数,AVG(S_AGE)平均年龄 FROM StarType INNER JOIN StarInfo ON StarType.T_NO=StarInfo.S_T_NO GROUP BY T_NO HAVING COUNT(S_NO)>2 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +SELECT TOP 1 S_NAME 姓名,S_HOBBY 特技, S_NATIVE 明星籍贯 FROM StarInfo INNER JOIN StarType ON StarInfo.S_T_NO=StarType.T_NO WHERE T_NAME='体育明星' ORDER BY S_AGE DESC diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery5.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1b66ed20b120bfd8032a2097bec53c8cb0c266d4 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery5.sql" @@ -0,0 +1,253 @@ +-- 练习题目: +SELECT* FROM CourseInfo +SELECT* FROM StudentCourseScore +SELECT* FROM StudentInfo +SELECT* FROM Teachers +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +SELECT * FROM StudentCourseScore A,StudentCourseScore B where A.StudentId=B.StudentId and A.CourseId=1 and B.CourseId=2 and A.Score>B.Score +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select * from (select StudentId ,CourseId from StudentCourseScore WHERE CourseId=1) A , +(SELECT StudentId,CourseId from StudentCourseScore where CourseId=2 ) B +where A.StudentId=B.StudentId +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select * from (select StudentId ,CourseId from StudentCourseScore WHERE CourseId=1) A left join +(SELECT StudentId,CourseId from StudentCourseScore where CourseId=2 ) B on A.StudentId=B.StudentId +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select * from (select StudentId ,CourseId from StudentCourseScore where CourseId=1) A left join +(select StudentId ,CourseId from StudentCourseScore where CourseId=2)B ON A.StudentId=B.StudentId +WHERE B.CourseId is null and B.StudentId is null + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentId,StudentName,avg(Score) from StudentCourseScore inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id group by StudentId,StudentName having avg(Score)>60 + + +-- 3.查询在 成绩 表存在成绩的学生信息 +select distinct StudentInfo. * from StudentCourseScore inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id where Score is not null +select distinct S.*from StudentCourseScore SC left join StudentInfo S on SC.StudentId=S.Id +SELECT* FROM StudentCourseScore +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select StudentInfo.Id,StudentName, count(CourseId),Sum(Score) from StudentInfo inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId group by StudentInfo.Id,StudentName + + +-- 4.1 查有成绩的学生信息 + +select distinct S.*from StudentCourseScore SC left join StudentInfo S on SC.StudentId=S.Id + +-- 5.查询「李」姓老师的数量 +select count(Id) from Teachers where TeacherName like '李%' + + +-- 6.查询学过「张三」老师授课的同学的信息 +select distinct StudentInfo .* from StudentCourseScore right join StudentInfo on StudentInfo.Id=StudentCourseScore.StudentId +inner join CourseInfo on StudentCourseScore.CourseId=CourseInfo.Id where TeacherId=1 + +-- 7.查询没有学全所有课程的同学的信息 +SELECT count(courseId),StudentInfo.Id ,StudentCode,StudentName,Birthday,Sex,ClassId from StudentInfo inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId +group by StudentInfo.Id ,StudentCode,StudentName,Birthday,Sex,ClassId +having count(courseId)<3 + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +SELECT count(courseId),StudentInfo.Id ,StudentCode,StudentName,Birthday,Sex,ClassId from StudentInfo inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId +WHERE StudentInfo.Id!=01 +group by StudentInfo.Id ,StudentCode,StudentName,Birthday,Sex,ClassId +having count(courseId)=3 OR count(courseId)=2 + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + +SELECT count(courseId),StudentInfo.Id ,StudentCode,StudentName,Birthday,Sex,ClassId from StudentInfo inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId +WHERE StudentInfo.Id!=01 +group by StudentInfo.Id ,StudentCode,StudentName,Birthday,Sex,ClassId +having count(courseId)=3 + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 +SELECT DISTINCT StudentName FROM StudentInfo LEFT JOIN StudentCourseScore ON StudentInfo.Id=StudentCourseScore.StudentId INNER JOIN CourseInfo ON CourseInfo.Id=StudentCourseScore.CourseId WHERE TeacherId!=1 +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\257\205/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\257\205/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7fa71bf16f8bdd4af762fae4f78591271af8c6ef --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\257\205/SQLQuery1.sql" @@ -0,0 +1,46 @@ +use master +go +create database GoodsDB +on( +name='GoodsDB', +filename='D:\GoodsDB.mdf', +size=10, +maxsize=100, +filegrowth=10% +) +log on +( +name='GoodsDB_log', +filename='D:\GoodsDB_log.ldf', +size=10, +maxsize=100, +filegrowth=10% +) +go +use GoodsDB +go +create table GoodsType +( +TypeID int primary key identity(1,1) not null, +TypeName nvarchar(20) not null, +) +create table GoodsInfo +( +GoodsID int primary key identity(1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) not null, + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) +insert into GoodsType values('服装内衣'),(' 鞋包配饰'),('手机数码') +insert into GoodsInfo values( '提花小西装',' 红色',' 菲曼琪', 300 ,1),('百搭短裤' ,'绿色' ,'哥弟', 100 ,1), +('无袖背心','白色', '阿依莲', 700, 1),('低帮休闲鞋','红色','菲曼琪', 900, 2),('中跟单鞋', '绿色', '哥弟', 400 ,2), +('平底鞋', '白色', '阿依莲', 200, 2),('迷你照相机', '红色', '尼康', 500, 3),('硬盘', '黑色', '希捷' ,600, 3), +( '显卡', '黑色', '技嘉', 800 ,3) +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 名字,GoodsColor 颜色,GoodsMoney 价格 from GoodsInfo +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select GoodsID, max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney)平均价格 from GoodsInfo group by GoodsID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\257\205/SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\257\205/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..67c4ad796249265bbc6faa88b1d3db029ab11d11 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\257\205/SQLQuery3.sql" @@ -0,0 +1,54 @@ +use master +go +create database HOUSE_DB +on +( +name='HOUSE_DB', +filename='D:\HOUSE_DB.mdf', +size=5, +maxsize=50, +filegrowth=20% +) +log on +( +name='HOUSE_DB_log', +filename='D:\HOUSE_DB_log.ldf', +size=5, +maxsize=50, +filegrowth=20% +) +go +use HOUSE_DB +go +create table HOUSE_TYPE +( +type_id int not null primary key identity, +type_name varchar(50) unique not null , + +) +create table HOUSE +( +house_id int primary key identity not null, +house_name varchar(50) not null, +house_price float not null default(0), +type_id int not null references HOUSE_TYPE(type_id) +) +--3、添加表数据 + +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') +--HOUSE表中添加至少3条数据,不能全都为同一类型 +insert into HOUSE values('a房',2000,3),('b房',1500,2),('c房',1200,1) +--4、添加查询 +--查询所有房屋信息 +select*from HOUSE_TYPE +select*from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name ,house_price from HOUSE order by house_price DESC +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name ,type_name from HOUSE INNER JOIN HOUSE_TYPE ON HOUSE.type_id=HOUSE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +SELECT TOP 1 house_name,MAX(house_price) FROM HOUSE GROUP BY house_name + diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\257\205/SQLQuery4.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\257\205/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c4791da7e49cd850ea995d8eb641ed8b268f7104 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\257\205/SQLQuery4.sql" @@ -0,0 +1,46 @@ +USE MASTER +GO +CREATE DATABASE StarManagerDB + +on( +name='StarManagerDB', +filename='D:\StarManagerDB.mdf', +size=10, +maxsize=100, +filegrowth=10% +) +log on +( +name='StarManagerDB_log', +filename='D:\StarManagerDB_log.ldf', +size=10, +maxsize=100, +filegrowth=10% +) +GO +USE StarManagerDB +GO +CREATE TABLE StarType +( +T_NO INT NOT NULL PRIMARY KEY IDENTITY(1,1), +T_NAME nvarchar(20) +) +CREATE TABLE StarInfo +( +S_NO INT NOT NULL PRIMARY KEY IDENTITY(1,1), +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20), +S_NATIVE nvarchar(20) default('中国大陆'), +S_T_NO int references StarType(T_NO) +) +insert into StarType values('体育明星'),('IT明星'),('相声演员') +insert into StarInfo values('梅西',30, '射门', '阿根廷',1),('科比',35,'过人', '美国', 1), +('蔡景现',40,'敲代码','中国',2),('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国', 3),('黄铮', 41, '拼多多','中国',2) +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME,S_HOBBY 特技, S_NATIVE 明星籍贯 from StarInfo order by S_AGE DESC +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +SELECT T_NO 明星类型编号,COUNT(S_NO) 明星人数,AVG(S_AGE)平均年龄 FROM StarType INNER JOIN StarInfo ON StarType.T_NO=StarInfo.S_T_NO GROUP BY T_NO HAVING COUNT(S_NO)>2 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +SELECT TOP 1 S_NAME 姓名,S_HOBBY 特技, S_NATIVE 明星籍贯 FROM StarInfo INNER JOIN StarType ON StarInfo.S_T_NO=StarType.T_NO WHERE T_NAME='体育明星' ORDER BY S_AGE DESC diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\257\205/SQLQuery5.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\257\205/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1b66ed20b120bfd8032a2097bec53c8cb0c266d4 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\257\205/SQLQuery5.sql" @@ -0,0 +1,253 @@ +-- 练习题目: +SELECT* FROM CourseInfo +SELECT* FROM StudentCourseScore +SELECT* FROM StudentInfo +SELECT* FROM Teachers +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +SELECT * FROM StudentCourseScore A,StudentCourseScore B where A.StudentId=B.StudentId and A.CourseId=1 and B.CourseId=2 and A.Score>B.Score +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select * from (select StudentId ,CourseId from StudentCourseScore WHERE CourseId=1) A , +(SELECT StudentId,CourseId from StudentCourseScore where CourseId=2 ) B +where A.StudentId=B.StudentId +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select * from (select StudentId ,CourseId from StudentCourseScore WHERE CourseId=1) A left join +(SELECT StudentId,CourseId from StudentCourseScore where CourseId=2 ) B on A.StudentId=B.StudentId +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select * from (select StudentId ,CourseId from StudentCourseScore where CourseId=1) A left join +(select StudentId ,CourseId from StudentCourseScore where CourseId=2)B ON A.StudentId=B.StudentId +WHERE B.CourseId is null and B.StudentId is null + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentId,StudentName,avg(Score) from StudentCourseScore inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id group by StudentId,StudentName having avg(Score)>60 + + +-- 3.查询在 成绩 表存在成绩的学生信息 +select distinct StudentInfo. * from StudentCourseScore inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id where Score is not null +select distinct S.*from StudentCourseScore SC left join StudentInfo S on SC.StudentId=S.Id +SELECT* FROM StudentCourseScore +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select StudentInfo.Id,StudentName, count(CourseId),Sum(Score) from StudentInfo inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId group by StudentInfo.Id,StudentName + + +-- 4.1 查有成绩的学生信息 + +select distinct S.*from StudentCourseScore SC left join StudentInfo S on SC.StudentId=S.Id + +-- 5.查询「李」姓老师的数量 +select count(Id) from Teachers where TeacherName like '李%' + + +-- 6.查询学过「张三」老师授课的同学的信息 +select distinct StudentInfo .* from StudentCourseScore right join StudentInfo on StudentInfo.Id=StudentCourseScore.StudentId +inner join CourseInfo on StudentCourseScore.CourseId=CourseInfo.Id where TeacherId=1 + +-- 7.查询没有学全所有课程的同学的信息 +SELECT count(courseId),StudentInfo.Id ,StudentCode,StudentName,Birthday,Sex,ClassId from StudentInfo inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId +group by StudentInfo.Id ,StudentCode,StudentName,Birthday,Sex,ClassId +having count(courseId)<3 + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +SELECT count(courseId),StudentInfo.Id ,StudentCode,StudentName,Birthday,Sex,ClassId from StudentInfo inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId +WHERE StudentInfo.Id!=01 +group by StudentInfo.Id ,StudentCode,StudentName,Birthday,Sex,ClassId +having count(courseId)=3 OR count(courseId)=2 + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + +SELECT count(courseId),StudentInfo.Id ,StudentCode,StudentName,Birthday,Sex,ClassId from StudentInfo inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId +WHERE StudentInfo.Id!=01 +group by StudentInfo.Id ,StudentCode,StudentName,Birthday,Sex,ClassId +having count(courseId)=3 + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 +SELECT DISTINCT StudentName FROM StudentInfo LEFT JOIN StudentCourseScore ON StudentInfo.Id=StudentCourseScore.StudentId INNER JOIN CourseInfo ON CourseInfo.Id=StudentCourseScore.CourseId WHERE TeacherId!=1 +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\261\211\346\226\207/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\261\211\346\226\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7f158de07ee5f294d260afe8f305a131aebc08d7 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\261\211\346\226\207/SQLQuery1.sql" @@ -0,0 +1,39 @@ +create database GoodDB +go +use GoodDB +go + +create table GoodsType +( + TypeID int not null primary key identity , + TypeName nvarchar(20) not null +) + +create table GoodInfo +( + GoodsID int primary key identity not null, + GiidsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeId int foreign key references GoodsType(TypeID) +) +go +insert into GoodsType(TypeName) +select '服装内衣' union +select '鞋包配饰' union +select '手机数码' + +insert into GoodInfo(GiidsName,GoodsColor,GoodsBrand,GoodsMoney,TypeId) +values('提花小西装','红色','菲曼琪',300,1),('百搭短裤','绿色','哥弟',100,1),('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2),('中跟单鞋','绿色','哥弟',400,2),('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3),('硬盘','黑色','希捷',600,3),('显卡','黑色','技嘉',800,3) +go +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GiidsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodInfo where GoodsMoney = (select max(GoodsMoney) from GoodInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格 ,avg(GoodsMoney) 平均价格 from GoodInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\261\211\346\226\207/SQLQuery2.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\261\211\346\226\207/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..558812da7655f8cf3f4bc7b58bb2e294985699b6 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\261\211\346\226\207/SQLQuery2.sql" @@ -0,0 +1,50 @@ +create database HOUSE_DB +on +( + name = 'HOUSE_DB', + filename = 'D:\HOUSE_DB.mdf', + size = 5, + maxsize= 50, + filegrowth = 10% +) +log on +( + name= 'HOUSE_DB_log', + filename = 'D:\HOUSE_DB_log.ldf', + size = 5, + maxsize= 50, + filegrowth = 10% +) +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity, + type_name varchar(50) unique +) +create table HOUSE +( + house_id int primary key identity , + house_name varchar(50), + house_price float default (0), + type_id int foreign key references HOUSE_TYPE(type_id) +) +go +insert into HOUSE_TYPE(type_name) +select '小户型' union select '经济型' union select '别墅' + +insert into HOUSE(house_name,house_price,type_id) +values('鱼',40,1),('龟',45,2),('虾',60,3) +--go +--查询所有房屋信息 +select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE inner join HOUSE_TYPE on HOUSE_TYPE.type_id = HOUSE.type_id +where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name,type_name from HOUSE inner join HOUSE_TYPE on HOUSE_TYPE.type_id = HOUSE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select house_price,house_name from HOUSE where house_price = (select max(house_price) from HOUSE ) \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\261\211\346\226\207/SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\261\211\346\226\207/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5361c99fd94b4f8be426792b8b4b56e3d11e56be --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\346\261\211\346\226\207/SQLQuery3.sql" @@ -0,0 +1,36 @@ +create database StarManagerDB +use StarManagerDB +go + +create table StarType +( + T_NO int primary key identity , + T_NAME nvarchar(20) +) +create table StarInfo +( + S_NO int primary key identity, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20) , + S_NATIVE nvarchar(20) default ('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) +go +insert into StarType(T_NAME) +select '体育明星'union +select 'IT明星'union +select '相声演员' + +insert into StarInfo(S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) +values ('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',2),('蔡景现',40,'敲代码','中国',2) +,('马克斯',36,'造火箭','外星人',2),('郭德纲',50,'相声','中国',3),('黄峥',41,'拼多多','中国',2) + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 籍贯信息,S_NATIVE 特技 from StarInfo order by S_AGE desc +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO,count(*)明星人数,avg(S_AGE)平均年龄 from StarInfo group by S_T_NO having count(S_T_NO)>1 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo where S_AGE=(select max(S_AGE) from StarInfo where S_T_NO = 1) + +select * from StarInfo \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\210\230\351\207\221\346\265\267/\345\244\215\344\271\2401\344\273\243\347\240\201.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\351\207\221\346\265\267/\345\244\215\344\271\2401\344\273\243\347\240\201.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1f82c46fa261dfec536996d813a52738cacc33f9 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\351\207\221\346\265\267/\345\244\215\344\271\2401\344\273\243\347\240\201.sql" @@ -0,0 +1,71 @@ +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\GoodsDB.mdf', + size=50, + maxsize=100, + filegrowth=10 +) +log on +( + name='GoodsDB_log', + filename='D:\GoodsDB_log.ldf', + size=50, + maxsize=100, + filegrowth=10 +) +go +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int not null references GoodsType(TypeID) +) + + +--2、使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 + +insert into GoodsType values ('服装内衣'),('鞋包配饰'),('手机数码') +select * from GoodsType +--商品信息表(GoodsType) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 + +insert into GoodsInfo values ('提花小西装','红色','菲曼琪','300','1'),('百搭短裤','绿色','哥弟','100','1'),('无袖背心','白色','阿依莲','700','1'), +('低帮休闲鞋','红色','菲曼琪','900','2'),('中跟单鞋','绿色','哥弟','400','2'), +('平底鞋','白色','阿依莲','200','2'),('迷你照相机','红色','尼康','500','3'),('硬盘','黑色','希捷','600','3'),('显卡','黑色','技嘉','800','3') + +select * from GoodsType +select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select TypeName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsType inner join GoodsInfo on GoodsType.TypeID = GoodsInfo.TypeID +where GoodsMoney=(select MAX(GoodsMoney) from GoodsInfo) +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select GoodsInfo.TypeID 商品类型编号,MAX(GoodsMoney)最高价格,MIN(GoodsMoney)最低价格,AVG(GoodsMoney)平均价格 from GoodsInfo group by GoodsInfo.TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsType inner join GoodsInfo on GoodsType.TypeID = GoodsInfo.TypeID +where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 + + diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\210\230\351\207\221\346\265\267/\345\244\215\344\271\2402\344\273\243\347\240\201.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\351\207\221\346\265\267/\345\244\215\344\271\2402\344\273\243\347\240\201.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1a38406e0aa093a6ecad46c472241fe7d8981b1a --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\351\207\221\346\265\267/\345\244\215\344\271\2402\344\273\243\347\240\201.sql" @@ -0,0 +1,51 @@ +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5, + maxsize=50, + filegrowth=1 +) +log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5, + maxsize=50, + filegrowth=1 +) +go +create table HOUSE_TYPE +( + type_id int not null primary key identity(1,1), + type_name varchar(50) not null unique +) +create table HOUSE +( + house_id int not null primary key identity(1,1), + house_name varchar(50) not null, + house_price float not null default('0'), + type_id int not null references HOUSE_TYPE(type_id) +) + +insert into HOUSE_TYPE values ('小户型'),('经济型'),('别墅') +insert into HOUSE values ('小一','400','1'),('小二','500','2'),('小三','450','3') + + +--4、添加查询 +select * from HOUSE_TYPE +select * from HOUSE +--查询所有房屋信息 +select * from HOUSE inner join HOUSE_TYPE on HOUSE.type_id = HOUSE_TYPE.type_id +--使用模糊查询包含”型“字的房屋类型信息 +select HOUSE.* from HOUSE inner join HOUSE_TYPE on HOUSE.type_id = HOUSE_TYPE.type_id +where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 名称,house_price 租金 from HOUSE +order by house_price DESC +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,type_name 房屋类型 from HOUSE inner join HOUSE_TYPE on HOUSE.type_id = HOUSE_TYPE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select Top 1 house_price 最高的租金,house_name 房屋名称 from HOUSE +order by house_price DESC diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\210\230\351\207\221\346\265\267/\345\244\215\344\271\2403\344\273\243\347\240\201.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\351\207\221\346\265\267/\345\244\215\344\271\2403\344\273\243\347\240\201.sql" new file mode 100644 index 0000000000000000000000000000000000000000..32192e20919c46981adeb9a0a845238ff355c51c --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\210\230\351\207\221\346\265\267/\345\244\215\344\271\2403\344\273\243\347\240\201.sql" @@ -0,0 +1,47 @@ +create database StarManagerDB +on +( + name='StarManagerDB', + filename='E:\SQL课堂\StarManagerDB.mdf', + size=50, + maxsize=100, + filegrowth=10 +) +log on +( + name='StarManagerDB_log', + filename='E:\SQL课堂\StarManagerDB_log.ldf', + size=50, + maxsize=100, + filegrowth=10 +) +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int not null references StarType(T_NO) +) + +insert into StarType values ('体育明星'),('IT明星'),('相声演员') +insert into StarInfo values ('梅西','30','射门','阿根廷','1'),('科比','35','过人','美国','1'),('蔡景现','40','敲代码','中国','2'),('马斯克','36','造火箭','外星人','2'),('郭德纲','50','相声','中国','3'),('黄铮','41','拼多多','中国','2') + +select *from StarType +select *from StarInfo +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo +where S_AGE=(select MAX(S_AGE) from StarInfo) + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select T_NO 类型编号,COUNT(S_T_NO)明星人数,AVG(S_AGE)平均年龄 from StarInfo inner join StarType on StarInfo.S_T_NO = StarType.T_NO group by T_NO having COUNT(S_T_NO)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select Top 1 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo inner join StarType on StarInfo.S_T_NO = StarType.T_NO +where T_NAME=('体育明星') order by S_AGE DESC diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..22e87ac50357214e07732497ca002ceb0cc60af4 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/SQLQuery1.sql" @@ -0,0 +1,101 @@ +use master +go +create database wb +on +( +name='wb', +filename='c:\SQL\wb.mdf', +size=5mb, +maxsize=100mb, +filegrowth=10mb +) +log on +( +name='wb_log', +filename='c:\SQL\wb_log.mdf', +size=5mb, +maxsize=100mb, +filegrowth=10mb +) +go +use wb +go +create table tbl_card +( +ID varchar (20)primary key not null, +passWord varchar(20) not null, +balance int not null, +userName nvarchar(2) not null +) +create table tbl_computer +( +ID varchar(20) primary key not null, +onuse bit check(onuse=0 or onuse=1), +note int not null +) +create table tbl_record +( +ID int primary key not null, +cardId varchar(20) foreign key references tbl_card (ID), +ComputerId varchar(20)foreign key references tbl_computer(ID), +beginTime datetime not null, +endTime datetime not null, +fee int not null +) +go +insert into tbl_card(ID,passWord,balance,userName) + 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(ID,onuse,note) values ('02',0,25555),('03',1,55555),('04',0,66666),('05',1,88888), +('06',0,688878),('b01',0,558558) +insert into tbl_record(id ,cardid ,computerid,begintime,endtime,fee) +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 15:26:00','2007-01-06 22:55:00',6), +(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00',50), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:55:00',3), +(65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00',23), +(98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00',23) +go +select *from tbl_card +select *from tbl_computer +select *from tbl_record + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select username,cardId,computerid,begintime,endtime,fee from tbl_card inner join tbl_record on tbl_card .ID=tbl_record.cardId where userName='张军' +group by cardId,computerid,begintime,endtime,username ,fee order by fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select computerid 每台机器 ,fee 消费金额,count (computerid)上网次数,sum (fee) 消费总金额 from tbl_computer inner join tbl_record on tbl_computer.ID=tbl_record.ComputerId group by computerid ,fee + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select +--4. 查询出从未消费过的上网卡的卡号和用户名 + + +--5. 将密码与用户名一样的上网卡信息查询出来 + + +--6. 查询出使用次数最多的机器号和使用次数 + + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + + + + diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/GoodDB.mdf" "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/GoodDB.mdf" new file mode 100644 index 0000000000000000000000000000000000000000..3e97aaf157a95131b736d4c82b224c0533f9b69f Binary files /dev/null and "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/GoodDB.mdf" differ diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/GoodDB_log.ldf" "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/GoodDB_log.ldf" new file mode 100644 index 0000000000000000000000000000000000000000..8082860e7c6af8d50e2a0cc1cd6beba47bebfd42 Binary files /dev/null and "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/GoodDB_log.ldf" differ diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/HOUSE_DB.mdf" "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/HOUSE_DB.mdf" new file mode 100644 index 0000000000000000000000000000000000000000..e56eb1efe058ea46075e890b14b224e04a6e54cf Binary files /dev/null and "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/HOUSE_DB.mdf" differ diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/HOUSE_DB_log.ldf" "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/HOUSE_DB_log.ldf" new file mode 100644 index 0000000000000000000000000000000000000000..11e45edba98dec8239aa63ba8661574cf78501ee Binary files /dev/null and "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/HOUSE_DB_log.ldf" differ diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d38278022c2646f5801e6ede99ba6985315093d0 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/SQLQuery1.sql" @@ -0,0 +1,60 @@ +use master +go +create database GoodDB +on +( +name='GoodDB', +filename='C:\sql\GoodDB.mdf', +size=5mb, +maxsize=100mb, +filegrowth=10mb +) +log on +( +name='GoodDB_log', +filename='C:\sql\GoodDB_log.ldf', +size=5mb, +maxsize=100mb, +filegrowth=10mb +) +go +use GoodDB +go +create table GoodsType +( +TypeID int primary key identity not null, +TypeName nvarchar(20) not null +) +create table GoodsInfo +( +GoodsID int primary key identity not null, +GoodsName nvarchar(20)not null, +GoodsColor nvarchar(20)not null, +GoodsBrand nvarchar(20) null, +GoodsMoney money not null, +TypeID int foreign key references GoodsType(TypeID) +) +go +insert into GoodsType(TypeName) + values('服装内衣'), + ('鞋包服饰'), + ('手机数码') +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +values ('提花小西装','红色',' 菲曼琪', 300, 1), +('百搭短裤 ','绿色 ','哥弟' ,100 ,1), +('无袖背心',' 白色', '阿依莲', 700, 1), +('低帮休闲鞋','红色', '菲曼琪', 900, 2), +('中跟单鞋',' 绿色',' 哥弟' ,400, 2), +('平底鞋', '白色', '阿依莲',200,2), +('迷你照相机',' 红色',' 尼康', 500, 3), +('硬盘',' 黑色',' 希捷', 600, 3), +('显卡',' 黑色',' 技嘉', 800, 3) + +select*from GoodsType +select*from GoodsInfo +--查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 goodsname 商品名称,goodscolor 商品颜色,GoodsMoney 商品价格,max(GoodsMoney)from GoodsInfo group by goodsname,goodscolor,GoodsMoney order by GoodsMoney desc +--按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型编号 ,max(GoodsMoney),min(GoodsMoney),avg(GoodsMoney)from GoodsInfo group by TypeID +--查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select goodsid 商品编号 ,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/SQLQuery5.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4a7eda745dbddf21853e66c72dda8e44256f5282 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/SQLQuery5.sql" @@ -0,0 +1,53 @@ +use master +go +create database HOUSE_DB +on +( +name='HOUSE_DB', +size=5mb, +filename='c:\sql\HOUSE_DB.mdf', +maxsize=50mb, +filegrowth=1mb +) +log on +( +name='HOUSE_DB_log', +size=5mb, +filename='c:\sql\HOUSE_DB_log.ldf', +maxsize=50mb, +filegrowth=1mb +) +go +use HOUSE_DB +create table HOUSE_TYPE +( +type_id int not null primary key identity, +type_name varchar(50) not null unique +) +create table HOUSE +( +house_id int not null primary key identity, +house_name varchar(50) not null, +house_price float not null default(0), +type_id int foreign key references HOUSE_TYPE(type_id) +) +go +insert into HOUSE_TYPE(type_name) +values('小户型'),('经济型'),('别墅') +insert into HOUSE(house_name,house_price,type_id ) +values('北京四合院',80000000,1), +('上海海景房',2000000,3), +('浙江大别墅',40000000,2) +delete HOUSE where house_id>=4 +select*from HOUSE +select*from HOUSE_TYPE +--查询所有房屋信息 +select*from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select TYPE_NAME from HOUSE_TYPE where TYPE_NAME like'%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price ,count(house_price)from HOUSE group by house_name,house_price order by house_price +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select *from HOUSE H inner join HOUSE_TYPE T on H.type_id=T.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_name,house_price, max(house_price) from HOUSE group by house_name,house_price order by house_price DESC \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/SQLQuery6.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/SQLQuery6.sql" new file mode 100644 index 0000000000000000000000000000000000000000..387e7bd49c16942edd3cd7946098a12ce0a2518c --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/SQLQuery6.sql" @@ -0,0 +1,57 @@ +use master +go +create database StarManagerDB +on +( +name='StarManagerDB', +size=5mb, +filename='c:\sql\StarManagerDB.mdf', +maxsize=50mb, +filegrowth=1mb +) +log on +( +name='StarManagerDB_log', +size=5mb, +filename='c:\sql\StarManagerDB_log.ldf', +maxsize=50mb, +filegrowth=1mb +) +go +use StarManagerDB +create table StarType +( +T_NO int not null primary key identity, +T_NAME nvarchar(20) +) +create table StarInfo +( +S_NO int not null primary key identity, +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20), +S_NATIVE nvarchar(20) default('中国大陆'), +S_T_NO int foreign key references StarType(T_NO) +) +go +insert into StarType values('体育明星'), +('IT明星'), +('相声演员') +insert into StarInfo(S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) +values('梅西',30,'射门','阿根廷',1), +('科比',25,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) + +select*from StarType +select*from StarInfo +--查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯,S_AGE,max(S_AGE) +from StarInfo group by S_NAME,S_HOBBY,S_NATIVE,S_AGE order by S_AGE desc +--按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_NO 明星编号,S_AGE 年龄,count(S_NO), avg (S_AGE) from StarInfo where S_NO>2 group by S_NO,S_AGE +--查询 明星类型为“体育明星”中年龄最大 的 姓名、特技、籍贯信息,要求显示列别名。 +select * from StarInfo where S_AGE = (select MAX(S_AGE) from StarInfo T inner join StarType S on T.S_T_NO=S.T_NO where T_NAME='体育明星' ) +--S_NAME,S_AGE,S_HOBBY,S_NATIVE \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/StarManagerDB.mdf" "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/StarManagerDB.mdf" new file mode 100644 index 0000000000000000000000000000000000000000..b1f0ee31e1e72833a8c703ca9dae0f9d27037b9b Binary files /dev/null and "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/StarManagerDB.mdf" differ diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/StarManagerDB_log.ldf" "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/StarManagerDB_log.ldf" new file mode 100644 index 0000000000000000000000000000000000000000..768d91da2ef693d155809e9a249cc300420c2947 Binary files /dev/null and "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/sql/StarManagerDB_log.ldf" differ diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/\344\275\234\344\270\232/1.bmp" "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/\344\275\234\344\270\232/1.bmp" new file mode 100644 index 0000000000000000000000000000000000000000..65b6cf3101996a4c52218b7a5dad3fa9880001ad Binary files /dev/null and "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/\344\275\234\344\270\232/1.bmp" differ diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/\344\275\234\344\270\232/2.bmp" "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/\344\275\234\344\270\232/2.bmp" new file mode 100644 index 0000000000000000000000000000000000000000..93b6faf52d8cb48c72c03fccb90af8e20f23d80a Binary files /dev/null and "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/\344\275\234\344\270\232/2.bmp" differ diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/\344\275\234\344\270\232/\344\275\234\344\270\232\344\270\200.txt" "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/\344\275\234\344\270\232/\344\275\234\344\270\232\344\270\200.txt" new file mode 100644 index 0000000000000000000000000000000000000000..1f307c9dffddd35ed89064ab5521ab9289d205d1 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/\344\275\234\344\270\232/\344\275\234\344\270\232\344\270\200.txt" @@ -0,0 +1,23 @@ +有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) + +题目: +1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 + +2.查询出每门课程的选修的学生的个数和学生考试的总分 + +3.查询出性别一样并且年龄一样的学生的信息 + +4.查询出学分一样的课程信息 + +5.查询出参加了考试的学生的学号,姓名,课程号和分数 + +6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 + +7.查询出没有参加考试的学生的学号和姓名 + +8.查询出是周六周天来报到的学生 + +9.查询出姓名中有字母a的学生的信息 + +10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 + diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/\344\275\234\344\270\232/\344\275\234\344\270\232\344\272\214.txt" "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/\344\275\234\344\270\232/\344\275\234\344\270\232\344\272\214.txt" new file mode 100644 index 0000000000000000000000000000000000000000..bb1c878e1b1f1364677a715077a5e0ee0369db4c --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\214\205\346\254\243\345\246\202/\344\275\234\344\270\232/\344\275\234\344\270\232\344\272\214.txt" @@ -0,0 +1,47 @@ +首先创建网吧计费系统的数据库,表结构和数据如2.bmp所示,表的说明如下: + +表一为上网卡信息表(tbl_card) +id: 卡号,主键 +passWord:密码 +balance:卡上的余额 +userName:该上网卡所属的用户名 + +表2为网吧机器说明表(tbl_computer) +id:机器编号,主键 +onUse:该机器是否正在使用,0为未使用,1为正在使用 +note:该机器的说明 + + +表3为上网记录表(tbl_record): +id:记录编号,主键 +cardId:本次上网的卡号,外键 +ComputerId:本次上网记录所使用的机器号,外键 +beginTime:本次上网记录的开始时间 +endTime:本次上网记录的结束时间 +fee:本次上网的费用 + + +请完成以下题目: +1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 + +2. 查询出每台机器上的上网次数和消费的总金额 + +3. 查询出所有已经使用过的上网卡的消费总金额 + +4. 查询出从未消费过的上网卡的卡号和用户名 + +5. 将密码与用户名一样的上网卡信息查询出来 + +6. 查询出使用次数最多的机器号和使用次数 + +7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + +8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + + + + diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\217\266\347\234\237/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\217\266\347\234\237/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..914c1778e5309c46297012ceee2e3010ac8637dd --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\217\266\347\234\237/SQLQuery1.sql" @@ -0,0 +1,86 @@ +--1、创建商品数据库(GoodsDB),然后建立两张表,GoodsType(商品类型表),GoodsInfo(商品信息表),表结构分别如下: +--商品类型表(GoodsType) +--字段名 说明 类型 长度 可否为空 约束 +--TypeID 商品类型编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--TypeName 商品类型名称 nvarchar 20 否 + +create database GoodsDB +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) + +--商品信息表(GoodsInfo) +--字段名 说明 类型 长度 可否为空 约束 +--GoodsID 商品编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--GoodsName 商品名称 nvarchar 20 否 +--GoodsColor 商品颜色 nvarchar 20 否 +--GoodsBrand 商品品牌 nvarchar 20 +--GoodsMoney 商品价格 money 否 +--TypeID 商品类型编号 int 外键,参照GoodsType中的TypeID + +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +--2、使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 + +insert into GoodsType(TypeName) +select '服装内衣' union +select '鞋包配饰' union +select '手机数码' + +--商品信息表(GoodsType) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 + +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +select '提花小西装','红色','菲曼琪',300,1 union +select '百搭短裤','绿色','哥弟',100,1 union +select '无袖背心','白色','阿依莲',700,1 union +select '低帮休闲鞋','红色','菲曼琪',900,2 union +select '中跟单鞋','绿色','哥弟',400,2 union +select '平底鞋','白色','阿依莲',200,2 union +select '迷你照相机','红色','尼康',500,3 union +select '硬盘','黑色','希捷',600,3 union +select '显卡','黑色','技嘉',800,3 + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo + where GoodsMoney = (select MAX(GoodsMoney) from GoodsInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select MAX(GoodsMoney) 最高价格,MIN(GoodsMoney) 最低价格,AVG(GoodsMoney) 平均价 from GoodsInfo + group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select * from GoodsInfo + where GoodsColor = '红色' and GoodsMoney between 300 and 600 diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\217\266\347\234\237/SQLQuery2.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\217\266\347\234\237/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..294edaf23874fd6f25ec8c3b411c367d2a23211f --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\217\266\347\234\237/SQLQuery2.sql" @@ -0,0 +1,458 @@ +-- 练习题目: + + + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + +select * from + (select * from StudentCourseScore where courseId = 1) as A + inner join StudentInfo s on s.Id = A.Id, + (select * from StudentCourseScore where CourseId = 2) as B + where A.Score > B.Score and A.StudentId = B.StudentId + + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 + +select StudentId from StudentCourseScore + where CourseId = 1 or CourseId = 2 + group by StudentId + HAVING COUNT(distinct CourseId) = 2 + + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) + +select * from + (select * from StudentCourseScore where CourseId = 2) A + left join (select * from StudentCourseScore where CourseId = 1) B on A.StudentId = B.StudentId + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 + +select * from + (select * from StudentCourseScore where CourseId = 1) A + left join (select * from StudentCourseScore where CourseId = 2) B on A.StudentId = B.StudentId + where B.CourseId is null + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 + +select Studentcode 学生编号,StudentName 学生姓名,AVG(Score) 平均成绩 from StudentCourseScore + inner join StudentInfo on StudentCourseScore.StudentId = StudentInfo.StudentCode + group by Studentcode,StudentName + HAVING AVG(Score) >= 60 + +-- 3.查询在 成绩 表存在成绩的学生信息 + +select * from StudentCourseScore + left join StudentInfo on StudentCourseScore.StudentId = StudentInfo.StudentCode + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +select StudentCode ,StudentName ,COUNT(CourseId) ,SUM(Score) from StudentCourseScore scs + right join StudentInfo si on scs.StudentId = si.StudentCode + group by StudentCode,StudentName + +-- 4.1 查有成绩的学生信息 + +select * from StudentCourseScore scs + inner join StudentInfo si on scs.StudentId = si.StudentCode + +-- 5.查询「李」姓老师的数量 + +select COUNT(TeacherName) from Teachers + where TeacherName like '李%' + +-- 6.查询学过 「张三」老师授课 的 同学的信息 + +select * from StudentCourseScore A + inner join CourseInfo B on A.CourseId = B.Id + inner join StudentInfo C on A.StudentId = C.StudentCode + inner join Teachers D on B.TeacherId = D.Id + where TeacherName = '张三' + +-- 7.查询没有学全所有课程的同学的信息 +select * from CourseInfo +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers + +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode,StudentName + HAVING COUNT(CourseId) < (select COUNT(CourseName) from CourseInfo) + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode ,StudentName + having count(CourseId) > 0 + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode ,StudentName + having count(CourseId)=(select COUNT(CourseName) from CourseInfo) + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select distinct studentName from StudentCourseScore A + inner join CourseInfo B on A.CourseId = B.Id + inner join StudentInfo C on A.StudentId = C.StudentCode + inner join Teachers D on B.TeacherId = D.Id + where TeacherName != '张三' + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 + +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\217\266\347\234\237/SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\217\266\347\234\237/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..bc0eff663147abc5dde96231a9b1eba649b05c1a --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\217\266\347\234\237/SQLQuery3.sql" @@ -0,0 +1,86 @@ +--1、创建数据库HOUSE_DB, +--要求: +--指定数据文件大小:5兆, +--指定文件最大值:50兆, +--文件增长率:1兆 +use master +go + +create database HOUSE_DB +on +( + name = HOUSE_DB, + filename ='D:\HOUSE_DB.mfg', + size = 5MB, + maxsize = 50MB, + filegrowth = 1MB +) +use HOUSE_DB + +--2、创建数据表 +--房屋类型表(HOUSE_TYPE) +--字段名 类型 是否可为空 约束 说明 +--type_id int N 主键,自增长 类型编号 +--type_name varchar(50) N 唯一约束 类型名称 + +create table HOUSE_TYPE +( + type_id int primary key identity , + type_name varchar(20) unique +) + +--房屋信息表(HOUSE) +--字段名 类型 是否可为空 约束 说明 +--house_id int N 主键,自增长 房屋编号 +--house_name varchar(50) N 房屋名称 +--house_price float N 默认值0 房租 +--type_id int N 外键依赖HOUSE_TYPE表 房屋类型 + +create table HOUSE +( + house_id int primary key identity , + house_name varchar(50) , + house_price float default('0'), + type_id int references HOUSE_TYPE(type_id) +) + +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 + +insert into HOUSE_TYPE(type_name) +select '小户型' union +select '经济型' union +select '别墅' + +insert into HOUSE(house_name,house_price,type_id) +select '蔡','5000','3' union +select '东','10000','2' union +select '生','50000','1' + +--4、添加查询 +--查询所有房屋信息 + +select * from HOUSE A + inner join HOUSE_TYPE B on A.type_id = B.type_id + +--使用模糊查询包含”型“字的房屋类型信息 + +select * from HOUSE A + inner join HOUSE_TYPE B on a.type_id = B.type_id + where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 + +select house_name 名字,house_price 租金 from HOUSE + order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 + +select house_name 名称,type_name 类型名称 from HOUSE A + inner join HOUSE_TYPE B on A.type_id = B.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 + +select house_price ,house_name from HOUSE where house_price = (select MAX(house_price) from HOUSE) + diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\217\266\347\234\237/SQLQuery4.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\217\266\347\234\237/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c6f9245f44dd18ee1c60cf0f97494dc62aef1744 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\217\266\347\234\237/SQLQuery4.sql" @@ -0,0 +1,88 @@ +--1、创建明星数据库(StarManagerDB),然后建立两张表,StarType(明星类型表),StarInfo(明星信息表),表结构分别如下: +--StarType(明星类型表) +--字段名 说明 类型 长度 可否为空 约束 +--T_NO 明星类型编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--T_NAME 明星类型 nvarchar 20 + +use master +go + +create database StarManagerDB +go + +use StarManagerDB +go + +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) + +--StarInfo(明星信息表) +--字段名 说明 类型 长度 可否为空 约束 +--S_NO 明星编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--S_NAME 明星姓名 nvarchar 20 否 +--S_AGE 明星年龄 int 否 +--S_HOBBY 特技 nvarchar 20 +--S_NATIVE 明星籍贯 nvarchar 20 默认约束:中国大陆 +--S_T_NO 明星类型编号 int 外键,参照StarType表的主键T_NO + +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20) , + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) + +--2、使用插入语句为两张表添加数据 + +--明星类型表(StarType) +--明星类型编号 明星类型 +--1 体育明星 +--2 IT明星 +--3 相声演员 + +insert into StarType +select '体育明星' union +select 'IT明星' union +select '相声演员' + +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 + +insert into StarInfo(S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) +select '梅西',30,'射门','阿根廷',1 union +select '科比',35,'过人','美国',1 union +select '蔡景现',40,'敲代码','中国',2 union +select '马斯克',36,'造火箭','外星人',2 union +select '郭德纲',50,'相声','中国',3 union +select '黄铮',41,'拼多多','中国',2 + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 + +select top 3 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo + order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 + +select S_T_NO as 类型,count(*) as 人数,avg(S_Age) as 平均年龄 from StarInfo + group by S_T_NO + + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 + +select top 1 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo + where S_T_NO = 1 + order by S_AGE desc + + diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\217\266\351\231\210\350\276\211/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\217\266\351\231\210\350\276\211/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c3e3901961163e71c3ba573ca0fdc089b4ecc9f0 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\217\266\351\231\210\350\276\211/SQLQuery1.sql" @@ -0,0 +1,86 @@ +--1、创建商品数据库(GoodsDB),然后建立两张表,GoodsType(商品类型表),GoodsInfo(商品信息表),表结构分别如下: +--商品类型表(GoodsType) +--字段名 说明 类型 长度 可否为空 约束 +--TypeID 商品类型编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--TypeName 商品类型名称 nvarchar 20 否 + +create database GoodsDB +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) + +--商品信息表(GoodsInfo) +--字段名 说明 类型 长度 可否为空 约束 +--GoodsID 商品编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--GoodsName 商品名称 nvarchar 20 否 +--GoodsColor 商品颜色 nvarchar 20 否 +--GoodsBrand 商品品牌 nvarchar 20 +--GoodsMoney 商品价格 money 否 +--TypeID 商品类型编号 int 外键,参照GoodsType中的TypeID + +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +--2、使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 + +insert into GoodsType(TypeName) +select '服装内衣' union +select '鞋包配饰' union +select '手机数码' + +--商品信息表(GoodsType) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 + +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +select '提花小西装','红色','菲曼琪',300,1 union +select '百搭短裤','绿色','哥弟',100,1 union +select '无袖背心','白色','阿依莲',700,1 union +select '低帮休闲鞋','红色','菲曼琪',900,2 union +select '中跟单鞋','绿色','哥弟',400,2 union +select '平底鞋','白色','阿依莲',200,2 union +select '迷你照相机','红色','尼康',500,3 union +select '硬盘','黑色','希捷',600,3 union +select '显卡','黑色','技嘉',800,3 + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo + where GoodsMoney = (select MAX(GoodsMoney) from GoodsInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select MAX(GoodsMoney) 最高价格,MIN(GoodsMoney) 最低价格,AVG(GoodsMoney) 平均价 from GoodsInfo + group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select * from GoodsInfo + where GoodsColor = '红色' and GoodsMoney between 300 and 600 diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\217\266\351\231\210\350\276\211/SQLQuery2.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\217\266\351\231\210\350\276\211/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d9f698e5f1d369312a624b2aebb5c9805f2b6476 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\217\266\351\231\210\350\276\211/SQLQuery2.sql" @@ -0,0 +1,458 @@ +-- 练习题目: + + + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + +select * from + (select * from StudentCourseScore where courseId = 1) as A + inner join StudentInfo s on s.Id = A.Id, + (select * from StudentCourseScore where CourseId = 2) as B + where A.Score > B.Score and A.StudentId = B.StudentId + + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 + +select StudentId from StudentCourseScore + where CourseId = 1 or CourseId = 2 + group by StudentId + HAVING COUNT(distinct CourseId) = 2 + + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) + +select * from + (select * from StudentCourseScore where CourseId = 2) A + left join (select * from StudentCourseScore where CourseId = 1) B on A.StudentId = B.StudentId + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 + +select * from + (select * from StudentCourseScore where CourseId = 1) A + left join (select * from StudentCourseScore where CourseId = 2) B on A.StudentId = B.StudentId + where B.CourseId is null + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 + +select Studentcode 学生编号,StudentName 学生姓名,AVG(Score) 平均成绩 from StudentCourseScore + inner join StudentInfo on StudentCourseScore.StudentId = StudentInfo.StudentCode + group by Studentcode,StudentName + HAVING AVG(Score) >= 60 + +-- 3.查询在 成绩 表存在成绩的学生信息 + +select * from StudentCourseScore + left join StudentInfo on StudentCourseScore.StudentId = StudentInfo.StudentCode + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +select StudentCode ,StudentName ,COUNT(CourseId) ,SUM(Score) from StudentCourseScore scs + right join StudentInfo si on scs.StudentId = si.StudentCode + group by StudentCode,StudentName + +-- 4.1 查有成绩的学生信息 + +select * from StudentCourseScore scs + inner join StudentInfo si on scs.StudentId = si.StudentCode + +-- 5.查询「李」姓老师的数量 + +select COUNT(TeacherName) from Teachers + where TeacherName like '李%' + +-- 6.查询学过 「张三」老师授课 的 同学的信息 + +select * from StudentCourseScore A + inner join CourseInfo B on A.CourseId = B.Id + inner join StudentInfo C on A.StudentId = C.StudentCode + inner join Teachers D on B.TeacherId = D.Id + where TeacherName = '张三' + +-- 7.查询没有学全所有课程的同学的信息 +select * from CourseInfo +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers + +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode,StudentName + HAVING COUNT(CourseId) < (select COUNT(CourseName) from CourseInfo) + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode ,StudentName + having count(CourseId) > 0 + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode ,StudentName + having count(CourseId)=(select COUNT(CourseName) from CourseInfo) + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select distinct studentName from StudentCourseScore A + inner join CourseInfo B on A.CourseId = B.Id + inner join StudentInfo C on A.StudentId = C.StudentCode + inner join Teachers D on B.TeacherId = D.Id + where TeacherName != '张三' + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 + +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\217\266\351\231\210\350\276\211/SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\217\266\351\231\210\350\276\211/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..11b06ac7e296af040556d3a0a6b74bd192e71ea7 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\217\266\351\231\210\350\276\211/SQLQuery3.sql" @@ -0,0 +1,86 @@ +--1、创建数据库HOUSE_DB, +--要求: +--指定数据文件大小:5兆, +--指定文件最大值:50兆, +--文件增长率:1兆 +use master +go + +create database HOUSE_DB +on +( + name = HOUSE_DB, + filename ='D:\HOUSE_DB.mfg', + size = 5MB, + maxsize = 50MB, + filegrowth = 1MB +) +use HOUSE_DB + +--2、创建数据表 +--房屋类型表(HOUSE_TYPE) +--字段名 类型 是否可为空 约束 说明 +--type_id int N 主键,自增长 类型编号 +--type_name varchar(50) N 唯一约束 类型名称 + +create table HOUSE_TYPE +( + type_id int primary key identity , + type_name varchar(20) unique +) + +--房屋信息表(HOUSE) +--字段名 类型 是否可为空 约束 说明 +--house_id int N 主键,自增长 房屋编号 +--house_name varchar(50) N 房屋名称 +--house_price float N 默认值0 房租 +--type_id int N 外键依赖HOUSE_TYPE表 房屋类型 + +create table HOUSE +( + house_id int primary key identity , + house_name varchar(50) , + house_price float default('0'), + type_id int references HOUSE_TYPE(type_id) +) + +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 + +insert into HOUSE_TYPE(type_name) +select '小户型' union +select '经济型' union +select '别墅' + +insert into HOUSE(house_name,house_price,type_id) +select '蔡','5000','3' union +select '东','10000','2' union +select '生','50000','1' + +--4、添加查询 +--查询所有房屋信息 + +select * from HOUSE A + inner join HOUSE_TYPE B on A.type_id = B.type_id + +--使用模糊查询包含”型“字的房屋类型信息 + +select * from HOUSE A + inner join HOUSE_TYPE B on a.type_id = B.type_id + where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 + +select house_name 名字,house_price 租金 from HOUSE + order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 + +select house_name 名称,type_name 类型名称 from HOUSE A + inner join HOUSE_TYPE B on A.type_id = B.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 + +select house_price ,house_name from HOUSE where house_price = (select MAX(house_price) from HOUSE) + diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\217\266\351\231\210\350\276\211/SQLQuery4.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\217\266\351\231\210\350\276\211/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cd3d67f97d3cb4545623816fb8e8c9fd1d4fcae9 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\217\266\351\231\210\350\276\211/SQLQuery4.sql" @@ -0,0 +1,88 @@ +--1、创建明星数据库(StarManagerDB),然后建立两张表,StarType(明星类型表),StarInfo(明星信息表),表结构分别如下: +--StarType(明星类型表) +--字段名 说明 类型 长度 可否为空 约束 +--T_NO 明星类型编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--T_NAME 明星类型 nvarchar 20 + +use master +go + +create database StarManagerDB +go + +use StarManagerDB +go + +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) + +--StarInfo(明星信息表) +--字段名 说明 类型 长度 可否为空 约束 +--S_NO 明星编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--S_NAME 明星姓名 nvarchar 20 否 +--S_AGE 明星年龄 int 否 +--S_HOBBY 特技 nvarchar 20 +--S_NATIVE 明星籍贯 nvarchar 20 默认约束:中国大陆 +--S_T_NO 明星类型编号 int 外键,参照StarType表的主键T_NO + +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20) , + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) + +--2、使用插入语句为两张表添加数据 + +--明星类型表(StarType) +--明星类型编号 明星类型 +--1 体育明星 +--2 IT明星 +--3 相声演员 + +insert into StarType +select '体育明星' union +select 'IT明星' union +select '相声演员' + +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 + +insert into StarInfo(S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) +select '梅西',30,'射门','阿根廷',1 union +select '科比',35,'过人','美国',1 union +select '蔡景现',40,'敲代码','中国',2 union +select '马斯克',36,'造火箭','外星人',2 union +select '郭德纲',50,'相声','中国',3 union +select '黄铮',41,'拼多多','中国',2 + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 + +select top 3 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo + order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 + +select S_T_NO as 类型,count(*) as 人数,avg(S_Age) as 平均年龄 from StarInfo + group by S_T_NO + + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 + +select top 1 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo + where S_T_NO = 1 + order by S_AGE desc + + diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\217\266\351\242\200/50\351\242\230.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\217\266\351\242\200/50\351\242\230.sql" new file mode 100644 index 0000000000000000000000000000000000000000..369836edf32a33e22ab369c53a7808f2900d9394 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\217\266\351\242\200/50\351\242\230.sql" @@ -0,0 +1,299 @@ +use ClassicDb +go + + +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + +select D.* from +(select A.StudentId a,A.CourseId b,A.Score c,B.StudentId d,B.CourseId e,B.Score f from +(select * from StudentCourseScore where CourseId = 1) as A , +(select * from StudentCourseScore where CourseId = 2) as B +where A.Score > B.Score and A.StudentId = B.StudentId) as C +inner join StudentInfo D on C.a = D.StudentCode + + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 + +select StudentId,count(CourseId) from StudentCourseScore +where CourseId = 1 or CourseId = 2 +group by StudentId +having count(CourseId) = 2 + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) + +select * from (select * from StudentCourseScore where CourseId = 1) as A +right join (select * from StudentCourseScore where CourseId = 2) as B on A.StudentId = B.StudentId + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 + +select * from (select * from StudentCourseScore where CourseId = 1) as A +left join (select * from StudentCourseScore where CourseId = 2) as B on A.StudentId = B.StudentId + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 + +select StudentId as 学生编号,StudentName as 学生姓名,avg(Score) as 平均成绩 from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +group by StudentId,StudentName +having avg(Score) >= 60 + +-- 3.查询在 成绩表 存在成绩的学生信息 + +select B.* from StudentCourseScore A +left join StudentInfo B on A.StudentId = B.StudentCode + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +select +StudentCode as 学生编号,StudentName as 学生姓名, +count(CourseId) as 选课总数,sum(Score) as 总成绩 +from StudentCourseScore A +right join StudentInfo B on A.StudentId = B.StudentCode +group by StudentCode,StudentName +order by StudentCode + +-- 4.1 查有成绩的学生信息 + +select StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +left join StudentInfo B on A.StudentId = B.StudentCode +group by StudentCode,StudentName,Birthday,Sex,ClassId + +-- 5.查询「李」姓老师的数量 + +select count(*) as 姓老师的数量 from Teachers +group by TeacherName +having TeacherName like ('李%') + + +-- 6.查询学过「张三」老师授课的同学的信息 + +select B.* from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +where A.CourseId = 1 + +-- 7.查询没有学全所有课程的同学的信息 + +select StudentId,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +group by StudentId,StudentName,Birthday,Sex,ClassId +having count(CourseId)<3 + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + +select StudentId,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 1 or CourseId = 2 or CourseId = 3 +group by StudentId,StudentName,Birthday,Sex,ClassId + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + +select StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 1 or CourseId = 2 or CourseId = 3 +group by StudentCode,StudentName,Birthday,Sex,ClassId +having count(CourseId) = 3 + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select StudentName from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +where A.CourseId != 1 +group by StudentCode,StudentName + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + +select * from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 1 and Score < 60 + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + +select CourseId,Score,avg(Score) from StudentCourseScore +group by StudentId,CourseId,Score +order by avg(Score) desc + +-- 14.查询各科成绩最高分、最低分和平均分: + +select CourseId,max(Score),min(Score),avg(Score) from StudentCourseScore +group by CourseId + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + +select CourseId,count(CourseId) from StudentCourseScore +group by CourseId + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + +select Sex,count(*) from StudentInfo +group by Sex + +-- 22.查询名字中含有「风」字的学生信息 + +select * from StudentInfo +where StudentName like ('%风%') + +-- 23.查询同名同性学生名单,并统计同名人数 + +select count(*) from StudentInfo A +inner join StudentInfo B on A.Id = B.Id +where A.StudentName = B.StudentName and A.Sex = B.Sex and A.StudentCode != B.StudentCode + +-- 24.查询 1990 年出生的学生名单 + +select StudentName from StudentInfo +where Birthday like '1990%' + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + +select CourseId,avg(Score) from StudentCourseScore +group by CourseId +order by avg(Score) desc + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + +select StudentId,StudentName,avg(Score) from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +group by StudentId,StudentName +having avg(Score)>85 + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + +select StudentName,Score from StudentCourseScore A +inner join CourseInfo B on A.CourseId = B.Id +inner join StudentInfo C on A.StudentId = C.StudentCode +where CourseName = '数学' and Score<60 + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + +select StudentName,CourseName,Score from StudentInfo A +full join StudentCourseScore B on B.StudentId = A.StudentCode +full join CourseInfo C on B.CourseId = C.Id + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + +select CourseName,count(*) from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +group by CourseId,CourseName + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + +select distinct top 1 * from StudentCourseScore +where CourseId = 2 +order by Score desc + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + +select top 1 * from StudentCourseScore +where CourseId = 2 +order by Score desc + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + +select count(*) from StudentCourseScore +group by CourseId + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\217\266\351\242\200/\345\244\215\344\271\240\351\242\2301.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\217\266\351\242\200/\345\244\215\344\271\240\351\242\2301.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b58c668faeb77a43a5e225f550ca446f99fcfd8a --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\217\266\351\242\200/\345\244\215\344\271\240\351\242\2301.sql" @@ -0,0 +1,80 @@ +use master +go + +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\test\GoodsDB.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='GoodsDB_log', + filename='D:\test\GoodsDB_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity not null , + TypeName nvarchar(20) not null , +) +go + +create table GoodsInfo +( + GoodsId int primary key identity not null , + GoodsName nvarchar(20) not null , + GoodsColor nvarchar(20) not null , + GoodsBrand nvarchar(20) , + GoodsMoney money not null , + TypeID int references GoodsType(TypeID) +) +go + +insert GoodsType values +('服装内衣'), +('鞋包配饰'), +('手机数码') +go + +insert GoodsInfo values +('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','阿依莲',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) +go + +select top 1 +GoodsName as 商品名称, +GoodsColor as 商品颜色, +GoodsMoney as 商品价格 +from GoodsInfo +order by GoodsMoney desc +go + +select +max(GoodsMoney) as 最高价格, +min(GoodsMoney) as 最低价格, +avg(GoodsMoney) as 平均价格 +from GoodsInfo +group by TypeID +go + +select * from GoodsInfo +where GoodsMoney >= 300 and GoodsMoney <= 600 and GoodsColor = '红色' + diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\217\266\351\242\200/\345\244\215\344\271\240\351\242\2302.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\217\266\351\242\200/\345\244\215\344\271\240\351\242\2302.sql" new file mode 100644 index 0000000000000000000000000000000000000000..bbc57216c0ac1adf98db07fa2643e49943ac7313 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\217\266\351\242\200/\345\244\215\344\271\240\351\242\2302.sql" @@ -0,0 +1,71 @@ +use master +go + +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\test\HOUSE_DB.mdf', + size=5, + maxsize=50, + filegrowth=1 +) +log on +( + name='HOUSE_DB_log', + filename='D:\test\HOUSE_DB_log.ldf', + size=5, + maxsize=50, + filegrowth=1 +) +go + +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity not null , + type_name varchar(50) not null unique +) +go + +create table HOUSE +( + house_id int primary key identity not null , + house_name varchar(50) not null , + house_price float not null , + type_id int references HOUSE_TYPE(type_id) +) +go + +insert HOUSE_TYPE values +('小户型'),('经济型'),('别墅') +go + +insert HOUSE values +('asd',2000,1), +('dsa',1500,3), +('das',1800,2) +go + +select * from HOUSE +go + +select B.* from HOUSE A +inner join HOUSE_TYPE B on A.type_id = B.type_id +where type_name like '%型' +go + +select house_name,house_price from HOUSE +order by house_price desc +go + + +select A.*,B.type_name from HOUSE A +inner join HOUSE_TYPE B on A.type_id = B.type_id +go + +select top 1 house_name,house_price from HOUSE +order by house_price desc +go \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\217\266\351\242\200/\345\244\215\344\271\240\351\242\2303.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\217\266\351\242\200/\345\244\215\344\271\240\351\242\2303.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3c3ef987b4e8418896f3d20f18c72ae8249f6000 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\217\266\351\242\200/\345\244\215\344\271\240\351\242\2303.sql" @@ -0,0 +1,70 @@ +use master +go + +create database StarManagerDB +on +( + name='StarManagerDB', + filename='D:\test\StarManagerDB.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='StarManagerDB_log', + filename='D:\test\StarManagerDB_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +go + +use StarManagerDB +go + +create table StarType +( + T_NO int primary key identity not null , + T_NAME nvarchar(20) , +) +go + +create table StarInfo +( + S_NO int primary key identity not null , + S_NAME nvarchar(20) not null , + S_AGE int not null , + S_HOBBY nvarchar(20) , + S_NATIVE nvarchar(20) default('中国大陆') , + S_T_NO int references StarType(T_NO) +) +go + +insert StarType values +('体育明星'), +('IT明星'), +('相声演员') +go + +insert StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) +go + +select top 3 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo +order by S_AGE desc +go + +select S_T_NO as 类型,count(*) as 人数,avg(S_Age) as 平均年龄 from StarInfo +group by S_T_NO +go + +select top 1 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo +where S_T_NO = 1 +order by S_AGE desc + diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b6217817a105df4b5d9b72bbd011fb314722a1c4 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery1.sql" @@ -0,0 +1,65 @@ +use master +go +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\新建文件夹.mdf', + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) +log on +( + name='GoodsDB_log', + filename='D:\新建文件夹_1og.ldf', + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) +go +use GoodsDB +go +create table GoodsType +( +TypeID int primary key identity(1,1) not null, +TypeName nvarchar(20) not null +) +go +use GoodsDB +go +create table GoodsInfo +( +GoodsID int primary key identity(1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int references GoodsType(TypeID) +) +insert into GoodsType values +('服装内衣'), +('鞋包配饰'), +('手机数码') +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values +('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo where GoodsMoney=(select max(GoodsMoney) from GoodsInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select T.TypeName 商品类型名称, max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney)平均价格 from GoodsType T inner join GoodsInfo I on T.TypeID=I.TypeID group by T.TypeName + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 + +select * from GoodsType +select * from GoodsInfo diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery2 (2).sql" "b/2021 04 02 \344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery2 (2).sql" new file mode 100644 index 0000000000000000000000000000000000000000..e1e6d6d6fa18bc0096dd894cd0eb9af5cb69c872 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery2 (2).sql" @@ -0,0 +1,306 @@ +create database ClassicDb +Go +use ClassicDb +GO +create table StudentInfo +( + Id int PRIMARY key not null IDENTITY, + StudentCode nvarchar(80), + StudentName nvarchar(80), + Birthday date not null, + Sex nvarchar(2), + ClassId int not null +) +GO +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) +GO +CREATE TABLE Teachers +( + Id int PRIMARY key not null IDENTITY, + TeacherName nvarchar(80) +) +go +insert into Teachers (TeacherName) values('张三') +insert into Teachers (TeacherName) values('李四') +insert into Teachers (TeacherName) values('王五') +GO +create table CourseInfo +( + Id int PRIMARY key not null IDENTITY, + CourseName NVARCHAR(80) not null, + TeacherId int not null + ) +go +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) +GO +create table StudentCourseScore +( + Id int PRIMARY key not null IDENTITY, + StudentId int not null, + CourseId int not null, + Score int not null +) +go +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + +-- 练习题目: + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +select X.Id 学生编号,X.StudentCode 座号,X.StudentName 姓名,X.Birthday 生日,X.Sex 性别,X.ClassId 班级编号,CA.Score 语文成绩,CB.Score 数学成绩 from StudentInfo X,(select StudentId,Score from StudentCourseScore where CourseId=1) CA,(select StudentId,Score from StudentCourseScore where CourseId=2) CB +where X.Id=CA.StudentId and CA.StudentId=CB.StudentId and CA.Score60 + +-- 3.查询在 成绩 表存在成绩的学生信息 +select X.* from StudentCourseScore C inner join StudentInfo X on C.StudentId=X.Id where Score is not null + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select X.Id 学生编号,X.StudentName 学生姓名,count(*)选课总数,sum(Score) 所有课程的总成绩 from StudentInfo X left join StudentCourseScore C on C.StudentId=X.Id +group by X.Id,X.StudentName++ + +-- 4.1 查有成绩的学生信息 +select X.* from StudentCourseScore C inner join StudentInfo X on C.StudentId=X.Id where Score is not null + +-- 5.查询「李」姓老师的数量 +select count(*)姓李老师数量 from Teachers where TeacherName like'李%' + +-- 6.查询学过「张三」老师授课的同学的信息 +select X.* from StudentCourseScore C left join StudentInfo X on C.StudentId=X.Id +where C.CourseId=2 + +-- 7.查询没有学全所有课程的同学的信息 +select X.* from StudentInfo X,(select StudentId,count(*) 选课数量 from StudentCourseScore group by StudentId) C where X.Id=C.StudentId and C.选课数量<3 + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select X.* from StudentInfo X,(select StudentId,count(*) 选课数量 from StudentCourseScore where not StudentId=1 group by StudentId) C where X.Id=C.StudentId and C.选课数量>1 + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +select X.* from StudentInfo X,(select StudentId,count(*) 选课数量 from StudentCourseScore where not StudentId=1 group by StudentId) C where X.Id=C.StudentId and C.选课数量=3 + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 +select X.StudentName 学生姓名 from StudentInfo X inner join StudentCourseScore C on X.Id=C.StudentId where not C.CourseId=2 + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery3 (1).sql" "b/2021 04 02 \344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery3 (1).sql" new file mode 100644 index 0000000000000000000000000000000000000000..09de5bf066bb2a244ef1676353563702edbed6f1 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery3 (1).sql" @@ -0,0 +1,64 @@ +use master +go +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\新建文件夹 (2).mdf', + size=5MB, + maxsize=50MB, + filegrowth=1Mb +) +log on +( + name='HOUSE_DB_log', + filename='D:\新建文件夹 (2)_1og.ldf', + size=5MB, + maxsize=50MB, + filegrowth=1Mb +) +go +use HOUSE_DB +go +create table HOUSE_TYPE +( +type_id int primary key identity not null, +type_name nvarchar(50) unique not null, +) +go +use HOUSE_DB +go +create table HOUSE +( +house_id int primary key identity not null, +house_name varchar(50) not null, +house_price float default(0) not null, +type_id int references HOUSE_TYPE(type_id) +) +insert into HOUSE_TYPE(type_name) values +('小户型'), +('经济型'), +('别墅') +insert into HOUSE(house_name,house_price,type_id) values +('平顶房',100000,1), +('豪华套房',200000,2), +('空中豪华别墅',1000000,3) + +--查询所有房屋信息 +select * from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 房屋的名称,house_price 租金 from HOUSE order by house_price DESC + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select X.house_name 房屋的名称,L.type_name 房屋类型名称 from HOUSE_TYPE L inner join HOUSE X on L.type_id=X.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select house_name 房屋名称,house_price 最高的租金 from HOUSE where house_price=(select max(house_price) from HOUSE) + +select * from HOUSE_TYPE +select * from HOUSE + diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1c3e88b1542d7b18193db7e33df7eb707d89fd5d --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery3.sql" @@ -0,0 +1,60 @@ +use master +go +create database StarManagerDB +on +( + name='StarManagerDB', + filename='D:\新建文件夹5.mdf', + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) +log on +( + name='StarManagerDB_log', + filename='D:\新建文件夹5_1og.ldf', + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) +go +use StarManagerDB +go +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) +insert into StarType(T_NAME) values + ('体育明星'), + ('IT明星'), + ('相声演员') + +insert into StarInfo(S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) values + ('梅西',30,'射门','阿根廷',1), + ('科比',35,'过人','美国',1), + ('菜景观',40,'敲代码','中国',2), + ('马克思',36,'造火箭','外星人',2), + ('郭德纲',50,'相声','中国',3), + ('黄铮',41,'拼多多','中国',2) + +select * from StarType +select * from StarInfo + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo order by S_AGE DESC + +--4、按 明星类型编号 分类查询 明星人数,明星平均年龄 ,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select L.T_NAME 明星类型,count(*) 明星人数,avg(S_AGE) 明星平均年龄 from StarType L left join StarInfo X on L.T_NO=X.S_T_NO group by S_T_NO,L.T_NAME having count(X.S_NO)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo where S_AGE=(select max(S_AGE) from StarInfo where S_T_NO=1) \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\220\264\346\230\237/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\220\264\346\230\237/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..180832bbb9627951fe5be026e83f6633f38c46ab --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\220\264\346\230\237/SQLQuery1.sql" @@ -0,0 +1,61 @@ +use master +go +create database GoodsDB +on( +name='GoodsDB', +filename='D:\GoodsDB_ldf', +size=5, +maxsize=100, +filegrowth=5% + + +) +log on ( +name='GoodsDB_log', +filename='D:\GoodsDB_log_mdf', +size=5, +maxsize=100, +filegrowth=5% + +) +go +use GoodsDB +go +create table GoodsType +( +TypeID int primary key identity(1,1) not null, +TypeName nvarchar(20) not null +) +create table GoodsInfo +( +GoodsID int primary key identity(1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int references GoodsType(TypeID) + +) +insert into GoodsType values('服装内衣'),('鞋包服'),('手机数码') +insert into GoodsInfo values('提花小西装', '红色','菲曼琪', '300', 1), +('无袖背心',' 白色', '阿依莲', '700', 1), +('低帮休闲鞋', '红色', '菲曼琪', '900', 2), +('提花小西装', '红色','菲曼琪', '300', 1), +('中跟单鞋' ,'绿色' ,'哥弟 ','400 ',2), + ('平底鞋', '白色', '阿依莲', '200', 2), +(' 迷你照相机', '红色','尼康', '500', 3), +('硬盘', '黑色', '希捷', '600', 3), +(' 显卡', '黑色', '技嘉','800', 3) + + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + +select MAX(GoodsMoney) from GoodsInfo +select GoodsName 商品名称,GoodsMoney 商品价格 from GoodsInfo inner join GoodsType on GoodsInfo.TypeID=GoodsType.TypeID where GoodsMoney=(select MAX(GoodsMoney) from GoodsInfo ) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select GoodsInfo.TypeID 商品编号,MAX(GoodsMoney)最高价格,min(GoodsMoney)最低价格 ,avg(GoodsMoney)平均价格 from GoodsInfo inner join GoodsType on GoodsInfo.TypeID=GoodsType.TypeID group by GoodsInfo.TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney in(300 , 600) \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\220\264\346\230\237/SQLQuery2.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\220\264\346\230\237/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1fb1473ae8ebc218ff35dcacb679e8218f9c4f00 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\220\264\346\230\237/SQLQuery2.sql" @@ -0,0 +1,288 @@ + + + + +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + +-- 练习题目: +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +SELECT StudentName,B.StudentId,B.Score 数学,A.Score 语文 FROM +(select CourseId,Score,StudentId from StudentCourseScore WHERE CourseId='2')A INNER JOIN +(select CourseId,Score,StudentId from StudentCourseScore WHERE CourseId='1')B INNER JOIN +StudentInfo ON B.StudentId=StudentInfo.id +ON A.studentid=B.StudentId +WHERE A.Score60 +-- 3.查询在 成绩 表存在成绩的学生信息 + +select * from StudentCourseScore S LEFT join StudentInfo ST on S.StudentId=ST.Id + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +select StudentInfo.Id 学生编号,StudentInfo.StudentName 学生姓名,count(*) 选课总数, sum(Score)总分 from StudentCourseScore + inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id +group by StudentInfo.Id,StudentInfo.StudentName + +-- 4.1 查有成绩的学生信息 + +select * from StudentCourseScore inner join StudentInfo on StudentCourseScore.Id=StudentInfo.Id + +-- 5.查询「李」姓老师的数量 + +SELECT * FROM Teachers WHERE TeacherName LIKE '李%' + +-- 6.查询学过「张三」老师授课的同学的信息 + +select * from StudentCourseScore inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id inner join Teachers on StudentCourseScore.CourseId=Teachers.Id where Teachers.Id=1 + +-- 7.查询没有学全所有课程的同学的信息 +select distinct(s.StudentId),StudentInfo.StudentName, s.*,sc1.score,sc2.score,sc3.score from StudentCourseScore s +left join StudentCourseScore sc1 on s.studentid = sc1.studentid and sc1.CourseId = '1' +left join StudentCourseScore sc2 on s.studentid = sc2.studentid and sc2.CourseId = '2' +left join StudentCourseScore sc3 on s.studentid = sc3.studentid and sc3.CourseId = '3' +inner join StudentInfo on s.StudentId=StudentInfo.Id +where sc1.score is null or sc2.score is null or sc3.score is null + + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode ,StudentName + having count(CourseId) > 0 +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + + +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode ,StudentName + having count(CourseId)=(select COUNT(CourseName) from CourseInfo) + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select distinct studentName from StudentCourseScore A + inner join CourseInfo B on A.CourseId = B.Id + inner join StudentInfo C on A.StudentId = C.StudentCode + inner join Teachers D on B.TeacherId = D.Id + where TeacherName != '张三' + diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\220\264\346\230\237/SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\220\264\346\230\237/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..11b06ac7e296af040556d3a0a6b74bd192e71ea7 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\220\264\346\230\237/SQLQuery3.sql" @@ -0,0 +1,86 @@ +--1、创建数据库HOUSE_DB, +--要求: +--指定数据文件大小:5兆, +--指定文件最大值:50兆, +--文件增长率:1兆 +use master +go + +create database HOUSE_DB +on +( + name = HOUSE_DB, + filename ='D:\HOUSE_DB.mfg', + size = 5MB, + maxsize = 50MB, + filegrowth = 1MB +) +use HOUSE_DB + +--2、创建数据表 +--房屋类型表(HOUSE_TYPE) +--字段名 类型 是否可为空 约束 说明 +--type_id int N 主键,自增长 类型编号 +--type_name varchar(50) N 唯一约束 类型名称 + +create table HOUSE_TYPE +( + type_id int primary key identity , + type_name varchar(20) unique +) + +--房屋信息表(HOUSE) +--字段名 类型 是否可为空 约束 说明 +--house_id int N 主键,自增长 房屋编号 +--house_name varchar(50) N 房屋名称 +--house_price float N 默认值0 房租 +--type_id int N 外键依赖HOUSE_TYPE表 房屋类型 + +create table HOUSE +( + house_id int primary key identity , + house_name varchar(50) , + house_price float default('0'), + type_id int references HOUSE_TYPE(type_id) +) + +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 + +insert into HOUSE_TYPE(type_name) +select '小户型' union +select '经济型' union +select '别墅' + +insert into HOUSE(house_name,house_price,type_id) +select '蔡','5000','3' union +select '东','10000','2' union +select '生','50000','1' + +--4、添加查询 +--查询所有房屋信息 + +select * from HOUSE A + inner join HOUSE_TYPE B on A.type_id = B.type_id + +--使用模糊查询包含”型“字的房屋类型信息 + +select * from HOUSE A + inner join HOUSE_TYPE B on a.type_id = B.type_id + where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 + +select house_name 名字,house_price 租金 from HOUSE + order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 + +select house_name 名称,type_name 类型名称 from HOUSE A + inner join HOUSE_TYPE B on A.type_id = B.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 + +select house_price ,house_name from HOUSE where house_price = (select MAX(house_price) from HOUSE) + diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\220\264\346\230\237/SQLQuery4.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\220\264\346\230\237/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cd3d67f97d3cb4545623816fb8e8c9fd1d4fcae9 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\220\264\346\230\237/SQLQuery4.sql" @@ -0,0 +1,88 @@ +--1、创建明星数据库(StarManagerDB),然后建立两张表,StarType(明星类型表),StarInfo(明星信息表),表结构分别如下: +--StarType(明星类型表) +--字段名 说明 类型 长度 可否为空 约束 +--T_NO 明星类型编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--T_NAME 明星类型 nvarchar 20 + +use master +go + +create database StarManagerDB +go + +use StarManagerDB +go + +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) + +--StarInfo(明星信息表) +--字段名 说明 类型 长度 可否为空 约束 +--S_NO 明星编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--S_NAME 明星姓名 nvarchar 20 否 +--S_AGE 明星年龄 int 否 +--S_HOBBY 特技 nvarchar 20 +--S_NATIVE 明星籍贯 nvarchar 20 默认约束:中国大陆 +--S_T_NO 明星类型编号 int 外键,参照StarType表的主键T_NO + +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20) , + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) + +--2、使用插入语句为两张表添加数据 + +--明星类型表(StarType) +--明星类型编号 明星类型 +--1 体育明星 +--2 IT明星 +--3 相声演员 + +insert into StarType +select '体育明星' union +select 'IT明星' union +select '相声演员' + +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 + +insert into StarInfo(S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) +select '梅西',30,'射门','阿根廷',1 union +select '科比',35,'过人','美国',1 union +select '蔡景现',40,'敲代码','中国',2 union +select '马斯克',36,'造火箭','外星人',2 union +select '郭德纲',50,'相声','中国',3 union +select '黄铮',41,'拼多多','中国',2 + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 + +select top 3 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo + order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 + +select S_T_NO as 类型,count(*) as 人数,avg(S_Age) as 平均年龄 from StarInfo + group by S_T_NO + + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 + +select top 1 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo + where S_T_NO = 1 + order by S_AGE desc + + diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e9da4a41ed145062f87c93bd7a3758a3d8cacab8 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery1.sql" @@ -0,0 +1,74 @@ +use master +go + +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\GoodsDB.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) + +log on +( + name='GoodsDB_log', + filename='D:\GoodsDB_log.dfl', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +go + +use GoodsDB +go +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) + + +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +insert into GoodsType values('服装内衣'), +('鞋包配饰'), +('手机数码') + + +insert into GoodsInfo values +('提花小西装', '红色', '菲曼琪', 300, 1), +('百搭短裤', '绿色', '哥弟', 100, 1), +('无袖背心','白色','阿依莲', 700 ,1), +('低帮休闲鞋',' 红色',' 菲曼琪', 900 ,2), +('中跟单鞋',' 绿色',' 哥弟', 400, 2), +('平底鞋', '白色', '阿依莲', 200, 2), +('迷你照相机',' 红色',' 尼康', 500, 3), +('硬盘',' 黑色',' 希捷' ,600, 3), +('显卡',' 黑色',' 技嘉' ,800, 3) + + +select*from GoodsType +select*from GoodsInfo + + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName,GoodsColor,GoodsMoney from GoodsInfo +order by GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select MAX(GoodsMoney)最高价格,MIN(GoodsMoney)最低价格,AVG(GoodsMoney)平均价格 from GoodsInfo +group by TypeID + + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select*from GoodsInfo where GoodsColor='red' and GoodsMoney>=300 and GoodsMoney<=600 \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery2.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6eebfea2b767cba3ec5230fd0e35d4dda2a6923e --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery2.sql" @@ -0,0 +1,71 @@ +use master +go + +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5MB, + maxsize=50MB, + filegrowth=1MB +) + +log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.dfl', + size=5MB, + maxsize=50MB, + filegrowth=1MB +) + + +use HOUSE_DB +go +create table HOUSE_TYPE +( + type_id int not null primary key identity(1,1), + type_name varchar(50) not null unique +) + +create table HOUSE +( + HOUSE int not null primary key identity(1,1), + house_name varchar(50) not null, + house_price float not null default(0), + type_id int not null references HOUSE_TYPE(type_id) +) + + +insert into HOUSE_TYPE values +('小户型'), +('经济型'), +('别墅') + +insert into HOUSE values +('大都会','800','1'), +('中心城','1000','2'), +('哥谭市','2500','3') + +--查询所有房屋信息 +select*from HOUSE + + +--使用模糊查询包含”型“字的房屋类型信息 +select type_name from HOUSE_TYPE +where type_name like ('%型%') + + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE +order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋type_name类型名称 +select type_name,house_name from HOUSE_TYPE A +inner join HOUSE B on A.type_id=B.type_id + + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select house_name,house_price from HOUSE +order by house_price desc \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2ac1635ec3a6cb23357be7293f44d0d510a7f90f --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery3.sql" @@ -0,0 +1,69 @@ +use master +go + +create database StarManagerDB +on +( + name='StarManagerDB', + filename='D:\StarManagerDB.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) + +log on +( + name='StarManagerDB_log', + filename='D:\StarManagerDB_log.dfl', + size=5MB, + maxsize=50MB, + filegrowth=10% +) + + +use StarManagerDB +go + +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) + +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) + + +insert into StarType values +('体育明星'), +('IT明星'), +('IT明星') + +insert into StarInfo values +('梅西','30','射门','阿根廷','1'), +('科比','35','过人','美国','1'), +('蔡景现','40','敲代码','中国','2'), +('马斯克','36','造火箭','外星人','2'), +('郭德纲','50','相声','中国','3'), +('黄铮','41','拼多多','中国','2') + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME,S_AGE,S_HOBBY,S_NATIVE from StarInfo +order by S_AGE desc + + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select A.T_NO,AVG(S_AGE) from StarType A +inner join StarInfo B on A.T_NO=B.S_NO +group by S_AGE,A.T_NO + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select S_NAME,S_AGE,S_HOBBY,S_NATIVE from StarInfo +order by S_AGE desc \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery50.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery50.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2d5dc74b1908688d3c2e893f72b0e1fd37025aca --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery50.sql" @@ -0,0 +1,280 @@ + + +select * from StudentInfo +select * from Teachers +select * from StudentCourseScore +select * from CourseInfo + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +select StudentName,Birthday,B.Score from StudentInfo A +inner join StudentCourseScore B on A.Id = B.StudentId and B.CourseId = 2 +inner join StudentCourseScore C on A.Id = B.StudentId and C.CourseId = 1 +where B.Score>C.Score + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select StudentId from StudentCourseScore +where CourseId = 1 or CourseId = 2 group by StudentId having COUNT(distinct CourseId) = 2 + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select *from StudentCourseScore where CourseId = 1 A +right join select * from StudentCourseScore where CourseId = 2 B +on A.StudentId=B.StudentId + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select StudentId,Score from StudentCourseScore A +left join StudentInfo B on A.StudentId = B.Id where CourseId = 1 + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentId,StudentName,avg(Score)平均成绩 from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +group by StudentId,StudentName +having avg(Score) >= 60 + +-- 3.查询在 成绩 表存在成绩的学生信息 +select B.* from StudentCourseScore A +left join StudentInfo B on A.StudentId = B.StudentCode + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select * from StudentInfo B +left join (select StudentId from StudentCourseScore group by StudentId) A on B.Id = A.StudentId + +-- 4.1 查有成绩的学生信息 +select * from StudentInfo B +inner join (select StudentId from StudentCourseScore group by StudentId) A on B.Id = A.StudentId + +-- 5.查询「李」姓老师的数量 +select * from Teachers where TeacherName like '李%' + +-- 6.查询学过「张三」老师授课的同学的信息 +select B.* from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +where A.CourseId = 1 + +-- 7.查询没有学全所有课程的同学的信息 +select StudentId,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +group by StudentId,StudentName,Birthday,Sex,ClassId +having count(CourseId)<3 + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select StudentId,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 1 or CourseId = 2 or CourseId = 3 +group by StudentId,StudentName,Birthday,Sex,ClassId + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +select StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 1 or CourseId = 2 or CourseId = 3 +group by StudentCode,StudentName,Birthday,Sex,ClassId +having count(CourseId) = 3 + + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 +select distinct studentName from StudentCourseScore A +inner join CourseInfo B on A.CourseId = B.Id +inner join StudentInfo C on A.StudentId = C.StudentCode +inner join Teachers D on B.TeacherId = D.Id +where TeacherName != '张三' + + + + + + +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\224\220\345\207\241\350\276\211/50.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\224\220\345\207\241\350\276\211/50.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d5b6f40ea45fd83ce0c61d65ddd58b71b880cc7d --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\224\220\345\207\241\350\276\211/50.sql" @@ -0,0 +1,294 @@ +use ClassicDb +go + +create database ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '璧甸浄' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '閽辩數' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '瀛欓' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '鏉庝簯' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '鍛ㄦ' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '鍚村叞' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '閮戠' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '寮犱笁' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '鏉庡洓' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '鏉庡洓' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '璧靛叚' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '瀛欎竷' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('寮犱笁') + +insert into Teachers (TeacherName) values('鏉庡洓') + +insert into Teachers (TeacherName) values('鐜嬩簲') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '璇枃' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '鏁板' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '鑻辫' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore +-- 1.鏌ヨ"鏁板 "璇剧▼姣" 璇枃 "璇剧▼鎴愮哗楂樼殑瀛︾敓鐨勪俊鎭強璇剧▼鍒嗘暟 + +select D.* from +(select A.StudentId a,A.CourseId b,A.Score c,B.StudentId d,B.CourseId e,B.Score f from +(select * from StudentCourseScore where CourseId = 1) as A , +(select * from StudentCourseScore where CourseId = 2) as B +where A.Score > B.Score and A.StudentId = B.StudentId) as C +inner join StudentInfo D on C.a = D.StudentCode + + +-- 1.1 鏌ヨ鍚屾椂瀛樺湪" 鏁板 "璇剧▼鍜" 璇枃 "璇剧▼鐨勬儏鍐 + +select StudentId,count(CourseId) from StudentCourseScore +where CourseId = 1 or CourseId = 2 +group by StudentId +having count(CourseId) = 2 + +-- 1.2 鏌ヨ瀛樺湪" 鏁板 "璇剧▼浣嗗彲鑳戒笉瀛樺湪" 璇枃 "璇剧▼鐨勬儏鍐(涓嶅瓨鍦ㄦ椂鏄剧ず涓 null ) + +select * from (select * from StudentCourseScore where CourseId = 1) as A +right join (select * from StudentCourseScore where CourseId = 2) as B on A.StudentId = B.StudentId + +-- 1.3 鏌ヨ涓嶅瓨鍦" 鏁板 "璇剧▼浣嗗瓨鍦" 璇枃 "璇剧▼鐨勬儏鍐 + +select * from (select * from StudentCourseScore where CourseId = 1) as A +left join (select * from StudentCourseScore where CourseId = 2) as B on A.StudentId = B.StudentId + +-- 2.鏌ヨ骞冲潎鎴愮哗澶т簬绛変簬 60 鍒嗙殑鍚屽鐨勫鐢熺紪鍙峰拰瀛︾敓濮撳悕鍜屽钩鍧囨垚缁 + +select StudentId as 瀛︾敓缂栧彿,StudentName as 瀛︾敓濮撳悕,avg(Score) as 骞冲潎鎴愮哗 from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +group by StudentId,StudentName +having avg(Score) >= 60 + +-- 3.鏌ヨ鍦 鎴愮哗琛 瀛樺湪鎴愮哗鐨勫鐢熶俊鎭 + +select B.* from StudentCourseScore A +left join StudentInfo B on A.StudentId = B.StudentCode + + +-- 4.鏌ヨ鎵鏈夊悓瀛︾殑瀛︾敓缂栧彿銆佸鐢熷鍚嶃侀夎鎬绘暟銆佹墍鏈夎绋嬬殑鎬绘垚缁(娌℃垚缁╃殑鏄剧ず涓 null ) + +select +StudentCode as 瀛︾敓缂栧彿,StudentName as 瀛︾敓濮撳悕, +count(CourseId) as 閫夎鎬绘暟,sum(Score) as 鎬绘垚缁 +from StudentCourseScore A +right join StudentInfo B on A.StudentId = B.StudentCode +group by StudentCode,StudentName +order by StudentCode + +-- 4.1 鏌ユ湁鎴愮哗鐨勫鐢熶俊鎭 + +select StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +left join StudentInfo B on A.StudentId = B.StudentCode +group by StudentCode,StudentName,Birthday,Sex,ClassId + +-- 5.鏌ヨ銆屾潕銆嶅鑰佸笀鐨勬暟閲 + +select count(*) as 濮撹佸笀鐨勬暟閲 from Teachers +group by TeacherName +having TeacherName like ('鏉%') + + +-- 6.鏌ヨ瀛﹁繃銆屽紶涓夈嶈佸笀鎺堣鐨勫悓瀛︾殑淇℃伅 + +select B.* from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +where A.CourseId = 1 + +-- 7.鏌ヨ娌℃湁瀛﹀叏鎵鏈夎绋嬬殑鍚屽鐨勪俊鎭 + +select StudentId,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +group by StudentId,StudentName,Birthday,Sex,ClassId +having count(CourseId)<3 + +-- 8.鏌ヨ鑷冲皯鏈変竴闂ㄨ涓庡鍙蜂负" 01 "鐨勫悓瀛︽墍瀛︾浉鍚岀殑鍚屽鐨勪俊鎭 + +select StudentId,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 1 or CourseId = 2 or CourseId = 3 +group by StudentId,StudentName,Birthday,Sex,ClassId + +-- 9.鏌ヨ鍜" 01 "鍙风殑鍚屽瀛︿範鐨勮绋 瀹屽叏鐩稿悓鐨勫叾浠栧悓瀛︾殑淇℃伅 + +select StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 1 or CourseId = 2 or CourseId = 3 +group by StudentCode,StudentName,Birthday,Sex,ClassId +having count(CourseId) = 3 + +-- 10.鏌ヨ娌″杩"寮犱笁"鑰佸笀璁叉巿鐨勪换涓闂ㄨ绋嬬殑瀛︾敓濮撳悕 + +select StudentName from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +where A.CourseId != 1 +group by StudentCode,StudentName \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\224\220\345\207\241\350\276\211/SQLserver12.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\224\220\345\207\241\350\276\211/SQLserver12.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d676cf5d48ce201d4a60d86332f921bf0306976d --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\224\220\345\207\241\350\276\211/SQLserver12.sql" @@ -0,0 +1,57 @@ +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\GoodsDB.mdf', + size=10, + maxsize=100, + filegrowth=10 +) +log on +( + name='GoodsDB_log', + filename='D:\GoodsDB_log.ldf', + size=10, + maxsize=100, + filegrowth=10 +) +go +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity(1,1), + TypeName nvarchar(20) not null, +) +create table GoodsInfo +( + GoodsID int primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) not null, + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) +insert into GoodsType values('鏈嶈鍐呰。'),('闉嬪寘閰嶉グ'),('鎵嬫満鏁扮爜') +insert into GoodsInfo values('灏忚タ瑁',' 绾㈣壊' ,'鑿叉浖鐞' ,'300' ,'1'), +(' 鐧炬惌鐭¥',' 缁胯壊' ,'鍝ュ紵' ,'100 ' ,'1'), +('鏃犺鑳屽績',' 鐧借壊' ,' 闃夸緷鑾' ,'700 ' ,'1'), +('浣庡府浼戦棽闉',' 绾㈣壊' ,'鑿叉浖鐞 ' ,'900 ' ,'2'), +('涓窡鍗曢瀷',' 缁胯壊' ,'鍝ュ紵' ,'400 ' ,'2'), +('骞冲簳闉',' 鐧借壊' ,'闃夸緷鑾' ,'200 ' ,'2'), +('杩蜂綘鐓х浉鏈',' 绾㈣壊' ,'灏煎悍' ,'500 ' ,'3'), +('纭洏',' 榛戣壊' ,'甯屾嵎' ,'600 ' ,'3'), +('鏄惧崱',' 榛戣壊' ,'鎶鍢' ,'800 ' ,'3') + +select * from GoodsType +select * from GoodsInfo + +--3銆佹煡璇环鏍兼渶璐电殑鍟嗗搧鍚嶇О锛屽晢鍝侀鑹插拰鍟嗗搧浠锋牸锛岃姹備娇鐢ㄥ埆鍚嶆樉绀哄垪鍚 +select top 1 GoodsName as 鍟嗗搧鍚嶇О,GoodsColor as 鍟嗗搧棰滆壊,GoodsMoney 鍟嗗搧浠锋牸 from GoodsInfo order by GoodsMoney desc + +--4銆佹寜鍟嗗搧绫诲瀷缂栧彿鍒嗙粍鏌ヨ鍟嗗搧鏈楂樹环鏍硷紝鏈浣庝环鏍煎拰骞冲潎浠锋牸锛岃姹備娇鐢ㄥ埆鍚嶆樉绀哄垪鍚 +select TypeID as 鍟嗗搧绫诲瀷缂栧彿, MAX(GoodsMoney) as 鏈楂樹环鏍,MIN(GoodsMoney) as 鏈浣庝环鏍,AVG(GoodsMoney) as 骞冲潎浠锋牸 from GoodsInfo group by TypeID + +--5銆佹煡璇㈠晢鍝佷俊鎭墍鏈夊垪锛岃姹傚晢鍝侀鑹蹭负绾㈣壊锛屼环鏍煎湪300~600涔嬮棿 +select * from GoodsInfo where GoodsMoney>=300 and GoodsMoney<=600 and GoodsColor='绾㈣壊' \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\224\220\345\207\241\350\276\211/SQLserver13.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\224\220\345\207\241\350\276\211/SQLserver13.sql" new file mode 100644 index 0000000000000000000000000000000000000000..31194db84d0cb735bdb2d0ee8003130743336732 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\224\220\345\207\241\350\276\211/SQLserver13.sql" @@ -0,0 +1,66 @@ +--1銆佸垱寤烘暟鎹簱HOUSE_DB锛 +--瑕佹眰锛 +--鎸囧畾鏁版嵁鏂囦欢澶у皬锛5鍏嗭紝 +--鎸囧畾鏂囦欢鏈澶у硷細50鍏嗭紝 +--鏂囦欢澧為暱鐜囷細1鍏 + +CREATE database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5, + maxsize=50, + filegrowth=1 +) +log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5, + maxsize=50, + filegrowth=1 +) +go +use HOUSE_DB +go +--2銆佸垱寤烘暟鎹〃 +--鎴垮眿绫诲瀷琛紙HOUSE_TYPE锛 +--瀛楁鍚 绫诲瀷 鏄惁鍙负绌 绾︽潫 璇存槑 +--type_id int N 涓婚敭锛岃嚜澧為暱 绫诲瀷缂栧彿 +--type_name varchar(50) N 鍞竴绾︽潫 绫诲瀷鍚嶇О +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name varchar(50) not null unique +) +--鎴垮眿淇℃伅琛紙HOUSE锛 +--瀛楁鍚 绫诲瀷 鏄惁鍙负绌 绾︽潫 璇存槑 +--house_id int N 涓婚敭锛岃嚜澧為暱 鎴垮眿缂栧彿 +--house_name varchar(50) N 鎴垮眿鍚嶇О +--house_price float N 榛樿鍊0 鎴跨 +--type_id int N 澶栭敭渚濊禆HOUSE_TYPE琛 鎴垮眿绫诲瀷 +create table HOUSE +( +house_id int primary key identity(1,1), +house_name varchar(50) not null , +house_price float not null default(0), +type_id int references HOUSE_TYPE(type_id) +) +--3銆佹坊鍔犺〃鏁版嵁 +--HOUSE_TYPE琛ㄤ腑娣诲姞3鏉℃暟鎹紝渚嬪锛氬皬鎴峰瀷銆佺粡娴庡瀷銆佸埆澧 +--HOUSE琛ㄤ腑娣诲姞鑷冲皯3鏉℃暟鎹紝涓嶈兘鍏ㄩ兘涓哄悓涓绫诲瀷 +insert into HOUSE_TYPE values('灏忔埛鍨'),('缁忔祹鍨'),('鍒' ) +insert into HOUSE values('灏忕孩灞',500,'1'),('缁忔祹灞',1000,'2'),('鍒灞',5000,'3') +--4銆佹坊鍔犳煡璇 +select * from HOUSE_TYPE +select * from HOUSE +--鏌ヨ鎵鏈夋埧灞嬩俊鎭 +--浣跨敤妯$硦鏌ヨ鍖呭惈鈥濆瀷鈥滃瓧鐨勬埧灞嬬被鍨嬩俊鎭 +select * from HOUSE_TYPE where type_name like '%鍨%' +--鏌ヨ鍑烘埧灞嬬殑鍚嶇О鍜岀閲戯紝骞朵笖鎸夌収绉熼噾闄嶅簭鎺掑簭 +select * from HOUSE order by house_price desc +--浣跨敤杩炴帴鏌ヨ锛屾煡璇俊鎭紝鏄剧ず鎴垮眿鍚嶇О鍜屾埧灞嬬被鍨嬪悕绉 +select house_name 鎴垮眿鍚嶇О,type_name 鎴垮眿绫诲瀷鍚嶇О from HOUSE A INNER JOIN HOUSE_TYPE B ON A.type_id=B.type_id +--鏌ヨ鎵鏈夋埧灞嬩腑鏈堢鏈楂樼殑鎴垮眿锛屾樉绀烘渶楂樼殑绉熼噾鍜屾埧灞嬪悕绉 +select top 1 house_price 绉熼噾,house_name 鎴垮眿鍚嶇О from HOUSE order by house_price desc \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\224\220\345\207\241\350\276\211/SQLserver14.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\224\220\345\207\241\350\276\211/SQLserver14.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b57bd14768cefa394f0171595e411542e5e8217d --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\224\220\345\207\241\350\276\211/SQLserver14.sql" @@ -0,0 +1,92 @@ +--1銆佸垱寤烘槑鏄熸暟鎹簱锛圫tarManagerDB锛,鐒跺悗寤虹珛涓ゅ紶琛紝StarType锛堟槑鏄熺被鍨嬭〃锛夛紝StarInfo锛堟槑鏄熶俊鎭〃锛夛紝琛ㄧ粨鏋勫垎鍒涓嬶細 + + CREATE DATABASE StarManagerDB + ON + ( + name='StarManagerDB', + filename='D:\StarManagerDB.mdf', + size=5, + maxsize=50, + filegrowth=10 + ) + log on + ( + name='StarManagerDB_log', + filename='D:\StarManagerDB_log.ldf', + size=5, + maxsize=50, + filegrowth=10 + ) + go + use StarManagerDB + go + --StarType锛堟槑鏄熺被鍨嬭〃锛 +--瀛楁鍚 璇存槑 绫诲瀷 闀垮害 鍙惁涓虹┖ 绾︽潫 +--T_NO 鏄庢槦绫诲瀷缂栧彿 int 鍚 涓婚敭绾︽潫锛岃嚜澧烇紝鏍囪瘑绉嶅瓙鍜屾爣璇嗗閲忛兘鏄1 +--T_NAME 鏄庢槦绫诲瀷 nvarchar 20 +create table StarType +( + T_NO int primary key identity(1,1), + T_NAME nvarchar(20) +) + + +--StarInfo锛堟槑鏄熶俊鎭〃锛 +--瀛楁鍚 璇存槑 绫诲瀷 闀垮害 鍙惁涓虹┖ 绾︽潫 +--S_NO 鏄庢槦缂栧彿 int 鍚 涓婚敭绾︽潫锛岃嚜澧烇紝鏍囪瘑绉嶅瓙鍜屾爣璇嗗閲忛兘鏄1 +--S_NAME 鏄庢槦濮撳悕 nvarchar 20 鍚 +--S_AGE 鏄庢槦骞撮緞 int 鍚 +--S_HOBBY 鐗规妧 nvarchar 20 +--S_NATIVE 鏄庢槦绫嶈疮 nvarchar 20 榛樿绾︽潫锛氫腑鍥藉ぇ闄 +--S_T_NO 鏄庢槦绫诲瀷缂栧彿 int 澶栭敭锛屽弬鐓tarType琛ㄧ殑涓婚敭T_NO + +create table StarInfo +( + S_NO int primary key identity not null , + S_NAME nvarchar(20) not null , + S_AGE int not null , + S_HOBBY nvarchar(20) , + S_NATIVE nvarchar(20) default('涓浗澶ч檰') , + S_T_NO int references StarType(T_NO) +) + + +--2銆佷娇鐢ㄦ彃鍏ヨ鍙ヤ负涓ゅ紶琛ㄦ坊鍔犳暟鎹 + +--鏄庢槦绫诲瀷琛紙StarType锛 +--鏄庢槦绫诲瀷缂栧彿 鏄庢槦绫诲瀷 +--1 浣撹偛鏄庢槦 +--2 IT鏄庢槦 +--3 鐩稿0婕斿憳 + +insert StarType values +('浣撹偛鏄庢槦'), +('IT鏄庢槦'), +('鐩稿0婕斿憳') +--鏄庢槦琛紙StarInfo锛 +--鏄庢槦缂栧彿 濮撳悕 骞撮緞 鐗规妧 绫嶈疮 鏄庢槦绫诲瀷缂栧彿 +--1 姊呰タ 30 灏勯棬 闃挎牴寤 1 +--2 绉戞瘮 35 杩囦汉 缇庡浗 1 +--3 钄℃櫙鐜 40 鏁蹭唬鐮 涓浗 2 +--4 椹柉鍏 36 閫犵伀绠 澶栨槦浜 2 +--5 閮痉绾 50 鐩稿0 涓浗 3 +--6 榛勯摦 41 鎷煎澶 涓浗 2 + +insert StarInfo values +('姊呰タ',30,'灏勯棬','闃挎牴寤',1), +('绉戞瘮',35,'杩囦汉','缇庡浗',1), +('钄℃櫙鐜',40,'鏁蹭唬鐮','涓浗',2), +('椹柉鍏',36,'閫犵伀绠','澶栨槦浜',2), +('閮痉绾',50,'鐩稿0','涓浗',3), +('榛勯摦',41,'鎷煎澶','涓浗',2) +select * from StarType +select * from StarInfo +--3銆佹煡璇㈠勾榫勬渶澶х殑3涓槑鏄熺殑濮撳悕锛岀壒鎶鍜岀睄璐俊鎭紝瑕佹眰浣跨敤鍒悕鏄剧ず鍒楀悕銆 +select top 3 S_NAME 鍚嶅瓧, S_HOBBY 鐗规妧, S_NATIVE 绫嶈疮淇℃伅 from StarInfo order by S_AGE desc +--4銆佹寜鏄庢槦绫诲瀷缂栧彿鍒嗙被鏌ヨ鏄庢槦浜烘暟锛屾槑鏄熷钩鍧囧勾榫勶紝鏄剧ず鏄庢槦浜烘暟澶т簬2鐨勫垎缁勪俊鎭紝瑕佹眰浣跨敤鍒悕鏄剧ず鍒楀悕銆 +select S_T_NO as 绫诲瀷,count(*) as 浜烘暟,avg(S_Age) as 骞冲潎骞撮緞 from StarInfo +group by S_T_NO +--5銆佹煡璇㈡槑鏄熺被鍨嬩负鈥滀綋鑲叉槑鏄熲濅腑骞撮緞鏈澶х殑濮撳悕銆佺壒鎶銆佺睄璐俊鎭紝瑕佹眰鏄剧ず鍒楀埆鍚嶃 +select top 1 S_NAME as 濮撳悕,S_AGE as 骞撮緞,S_HOBBY as 鐗规妧,S_NATIVE as 绫嶈疮淇℃伅 from StarInfo +where S_T_NO = 1 +order by S_AGE desc \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/50\351\242\230.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/50\351\242\230.sql" new file mode 100644 index 0000000000000000000000000000000000000000..31f24438f08bd6c22fbfb48b43006beb0e7e20a0 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/50\351\242\230.sql" @@ -0,0 +1,245 @@ + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + +select StudentName,Birthday,A.Score from StudentInfo S +inner join StudentCourseScore A +on S.Id = A.StudentId and A.CourseId = 2 +inner join StudentCourseScore B +on S.Id = B.StudentId and B.CourseId = 1 +where A.Score>B.Score + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select * from (select StudentId,Score,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 2) B +inner join +(select StudentId,Score,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 1) A +on A.StudentId = B.StudentId +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) + +select * from (select StudentId,Score,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 2) B +left join +(select StudentId,Score,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 1) A +on A.StudentId = B.StudentId +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select * from +(select StudentId,Score from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 1 )A +left join +(select StudentId,Score from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 2) B +on A.StudentId = B.StudentId +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentId,StudentName,avg(Score) from StudentCourseScore SC inner join StudentInfo SI +on SC.StudentId = SI.Id +group by StudentId,StudentName having avg(Score)>60 + + +-- 3.查询在 成绩 表存在成绩的学生信息 +select * from (select StudentId from StudentCourseScore group by StudentId) SC inner join StudentInfo SI on SC.StudentId = SI.Id + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select * from StudentInfo SI left join (select StudentId from StudentCourseScore group by StudentId) SC on SI.Id = SC.StudentId + + +-- 4.1 查有成绩的学生信息 +select * from StudentInfo SI inner join (select StudentId from StudentCourseScore group by StudentId) SC on SI.Id = SC.StudentId + + +-- 5.查询「李」姓老师的数量 +select * from Teachers where TeacherName like '李%' + + +-- 6.查询学过「张三」老师授课的同学的信息 +select * from StudentInfo SI inner join (select StudentId from StudentCourseScore where CourseId = 2 group by StudentId ) SC on SI.Id = SC.StudentId + + +-- 7.查询没有学全所有课程的同学的信息 +select * from +(select * from StudentInfo A +right join +(select StudentId from StudentCourseScore group by StudentId having count(StudentId)<3) C +on A.Id = C.StudentId) Q +full join +(select SI.* from StudentInfo SI left join StudentCourseScore SC on SI.Id = SC.StudentId where Score is null) W +on Q.Id=W.Id +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select * from StudentInfo SI inner join +(select A.StudentId from +(select StudentId,CourseId from StudentCourseScore) A, +(select StudentId,CourseId from StudentCourseScore where StudentId = 1) B +where A.CourseId = B.CourseId and A.StudentId!=1 +group by A.StudentId) A +on SI.Id = A.StudentId +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +select * from StudentInfo SI inner join +(select A.StudentId from +(select StudentId,CourseId from StudentCourseScore) A, +(select StudentId,CourseId from StudentCourseScore where StudentId = 1) B +where A.CourseId = B.CourseId and A.StudentId!=1 +group by A.StudentId having count(A.StudentId)>2) A +on SI.Id = A.StudentId +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select * from StudentInfo SI +left join +(select StudentId from StudentCourseScore where CourseId = 2) S +on SI.Id = S.StudentId +where StudentId is null + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 +select * from StudentInfo + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/\345\244\215\344\271\240\351\242\2301.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/\345\244\215\344\271\240\351\242\2301.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cfcb28b918285b1c6a3c1c9fdc6664aa679f68b2 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/\345\244\215\344\271\240\351\242\2301.sql" @@ -0,0 +1,40 @@ +create database GoodDB +go +use GoodDB +go + +create table GoodsType +( + TypeID int not null primary key identity , + TypeName nvarchar(20) not null +) + +create table GoodInfo +( + GoodsID int primary key identity not null, + GiidsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeId int foreign key references GoodsType(TypeID) +) +go +insert into GoodsType(TypeName) +select '服装内衣' union +select '鞋包配饰' union +select '手机数码' + +insert into GoodInfo(GiidsName,GoodsColor,GoodsBrand,GoodsMoney,TypeId) +values('提花小西装','红色','菲曼琪',300,1),('百搭短裤','绿色','哥弟',100,1),('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2),('中跟单鞋','绿色','哥弟',400,2),('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3),('硬盘','黑色','希捷',600,3),('显卡','黑色','技嘉',800,3) +go +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GiidsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodInfo where GoodsMoney = (select max(GoodsMoney) from GoodInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格 ,avg(GoodsMoney) 平均价格 from GoodInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 + diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/\345\244\215\344\271\240\351\242\2302.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/\345\244\215\344\271\240\351\242\2302.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6d3ca1a6fb53e730b1eb4ba74f0cd43a0cb5fa79 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/\345\244\215\344\271\240\351\242\2302.sql" @@ -0,0 +1,51 @@ +create database HOUSE_DB +on +( + name = 'HOUSE_DB', + filename = 'D:\HOUSE_DB.mdf', + size = 5, + maxsize= 50, + filegrowth = 10% +) +log on +( + name= 'HOUSE_DB_log', + filename = 'D:\HOUSE_DB_log.ldf', + size = 5, + maxsize= 50, + filegrowth = 10% +) +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity, + type_name varchar(50) unique +) +create table HOUSE +( + house_id int primary key identity , + house_name varchar(50), + house_price float default (0), + type_id int foreign key references HOUSE_TYPE(type_id) +) +go +insert into HOUSE_TYPE(type_name) +select '小户型' union select '经济型' union select '别墅' + +insert into HOUSE(house_name,house_price,type_id) +values('鱼',40,1),('龟',45,2),('虾',60,3) +--go +--查询所有房屋信息 +select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE inner join HOUSE_TYPE on HOUSE_TYPE.type_id = HOUSE.type_id +where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name,type_name from HOUSE inner join HOUSE_TYPE on HOUSE_TYPE.type_id = HOUSE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select house_price,house_name from HOUSE where house_price = (select max(house_price) from HOUSE ) + diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/\345\244\215\344\271\240\351\242\2303.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/\345\244\215\344\271\240\351\242\2303.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5361c99fd94b4f8be426792b8b4b56e3d11e56be --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/\345\244\215\344\271\240\351\242\2303.sql" @@ -0,0 +1,36 @@ +create database StarManagerDB +use StarManagerDB +go + +create table StarType +( + T_NO int primary key identity , + T_NAME nvarchar(20) +) +create table StarInfo +( + S_NO int primary key identity, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20) , + S_NATIVE nvarchar(20) default ('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) +go +insert into StarType(T_NAME) +select '体育明星'union +select 'IT明星'union +select '相声演员' + +insert into StarInfo(S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) +values ('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',2),('蔡景现',40,'敲代码','中国',2) +,('马克斯',36,'造火箭','外星人',2),('郭德纲',50,'相声','中国',3),('黄峥',41,'拼多多','中国',2) + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 籍贯信息,S_NATIVE 特技 from StarInfo order by S_AGE desc +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO,count(*)明星人数,avg(S_AGE)平均年龄 from StarInfo group by S_T_NO having count(S_T_NO)>1 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo where S_AGE=(select max(S_AGE) from StarInfo where S_T_NO = 1) + +select * from StarInfo \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\274\240\345\256\217/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\274\240\345\256\217/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8c0a5841227ddc9b8c2aa8fa6ae1d1a60ac4e643 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\274\240\345\256\217/SQLQuery1.sql" @@ -0,0 +1,102 @@ +-- 练习题目: + + + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + +select * from + (select * from StudentCourseScore where courseId = 1) as A + inner join StudentInfo s on s.Id = A.Id, + (select * from StudentCourseScore where CourseId = 2) as B + where A.Score > B.Score and A.StudentId = B.StudentId + + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 + +select StudentId from StudentCourseScore + where CourseId = 1 or CourseId = 2 + group by StudentId + HAVING COUNT(distinct CourseId) = 2 + + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) + +select * from + (select * from StudentCourseScore where CourseId = 2) A + left join (select * from StudentCourseScore where CourseId = 1) B on A.StudentId = B.StudentId + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 + +select * from + (select * from StudentCourseScore where CourseId = 1) A + left join (select * from StudentCourseScore where CourseId = 2) B on A.StudentId = B.StudentId + where B.CourseId is null + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 + +select Studentcode 学生编号,StudentName 学生姓名,AVG(Score) 平均成绩 from StudentCourseScore + inner join StudentInfo on StudentCourseScore.StudentId = StudentInfo.StudentCode + group by Studentcode,StudentName + HAVING AVG(Score) >= 60 + +-- 3.查询在 成绩 表存在成绩的学生信息 + +select * from StudentCourseScore + left join StudentInfo on StudentCourseScore.StudentId = StudentInfo.StudentCode + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +select StudentCode ,StudentName ,COUNT(CourseId) ,SUM(Score) from StudentCourseScore scs + right join StudentInfo si on scs.StudentId = si.StudentCode + group by StudentCode,StudentName + +-- 4.1 查有成绩的学生信息 + +select * from StudentCourseScore scs + inner join StudentInfo si on scs.StudentId = si.StudentCode + +-- 5.查询「李」姓老师的数量 + +select COUNT(TeacherName) from Teachers + where TeacherName like '李%' + +-- 6.查询学过 「张三」老师授课 的 同学的信息 + +select * from StudentCourseScore A + inner join CourseInfo B on A.CourseId = B.Id + inner join StudentInfo C on A.StudentId = C.StudentCode + inner join Teachers D on B.TeacherId = D.Id + where TeacherName = '张三' + +-- 7.查询没有学全所有课程的同学的信息 +select * from CourseInfo +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers + +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode,StudentName + HAVING COUNT(CourseId) < (select COUNT(CourseName) from CourseInfo) + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode ,StudentName + having count(CourseId) > 0 + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode ,StudentName + having count(CourseId)=(select COUNT(CourseName) from CourseInfo) + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select distinct studentName from StudentCourseScore A + inner join CourseInfo B on A.CourseId = B.Id + inner join StudentInfo C on A.StudentId = C.StudentCode + inner join Teachers D on B.TeacherId = D.Id + where TeacherName != '张三' \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\274\240\345\256\217/SQLQuery2.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\274\240\345\256\217/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..50b6c8e018f8fc48e99b1048cb590176e868daf8 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\274\240\345\256\217/SQLQuery2.sql" @@ -0,0 +1,65 @@ +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\GoodsDB.mdf', + size=10mb, + maxsize=100mb, + filegrowth=10mb +) +log on +( + name='GoodsDB_log', + filename='D:\GoodsDB_log.ldf', + size=10mb, + maxsize=100mb, + filegrowth=10mb +) + +go +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity(1,1), + TypeName nvarchar(20) not null +) + +insert into GoodsType (TypeName) values ('服装内衣'),('鞋包服饰'),('手机数码') + +select * from GoodsType + +create table GoodsInfo +( + GoodsID int primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +insert into GoodsInfo (GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values('提花小西装','红色','菲曼其','300',1), +('百搭短裤','绿色','哥弟','100',1), +('无袖背心','白色','阿依莲','700',1), +('低帮休闲鞋','红色','菲曼其','900',2), +('中跟单鞋','绿色','哥弟','400',2), +('平底鞋','白色','阿依莲','200',2), +('迷你照相机','红色','尼康','500',3), +('硬盘','黑色','希捷','600',3), +('显卡','黑色','技嘉','800',3) + +select * from GoodsType +select * from GoodsInfo + + + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select TOP 1 GoodsName 商品名称 ,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo GI order by GoodsMoney DESC + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeName,max(GoodsMoney)最高价格,min(GoodsMoney)最低价格,avg(GoodsMoney)平均价格 from GoodsType GT inner join GoodsInfo GI on GT.TypeID=GI.TypeID group by TypeName + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>300 and GoodsMoney<600 \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\274\240\345\256\217/SQLQuery5.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\274\240\345\256\217/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3dfec7afe9e8b6b366bb5662e299be88b1f1f9eb --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\274\240\345\256\217/SQLQuery5.sql" @@ -0,0 +1,60 @@ + +create database HOUSE_DB +on +( + name='HOUSED_B', + filename='D;\HOUSE_DB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) +log on +( + name='HOUSE_DBlog', + filename='D;\HOUSE_DB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) + +go +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity(1,1), + type_name varchar(50) unique not null, +) + +insert into HOUSE_TYPE (type_name) values('小型户'),('经济户'),('别墅') +select * from HOUSE_TYPE + +create table HOUSE +( + house_id int primary key identity(1,1), + house_name varchar(50) not null, + house_price float default('0') not null, + type_id int references HOUSE_TYPE(type_id) +) + +insert into HOUSE (house_name,house_price,type_id) values('中式屋','60000',1) +insert into HOUSE (house_name,house_price,type_id) values('法式屋','40000',2) +insert into HOUSE (house_name,house_price,type_id) values('美式屋','50000',3) + + +--查询所有房屋信息 +select *,type_name from HOUSE HO inner join HOUSE_TYPE HT on HO.type_id=HT.type_id + +--使用模糊查询包含”型“字的房屋类型信息 +select HT.type_id,type_name,house_name,house_price from HOUSE_TYPE HT inner join HOUSE HO on HT.type_id=HO.type_id WHERE type_name LIKE '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price DESC + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name,type_name from HOUSE HO inner join HOUSE_TYPE HT on HO.type_id=HT.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price,house_name from HOUSE order by house_price DESC + diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\274\240\345\256\217/SQLQuery6.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\274\240\345\256\217/SQLQuery6.sql" new file mode 100644 index 0000000000000000000000000000000000000000..33917a9bcd042c106f886cf214d6c978d0778898 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\274\240\345\256\217/SQLQuery6.sql" @@ -0,0 +1,57 @@ +create database StarManagerDB +on +( + name='StarManagerDB', + filename='D;\StarManagerDB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) + log on + ( + name='StarManagerDB_log', + filename='D;\StarManagerDB_log.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) + +go +use StarManagerDB +go + +create table StarType +( + T_NO int primary key identity(1,1), + T_NAME nvarchar(20) +) +insert into StarType(T_NAME) values('体育明星','IT明星','相声演员') + +select * from StarType + +create table StarInfo +( + S_NO int primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) + +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) values('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马克思',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名 ,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo SI order by S_AGE DESC + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +SELECT S_T_NO,count(S_NO)人数,AVG(S_AGE)平均年龄 FROM StarInfo SI where count(S_NO)>2 group by S_T_NO + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select TOP 1 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo SI where S_T_NO ='1' order BY S_AGE DESC + diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..73c6bc729777dc3e7ecdbb9f8c976b1c85b2a51a --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery1.sql" @@ -0,0 +1,52 @@ +use master +go + +create database GoodsDB +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) + +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) +go + +insert GoodsType values +('服装内衣'), +('鞋包配饰'), +('手机数码') +go + +insert GoodsInfo values +('提花小西装','红色','菲曼琪','300','1'), +('百搭短裤','绿色','哥弟','100','1'), +('无袖背心','白色','阿依莲','700','1'), +('低帮休闲鞋','红色','菲曼琪','900','2'), +('中跟单鞋','绿色','哥弟','400','2'), +('平底鞋','白色','阿依莲','200','2'), +('迷你照相机','红色','尼康','500','3'), +('硬盘','黑色','希捷','600','3'), +('显卡','黑色','技嘉','800','3') +go + +select top 1 GoodsName,GoodsColor,GoodsMoney 商品价格 +from GoodsInfo order by GoodsMoney desc + +select TypeID,max(GoodsMoney)最高价格,min(GoodsMoney)最低价格,AVG(GoodsMoney)平均价格 from GoodsInfo +group by TypeID + +select GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID from GoodsInfo +where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery2.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..adfdcc718e3e1e6908539e24b38eb483a63db489 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery2.sql" @@ -0,0 +1,55 @@ +use master +go + +create database HOUSE_DB +on +( + name=HOUSE_DB, + filename='D:\ATM.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1% +) +log on +( + name=HOUSE_DB_log, + filename='D:\ATM_log.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1% +) +go + +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int not null primary key identity(1,1), + type_name varchar(50) not null +) +go + +create table HOUSE +( + house_id int not null primary key identity, + house_name varchar(50) not null, + house_price float not null default('0'), + type_id int not null references HOUSE_TYPE(type_id) +) + +insert HOUSE_TYPE values ('小户型'),('经济型'),('别墅') +insert HOUSE values +('小屋','500','1'),('中屋','300','2'),('大屋','200','3') + +select * from HOUSE + +select type_name from HOUSE_TYPE where type_name like '%型' + +select house_name,house_price from HOUSE order by house_price desc + +select distinct house_name,type_name from HOUSE_TYPE +inner join HOUSE on HOUSE_TYPE.type_id=HOUSE_TYPE.type_id + +select top 1 house_name,house_price from HOUSE +order by house_price desc \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6c08f833acf77beaa071ce4d71eef843a79da4ea --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery3.sql" @@ -0,0 +1,59 @@ +use master +go + +create database StarManagerDB +go + +use StarManagerDB +go + +create table StarType +( + T_NO int not null primary key identity, + T_NAME nvarchar(20) +) +go + +create table StarInfo +( + S_NO int not null primary key identity, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) +go + +insert into StarType values +('体育明星'), +('IT明星'), +('相声演员') + +insert into StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) +go + +--查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名 +select top 3 S_NAME,S_HOBBY,S_NATIVE +from StarInfo SI inner join StarType ST on SI.S_T_NO=ST.T_NO +order by S_AGE desc + +--按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select count(S_T_NO)明星人数,avg(S_AGE) 平均年龄 +from StarInfo SI inner join StarType ST on SI.S_T_NO=ST.T_NO +group by T_NAME +having count(S_T_NO)>2 + + +select * from StarInfo +select * from StarType +--查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select S_NAME,S_HOBBY,S_NATIVE +from StarInfo SI inner join StarType ST on SI.S_T_NO=ST.T_NO +where S_T_NO=1 \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery4.sql" "b/2021 04 02 \344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1284c501e322c9a73143b36a04019e06e20e23c3 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery4.sql" @@ -0,0 +1,244 @@ +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + +select StudentName,Birthday,A.Score from StudentInfo S +inner join StudentCourseScore A +on S.Id = A.StudentId and A.CourseId = 2 +inner join StudentCourseScore B +on S.Id = B.StudentId and B.CourseId = 1 +where A.Score>B.Score + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select * from (select StudentId,Score,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 2) B +inner join +(select StudentId,Score,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 1) A +on A.StudentId = B.StudentId +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) + +select * from (select StudentId,Score,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 2) B +left join +(select StudentId,Score,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 1) A +on A.StudentId = B.StudentId +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select * from +(select StudentId,Score from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 1 )A +left join +(select StudentId,Score from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 2) B +on A.StudentId = B.StudentId +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentId,StudentName,avg(Score) from StudentCourseScore SC inner join StudentInfo SI +on SC.StudentId = SI.Id +group by StudentId,StudentName having avg(Score)>60 + + +-- 3.查询在 成绩 表存在成绩的学生信息 +select * from (select StudentId from StudentCourseScore group by StudentId) SC inner join StudentInfo SI on SC.StudentId = SI.Id + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select * from StudentInfo SI left join (select StudentId from StudentCourseScore group by StudentId) SC on SI.Id = SC.StudentId + + +-- 4.1 查有成绩的学生信息 +select * from StudentInfo SI inner join (select StudentId from StudentCourseScore group by StudentId) SC on SI.Id = SC.StudentId + + +-- 5.查询「李」姓老师的数量 +select * from Teachers where TeacherName like '李%' + + +-- 6.查询学过「张三」老师授课的同学的信息 +select * from StudentInfo SI inner join (select StudentId from StudentCourseScore where CourseId = 2 group by StudentId ) SC on SI.Id = SC.StudentId + + +-- 7.查询没有学全所有课程的同学的信息 +select * from +(select * from StudentInfo A +right join +(select StudentId from StudentCourseScore group by StudentId having count(StudentId)<3) C +on A.Id = C.StudentId) Q +full join +(select SI.* from StudentInfo SI left join StudentCourseScore SC on SI.Id = SC.StudentId where Score is null) W +on Q.Id=W.Id +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select * from StudentInfo SI inner join +(select A.StudentId from +(select StudentId,CourseId from StudentCourseScore) A, +(select StudentId,CourseId from StudentCourseScore where StudentId = 1) B +where A.CourseId = B.CourseId and A.StudentId!=1 +group by A.StudentId) A +on SI.Id = A.StudentId +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +select * from StudentInfo SI inner join +(select A.StudentId from +(select StudentId,CourseId from StudentCourseScore) A, +(select StudentId,CourseId from StudentCourseScore where StudentId = 1) B +where A.CourseId = B.CourseId and A.StudentId!=1 +group by A.StudentId having count(A.StudentId)>2) A +on SI.Id = A.StudentId +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select * from StudentInfo SI +left join +(select StudentId from StudentCourseScore where CourseId = 2) S +on SI.Id = S.StudentId +where StudentId is null + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 +select * from StudentInfo + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\226\271\350\215\243\346\230\237/50\351\242\230.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\226\271\350\215\243\346\230\237/50\351\242\230.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ff71c40cbd0c5ffc51b29f5e9ba4d3380df6119a --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\226\271\350\215\243\346\230\237/50\351\242\230.sql" @@ -0,0 +1,294 @@ +use ClassicDb +go + +create database ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + +select D.* from +(select A.StudentId a,A.CourseId b,A.Score c,B.StudentId d,B.CourseId e,B.Score f from +(select * from StudentCourseScore where CourseId = 1) as A , +(select * from StudentCourseScore where CourseId = 2) as B +where A.Score > B.Score and A.StudentId = B.StudentId) as C +inner join StudentInfo D on C.a = D.StudentCode + + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 + +select StudentId,count(CourseId) from StudentCourseScore +where CourseId = 1 or CourseId = 2 +group by StudentId +having count(CourseId) = 2 + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) + +select * from (select * from StudentCourseScore where CourseId = 1) as A +right join (select * from StudentCourseScore where CourseId = 2) as B on A.StudentId = B.StudentId + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 + +select * from (select * from StudentCourseScore where CourseId = 1) as A +left join (select * from StudentCourseScore where CourseId = 2) as B on A.StudentId = B.StudentId + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 + +select StudentId as 学生编号,StudentName as 学生姓名,avg(Score) as 平均成绩 from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +group by StudentId,StudentName +having avg(Score) >= 60 + +-- 3.查询在 成绩表 存在成绩的学生信息 + +select B.* from StudentCourseScore A +left join StudentInfo B on A.StudentId = B.StudentCode + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +select +StudentCode as 学生编号,StudentName as 学生姓名, +count(CourseId) as 选课总数,sum(Score) as 总成绩 +from StudentCourseScore A +right join StudentInfo B on A.StudentId = B.StudentCode +group by StudentCode,StudentName +order by StudentCode + +-- 4.1 查有成绩的学生信息 + +select StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +left join StudentInfo B on A.StudentId = B.StudentCode +group by StudentCode,StudentName,Birthday,Sex,ClassId + +-- 5.查询「李」姓老师的数量 + +select count(*) as 姓老师的数量 from Teachers +group by TeacherName +having TeacherName like ('李%') + + +-- 6.查询学过「张三」老师授课的同学的信息 + +select B.* from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +where A.CourseId = 1 + +-- 7.查询没有学全所有课程的同学的信息 + +select StudentId,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +group by StudentId,StudentName,Birthday,Sex,ClassId +having count(CourseId)<3 + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + +select StudentId,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 1 or CourseId = 2 or CourseId = 3 +group by StudentId,StudentName,Birthday,Sex,ClassId + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + +select StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 1 or CourseId = 2 or CourseId = 3 +group by StudentCode,StudentName,Birthday,Sex,ClassId +having count(CourseId) = 3 + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select StudentName from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +where A.CourseId != 1 +group by StudentCode,StudentName diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\226\271\350\215\243\346\230\237/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\226\271\350\215\243\346\230\237/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..28aaefa3fbef2da2ca7dd4ab0add4c692d422606 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\226\271\350\215\243\346\230\237/SQLQuery1.sql" @@ -0,0 +1,57 @@ +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\GoodsDB.mdf', + size=10, + maxsize=100, + filegrowth=10 +) +log on +( + name='GoodsDB_log', + filename='D:\GoodsDB_log.ldf', + size=10, + maxsize=100, + filegrowth=10 +) +go +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity(1,1), + TypeName nvarchar(20) not null, +) +create table GoodsInfo +( + GoodsID int primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) not null, + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) +insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') +insert into GoodsInfo values('小西装',' 红色' ,'菲曼琪' ,'300' ,'1'), +(' 百搭短裤',' 绿色' ,'哥弟' ,'100 ' ,'1'), +('无袖背心',' 白色' ,' 阿依莲' ,'700 ' ,'1'), +('低帮休闲鞋',' 红色' ,'菲曼琪 ' ,'900 ' ,'2'), +('中跟单鞋',' 绿色' ,'哥弟' ,'400 ' ,'2'), +('平底鞋',' 白色' ,'阿依莲' ,'200 ' ,'2'), +('迷你照相机',' 红色' ,'尼康' ,'500 ' ,'3'), +('硬盘',' 黑色' ,'希捷' ,'600 ' ,'3'), +('显卡',' 黑色' ,'技嘉' ,'800 ' ,'3') + +select * from GoodsType +select * from GoodsInfo + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName as 商品名称,GoodsColor as 商品颜色,GoodsMoney 商品价格 from GoodsInfo order by GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID as 商品类型编号, MAX(GoodsMoney) as 最高价格,MIN(GoodsMoney) as 最低价格,AVG(GoodsMoney) as 平均价格 from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsMoney>=300 and GoodsMoney<=600 and GoodsColor='红色' diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\226\271\350\215\243\346\230\237/SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\226\271\350\215\243\346\230\237/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5393dc72bcb39abc438e08119d66804871b56e95 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\226\271\350\215\243\346\230\237/SQLQuery3.sql" @@ -0,0 +1,67 @@ +--1、创建数据库HOUSE_DB, +--要求: +--指定数据文件大小:5兆, +--指定文件最大值:50兆, +--文件增长率:1兆 + +CREATE database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5, + maxsize=50, + filegrowth=1 +) +log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5, + maxsize=50, + filegrowth=1 +) +go +use HOUSE_DB +go +--2、创建数据表 +--房屋类型表(HOUSE_TYPE) +--字段名 类型 是否可为空 约束 说明 +--type_id int N 主键,自增长 类型编号 +--type_name varchar(50) N 唯一约束 类型名称 +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name varchar(50) not null unique +) +--房屋信息表(HOUSE) +--字段名 类型 是否可为空 约束 说明 +--house_id int N 主键,自增长 房屋编号 +--house_name varchar(50) N 房屋名称 +--house_price float N 默认值0 房租 +--type_id int N 外键依赖HOUSE_TYPE表 房屋类型 +create table HOUSE +( +house_id int primary key identity(1,1), +house_name varchar(50) not null , +house_price float not null default(0), +type_id int references HOUSE_TYPE(type_id) +) +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅' ) +insert into HOUSE values('小红屋',500,'1'),('经济屋',1000,'2'),('别墅屋',5000,'3') +--4、添加查询 +select * from HOUSE_TYPE +select * from HOUSE +--查询所有房屋信息 +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select * from HOUSE order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,type_name 房屋类型名称 from HOUSE A INNER JOIN HOUSE_TYPE B ON A.type_id=B.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price 租金,house_name 房屋名称 from HOUSE order by house_price desc + diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\226\271\350\215\243\346\230\237/SQLQuery4.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\226\271\350\215\243\346\230\237/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..58574be4cf3bca450f4160fc986a4c0bf925dc39 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\226\271\350\215\243\346\230\237/SQLQuery4.sql" @@ -0,0 +1,92 @@ +--1、创建明星数据库(StarManagerDB),然后建立两张表,StarType(明星类型表),StarInfo(明星信息表),表结构分别如下: + + CREATE DATABASE StarManagerDB + ON + ( + name='StarManagerDB', + filename='D:\StarManagerDB.mdf', + size=5, + maxsize=50, + filegrowth=10 + ) + log on + ( + name='StarManagerDB_log', + filename='D:\StarManagerDB_log.ldf', + size=5, + maxsize=50, + filegrowth=10 + ) + go + use StarManagerDB + go + --StarType(明星类型表) +--字段名 说明 类型 长度 可否为空 约束 +--T_NO 明星类型编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--T_NAME 明星类型 nvarchar 20 +create table StarType +( + T_NO int primary key identity(1,1), + T_NAME nvarchar(20) +) + + +--StarInfo(明星信息表) +--字段名 说明 类型 长度 可否为空 约束 +--S_NO 明星编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--S_NAME 明星姓名 nvarchar 20 否 +--S_AGE 明星年龄 int 否 +--S_HOBBY 特技 nvarchar 20 +--S_NATIVE 明星籍贯 nvarchar 20 默认约束:中国大陆 +--S_T_NO 明星类型编号 int 外键,参照StarType表的主键T_NO + +create table StarInfo +( + S_NO int primary key identity not null , + S_NAME nvarchar(20) not null , + S_AGE int not null , + S_HOBBY nvarchar(20) , + S_NATIVE nvarchar(20) default('中国大陆') , + S_T_NO int references StarType(T_NO) +) + + +--2、使用插入语句为两张表添加数据 + +--明星类型表(StarType) +--明星类型编号 明星类型 +--1 体育明星 +--2 IT明星 +--3 相声演员 + +insert StarType values +('体育明星'), +('IT明星'), +('相声演员') +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 + +insert StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) +select * from StarType +select * from StarInfo +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 名字, S_HOBBY 特技, S_NATIVE 籍贯信息 from StarInfo order by S_AGE desc +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO as 类型,count(*) as 人数,avg(S_Age) as 平均年龄 from StarInfo +group by S_T_NO +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo +where S_T_NO = 1 +order by S_AGE desc \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\226\275\346\261\237\345\263\260/50\351\242\230.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\226\275\346\261\237\345\263\260/50\351\242\230.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9bac90be34a0f4bb36897f8eb983e51f323d269d --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\226\275\346\261\237\345\263\260/50\351\242\230.sql" @@ -0,0 +1,245 @@ + +-- 1.鏌ヨ"鏁板 "璇剧▼姣" 璇枃 "璇剧▼鎴愮哗楂樼殑瀛︾敓鐨勪俊鎭強璇剧▼鍒嗘暟 + +select StudentName,Birthday,A.Score from StudentInfo S +inner join StudentCourseScore A +on S.Id = A.StudentId and A.CourseId = 2 +inner join StudentCourseScore B +on S.Id = B.StudentId and B.CourseId = 1 +where A.Score>B.Score + +-- 1.1 鏌ヨ鍚屾椂瀛樺湪" 鏁板 "璇剧▼鍜" 璇枃 "璇剧▼鐨勬儏鍐 +select * from (select StudentId,Score,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 2) B +inner join +(select StudentId,Score,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 1) A +on A.StudentId = B.StudentId +-- 1.2 鏌ヨ瀛樺湪" 鏁板 "璇剧▼浣嗗彲鑳戒笉瀛樺湪" 璇枃 "璇剧▼鐨勬儏鍐(涓嶅瓨鍦ㄦ椂鏄剧ず涓 null ) + +select * from (select StudentId,Score,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 2) B +left join +(select StudentId,Score,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 1) A +on A.StudentId = B.StudentId +-- 1.3 鏌ヨ涓嶅瓨鍦" 鏁板 "璇剧▼浣嗗瓨鍦" 璇枃 "璇剧▼鐨勬儏鍐 +select * from +(select StudentId,Score from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 1 )A +left join +(select StudentId,Score from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 2) B +on A.StudentId = B.StudentId +-- 2.鏌ヨ骞冲潎鎴愮哗澶т簬绛変簬 60 鍒嗙殑鍚屽鐨勫鐢熺紪鍙峰拰瀛︾敓濮撳悕鍜屽钩鍧囨垚缁 +select StudentId,StudentName,avg(Score) from StudentCourseScore SC inner join StudentInfo SI +on SC.StudentId = SI.Id +group by StudentId,StudentName having avg(Score)>60 + + +-- 3.鏌ヨ鍦 鎴愮哗 琛ㄥ瓨鍦ㄦ垚缁╃殑瀛︾敓淇℃伅 +select * from (select StudentId from StudentCourseScore group by StudentId) SC inner join StudentInfo SI on SC.StudentId = SI.Id + + +-- 4.鏌ヨ鎵鏈夊悓瀛︾殑瀛︾敓缂栧彿銆佸鐢熷鍚嶃侀夎鎬绘暟銆佹墍鏈夎绋嬬殑鎬绘垚缁(娌℃垚缁╃殑鏄剧ず涓 null ) +select * from StudentInfo SI left join (select StudentId from StudentCourseScore group by StudentId) SC on SI.Id = SC.StudentId + + +-- 4.1 鏌ユ湁鎴愮哗鐨勫鐢熶俊鎭 +select * from StudentInfo SI inner join (select StudentId from StudentCourseScore group by StudentId) SC on SI.Id = SC.StudentId + + +-- 5.鏌ヨ銆屾潕銆嶅鑰佸笀鐨勬暟閲 +select * from Teachers where TeacherName like '鏉%' + + +-- 6.鏌ヨ瀛﹁繃銆屽紶涓夈嶈佸笀鎺堣鐨勫悓瀛︾殑淇℃伅 +select * from StudentInfo SI inner join (select StudentId from StudentCourseScore where CourseId = 2 group by StudentId ) SC on SI.Id = SC.StudentId + + +-- 7.鏌ヨ娌℃湁瀛﹀叏鎵鏈夎绋嬬殑鍚屽鐨勪俊鎭 +select * from +(select * from StudentInfo A +right join +(select StudentId from StudentCourseScore group by StudentId having count(StudentId)<3) C +on A.Id = C.StudentId) Q +full join +(select SI.* from StudentInfo SI left join StudentCourseScore SC on SI.Id = SC.StudentId where Score is null) W +on Q.Id=W.Id +-- 8.鏌ヨ鑷冲皯鏈変竴闂ㄨ涓庡鍙蜂负" 01 "鐨勫悓瀛︽墍瀛︾浉鍚岀殑鍚屽鐨勪俊鎭 +select * from StudentInfo SI inner join +(select A.StudentId from +(select StudentId,CourseId from StudentCourseScore) A, +(select StudentId,CourseId from StudentCourseScore where StudentId = 1) B +where A.CourseId = B.CourseId and A.StudentId!=1 +group by A.StudentId) A +on SI.Id = A.StudentId +-- 9.鏌ヨ鍜" 01 "鍙风殑鍚屽瀛︿範鐨勮绋 瀹屽叏鐩稿悓鐨勫叾浠栧悓瀛︾殑淇℃伅 +select * from StudentInfo SI inner join +(select A.StudentId from +(select StudentId,CourseId from StudentCourseScore) A, +(select StudentId,CourseId from StudentCourseScore where StudentId = 1) B +where A.CourseId = B.CourseId and A.StudentId!=1 +group by A.StudentId having count(A.StudentId)>2) A +on SI.Id = A.StudentId +-- 10.鏌ヨ娌″杩"寮犱笁"鑰佸笀璁叉巿鐨勪换涓闂ㄨ绋嬬殑瀛︾敓濮撳悕 + +select * from StudentInfo SI +left join +(select StudentId from StudentCourseScore where CourseId = 2) S +on SI.Id = S.StudentId +where StudentId is null + +-- 11.鏌ヨ涓ら棬鍙婂叾浠ヤ笂涓嶅強鏍艰绋嬬殑鍚屽鐨勫鍙凤紝濮撳悕鍙婂叾骞冲潎鎴愮哗 + + + +-- 12.妫绱" 鏁板 "璇剧▼鍒嗘暟灏忎簬 60锛屾寜鍒嗘暟闄嶅簭鎺掑垪鐨勫鐢熶俊鎭 + + + +-- 13.鎸夊钩鍧囨垚缁╀粠楂樺埌浣庢樉绀烘墍鏈夊鐢熺殑鎵鏈夎绋嬬殑鎴愮哗浠ュ強骞冲潎鎴愮哗 +select * from StudentInfo + + +-- 14.鏌ヨ鍚勭鎴愮哗鏈楂樺垎銆佹渶浣庡垎鍜屽钩鍧囧垎锛 + + + +-- 15.浠ュ涓嬪舰寮忔樉绀猴細璇剧▼ ID锛岃绋 name锛屾渶楂樺垎锛屾渶浣庡垎锛屽钩鍧囧垎锛屽強鏍肩巼锛屼腑绛夌巼锛屼紭鑹巼锛屼紭绉鐜 + +/* + + 鍙婃牸涓>=60锛屼腑绛変负锛70-80锛屼紭鑹负锛80-90锛屼紭绉涓猴細>=90 + + + + 瑕佹眰杈撳嚭璇剧▼鍙峰拰閫変慨浜烘暟锛屾煡璇㈢粨鏋滄寜浜烘暟闄嶅簭鎺掑垪锛岃嫢浜烘暟鐩稿悓锛屾寜璇剧▼鍙峰崌搴忔帓鍒 + + + + 鎸夊悇绉戞垚缁╄繘琛屾帓搴忥紝骞舵樉绀烘帓鍚嶏紝 Score 閲嶅鏃朵繚鐣欏悕娆$┖缂 + +*/ + + + +-- 15.1 鎸夊悇绉戞垚缁╄繘琛屾帓搴忥紝骞舵樉绀烘帓鍚嶏紝 Score 閲嶅鏃跺悎骞跺悕娆 + + + +-- 16.鏌ヨ瀛︾敓鐨勬绘垚缁╋紝骞惰繘琛屾帓鍚嶏紝鎬诲垎閲嶅鏃朵繚鐣欏悕娆$┖缂 + + + +-- 16.1 鏌ヨ瀛︾敓鐨勬绘垚缁╋紝骞惰繘琛屾帓鍚嶏紝鎬诲垎閲嶅鏃朵笉淇濈暀鍚嶆绌虹己 + + + +-- 17.缁熻鍚勭鎴愮哗鍚勫垎鏁版浜烘暟锛氳绋嬬紪鍙凤紝璇剧▼鍚嶇О锛孾100-85]锛孾85-70]锛孾70-60]锛孾60-0] 鍙婃墍鍗犵櫨鍒嗘瘮 + + + +-- 18.鏌ヨ鍚勭鎴愮哗鍓嶄笁鍚嶇殑璁板綍 + + + +-- 19.鏌ヨ姣忛棬璇剧▼琚変慨鐨勫鐢熸暟 + + + +-- 20.鏌ヨ鍑哄彧閫変慨涓ら棬璇剧▼鐨勫鐢熷鍙峰拰濮撳悕 + + + +-- 21.鏌ヨ鐢风敓銆佸コ鐢熶汉鏁 + + + +-- 22.鏌ヨ鍚嶅瓧涓惈鏈夈岄銆嶅瓧鐨勫鐢熶俊鎭 + + + +-- 23.鏌ヨ鍚屽悕鍚屾у鐢熷悕鍗曪紝骞剁粺璁″悓鍚嶄汉鏁 + + + +-- 24.鏌ヨ 1990 骞村嚭鐢熺殑瀛︾敓鍚嶅崟 + + + +-- 25.鏌ヨ姣忛棬璇剧▼鐨勫钩鍧囨垚缁╋紝缁撴灉鎸夊钩鍧囨垚缁╅檷搴忔帓鍒楋紝骞冲潎鎴愮哗鐩稿悓鏃讹紝鎸夎绋嬬紪鍙峰崌搴忔帓鍒 + + + +-- 26.鏌ヨ骞冲潎鎴愮哗澶т簬绛変簬 85 鐨勬墍鏈夊鐢熺殑瀛﹀彿銆佸鍚嶅拰骞冲潎鎴愮哗 + + + +-- 27.鏌ヨ璇剧▼鍚嶇О涓恒屾暟瀛︺嶏紝涓斿垎鏁颁綆浜 60 鐨勫鐢熷鍚嶅拰鍒嗘暟 + + + +-- 28.鏌ヨ鎵鏈夊鐢熺殑璇剧▼鍙婂垎鏁版儏鍐碉紙瀛樺湪瀛︾敓娌℃垚缁╋紝娌¢夎鐨勬儏鍐碉級 + + + +-- 29.鏌ヨ浠讳綍涓闂ㄨ绋嬫垚缁╁湪 70 鍒嗕互涓婄殑濮撳悕銆佽绋嬪悕绉板拰鍒嗘暟 + + + +-- 30.鏌ヨ涓嶅強鏍肩殑璇剧▼ + + + +-- 31.鏌ヨ璇剧▼缂栧彿涓 01 涓旇绋嬫垚缁╁湪 80 鍒嗕互涓婄殑瀛︾敓鐨勫鍙峰拰濮撳悕 + + + +-- 32.姹傛瘡闂ㄨ绋嬬殑瀛︾敓浜烘暟 + + + +-- 33.鎴愮哗涓嶉噸澶嶏紝鏌ヨ閫変慨銆屽紶涓夈嶈佸笀鎵鎺堣绋嬬殑瀛︾敓涓紝鎴愮哗鏈楂樼殑瀛︾敓淇℃伅鍙婂叾鎴愮哗 + + + +--34.鎴愮哗鏈夐噸澶嶇殑鎯呭喌涓嬶紝鏌ヨ閫変慨銆屽紶涓夈嶈佸笀鎵鎺堣绋嬬殑瀛︾敓涓紝鎴愮哗鏈楂樼殑瀛︾敓淇℃伅鍙婂叾鎴愮哗 + + + +-- 35.鏌ヨ涓嶅悓璇剧▼鎴愮哗鐩稿悓鐨勫鐢熺殑瀛︾敓缂栧彿銆佽绋嬬紪鍙枫佸鐢熸垚缁 + + + +-- 36.鏌ヨ姣忛棬鍔熸垚缁╂渶濂界殑鍓嶄袱鍚 + + + +-- 37.缁熻姣忛棬璇剧▼鐨勫鐢熼変慨浜烘暟锛堣秴杩 5 浜虹殑璇剧▼鎵嶇粺璁★級銆 + + + +-- 38.妫绱㈣嚦灏戦変慨涓ら棬璇剧▼鐨勫鐢熷鍙 + + + +-- 39.鏌ヨ閫変慨浜嗗叏閮ㄨ绋嬬殑瀛︾敓淇℃伅 + + + +-- 40.鏌ヨ鍚勫鐢熺殑骞撮緞锛屽彧鎸夊勾浠芥潵绠 + + + +-- 41.鎸夌収鍑虹敓鏃ユ湡鏉ョ畻锛屽綋鍓嶆湀鏃 < 鍑虹敓骞存湀鐨勬湀鏃ュ垯锛屽勾榫勫噺涓 + + + +-- 42.鏌ヨ鏈懆杩囩敓鏃ョ殑瀛︾敓 + + + +-- 43.鏌ヨ涓嬪懆杩囩敓鏃ョ殑瀛︾敓 + + + +-- 44.鏌ヨ鏈湀杩囩敓鏃ョ殑瀛︾敓 + + + +-- 45.鏌ヨ涓嬫湀杩囩敓鏃ョ殑瀛︾敓 \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\226\275\346\261\237\345\263\260/\345\244\215\344\271\240\351\242\2301.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\226\275\346\261\237\345\263\260/\345\244\215\344\271\240\351\242\2301.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fad9a153504205e3f59febcd08c697f6e39c0770 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\226\275\346\261\237\345\263\260/\345\244\215\344\271\240\351\242\2301.sql" @@ -0,0 +1,39 @@ +create database GoodDB +go +use GoodDB +go + +create table GoodsType +( + TypeID int not null primary key identity , + TypeName nvarchar(20) not null +) + +create table GoodInfo +( + GoodsID int primary key identity not null, + GiidsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeId int foreign key references GoodsType(TypeID) +) +go +insert into GoodsType(TypeName) +select '鏈嶈鍐呰。' union +select '闉嬪寘閰嶉グ' union +select '鎵嬫満鏁扮爜' + +insert into GoodInfo(GiidsName,GoodsColor,GoodsBrand,GoodsMoney,TypeId) +values('鎻愯姳灏忚タ瑁','绾㈣壊','鑿叉浖鐞',300,1),('鐧炬惌鐭¥','缁胯壊','鍝ュ紵',100,1),('鏃犺鑳屽績','鐧借壊','闃夸緷鑾',700,1), +('浣庡府浼戦棽闉','绾㈣壊','鑿叉浖鐞',900,2),('涓窡鍗曢瀷','缁胯壊','鍝ュ紵',400,2),('骞冲簳闉','鐧借壊','闃夸緷鑾',200,2), +('杩蜂綘鐓х浉鏈','绾㈣壊','灏煎悍',500,3),('纭洏','榛戣壊','甯屾嵎',600,3),('鏄惧崱','榛戣壊','鎶鍢',800,3) +go +--3銆佹煡璇环鏍兼渶璐电殑鍟嗗搧鍚嶇О锛屽晢鍝侀鑹插拰鍟嗗搧浠锋牸锛岃姹備娇鐢ㄥ埆鍚嶆樉绀哄垪鍚 +select GiidsName 鍟嗗搧鍚嶇О,GoodsColor 鍟嗗搧棰滆壊,GoodsMoney 鍟嗗搧浠锋牸 from GoodInfo where GoodsMoney = (select max(GoodsMoney) from GoodInfo) + +--4銆佹寜鍟嗗搧绫诲瀷缂栧彿鍒嗙粍鏌ヨ鍟嗗搧鏈楂樹环鏍硷紝鏈浣庝环鏍煎拰骞冲潎浠锋牸锛岃姹備娇鐢ㄥ埆鍚嶆樉绀哄垪鍚 +select TypeID 鍟嗗搧绫诲瀷,max(GoodsMoney) 鏈楂樹环鏍,min(GoodsMoney) 鏈浣庝环鏍 ,avg(GoodsMoney) 骞冲潎浠锋牸 from GoodInfo group by TypeID + +--5銆佹煡璇㈠晢鍝佷俊鎭墍鏈夊垪锛岃姹傚晢鍝侀鑹蹭负绾㈣壊锛屼环鏍煎湪300~600涔嬮棿 +select * from GoodInfo where GoodsColor='绾㈣壊' and GoodsMoney between 300 and 600 \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\226\275\346\261\237\345\263\260/\345\244\215\344\271\240\351\242\2302.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\226\275\346\261\237\345\263\260/\345\244\215\344\271\240\351\242\2302.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ff5d0b0a5a64df98273b68b54faff8835d0e74a1 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\226\275\346\261\237\345\263\260/\345\244\215\344\271\240\351\242\2302.sql" @@ -0,0 +1,50 @@ +create database HOUSE_DB +on +( + name = 'HOUSE_DB', + filename = 'D:\HOUSE_DB.mdf', + size = 5, + maxsize= 50, + filegrowth = 10% +) +log on +( + name= 'HOUSE_DB_log', + filename = 'D:\HOUSE_DB_log.ldf', + size = 5, + maxsize= 50, + filegrowth = 10% +) +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity, + type_name varchar(50) unique +) +create table HOUSE +( + house_id int primary key identity , + house_name varchar(50), + house_price float default (0), + type_id int foreign key references HOUSE_TYPE(type_id) +) +go +insert into HOUSE_TYPE(type_name) +select '灏忔埛鍨' union select '缁忔祹鍨' union select '鍒' + +insert into HOUSE(house_name,house_price,type_id) +values('楸',40,1),('榫',45,2),('铏',60,3) +--go +--鏌ヨ鎵鏈夋埧灞嬩俊鎭 +select * from HOUSE +--浣跨敤妯$硦鏌ヨ鍖呭惈鈥濆瀷鈥滃瓧鐨勬埧灞嬬被鍨嬩俊鎭 +select * from HOUSE inner join HOUSE_TYPE on HOUSE_TYPE.type_id = HOUSE.type_id +where type_name like '%鍨%' +--鏌ヨ鍑烘埧灞嬬殑鍚嶇О鍜岀閲戯紝骞朵笖鎸夌収绉熼噾闄嶅簭鎺掑簭 +select house_name,house_price from HOUSE order by house_price desc +--浣跨敤杩炴帴鏌ヨ锛屾煡璇俊鎭紝鏄剧ず鎴垮眿鍚嶇О鍜屾埧灞嬬被鍨嬪悕绉 +select house_name,type_name from HOUSE inner join HOUSE_TYPE on HOUSE_TYPE.type_id = HOUSE.type_id +--鏌ヨ鎵鏈夋埧灞嬩腑鏈堢鏈楂樼殑鎴垮眿锛屾樉绀烘渶楂樼殑绉熼噾鍜屾埧灞嬪悕绉 +select house_price,house_name from HOUSE where house_price = (select max(house_price) from HOUSE ) diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\226\275\346\261\237\345\263\260/\345\244\215\344\271\240\351\242\2303.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\226\275\346\261\237\345\263\260/\345\244\215\344\271\240\351\242\2303.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b8c51f286d86aa87da3b98a0be3babe29cd02145 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\226\275\346\261\237\345\263\260/\345\244\215\344\271\240\351\242\2303.sql" @@ -0,0 +1,36 @@ +create database StarManagerDB +use StarManagerDB +go + +create table StarType +( + T_NO int primary key identity , + T_NAME nvarchar(20) +) +create table StarInfo +( + S_NO int primary key identity, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20) , + S_NATIVE nvarchar(20) default ('涓浗澶ч檰'), + S_T_NO int foreign key references StarType(T_NO) +) +go +insert into StarType(T_NAME) +select '浣撹偛鏄庢槦'union +select 'IT鏄庢槦'union +select '鐩稿0婕斿憳' + +insert into StarInfo(S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) +values ('姊呰タ',30,'灏勯棬','闃挎牴寤',1),('绉戞瘮',35,'杩囦汉','缇庡浗',2),('钄℃櫙鐜',40,'鏁蹭唬鐮','涓浗',2) +,('椹厠鏂',36,'閫犵伀绠','澶栨槦浜',2),('閮痉绾',50,'鐩稿0','涓浗',3),('榛勫偿',41,'鎷煎澶','涓浗',2) + +--3銆佹煡璇㈠勾榫勬渶澶х殑3涓槑鏄熺殑濮撳悕锛岀壒鎶鍜岀睄璐俊鎭紝瑕佹眰浣跨敤鍒悕鏄剧ず鍒楀悕銆 +select top 3 S_NAME 濮撳悕,S_HOBBY 绫嶈疮淇℃伅,S_NATIVE 鐗规妧 from StarInfo order by S_AGE desc +--4銆佹寜鏄庢槦绫诲瀷缂栧彿鍒嗙被鏌ヨ鏄庢槦浜烘暟锛屾槑鏄熷钩鍧囧勾榫勶紝鏄剧ず鏄庢槦浜烘暟澶т簬2鐨勫垎缁勪俊鎭紝瑕佹眰浣跨敤鍒悕鏄剧ず鍒楀悕銆 +select S_T_NO,count(*)鏄庢槦浜烘暟,avg(S_AGE)骞冲潎骞撮緞 from StarInfo group by S_T_NO having count(S_T_NO)>1 +--5銆佹煡璇㈡槑鏄熺被鍨嬩负鈥滀綋鑲叉槑鏄熲濅腑骞撮緞鏈澶х殑濮撳悕銆佺壒鎶銆佺睄璐俊鎭紝瑕佹眰鏄剧ず鍒楀埆鍚嶃 +select S_NAME 濮撳悕,S_HOBBY 鐗规妧,S_NATIVE 绫嶈疮 from StarInfo where S_AGE=(select max(S_AGE) from StarInfo where S_T_NO = 1) + +select * from StarInfo \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/50\351\242\230.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/50\351\242\230.sql" new file mode 100644 index 0000000000000000000000000000000000000000..31f24438f08bd6c22fbfb48b43006beb0e7e20a0 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/50\351\242\230.sql" @@ -0,0 +1,245 @@ + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + +select StudentName,Birthday,A.Score from StudentInfo S +inner join StudentCourseScore A +on S.Id = A.StudentId and A.CourseId = 2 +inner join StudentCourseScore B +on S.Id = B.StudentId and B.CourseId = 1 +where A.Score>B.Score + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select * from (select StudentId,Score,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 2) B +inner join +(select StudentId,Score,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 1) A +on A.StudentId = B.StudentId +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) + +select * from (select StudentId,Score,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 2) B +left join +(select StudentId,Score,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 1) A +on A.StudentId = B.StudentId +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select * from +(select StudentId,Score from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 1 )A +left join +(select StudentId,Score from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 2) B +on A.StudentId = B.StudentId +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentId,StudentName,avg(Score) from StudentCourseScore SC inner join StudentInfo SI +on SC.StudentId = SI.Id +group by StudentId,StudentName having avg(Score)>60 + + +-- 3.查询在 成绩 表存在成绩的学生信息 +select * from (select StudentId from StudentCourseScore group by StudentId) SC inner join StudentInfo SI on SC.StudentId = SI.Id + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select * from StudentInfo SI left join (select StudentId from StudentCourseScore group by StudentId) SC on SI.Id = SC.StudentId + + +-- 4.1 查有成绩的学生信息 +select * from StudentInfo SI inner join (select StudentId from StudentCourseScore group by StudentId) SC on SI.Id = SC.StudentId + + +-- 5.查询「李」姓老师的数量 +select * from Teachers where TeacherName like '李%' + + +-- 6.查询学过「张三」老师授课的同学的信息 +select * from StudentInfo SI inner join (select StudentId from StudentCourseScore where CourseId = 2 group by StudentId ) SC on SI.Id = SC.StudentId + + +-- 7.查询没有学全所有课程的同学的信息 +select * from +(select * from StudentInfo A +right join +(select StudentId from StudentCourseScore group by StudentId having count(StudentId)<3) C +on A.Id = C.StudentId) Q +full join +(select SI.* from StudentInfo SI left join StudentCourseScore SC on SI.Id = SC.StudentId where Score is null) W +on Q.Id=W.Id +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select * from StudentInfo SI inner join +(select A.StudentId from +(select StudentId,CourseId from StudentCourseScore) A, +(select StudentId,CourseId from StudentCourseScore where StudentId = 1) B +where A.CourseId = B.CourseId and A.StudentId!=1 +group by A.StudentId) A +on SI.Id = A.StudentId +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +select * from StudentInfo SI inner join +(select A.StudentId from +(select StudentId,CourseId from StudentCourseScore) A, +(select StudentId,CourseId from StudentCourseScore where StudentId = 1) B +where A.CourseId = B.CourseId and A.StudentId!=1 +group by A.StudentId having count(A.StudentId)>2) A +on SI.Id = A.StudentId +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select * from StudentInfo SI +left join +(select StudentId from StudentCourseScore where CourseId = 2) S +on SI.Id = S.StudentId +where StudentId is null + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 +select * from StudentInfo + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/\345\244\215\344\271\240\351\242\2301.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/\345\244\215\344\271\240\351\242\2301.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cfcb28b918285b1c6a3c1c9fdc6664aa679f68b2 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/\345\244\215\344\271\240\351\242\2301.sql" @@ -0,0 +1,40 @@ +create database GoodDB +go +use GoodDB +go + +create table GoodsType +( + TypeID int not null primary key identity , + TypeName nvarchar(20) not null +) + +create table GoodInfo +( + GoodsID int primary key identity not null, + GiidsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeId int foreign key references GoodsType(TypeID) +) +go +insert into GoodsType(TypeName) +select '服装内衣' union +select '鞋包配饰' union +select '手机数码' + +insert into GoodInfo(GiidsName,GoodsColor,GoodsBrand,GoodsMoney,TypeId) +values('提花小西装','红色','菲曼琪',300,1),('百搭短裤','绿色','哥弟',100,1),('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2),('中跟单鞋','绿色','哥弟',400,2),('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3),('硬盘','黑色','希捷',600,3),('显卡','黑色','技嘉',800,3) +go +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GiidsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodInfo where GoodsMoney = (select max(GoodsMoney) from GoodInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格 ,avg(GoodsMoney) 平均价格 from GoodInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 + diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/\345\244\215\344\271\240\351\242\2302.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/\345\244\215\344\271\240\351\242\2302.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6d3ca1a6fb53e730b1eb4ba74f0cd43a0cb5fa79 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/\345\244\215\344\271\240\351\242\2302.sql" @@ -0,0 +1,51 @@ +create database HOUSE_DB +on +( + name = 'HOUSE_DB', + filename = 'D:\HOUSE_DB.mdf', + size = 5, + maxsize= 50, + filegrowth = 10% +) +log on +( + name= 'HOUSE_DB_log', + filename = 'D:\HOUSE_DB_log.ldf', + size = 5, + maxsize= 50, + filegrowth = 10% +) +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity, + type_name varchar(50) unique +) +create table HOUSE +( + house_id int primary key identity , + house_name varchar(50), + house_price float default (0), + type_id int foreign key references HOUSE_TYPE(type_id) +) +go +insert into HOUSE_TYPE(type_name) +select '小户型' union select '经济型' union select '别墅' + +insert into HOUSE(house_name,house_price,type_id) +values('鱼',40,1),('龟',45,2),('虾',60,3) +--go +--查询所有房屋信息 +select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE inner join HOUSE_TYPE on HOUSE_TYPE.type_id = HOUSE.type_id +where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name,type_name from HOUSE inner join HOUSE_TYPE on HOUSE_TYPE.type_id = HOUSE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select house_price,house_name from HOUSE where house_price = (select max(house_price) from HOUSE ) + diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/\345\244\215\344\271\240\351\242\2303.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/\345\244\215\344\271\240\351\242\2303.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5361c99fd94b4f8be426792b8b4b56e3d11e56be --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/\345\244\215\344\271\240\351\242\2303.sql" @@ -0,0 +1,36 @@ +create database StarManagerDB +use StarManagerDB +go + +create table StarType +( + T_NO int primary key identity , + T_NAME nvarchar(20) +) +create table StarInfo +( + S_NO int primary key identity, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20) , + S_NATIVE nvarchar(20) default ('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) +go +insert into StarType(T_NAME) +select '体育明星'union +select 'IT明星'union +select '相声演员' + +insert into StarInfo(S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) +values ('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',2),('蔡景现',40,'敲代码','中国',2) +,('马克斯',36,'造火箭','外星人',2),('郭德纲',50,'相声','中国',3),('黄峥',41,'拼多多','中国',2) + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 籍贯信息,S_NATIVE 特技 from StarInfo order by S_AGE desc +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO,count(*)明星人数,avg(S_AGE)平均年龄 from StarInfo group by S_T_NO having count(S_T_NO)>1 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo where S_AGE=(select max(S_AGE) from StarInfo where S_T_NO = 1) + +select * from StarInfo \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/SQLQuery0402\344\270\200.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/SQLQuery0402\344\270\200.sql" new file mode 100644 index 0000000000000000000000000000000000000000..79bfc95b4e10088fa7102670a3abe0218d413863 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/SQLQuery0402\344\270\200.sql" @@ -0,0 +1,67 @@ +use master +go + +create database GoodsDB +on +( + name = 'GoodsDB', + filename = 'D:\GoodsDB.mdf', + size = 5, + maxsize = 50, + filegrowth = 10% +) +log on +( + name = 'GoodsDB_log', + filename = 'D:\GoodsDB_log.ldf', + size = 5, + maxsize = 50, + filegrowth = 10% +) +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null, +) +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) +insert GoodsType values +('服装内衣'),('鞋包配饰'),('手机数码') +go + +insert GoodsInfo values +('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) +go + +--查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName as 商品名称 , GoodsColor as 商品颜色 , GoodsMoney as 商品价格 from GoodsInfo +order by GoodsMoney desc +go + +--按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select MAX(GoodsMoney) as 最高价格 , MIN(GoodsMoney) as 最低价格 , AVG(GoodsMoney) as 平均价格 from GoodsInfo +group by TypeID +go + +--查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo +where GoodsColor = '红色' and GoodsMoney>=300 and GoodsMoney<=600 +go diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/SQLQuery0402\344\270\211.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/SQLQuery0402\344\270\211.sql" new file mode 100644 index 0000000000000000000000000000000000000000..72694c119710367c5730bd8a44adf2890d25f56b --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/SQLQuery0402\344\270\211.sql" @@ -0,0 +1,68 @@ +use master +go + +create database StarManagerDB +on +( + name = 'StarManagerDB', + filename = 'D:\StarManagerDB.mdf', + size = 5, + maxsize = 50, + filegrowth = 10% +) +log on +( + name = 'StarManagerDB_log', + filename = 'D:\StarManagerDB_log.ldf', + size = 5, + maxsize = 50, + filegrowth = 10% +) +use StarManagerDB +go + +create table StarType +( + T_NO int primary key identity(1,1) not null, + T_NAME nvarchar(20) +) +create table StarInfo +( + S_NO int primary key identity(1,1) not null, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) + +insert StarType values +('体育明星'), +('IT明星'), +('相声演员') +go + +insert StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) +go + +--查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo +order by S_AGE desc +go + +--按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO as 类型,count(*) as 人数,avg(S_Age) as 平均年龄 from StarInfo +group by S_T_NO +go + +--查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo +where S_T_NO = 1 +order by S_AGE desc + diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/SQLQuery0402\344\272\214.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/SQLQuery0402\344\272\214.sql" new file mode 100644 index 0000000000000000000000000000000000000000..38fdd29e3189e101bcda1122de55fc8f311ff84f --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/SQLQuery0402\344\272\214.sql" @@ -0,0 +1,61 @@ +use master +go + +create database HOUSE_DB +on +( + name = 'HOUSE_DB', + filename = 'D:\HOUSE_DB.mdf', + size = 5, + maxsize = 50, + filegrowth = 10% +) +log on +( + name = 'HOUSE_DB_log', + filename = 'D:\HOUSE_DB_log.ldf', + size = 5, + maxsize = 50, + filegrowth = 10% +) +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_Id int primary key identity(1,1) not null, + type_name varchar(50) not null +) +create table HOUSE +( + house_id int primary key identity(1,1) not null, + house_name varchar(50) not null, + house_price float default(0) not null, + type_id int references HOUSE_TYPE(type_Id) not null +) +insert HOUSE_TYPE values +('小户型'),('经济型'),('别墅') +go + +insert HOUSE values +('学区房' , '5000' , 1), +('海景房' , '8000' , 3), +('安全屋' , '4000' , 2) +go + +--查询所有房屋信息 +select * from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like('%型') + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name as 名称 , house_price as 租金 from HOUSE +order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name type_name from HOUSE A inner join HOUSE_TYPE B on A.type_id = B.type_Id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_name as 名称 , house_price as 租金 from HOUSE +order by house_price desc \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/SQLQuery0402\344\272\224\345\215\201\351\242\230.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/SQLQuery0402\344\272\224\345\215\201\351\242\230.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2390a10cceecb4b9608b4d9dcf1c41be0a7f210c --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/SQLQuery0402\344\272\224\345\215\201\351\242\230.sql" @@ -0,0 +1,289 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + +select * from CourseInfo +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 + +select StudentId,count(CourseId) from StudentCourseScore +where CourseId = 1 or CourseId = 2 +group by StudentId +having count(CourseId) = 2 + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) + +select * from (select * from StudentCourseScore where CourseId = 1) as A +right join (select * from StudentCourseScore where CourseId = 2) as B on A.StudentId = B.StudentId + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 + +select * from (select * from StudentCourseScore where CourseId = 1) as A +left join (select * from StudentCourseScore where CourseId = 2) as B on A.StudentId = B.StudentId + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 + +select StudentId as 学生编号,StudentName as 学生姓名,avg(Score) as 平均成绩 from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +group by StudentId,StudentName +having avg(Score) >= 60 + +-- 3.查询在 成绩表 存在成绩的学生信息 + +select B.* from StudentCourseScore A +left join StudentInfo B on A.StudentId = B.StudentCode + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +select +StudentCode as 学生编号,StudentName as 学生姓名, +count(CourseId) as 选课总数,sum(Score) as 总成绩 +from StudentCourseScore A +right join StudentInfo B on A.StudentId = B.StudentCode +group by StudentCode,StudentName +order by StudentCode + +-- 4.1 查有成绩的学生信息 + +select StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +left join StudentInfo B on A.StudentId = B.StudentCode +group by StudentCode,StudentName,Birthday,Sex,ClassId + +-- 5.查询「李」姓老师的数量 + +select count(*) as 姓老师的数量 from Teachers +group by TeacherName +having TeacherName like ('李%') + + +-- 6.查询学过「张三」老师授课的同学的信息 + +select B.* from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +where A.CourseId = 1 + +-- 7.查询没有学全所有课程的同学的信息 + +select StudentId,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +group by StudentId,StudentName,Birthday,Sex,ClassId +having count(CourseId)<3 + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + +select StudentId,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 1 or CourseId = 2 or CourseId = 3 +group by StudentId,StudentName,Birthday,Sex,ClassId + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + +select StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 1 or CourseId = 2 or CourseId = 3 +group by StudentCode,StudentName,Birthday,Sex,ClassId +having count(CourseId) = 3 + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select StudentName from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +where A.CourseId != 1 +group by StudentCode,StudentName \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2e6f7c933190ceafcbe0b311805e313408048475 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/SQLQuery1.sql" @@ -0,0 +1,369 @@ +create database ClassicDb + +GO +use ClassicDb + +GO +create table StudentInfo + +( + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null +) + +GO +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + +GO + +CREATE TABLE Teachers + +( + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) +) + + + +go +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) +go +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) +GO +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go +-- 练习题目: + select * from StudentInfo +Select * from Teachers +select * from CourseInfo +select * from StudentCourseScore +-- 1.查询"2数学 "课程比" 1语文 "课程成绩高的学生的信息及课程分数 +select a.*,b.CourseId,b.Score 语文分数,d.Score 数学分数 from StudentInfo a inner join StudentCourseScore b on a.StudentCode=b.StudentId and b.CourseId=1 +inner join StudentCourseScore d on a.StudentCode=d.StudentId and d.CourseId=2 where d.Score>b.Score +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select StudentCode,StudentName from StudentInfo a inner join StudentCourseScore b on a.StudentCode=b.StudentId +where CourseId=1 or CourseId=2 group by StudentCode,StudentId,StudentName having count(CourseId)=2 +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select * from (select * from StudentCourseScore where CourseId = 1) a +right join (select * from StudentCourseScore where CourseId = 2) b on a.StudentId = b.StudentId +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select a.*,c.CourseName from StudentInfo a inner join StudentCourseScore b on a.StudentCode=b.StudentId +inner join CourseInfo c on b.CourseId=c.Id where CourseId<>2 +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentCode 学生编号,StudentName 学生姓名,avg(Score) 平均成绩 from StudentInfo a inner join StudentCourseScore b on a.StudentCode=b.StudentId group by StudentCode,StudentName having avg(Score)>60 +-- 3.查询在 成绩 表存在成绩的学生信息 +select distinct a.* from StudentInfo a inner join StudentCourseScore b on a.StudentCode=b.StudentId +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select StudentCode,StudentName,count(CourseId) 选课数量,sum(Score) 总成绩 from StudentInfo a left join StudentCourseScore b on a.StudentCode=b.StudentId group by StudentCode,StudentName order by StudentCode +-- 4.1 查有成绩的学生信息 + +select StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +left join StudentInfo B on A.StudentId = B.StudentCode +group by StudentCode,StudentName,Birthday,Sex,ClassId + +-- 5.查询「李」姓老师的数量 + +select count(*) as 姓老师的数量 from Teachers +group by TeacherName +having TeacherName like ('李%') + +-- 6.查询学过「张三」老师授课的同学的信息 + +select B.* from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +where A.CourseId = 1 + +-- 7.查询没有学全所有课程的同学的信息 + +select StudentId,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +group by StudentId,StudentName,Birthday,Sex,ClassId +having count(CourseId)<3 + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select StudentId,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 1 or CourseId = 2 or CourseId = 3 +group by StudentId,StudentName,Birthday,Sex,ClassId + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +select StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 1 or CourseId = 2 or CourseId = 3 +group by StudentCode,StudentName,Birthday,Sex,ClassId +having count(CourseId) = 3 + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select StudentName from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +where A.CourseId != 1 +group by StudentCode,StudentName +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/\345\244\215\344\271\240\351\242\230\344\275\234\344\270\232/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/\345\244\215\344\271\240\351\242\230\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c99a8a873b20c4750e2c36acec731a044128fba4 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/\345\244\215\344\271\240\351\242\230\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,39 @@ +create database GoodsDB +use GoodsDB +go +create table GoodsType +( + TypeID int not null primary key identity(1,1), --商品类型编号 + TypeName nvarchar(20) not null --商品类型名称 +) +go +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) +insert into GoodsType(TypeName) values ('服饰内衣'),('鞋包服饰'),('手机数码') +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +select '提花小西装','红色','菲曼斯',300,1 union +select '百搭短袖','绿色','哥弟',100,1 union +select '无袖背心','白色','阿依莲',700,1 union +select '低帮休闲鞋','红色','菲曼斯',900,2 union +select '中跟单鞋','绿色','哥弟',400,2 union +select '平底鞋','白色','阿依莲',200,2 union +select '迷你照相机','红色','尼康',500,3 union +select '硬盘','黑色','希捷',600,3 union +select '显卡','黑色','技嘉',800,3 + +select * from GoodsType +select * from GoodsInfo +--3.查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo order by GoodsMoney desc +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo where GoodsMoney=(select max(GoodsMoney) from GoodsInfo) +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/\345\244\215\344\271\240\351\242\230\344\275\234\344\270\232/SQLQuery2.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/\345\244\215\344\271\240\351\242\230\344\275\234\344\270\232/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b5ca32ce091a939abbb1ba675bb04b433f67f836 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/\345\244\215\344\271\240\351\242\230\344\275\234\344\270\232/SQLQuery2.sql" @@ -0,0 +1,47 @@ +create database HOUSE_DB +on +( + name = 'HOUSE_DB', + size = 5, + maxsize = 50, + filegrowth = 1, + filename = 'D:\新建文件夹\HOUSE_DB.mdf' +) +log on +( + name = 'HOUSE_DB_log', + size = 5, + maxsize = 50, + filegrowth = 1, + filename = 'D:\新建文件夹\HOUSE_DB_log.ldf' +) +use HOUSE_DB +go +create table HOUSE_TYPE +( + type_id int not null primary key identity(1,1), + type_name varchar(50) not null unique, +) +go +create table HOUSE +( + house_id int not null primary key identity(1,1), + house_name varchar(50) not null , + house_price float not null default(0), + type_id int not null references HOUSE_TYPE(type_id) +) +insert into HOUSE_TYPE(type_name) values('小户型'),('经济型'),('别墅') +insert into HOUSE(house_name,house_price,type_id) values ('别',1000,3),('墅',100,3),('经',89.2,2),('济',223.3,2),('小',43.2,1),('户',1222.2,1) +select * from HOUSE_TYPE +select * from HOUSE + +--查询所有房屋信息 +select a.*,b.type_name from HOUSE a inner join HOUSE_TYPE b on a.type_id=b.type_id +--使用模糊查询包含”型“字的房屋类型信息 +select a.*,b.type_name from HOUSE a inner join HOUSE_TYPE b on a.type_id=b.type_id where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select a.house_name,b.type_name from HOUSE a inner join HOUSE_TYPE b on a.type_id=b.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select house_price,house_name from HOUSE where house_price=(select max(house_price) from HOUSE) diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/\345\244\215\344\271\240\351\242\230\344\275\234\344\270\232/SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/\345\244\215\344\271\240\351\242\230\344\275\234\344\270\232/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6f3735aa642683fd7095f25012de0bac1f8173a2 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/\345\244\215\344\271\240\351\242\230\344\275\234\344\270\232/SQLQuery3.sql" @@ -0,0 +1,51 @@ +create database StarManagerDB +on +( + name = 'StarManagerDB', + size = 5, + maxsize =500, + filename ='D:\新建文件夹\StarManagerDB.mdf', + filegrowth=10% +) +log on +( + name = 'StarManagerDB_log', + size = 5, + maxsize =500, + filename ='D:\新建文件夹\StarManagerDB_log.ldf', + filegrowth=10% +) +use StarManagerDB +go +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) +go +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) + +insert into StarType(T_NAME) values ('体育明星'),('IT体育明星'),('相声演员') +insert into StarInfo(S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) +select '梅西',30,'射门','阿根廷',1 union +select '科比',35,'过人','美国',1 union +select '茶经先',40,'敲代码','中国',2 union +select '马什克',36,'找火箭','外星人',2 union +select '郭德纲',50,'相声','中国',3 union +select '黄峥',41,'拼多多','中国',2 +select * from StarType +select * from StarInfo +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo order by S_AGE desc +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select avg(S_AGE) 平均年龄,count(S_T_NO) 该类型人数,S_T_NO 明星类型编号 from StarInfo group by S_T_NO having count(S_T_NO)>2 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 a.S_NAME 姓名,a.S_HOBBY 特技,a.S_NATIVE 籍贯,a.S_AGE 年龄 from StarInfo a inner join StarType b on a.S_T_NO=b.T_NO where b.T_NAME='体育明星' order by S_AGE desc diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery2.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3b2d86c8ad5098e9866088bd45ece188b44856a7 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery2.sql" @@ -0,0 +1,417 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 + + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) + + + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 + + + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select studentInfo.Id 学生编号,studentName 学生姓名,sum(Score) 平均成绩 from StudentInfo +inner join StudentCourseScore on StudentCourseScore.Id=StudentInfo.Id group by studentInfo.Id,studentName HAVING SUM(Score)>60 + +-- 3.查询在 成绩 表存在成绩的学生信息 +select distinct studentInfo.Id 学生编号,studentName 学生姓名,birthday 生日,Sex 性别,ClassId 班级 from StudentInfo +inner join StudentCourseScore on StudentCourseScore.StudentId=StudentInfo.Id + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select studentCode 学生编号,StudentName 学生姓名,COUNT(courseId) 选课总数,SUM(score) from StudentInfo +left join StudentCourseScore on StudentCourseScore.StudentId=StudentInfo.Id +group by StudentCode,StudentName order by studentCode + +-- 4.1 查有成绩的学生信息 +select distinct studentInfo.Id 学生编号,studentName 学生姓名,birthday 生日,Sex 性别,ClassId 班级 from StudentInfo +inner join StudentCourseScore on StudentCourseScore.StudentId=StudentInfo.Id + +-- 5.查询「李」姓老师的数量 +select * from Teachers where TeacherName like'%李_%' + +-- 6.查询学过「张三」老师授课的同学的信息 +select StudentCode 学生编号,StudentName 学生名字,Birthday 生日,Sex 性别,ClassId 班级 from StudentCourseScore +inner join StudentInfo on StudentInfo.Id=StudentCourseScore.StudentId +inner join CourseInfo on CourseInfo.Id=StudentCourseScore.CourseId +where CourseId=2 + +-- 7.查询没有学全所有课程的同学的信息 +select StudentCode 学生编号,StudentName 学生名字,Birthday 生日,Sex 性别,ClassId 班级 from StudentCourseScore +inner join StudentInfo on StudentInfo.Id=StudentCourseScore.StudentId +inner join CourseInfo on CourseInfo.Id=StudentCourseScore.CourseId + + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + + + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + + + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + + + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..573d2a0b5818bab8dd4fd68ebcc4b71ac418851e --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery3.sql" @@ -0,0 +1,95 @@ +go +use master + +create database GoodsDB +on +( +name='GoodsDB', +filename='D:\test\GoodsDB.mdf', +size=10MB, +maxsize=50MB, +filegrowth=10% +) +log on +( +name='GoodsDB_log', +filename='D:\test\GoodsDB_log.ldf', +size=10MB, +maxsize=50MB, +filegrowth=10% +) + +go +use GoodsDB + +go +create table GoodsType +( +TypeID int primary key identity(1,1), +TypeName nvarchar(20) not null, +) + +go +create table GoodsInfo +( +GoodsID int primary key identity(1,1), +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int references GoodsType(TypeID) +) + +--2、使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 + +--商品信息表(GoodsType) +--商品编号 商品 + +go +insert into GoodsType (TypeName) values ('服装内衣'),('鞋包配饰'),('手机数码') + +--商品信息表(GoodsInfo) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 + +go +insert into GoodsInfo (GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values +('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) + +select * from GoodsType +select * from GoodsInfo + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select TypeName 列名, GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo +inner join GoodsType on GoodsType.TypeID=GoodsInfo.TypeID +where GoodsMoney=(select MAX(GoodsMoney)from GoodsInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select GoodsType.TypeID 品类型编号,MAX(GoodsMoney) 最高价格,MIN(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo +inner join GoodsType on GoodsType.TypeID=GoodsInfo.TypeID +group by GoodsType.TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select GoodsID 商品编号,GoodsName 商品名称,GoodsColor 商品颜色,GoodsBrand 商品价格,GoodsMoney 商品类型编号, +TypeID from GoodsInfo where GoodsColor='红色'and GoodsMoney<600 and GoodsMoney>300 diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery4.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..62b78aed682a7567cbd55bc3d78d2847158e2822 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery4.sql" @@ -0,0 +1,61 @@ +use master +go + +create database HOUSE_DB +on +( +name='HOUSE_DB', +filename='D:\test\HOUSE_DB.mdf', +size=5MB, +maxsize=50MB, +filegrowth=1 +) +log on +( +name='HOUSE_DB_log', +filename='D:\test\HOUSE_DB_log.ldf', +size=5MB, +maxsize=50MB, +filegrowth=10% +) + +go +use HOUSE_DB + +go +create table HOUSE_TYPE +( +type_id int not null primary key identity(1,1), +type_name varchar(50) unique not null, +) + +go +create table HOUSE +( +house_id int primary key identity(1,1) not null, +house_name varchar(50) not null, +house_price float default(0) not null, +type_id int references HOUSE_TYPE(type_id) +) + +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 +go +insert into HOUSE_TYPE (type_name) values ('小户型'),('经济型'),('别墅') +insert into HOUSE (house_name,house_price,type_id) values('单身公寓',500,1),('商品房',1200,2),('海边别墅',5000,3) + +go +select * from HOUSE_TYPE +select * from HOUSE +--添加查询 +--查询所有房屋信息 +select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like'%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 房屋的名称,house_price 租金 from HOUSE order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select type_name 房屋类型,HOUSE.house_name 房屋名称,house_price 房屋租金 from HOUSE_TYPE +inner join HOUSE on HOUSE.type_id=HOUSE_TYPE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select house_name 房屋名称,house_price 最高租金 from HOUSE where house_price=(select MAX(house_price) from HOUSE) group by house_name,house_price diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery5.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..288279d072efaae5de9cc593056c312be6dbc549 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery5.sql" @@ -0,0 +1,72 @@ +use master +go + +create database StarManagerDB +on +( +name='StarManagerDB', +filename='D:\test\StarManagerDB.mdf', +size=5MB, +maxsize=50mb, +filegrowth=10% +) +log on +( +name='StarManagerDB_log', +filename='D:\test\StarManagerDB_log.ldf', +size=5MB, +maxsize=50mb, +filegrowth=10% +) + +go +use StarManagerDB + +go +create table StarType +( +T_NO int primary key identity(1,1) not null, +T_NAME nvarchar(20) +) + +go +create table StarInfo +( +S_NO int primary key identity(1,1), +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20), +S_NATIVE nvarchar(20) default('中国大陆'), +S_T_NO int references StarType(T_NO) +) + +go +insert into StarType (T_NAME) values ('体育明星'),('IT明星'),('相声演员') + +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 + + +go +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) values +('梅西',30,'射门','阿根延',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_AGE 年龄,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo order by S_AGE desc + +--4select S_T_NO as 类型,count(*) as 人数,avg(S_Age) as 平均年龄 from StarInfo +select S_T_NO as 类型,count(*) 人数,avg(S_Age) 平均年龄 from StarInfo group by S_T_NO + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\235\250\345\270\206/50\351\242\230.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\235\250\345\270\206/50\351\242\230.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f80a72fe18a4dded2508fe6adb58cdf908a2b333 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\235\250\345\270\206/50\351\242\230.sql" @@ -0,0 +1,299 @@ +use ClassicDb +go + + +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + +select D.* from +(select A.StudentId a,A.CourseId b,A.Score c,B.StudentId d,B.CourseId e,B.Score f from +(select * from StudentCourseScore where CourseId = 1) as A , +(select * from StudentCourseScore where CourseId = 2) as B +where A.Score > B.Score and A.StudentId = B.StudentId) as C +inner join StudentInfo D on C.a = D.StudentCode + + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 + +select StudentId,count(CourseId) from StudentCourseScore +where CourseId = 1 or CourseId = 2 +group by StudentId +having count(CourseId) = 2 + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) + +select * from (select * from StudentCourseScore where CourseId = 1) as A +right join (select * from StudentCourseScore where CourseId = 2) as B on A.StudentId = B.StudentId + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 + +select * from (select * from StudentCourseScore where CourseId = 1) as A +left join (select * from StudentCourseScore where CourseId = 2) as B on A.StudentId = B.StudentId + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 + +select StudentId as 学生编号,StudentName as 学生姓名,avg(Score) as 平均成绩 from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +group by StudentId,StudentName +having avg(Score) >= 60 + +-- 3.查询在 成绩表 存在成绩的学生信息 + +select B.* from StudentCourseScore A +left join StudentInfo B on A.StudentId = B.StudentCode + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +select +StudentCode as 学生编号,StudentName as 学生姓名, +count(CourseId) as 选课总数,sum(Score) as 总成绩 +from StudentCourseScore A +right join StudentInfo B on A.StudentId = B.StudentCode +group by StudentCode,StudentName +order by StudentCode + +-- 4.1 查有成绩的学生信息 + +select StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +left join StudentInfo B on A.StudentId = B.StudentCode +group by StudentCode,StudentName,Birthday,Sex,ClassId + +-- 5.查询「李」姓老师的数量 + +select count(*) as 姓老师的数量 from Teachers +group by TeacherName +having TeacherName like ('李%') + + +-- 6.查询学过「张三」老师授课的同学的信息 + +select B.* from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +where A.CourseId = 1 + +-- 7.查询没有学全所有课程的同学的信息 + +select StudentId,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +group by StudentId,StudentName,Birthday,Sex,ClassId +having count(CourseId)<3 + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + +select StudentId,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 1 or CourseId = 2 or CourseId = 3 +group by StudentId,StudentName,Birthday,Sex,ClassId + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + +select StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 1 or CourseId = 2 or CourseId = 3 +group by StudentCode,StudentName,Birthday,Sex,ClassId +having count(CourseId) = 3 + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select StudentName from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +where A.CourseId != 1 +group by StudentCode,StudentName + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + +select * from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 1 and Score < 60 + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + +select CourseId,Score,avg(Score) from StudentCourseScore +group by StudentId,CourseId,Score +order by avg(Score) desc + +-- 14.查询各科成绩最高分、最低分和平均分: + +select CourseId,max(Score),min(Score),avg(Score) from StudentCourseScore +group by CourseId + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + +select CourseId,count(CourseId) from StudentCourseScore +group by CourseId + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + +select Sex,count(*) from StudentInfo +group by Sex + +-- 22.查询名字中含有「风」字的学生信息 + +select * from StudentInfo +where StudentName like ('%风%') + +-- 23.查询同名同性学生名单,并统计同名人数 + +select count(*) from StudentInfo A +inner join StudentInfo B on A.Id = B.Id +where A.StudentName = B.StudentName and A.Sex = B.Sex and A.StudentCode != B.StudentCode + +-- 24.查询 1990 年出生的学生名单 + +select StudentName from StudentInfo +where Birthday like '1990%' + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + +select CourseId,avg(Score) from StudentCourseScore +group by CourseId +order by avg(Score) desc + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + +select StudentId,StudentName,avg(Score) from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +group by StudentId,StudentName +having avg(Score)>85 + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + +select StudentName,Score from StudentCourseScore A +inner join CourseInfo B on A.CourseId = B.Id +inner join StudentInfo C on A.StudentId = C.StudentCode +where CourseName = '数学' and Score<60 + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + +select StudentName,CourseName,Score from StudentInfo A +full join StudentCourseScore B on B.StudentId = A.StudentCode +full join CourseInfo C on B.CourseId = C.Id + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + +select CourseName,count(*) from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +group by CourseId,CourseName + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + +select distinct top 1 * from StudentCourseScore +where CourseId = 2 +order by Score desc + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + +select top 1 * from StudentCourseScore +where CourseId = 2 +order by Score desc + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + +select count(*) from StudentCourseScore +group by CourseId + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\235\250\345\270\206/\345\244\215\344\271\240\351\242\2301.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\235\250\345\270\206/\345\244\215\344\271\240\351\242\2301.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2ca681c487d6e9a1be6d52e88383e8483636eb8c --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\235\250\345\270\206/\345\244\215\344\271\240\351\242\2301.sql" @@ -0,0 +1,80 @@ +use master +go + +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\test\GoodsDB.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='GoodsDB_log', + filename='D:\test\GoodsDB_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity not null , + TypeName nvarchar(20) not null , +) +go + +create table GoodsInfo +( + GoodsId int primary key identity not null , + GoodsName nvarchar(20) not null , + GoodsColor nvarchar(20) not null , + GoodsBrand nvarchar(20) , + GoodsMoney money not null , + TypeID int references GoodsType(TypeID) +) +go + +insert GoodsType values +('服装内衣'), +('鞋包配饰'), +('手机数码') +go + +insert GoodsInfo values +('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','阿依莲',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) +go + +select top 1 +GoodsName as 商品名称, +GoodsColor as 商品颜色, +GoodsMoney as 商品价格 +from GoodsInfo +order by GoodsMoney desc +go + +select +max(GoodsMoney) as 最高价格, +min(GoodsMoney) as 最低价格, +avg(GoodsMoney) as 平均价格 +from GoodsInfo +group by TypeID +go + +select * from GoodsInfo +where GoodsMoney >= 300 and GoodsMoney <= 600 and GoodsColor = '红色' + diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\235\250\345\270\206/\345\244\215\344\271\240\351\242\2302.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\235\250\345\270\206/\345\244\215\344\271\240\351\242\2302.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d5dea63f2eb52f922c19c6b3c232da9b6c481c83 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\235\250\345\270\206/\345\244\215\344\271\240\351\242\2302.sql" @@ -0,0 +1,71 @@ +use master +go + +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\test\HOUSE_DB.mdf', + size=5, + maxsize=50, + filegrowth=1 +) +log on +( + name='HOUSE_DB_log', + filename='D:\test\HOUSE_DB_log.ldf', + size=5, + maxsize=50, + filegrowth=1 +) +go + +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity not null , + type_name varchar(50) not null unique +) +go + +create table HOUSE +( + house_id int primary key identity not null , + house_name varchar(50) not null , + house_price float not null , + type_id int references HOUSE_TYPE(type_id) +) +go + +insert HOUSE_TYPE values +('小户型'),('经济型'),('别墅') +go + +insert HOUSE values +('asd',2000,1), +('dsa',1500,3), +('das',1800,2) +go + +select * from HOUSE +go + +select B.* from HOUSE A +inner join HOUSE_TYPE B on A.type_id = B.type_id +where type_name like '%型' +go + +select house_name,house_price from HOUSE +order by house_price desc +go + + +select A.*,B.type_name from HOUSE A +inner join HOUSE_TYPE B on A.type_id = B.type_id +go + +select top 1 house_name,house_price from HOUSE +order by house_price desc +go \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\235\250\345\270\206/\345\244\215\344\271\240\351\242\2303.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\235\250\345\270\206/\345\244\215\344\271\240\351\242\2303.sql" new file mode 100644 index 0000000000000000000000000000000000000000..401955aed318dbc137a670f2ae1c39432df0f0b3 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\235\250\345\270\206/\345\244\215\344\271\240\351\242\2303.sql" @@ -0,0 +1,70 @@ +use master +go + +create database StarManagerDB +on +( + name='StarManagerDB', + filename='D:\test\StarManagerDB.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='StarManagerDB_log', + filename='D:\test\StarManagerDB_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +go + +use StarManagerDB +go + +create table StarType +( + T_NO int primary key identity not null , + T_NAME nvarchar(20) , +) +go + +create table StarInfo +( + S_NO int primary key identity not null , + S_NAME nvarchar(20) not null , + S_AGE int not null , + S_HOBBY nvarchar(20) , + S_NATIVE nvarchar(20) default('中国大陆') , + S_T_NO int references StarType(T_NO) +) +go + +insert StarType values +('体育明星'), +('IT明星'), +('相声演员') +go + +insert StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) +go + +select top 3 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo +order by S_AGE desc +go + +select S_T_NO as 类型,count(*) as 人数,avg(S_Age) as 平均年龄 from StarInfo +group by S_T_NO +go + +select top 1 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo +where S_T_NO = 1 +order by S_AGE desc + diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/50\351\242\230\344\273\243\347\240\201" "b/2021 04 02 \344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/50\351\242\230\344\273\243\347\240\201" new file mode 100644 index 0000000000000000000000000000000000000000..3dddc3bda82b5315f2bed7c81882f5aacdc42329 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/50\351\242\230\344\273\243\347\240\201" @@ -0,0 +1,331 @@ +create database ClassicDb +GO +use ClassicDb +GO +create table StudentInfo +( + Id int PRIMARY key not null IDENTITY, + StudentCode nvarchar(80), + StudentName nvarchar(80), + Birthday date not null, + Sex nvarchar(2), + ClassId int not null +) +GO + +-- select * from StudentInfo +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '璧甸浄' , '1990-01-01' , 'm',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '閽辩數' , '1990-12-21' , 'm',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '瀛欓' , '1990-12-20' , 'm',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '鏉庝簯' , '1990-12-06' , 'm',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '鍛ㄦ' , '1991-12-01' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '鍚村叞' , '1992-01-01' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '閮戠' , '1989-01-01' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '寮犱笁' , '2017-12-20' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '鏉庡洓' , '2017-12-25' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '鏉庡洓' , '2012-06-06' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '璧靛叚' , '2013-06-13' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '瀛欎竷' , '2014-06-01' , 'f',1) + +GO + +CREATE TABLE Teachers +( + + Id int PRIMARY key not null IDENTITY, + TeacherName nvarchar(80) +) + +go + +-- select * from Teachers + +insert into Teachers (TeacherName) values('寮犱笁') +insert into Teachers (TeacherName) values('鏉庡洓') +insert into Teachers (TeacherName) values('鐜嬩簲') + +GO + +create table CourseInfo +( + Id int PRIMARY key not null IDENTITY, + CourseName NVARCHAR(80) not null, + TeacherId int not null +) + +go + +-- select * from CourseInfo + +insert into CourseInfo (CourseName,TeacherId) values( '璇枃' , 2) +insert into CourseInfo (CourseName,TeacherId) values( '鏁板' , 1) +insert into CourseInfo (CourseName,TeacherId) values( '鑻辫' , 3) + +GO + +create table StudentCourseScore +( + Id int PRIMARY key not null IDENTITY, + StudentId int not null, + CourseId int not null, + Score int not null +) + +go + +-- select * from StudentCourseScore + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + +go + + +-- 缁冧範棰樼洰锛 + +select * from CourseInfo --璇剧▼淇℃伅琛 +select * from StudentCourseScore --鍒嗘暟琛 +select * from StudentInfo --瀛︾敓淇℃伅琛 +select * from Teachers --鏁欏笀淇℃伅琛 + +--CourseInfo Id CourseName璇剧▼鍚 TeacherId鑰佸笀Id +--StudentCourseScore Id StudentId瀛︾敓Id CourseId璇剧▼Id Score鎴愮哗 +--StudentInfo Id StudentCode瀛︾敓缂栧彿 StudentName瀛︾敓濮撳悕 Birthday鍑虹敓鏃ユ湡 Sex ClassId鐝骇Id +--Teachers Id TeacherName鏁欏笀濮撳悕 + +-- 1.鏌ヨ"鏁板 "璇剧▼姣" 璇枃 "璇剧▼鎴愮哗楂樼殑瀛︾敓鐨勪俊鎭強璇剧▼鍒嗘暟 +select StudentInfo.*,CourseId 璇剧▼,Score 璇剧▼鍒嗘暟 from StudentInfo inner join StudentCourseScore on StudentInfo.Id = StudentCourseScore.StudentId +where Score=(select CourseId from StudentCourseScore where CourseId='2') ????????????????? + + +-- 1.1 鏌ヨ鍚屾椂瀛樺湪" 鏁板 "璇剧▼鍜" 璇枃 "璇剧▼鐨勬儏鍐 + + + +-- 1.2 鏌ヨ瀛樺湪" 鏁板 "璇剧▼浣嗗彲鑳戒笉瀛樺湪" 璇枃 "璇剧▼鐨勬儏鍐(涓嶅瓨鍦ㄦ椂鏄剧ず涓 null ) + + + +-- 1.3 鏌ヨ涓嶅瓨鍦" 鏁板 "璇剧▼浣嗗瓨鍦" 璇枃 "璇剧▼鐨勬儏鍐 + + + +-- 2.鏌ヨ骞冲潎鎴愮哗澶т簬绛変簬 60 鍒嗙殑鍚屽鐨勫鐢熺紪鍙峰拰瀛︾敓濮撳悕鍜屽钩鍧囨垚缁 +select StudentCode 瀛︾敓缂栧彿,StudentName 瀛︾敓濮撳悕,AVG(Score)骞冲潎鎴愮哗 from StudentInfo inner join StudentCourseScore on StudentInfo.Id = StudentCourseScore.StudentId group by StudentCode,StudentName having AVG(Score)>=60 + +-- 3.鏌ヨ鍦 鎴愮哗 琛ㄥ瓨鍦ㄦ垚缁╃殑瀛︾敓淇℃伅 +select StudentInfo.Id,StudentInfo.StudentCode,StudentInfo.StudentName,StudentInfo.Birthday,StudentInfo.Sex,StudentInfo.ClassId from StudentInfo inner join StudentCourseScore on StudentInfo.Id = StudentCourseScore.StudentId +where Score>0 +UNION +select StudentInfo.Id,StudentInfo.StudentCode,StudentInfo.StudentName,StudentInfo.Birthday,StudentInfo.Sex,StudentInfo.ClassId from StudentInfo inner join StudentCourseScore on StudentInfo.Id = StudentCourseScore.StudentId +where StudentCode=01 and StudentCode=02 and StudentCode=03 and StudentCode=04 and StudentCode=05 and StudentCode=06 and StudentCode=07 + +-- 4.鏌ヨ鎵鏈夊悓瀛︾殑瀛︾敓缂栧彿銆佸鐢熷鍚嶃侀夎鎬绘暟銆佹墍鏈夎绋嬬殑鎬绘垚缁(娌℃垚缁╃殑鏄剧ず涓 null ) +select StudentCode 瀛︾敓缂栧彿,StudentName 瀛︾敓濮撳悕,COUNT(CourseId)閫夎鎬绘暟,SUM(Score)鎬绘垚缁 from StudentInfo left join StudentCourseScore on StudentInfo.Id = StudentCourseScore.StudentId group by StudentCode,StudentName + +-- 4.1 鏌ユ湁鎴愮哗鐨勫鐢熶俊鎭 +select StudentInfo.* from StudentInfo inner join StudentCourseScore on StudentInfo.Id = StudentCourseScore.StudentId +UNION +select StudentInfo.* from StudentInfo inner join StudentCourseScore on StudentInfo.Id = StudentCourseScore.StudentId +where StudentInfo.Id=1 and StudentInfo.Id=2 and StudentInfo.Id=3 and StudentInfo.Id=4 and StudentInfo.Id=5 and StudentInfo.Id=6 and StudentInfo.Id=7 + +-- 5.鏌ヨ銆屾潕銆嶅鑰佸笀鐨勬暟閲 +select COUNT(TeacherName) from Teachers where TeacherName like '鏉%' + + +-- 6.鏌ヨ瀛﹁繃銆屽紶涓夈嶈佸笀鎺堣鐨勫悓瀛︾殑淇℃伅 +select StudentInfo.* from StudentInfo inner join StudentCourseScore on StudentInfo.Id = StudentCourseScore.StudentId +where CourseId='2' + +-- 7.鏌ヨ娌℃湁瀛﹀叏鎵鏈夎绋嬬殑鍚屽鐨勪俊鎭 +select * from StudentInfo +except +select StudentInfo.* from StudentInfo inner join StudentCourseScore on StudentInfo.Id = StudentCourseScore.StudentId + + +-- 8.鏌ヨ鑷冲皯鏈変竴闂ㄨ涓庡鍙蜂负" 01 "鐨勫悓瀛︽墍瀛︾浉鍚岀殑鍚屽鐨勪俊鎭 +select StudentInfo.* from StudentInfo A,StudentInfo B ????????????/ + + + select * from CourseInfo --璇剧▼淇℃伅琛 + select * from StudentCourseScore --鍒嗘暟琛 + select * from StudentInfo --瀛︾敓淇℃伅琛 + select * from Teachers --鏁欏笀淇℃伅琛 +-- 9.鏌ヨ鍜" 01 "鍙风殑鍚屽瀛︿範鐨勮绋 瀹屽叏鐩稿悓鐨勫叾浠栧悓瀛︾殑淇℃伅 + + + +-- 10.鏌ヨ娌″杩"寮犱笁"鑰佸笀璁叉巿鐨勪换涓闂ㄨ绋嬬殑瀛︾敓濮撳悕 + + + +-- 11.鏌ヨ涓ら棬鍙婂叾浠ヤ笂涓嶅強鏍艰绋嬬殑鍚屽鐨勫鍙凤紝濮撳悕鍙婂叾骞冲潎鎴愮哗 + + + +-- 12.妫绱" 鏁板 "璇剧▼鍒嗘暟灏忎簬 60锛屾寜鍒嗘暟闄嶅簭鎺掑垪鐨勫鐢熶俊鎭 +select StudentInfo.* from StudentInfo StudentInfo inner join StudentCourseScore on StudentInfo.Id = StudentCourseScore.StudentId +where CourseId='2' and Score<80 order by Score DESC + + +-- 13.鎸夊钩鍧囨垚缁╀粠楂樺埌浣庢樉绀烘墍鏈夊鐢熺殑鎵鏈夎绋嬬殑鎴愮哗浠ュ強骞冲潎鎴愮哗 + + + +-- 14.鏌ヨ鍚勭鎴愮哗鏈楂樺垎銆佹渶浣庡垎鍜屽钩鍧囧垎锛 + + + +-- 15.浠ュ涓嬪舰寮忔樉绀猴細璇剧▼ ID锛岃绋 name锛屾渶楂樺垎锛屾渶浣庡垎锛屽钩鍧囧垎锛屽強鏍肩巼锛屼腑绛夌巼锛屼紭鑹巼锛屼紭绉鐜 + +/* + + 鍙婃牸涓>=60锛屼腑绛変负锛70-80锛屼紭鑹负锛80-90锛屼紭绉涓猴細>=90 + + 瑕佹眰杈撳嚭璇剧▼鍙峰拰閫変慨浜烘暟锛屾煡璇㈢粨鏋滄寜浜烘暟闄嶅簭鎺掑垪锛岃嫢浜烘暟鐩稿悓锛屾寜璇剧▼鍙峰崌搴忔帓鍒,鎸夊悇绉戞垚缁╄繘琛屾帓搴忥紝骞舵樉绀烘帓鍚嶏紝 Score 閲嶅鏃朵繚鐣欏悕娆$┖缂 + +*/ + + + +-- 15.1 鎸夊悇绉戞垚缁╄繘琛屾帓搴忥紝骞舵樉绀烘帓鍚嶏紝 Score 閲嶅鏃跺悎骞跺悕娆 + + + +-- 16.鏌ヨ瀛︾敓鐨勬绘垚缁╋紝骞惰繘琛屾帓鍚嶏紝鎬诲垎閲嶅鏃朵繚鐣欏悕娆$┖缂 + + + +-- 16.1 鏌ヨ瀛︾敓鐨勬绘垚缁╋紝骞惰繘琛屾帓鍚嶏紝鎬诲垎閲嶅鏃朵笉淇濈暀鍚嶆绌虹己 + + + +-- 17.缁熻鍚勭鎴愮哗鍚勫垎鏁版浜烘暟锛氳绋嬬紪鍙凤紝璇剧▼鍚嶇О锛孾100-85]锛孾85-70]锛孾70-60]锛孾60-0] 鍙婃墍鍗犵櫨鍒嗘瘮 + + + +-- 18.鏌ヨ鍚勭鎴愮哗鍓嶄笁鍚嶇殑璁板綍 + + + +-- 19.鏌ヨ姣忛棬璇剧▼琚変慨鐨勫鐢熸暟 + + + +-- 20.鏌ヨ鍑哄彧閫変慨涓ら棬璇剧▼鐨勫鐢熷鍙峰拰濮撳悕 + + + +-- 21.鏌ヨ鐢风敓銆佸コ鐢熶汉鏁 + + + +-- 22.鏌ヨ鍚嶅瓧涓惈鏈夈岄銆嶅瓧鐨勫鐢熶俊鎭 + + + +-- 23.鏌ヨ鍚屽悕鍚屾у鐢熷悕鍗曪紝骞剁粺璁″悓鍚嶄汉鏁 + + + +-- 24.鏌ヨ 1990 骞村嚭鐢熺殑瀛︾敓鍚嶅崟 + + + +-- 25.鏌ヨ姣忛棬璇剧▼鐨勫钩鍧囨垚缁╋紝缁撴灉鎸夊钩鍧囨垚缁╅檷搴忔帓鍒楋紝骞冲潎鎴愮哗鐩稿悓鏃讹紝鎸夎绋嬬紪鍙峰崌搴忔帓鍒 + + + +-- 26.鏌ヨ骞冲潎鎴愮哗澶т簬绛変簬 85 鐨勬墍鏈夊鐢熺殑瀛﹀彿銆佸鍚嶅拰骞冲潎鎴愮哗 + + + +-- 27.鏌ヨ璇剧▼鍚嶇О涓恒屾暟瀛︺嶏紝涓斿垎鏁颁綆浜 60 鐨勫鐢熷鍚嶅拰鍒嗘暟 + + + +-- 28.鏌ヨ鎵鏈夊鐢熺殑璇剧▼鍙婂垎鏁版儏鍐碉紙瀛樺湪瀛︾敓娌℃垚缁╋紝娌¢夎鐨勬儏鍐碉級 + + + +-- 29.鏌ヨ浠讳綍涓闂ㄨ绋嬫垚缁╁湪 70 鍒嗕互涓婄殑濮撳悕銆佽绋嬪悕绉板拰鍒嗘暟 + + + +-- 30.鏌ヨ涓嶅強鏍肩殑璇剧▼ + + + +-- 31.鏌ヨ璇剧▼缂栧彿涓 01 涓旇绋嬫垚缁╁湪 80 鍒嗕互涓婄殑瀛︾敓鐨勫鍙峰拰濮撳悕 + + + +-- 32.姹傛瘡闂ㄨ绋嬬殑瀛︾敓浜烘暟 + + + +-- 33.鎴愮哗涓嶉噸澶嶏紝鏌ヨ閫変慨銆屽紶涓夈嶈佸笀鎵鎺堣绋嬬殑瀛︾敓涓紝鎴愮哗鏈楂樼殑瀛︾敓淇℃伅鍙婂叾鎴愮哗 + + + +--34.鎴愮哗鏈夐噸澶嶇殑鎯呭喌涓嬶紝鏌ヨ閫変慨銆屽紶涓夈嶈佸笀鎵鎺堣绋嬬殑瀛︾敓涓紝鎴愮哗鏈楂樼殑瀛︾敓淇℃伅鍙婂叾鎴愮哗 + + + +-- 35.鏌ヨ涓嶅悓璇剧▼鎴愮哗鐩稿悓鐨勫鐢熺殑瀛︾敓缂栧彿銆佽绋嬬紪鍙枫佸鐢熸垚缁 + + + +-- 36.鏌ヨ姣忛棬鍔熸垚缁╂渶濂界殑鍓嶄袱鍚 + + + +-- 37.缁熻姣忛棬璇剧▼鐨勫鐢熼変慨浜烘暟锛堣秴杩 5 浜虹殑璇剧▼鎵嶇粺璁★級銆 + + + +-- 38.妫绱㈣嚦灏戦変慨涓ら棬璇剧▼鐨勫鐢熷鍙 + + + +-- 39.鏌ヨ閫変慨浜嗗叏閮ㄨ绋嬬殑瀛︾敓淇℃伅 + + + +-- 40.鏌ヨ鍚勫鐢熺殑骞撮緞锛屽彧鎸夊勾浠芥潵绠 + + + +-- 41.鎸夌収鍑虹敓鏃ユ湡鏉ョ畻锛屽綋鍓嶆湀鏃 < 鍑虹敓骞存湀鐨勬湀鏃ュ垯锛屽勾榫勫噺涓 + + + +-- 42.鏌ヨ鏈懆杩囩敓鏃ョ殑瀛︾敓 + + + +-- 43.鏌ヨ涓嬪懆杩囩敓鏃ョ殑瀛︾敓 + + + +-- 44.鏌ヨ鏈湀杩囩敓鏃ョ殑瀛︾敓 + + + +-- 45.鏌ヨ涓嬫湀杩囩敓鏃ョ殑瀛︾敓 \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/\345\244\215\344\271\2401\344\273\243\347\240\201.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/\345\244\215\344\271\2401\344\273\243\347\240\201.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1f82c46fa261dfec536996d813a52738cacc33f9 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/\345\244\215\344\271\2401\344\273\243\347\240\201.sql" @@ -0,0 +1,71 @@ +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\GoodsDB.mdf', + size=50, + maxsize=100, + filegrowth=10 +) +log on +( + name='GoodsDB_log', + filename='D:\GoodsDB_log.ldf', + size=50, + maxsize=100, + filegrowth=10 +) +go +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int not null references GoodsType(TypeID) +) + + +--2、使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 + +insert into GoodsType values ('服装内衣'),('鞋包配饰'),('手机数码') +select * from GoodsType +--商品信息表(GoodsType) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 + +insert into GoodsInfo values ('提花小西装','红色','菲曼琪','300','1'),('百搭短裤','绿色','哥弟','100','1'),('无袖背心','白色','阿依莲','700','1'), +('低帮休闲鞋','红色','菲曼琪','900','2'),('中跟单鞋','绿色','哥弟','400','2'), +('平底鞋','白色','阿依莲','200','2'),('迷你照相机','红色','尼康','500','3'),('硬盘','黑色','希捷','600','3'),('显卡','黑色','技嘉','800','3') + +select * from GoodsType +select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select TypeName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsType inner join GoodsInfo on GoodsType.TypeID = GoodsInfo.TypeID +where GoodsMoney=(select MAX(GoodsMoney) from GoodsInfo) +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select GoodsInfo.TypeID 商品类型编号,MAX(GoodsMoney)最高价格,MIN(GoodsMoney)最低价格,AVG(GoodsMoney)平均价格 from GoodsInfo group by GoodsInfo.TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsType inner join GoodsInfo on GoodsType.TypeID = GoodsInfo.TypeID +where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 + + diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/\345\244\215\344\271\2402\344\273\243\347\240\201.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/\345\244\215\344\271\2402\344\273\243\347\240\201.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1a38406e0aa093a6ecad46c472241fe7d8981b1a --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/\345\244\215\344\271\2402\344\273\243\347\240\201.sql" @@ -0,0 +1,51 @@ +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5, + maxsize=50, + filegrowth=1 +) +log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5, + maxsize=50, + filegrowth=1 +) +go +create table HOUSE_TYPE +( + type_id int not null primary key identity(1,1), + type_name varchar(50) not null unique +) +create table HOUSE +( + house_id int not null primary key identity(1,1), + house_name varchar(50) not null, + house_price float not null default('0'), + type_id int not null references HOUSE_TYPE(type_id) +) + +insert into HOUSE_TYPE values ('小户型'),('经济型'),('别墅') +insert into HOUSE values ('小一','400','1'),('小二','500','2'),('小三','450','3') + + +--4、添加查询 +select * from HOUSE_TYPE +select * from HOUSE +--查询所有房屋信息 +select * from HOUSE inner join HOUSE_TYPE on HOUSE.type_id = HOUSE_TYPE.type_id +--使用模糊查询包含”型“字的房屋类型信息 +select HOUSE.* from HOUSE inner join HOUSE_TYPE on HOUSE.type_id = HOUSE_TYPE.type_id +where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 名称,house_price 租金 from HOUSE +order by house_price DESC +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,type_name 房屋类型 from HOUSE inner join HOUSE_TYPE on HOUSE.type_id = HOUSE_TYPE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select Top 1 house_price 最高的租金,house_name 房屋名称 from HOUSE +order by house_price DESC diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/\345\244\215\344\271\2403\344\273\243\347\240\201.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/\345\244\215\344\271\2403\344\273\243\347\240\201.sql" new file mode 100644 index 0000000000000000000000000000000000000000..32192e20919c46981adeb9a0a845238ff355c51c --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/\345\244\215\344\271\2403\344\273\243\347\240\201.sql" @@ -0,0 +1,47 @@ +create database StarManagerDB +on +( + name='StarManagerDB', + filename='E:\SQL课堂\StarManagerDB.mdf', + size=50, + maxsize=100, + filegrowth=10 +) +log on +( + name='StarManagerDB_log', + filename='E:\SQL课堂\StarManagerDB_log.ldf', + size=50, + maxsize=100, + filegrowth=10 +) +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int not null references StarType(T_NO) +) + +insert into StarType values ('体育明星'),('IT明星'),('相声演员') +insert into StarInfo values ('梅西','30','射门','阿根廷','1'),('科比','35','过人','美国','1'),('蔡景现','40','敲代码','中国','2'),('马斯克','36','造火箭','外星人','2'),('郭德纲','50','相声','中国','3'),('黄铮','41','拼多多','中国','2') + +select *from StarType +select *from StarInfo +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo +where S_AGE=(select MAX(S_AGE) from StarInfo) + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select T_NO 类型编号,COUNT(S_T_NO)明星人数,AVG(S_AGE)平均年龄 from StarInfo inner join StarType on StarInfo.S_T_NO = StarType.T_NO group by T_NO having COUNT(S_T_NO)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select Top 1 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo inner join StarType on StarInfo.S_T_NO = StarType.T_NO +where T_NAME=('体育明星') order by S_AGE DESC diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/1.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cfcb28b918285b1c6a3c1c9fdc6664aa679f68b2 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/1.sql" @@ -0,0 +1,40 @@ +create database GoodDB +go +use GoodDB +go + +create table GoodsType +( + TypeID int not null primary key identity , + TypeName nvarchar(20) not null +) + +create table GoodInfo +( + GoodsID int primary key identity not null, + GiidsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeId int foreign key references GoodsType(TypeID) +) +go +insert into GoodsType(TypeName) +select '服装内衣' union +select '鞋包配饰' union +select '手机数码' + +insert into GoodInfo(GiidsName,GoodsColor,GoodsBrand,GoodsMoney,TypeId) +values('提花小西装','红色','菲曼琪',300,1),('百搭短裤','绿色','哥弟',100,1),('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2),('中跟单鞋','绿色','哥弟',400,2),('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3),('硬盘','黑色','希捷',600,3),('显卡','黑色','技嘉',800,3) +go +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GiidsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodInfo where GoodsMoney = (select max(GoodsMoney) from GoodInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格 ,avg(GoodsMoney) 平均价格 from GoodInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 + diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/2.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6d3ca1a6fb53e730b1eb4ba74f0cd43a0cb5fa79 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/2.sql" @@ -0,0 +1,51 @@ +create database HOUSE_DB +on +( + name = 'HOUSE_DB', + filename = 'D:\HOUSE_DB.mdf', + size = 5, + maxsize= 50, + filegrowth = 10% +) +log on +( + name= 'HOUSE_DB_log', + filename = 'D:\HOUSE_DB_log.ldf', + size = 5, + maxsize= 50, + filegrowth = 10% +) +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity, + type_name varchar(50) unique +) +create table HOUSE +( + house_id int primary key identity , + house_name varchar(50), + house_price float default (0), + type_id int foreign key references HOUSE_TYPE(type_id) +) +go +insert into HOUSE_TYPE(type_name) +select '小户型' union select '经济型' union select '别墅' + +insert into HOUSE(house_name,house_price,type_id) +values('鱼',40,1),('龟',45,2),('虾',60,3) +--go +--查询所有房屋信息 +select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE inner join HOUSE_TYPE on HOUSE_TYPE.type_id = HOUSE.type_id +where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name,type_name from HOUSE inner join HOUSE_TYPE on HOUSE_TYPE.type_id = HOUSE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select house_price,house_name from HOUSE where house_price = (select max(house_price) from HOUSE ) + diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/3.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5361c99fd94b4f8be426792b8b4b56e3d11e56be --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/3.sql" @@ -0,0 +1,36 @@ +create database StarManagerDB +use StarManagerDB +go + +create table StarType +( + T_NO int primary key identity , + T_NAME nvarchar(20) +) +create table StarInfo +( + S_NO int primary key identity, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20) , + S_NATIVE nvarchar(20) default ('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) +go +insert into StarType(T_NAME) +select '体育明星'union +select 'IT明星'union +select '相声演员' + +insert into StarInfo(S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) +values ('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',2),('蔡景现',40,'敲代码','中国',2) +,('马克斯',36,'造火箭','外星人',2),('郭德纲',50,'相声','中国',3),('黄峥',41,'拼多多','中国',2) + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 籍贯信息,S_NATIVE 特技 from StarInfo order by S_AGE desc +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO,count(*)明星人数,avg(S_AGE)平均年龄 from StarInfo group by S_T_NO having count(S_T_NO)>1 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo where S_AGE=(select max(S_AGE) from StarInfo where S_T_NO = 1) + +select * from StarInfo \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/50.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/50.sql" new file mode 100644 index 0000000000000000000000000000000000000000..31f24438f08bd6c22fbfb48b43006beb0e7e20a0 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/50.sql" @@ -0,0 +1,245 @@ + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + +select StudentName,Birthday,A.Score from StudentInfo S +inner join StudentCourseScore A +on S.Id = A.StudentId and A.CourseId = 2 +inner join StudentCourseScore B +on S.Id = B.StudentId and B.CourseId = 1 +where A.Score>B.Score + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select * from (select StudentId,Score,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 2) B +inner join +(select StudentId,Score,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 1) A +on A.StudentId = B.StudentId +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) + +select * from (select StudentId,Score,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 2) B +left join +(select StudentId,Score,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 1) A +on A.StudentId = B.StudentId +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select * from +(select StudentId,Score from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 1 )A +left join +(select StudentId,Score from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 2) B +on A.StudentId = B.StudentId +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentId,StudentName,avg(Score) from StudentCourseScore SC inner join StudentInfo SI +on SC.StudentId = SI.Id +group by StudentId,StudentName having avg(Score)>60 + + +-- 3.查询在 成绩 表存在成绩的学生信息 +select * from (select StudentId from StudentCourseScore group by StudentId) SC inner join StudentInfo SI on SC.StudentId = SI.Id + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select * from StudentInfo SI left join (select StudentId from StudentCourseScore group by StudentId) SC on SI.Id = SC.StudentId + + +-- 4.1 查有成绩的学生信息 +select * from StudentInfo SI inner join (select StudentId from StudentCourseScore group by StudentId) SC on SI.Id = SC.StudentId + + +-- 5.查询「李」姓老师的数量 +select * from Teachers where TeacherName like '李%' + + +-- 6.查询学过「张三」老师授课的同学的信息 +select * from StudentInfo SI inner join (select StudentId from StudentCourseScore where CourseId = 2 group by StudentId ) SC on SI.Id = SC.StudentId + + +-- 7.查询没有学全所有课程的同学的信息 +select * from +(select * from StudentInfo A +right join +(select StudentId from StudentCourseScore group by StudentId having count(StudentId)<3) C +on A.Id = C.StudentId) Q +full join +(select SI.* from StudentInfo SI left join StudentCourseScore SC on SI.Id = SC.StudentId where Score is null) W +on Q.Id=W.Id +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select * from StudentInfo SI inner join +(select A.StudentId from +(select StudentId,CourseId from StudentCourseScore) A, +(select StudentId,CourseId from StudentCourseScore where StudentId = 1) B +where A.CourseId = B.CourseId and A.StudentId!=1 +group by A.StudentId) A +on SI.Id = A.StudentId +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +select * from StudentInfo SI inner join +(select A.StudentId from +(select StudentId,CourseId from StudentCourseScore) A, +(select StudentId,CourseId from StudentCourseScore where StudentId = 1) B +where A.CourseId = B.CourseId and A.StudentId!=1 +group by A.StudentId having count(A.StudentId)>2) A +on SI.Id = A.StudentId +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select * from StudentInfo SI +left join +(select StudentId from StudentCourseScore where CourseId = 2) S +on SI.Id = S.StudentId +where StudentId is null + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 +select * from StudentInfo + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\270\251\345\271\277\347\224\237/SQLQuery2.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\270\251\345\271\277\347\224\237/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fd81d59476e92dcf655a4f2520a853ae902aed73 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\270\251\345\271\277\347\224\237/SQLQuery2.sql" @@ -0,0 +1,462 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go +-- 练习题目: + +select *from CourseInfo +select *from StudentCourseScore +select *from StudentInfo +select *from Teachers + + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + +select * from + (select * from StudentCourseScore where courseId = 1) as A + inner join StudentInfo s on s.Id = A.Id, + (select * from StudentCourseScore where CourseId = 2) as B + where A.Score > B.Score and A.StudentId = B.StudentId + + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 + +select StudentId from StudentCourseScore + where CourseId = 1 or CourseId = 2 + group by StudentId + HAVING COUNT(distinct CourseId) = 2 + + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) + +select * from + (select * from StudentCourseScore where CourseId = 2) A + left join (select * from StudentCourseScore where CourseId = 1) B on A.StudentId = B.StudentId + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 + +select * from + (select * from StudentCourseScore where CourseId = 1) A + left join (select * from StudentCourseScore where CourseId = 2) B on A.StudentId = B.StudentId + where B.CourseId is null + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 + +select Studentcode 学生编号,StudentName 学生姓名,AVG(Score) 平均成绩 from StudentCourseScore + inner join StudentInfo on StudentCourseScore.StudentId = StudentInfo.StudentCode + group by Studentcode,StudentName + HAVING AVG(Score) >= 60 + +-- 3.查询在 成绩 表存在成绩的学生信息 + +select * from StudentCourseScore + left join StudentInfo on StudentCourseScore.StudentId = StudentInfo.StudentCode + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +select StudentCode ,StudentName ,COUNT(CourseId) ,SUM(Score) from StudentCourseScore scs + right join StudentInfo si on scs.StudentId = si.StudentCode + group by StudentCode,StudentName + +-- 4.1 查有成绩的学生信息 + +select * from StudentCourseScore scs + inner join StudentInfo si on scs.StudentId = si.StudentCode + +-- 5.查询「李」姓老师的数量 + +select COUNT(TeacherName) from Teachers + where TeacherName like '李%' + +-- 6.查询学过 「张三」老师授课 的 同学的信息 + +select * from StudentCourseScore A + inner join CourseInfo B on A.CourseId = B.Id + inner join StudentInfo C on A.StudentId = C.StudentCode + inner join Teachers D on B.TeacherId = D.Id + where TeacherName = '张三' + +-- 7.查询没有学全所有课程的同学的信息 +select * from CourseInfo +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers + +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode,StudentName + HAVING COUNT(CourseId) < (select COUNT(CourseName) from CourseInfo) + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode ,StudentName + having count(CourseId) > 0 + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode ,StudentName + having count(CourseId)=(select COUNT(CourseName) from CourseInfo) + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select distinct studentName from StudentCourseScore A + inner join CourseInfo B on A.CourseId = B.Id + inner join StudentInfo C on A.StudentId = C.StudentCode + inner join Teachers D on B.TeacherId = D.Id + where TeacherName != '张三' + + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\270\251\345\271\277\347\224\237/SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\270\251\345\271\277\347\224\237/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..724eefd957e123e055e88b77adc06ba331cae52f --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\270\251\345\271\277\347\224\237/SQLQuery3.sql" @@ -0,0 +1,41 @@ +create database GoodsDB +go +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) +insert into GoodsType(TypeName) +select '服装内衣' union +select '鞋包配饰' union +select '手机数码' + +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +select '提花小西装','红色','菲曼琪',300,1 union +select '百搭短裤','绿色','哥弟',100,1 union +select '无袖背心','白色' ,'阿依莲' ,700, 1 union +select '低帮休闲鞋','红色', '菲曼琪' ,900, 2 union +select '中跟单鞋','绿色', '哥弟' ,400 ,2 union +select '平底鞋','白色', '阿依莲', 200, 2 union +select '迷你照相机','红色' ,'尼康', 500 ,3 union +select '硬盘','黑色' ,'希捷' ,600 ,3 union +select '显卡','黑色' ,'技嘉',800 ,3 + +select * from GoodsInfo +select * from GoodsType + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称 ,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo where GoodsMoney=(select MAX(GoodsMoney) from GoodsInfo) +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID,min(GoodsMoney) 最低价格,MAX(GoodsMoney) 最高价格,AVG(GoodsMoney) 平均价格 from GoodsInfo group by typeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<600 \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\270\251\345\271\277\347\224\237/SQLQuery4.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\270\251\345\271\277\347\224\237/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..59573967bc33dd44e47383439deffeb02c5fc826 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\270\251\345\271\277\347\224\237/SQLQuery4.sql" @@ -0,0 +1,53 @@ +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='d:\temp\HOUSE_DB_mdf', + size=5, + maxsize=50, + filegrowth=1 +) +log on +( + name='HOUSE_DB_log', + filename='d:\temp\HOUSE_DB_log_ldf', + size=5, + maxsize=50, + filegrowth=1 +) +go +create table HOUSE_TYPE +( + type_id int not null primary key identity(1,1), + type_name varchar(50) not null, + +) +create table HOUSE +( + house_id int not null primary key identity(1,1), + house_name varchar(50) not null, + house_price float not null default(0), + type_id int not null references HOUSE_TYPE(type_id) +) + +insert into HOUSE_TYPE(type_name) +select '小户型' union +select '经济型' union +select '别墅' + +insert into HOUSE(house_name,house_price,type_id) +select '兴国型',500,1 union +select '小狗区',300,2 union +select '上都会型',1500,3 +--4、添加查询 +--查询所有房屋信息 +select * from HOUSE_TYPE +select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE where house_name like '%型' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 名称,house_price 租金 from HOUSE order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name,type_name from HOUSE a inner join HOUSE_TYPE b on a.house_id=b.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select *from HOUSE where house_price=(select max(house_price) from HOUSE) diff --git "a/2021 04 02 \344\275\234\344\270\232/\346\270\251\345\271\277\347\224\237/SQLQuery5.sql" "b/2021 04 02 \344\275\234\344\270\232/\346\270\251\345\271\277\347\224\237/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..71abe847328e0a150acdf4890c12744870bad5ca --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\346\270\251\345\271\277\347\224\237/SQLQuery5.sql" @@ -0,0 +1,39 @@ +create database StarManagerDB +go +use StarManagerDB +go +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) +insert into StarType(T_NAME) +select '体育明星' union +select 'IT明星' union +select '相声演员' + +insert into StarInfo(S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) +select '梅西',30,'射门','阿根廷',1 union +select '科比',35,'过人','美国',1 union +select '菜景观',40,'敲代码','中国',2 union +select '马克思',36,'造火箭','外星人',2 union +select '郭德纲',50,'相声','中国',3 union +select '黄铮',41,'拼多多','中国',2 + +select * from StarInfo +select * from StarType +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo order by S_AGE desc +--4、按 明星类型编号 分类查询 明星人数,明星平均年龄 ,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO,count(*) 人数,AVG(S_AGE) from StarType a inner join StarInfo b on a.T_NO=b.S_T_NO group by S_T_NO HAVING COUNT(S_NAME)>2 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select S_NAME,S_HOBBY,S_NATIVE,max(S_AGE) from StarInfo a inner join StarType b on a.S_T_NO=b.T_NO where S_AGE = (select MAX(S_AGE) from StarInfo where S_T_NO = 2) group by S_NAME,S_HOBBY,S_NATIVE \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..914c1778e5309c46297012ceee2e3010ac8637dd --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/SQLQuery1.sql" @@ -0,0 +1,86 @@ +--1、创建商品数据库(GoodsDB),然后建立两张表,GoodsType(商品类型表),GoodsInfo(商品信息表),表结构分别如下: +--商品类型表(GoodsType) +--字段名 说明 类型 长度 可否为空 约束 +--TypeID 商品类型编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--TypeName 商品类型名称 nvarchar 20 否 + +create database GoodsDB +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) + +--商品信息表(GoodsInfo) +--字段名 说明 类型 长度 可否为空 约束 +--GoodsID 商品编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--GoodsName 商品名称 nvarchar 20 否 +--GoodsColor 商品颜色 nvarchar 20 否 +--GoodsBrand 商品品牌 nvarchar 20 +--GoodsMoney 商品价格 money 否 +--TypeID 商品类型编号 int 外键,参照GoodsType中的TypeID + +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +--2、使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 + +insert into GoodsType(TypeName) +select '服装内衣' union +select '鞋包配饰' union +select '手机数码' + +--商品信息表(GoodsType) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 + +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +select '提花小西装','红色','菲曼琪',300,1 union +select '百搭短裤','绿色','哥弟',100,1 union +select '无袖背心','白色','阿依莲',700,1 union +select '低帮休闲鞋','红色','菲曼琪',900,2 union +select '中跟单鞋','绿色','哥弟',400,2 union +select '平底鞋','白色','阿依莲',200,2 union +select '迷你照相机','红色','尼康',500,3 union +select '硬盘','黑色','希捷',600,3 union +select '显卡','黑色','技嘉',800,3 + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo + where GoodsMoney = (select MAX(GoodsMoney) from GoodsInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select MAX(GoodsMoney) 最高价格,MIN(GoodsMoney) 最低价格,AVG(GoodsMoney) 平均价 from GoodsInfo + group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select * from GoodsInfo + where GoodsColor = '红色' and GoodsMoney between 300 and 600 diff --git "a/2021 04 02 \344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/SQLQuery2.sql" "b/2021 04 02 \344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..294edaf23874fd6f25ec8c3b411c367d2a23211f --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/SQLQuery2.sql" @@ -0,0 +1,458 @@ +-- 练习题目: + + + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + +select * from + (select * from StudentCourseScore where courseId = 1) as A + inner join StudentInfo s on s.Id = A.Id, + (select * from StudentCourseScore where CourseId = 2) as B + where A.Score > B.Score and A.StudentId = B.StudentId + + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 + +select StudentId from StudentCourseScore + where CourseId = 1 or CourseId = 2 + group by StudentId + HAVING COUNT(distinct CourseId) = 2 + + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) + +select * from + (select * from StudentCourseScore where CourseId = 2) A + left join (select * from StudentCourseScore where CourseId = 1) B on A.StudentId = B.StudentId + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 + +select * from + (select * from StudentCourseScore where CourseId = 1) A + left join (select * from StudentCourseScore where CourseId = 2) B on A.StudentId = B.StudentId + where B.CourseId is null + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 + +select Studentcode 学生编号,StudentName 学生姓名,AVG(Score) 平均成绩 from StudentCourseScore + inner join StudentInfo on StudentCourseScore.StudentId = StudentInfo.StudentCode + group by Studentcode,StudentName + HAVING AVG(Score) >= 60 + +-- 3.查询在 成绩 表存在成绩的学生信息 + +select * from StudentCourseScore + left join StudentInfo on StudentCourseScore.StudentId = StudentInfo.StudentCode + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +select StudentCode ,StudentName ,COUNT(CourseId) ,SUM(Score) from StudentCourseScore scs + right join StudentInfo si on scs.StudentId = si.StudentCode + group by StudentCode,StudentName + +-- 4.1 查有成绩的学生信息 + +select * from StudentCourseScore scs + inner join StudentInfo si on scs.StudentId = si.StudentCode + +-- 5.查询「李」姓老师的数量 + +select COUNT(TeacherName) from Teachers + where TeacherName like '李%' + +-- 6.查询学过 「张三」老师授课 的 同学的信息 + +select * from StudentCourseScore A + inner join CourseInfo B on A.CourseId = B.Id + inner join StudentInfo C on A.StudentId = C.StudentCode + inner join Teachers D on B.TeacherId = D.Id + where TeacherName = '张三' + +-- 7.查询没有学全所有课程的同学的信息 +select * from CourseInfo +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers + +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode,StudentName + HAVING COUNT(CourseId) < (select COUNT(CourseName) from CourseInfo) + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode ,StudentName + having count(CourseId) > 0 + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode ,StudentName + having count(CourseId)=(select COUNT(CourseName) from CourseInfo) + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select distinct studentName from StudentCourseScore A + inner join CourseInfo B on A.CourseId = B.Id + inner join StudentInfo C on A.StudentId = C.StudentCode + inner join Teachers D on B.TeacherId = D.Id + where TeacherName != '张三' + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 + +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..bc0eff663147abc5dde96231a9b1eba649b05c1a --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/SQLQuery3.sql" @@ -0,0 +1,86 @@ +--1、创建数据库HOUSE_DB, +--要求: +--指定数据文件大小:5兆, +--指定文件最大值:50兆, +--文件增长率:1兆 +use master +go + +create database HOUSE_DB +on +( + name = HOUSE_DB, + filename ='D:\HOUSE_DB.mfg', + size = 5MB, + maxsize = 50MB, + filegrowth = 1MB +) +use HOUSE_DB + +--2、创建数据表 +--房屋类型表(HOUSE_TYPE) +--字段名 类型 是否可为空 约束 说明 +--type_id int N 主键,自增长 类型编号 +--type_name varchar(50) N 唯一约束 类型名称 + +create table HOUSE_TYPE +( + type_id int primary key identity , + type_name varchar(20) unique +) + +--房屋信息表(HOUSE) +--字段名 类型 是否可为空 约束 说明 +--house_id int N 主键,自增长 房屋编号 +--house_name varchar(50) N 房屋名称 +--house_price float N 默认值0 房租 +--type_id int N 外键依赖HOUSE_TYPE表 房屋类型 + +create table HOUSE +( + house_id int primary key identity , + house_name varchar(50) , + house_price float default('0'), + type_id int references HOUSE_TYPE(type_id) +) + +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 + +insert into HOUSE_TYPE(type_name) +select '小户型' union +select '经济型' union +select '别墅' + +insert into HOUSE(house_name,house_price,type_id) +select '蔡','5000','3' union +select '东','10000','2' union +select '生','50000','1' + +--4、添加查询 +--查询所有房屋信息 + +select * from HOUSE A + inner join HOUSE_TYPE B on A.type_id = B.type_id + +--使用模糊查询包含”型“字的房屋类型信息 + +select * from HOUSE A + inner join HOUSE_TYPE B on a.type_id = B.type_id + where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 + +select house_name 名字,house_price 租金 from HOUSE + order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 + +select house_name 名称,type_name 类型名称 from HOUSE A + inner join HOUSE_TYPE B on A.type_id = B.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 + +select house_price ,house_name from HOUSE where house_price = (select MAX(house_price) from HOUSE) + diff --git "a/2021 04 02 \344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/SQLQuery4.sql" "b/2021 04 02 \344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c6f9245f44dd18ee1c60cf0f97494dc62aef1744 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/SQLQuery4.sql" @@ -0,0 +1,88 @@ +--1、创建明星数据库(StarManagerDB),然后建立两张表,StarType(明星类型表),StarInfo(明星信息表),表结构分别如下: +--StarType(明星类型表) +--字段名 说明 类型 长度 可否为空 约束 +--T_NO 明星类型编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--T_NAME 明星类型 nvarchar 20 + +use master +go + +create database StarManagerDB +go + +use StarManagerDB +go + +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) + +--StarInfo(明星信息表) +--字段名 说明 类型 长度 可否为空 约束 +--S_NO 明星编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--S_NAME 明星姓名 nvarchar 20 否 +--S_AGE 明星年龄 int 否 +--S_HOBBY 特技 nvarchar 20 +--S_NATIVE 明星籍贯 nvarchar 20 默认约束:中国大陆 +--S_T_NO 明星类型编号 int 外键,参照StarType表的主键T_NO + +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20) , + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) + +--2、使用插入语句为两张表添加数据 + +--明星类型表(StarType) +--明星类型编号 明星类型 +--1 体育明星 +--2 IT明星 +--3 相声演员 + +insert into StarType +select '体育明星' union +select 'IT明星' union +select '相声演员' + +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 + +insert into StarInfo(S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) +select '梅西',30,'射门','阿根廷',1 union +select '科比',35,'过人','美国',1 union +select '蔡景现',40,'敲代码','中国',2 union +select '马斯克',36,'造火箭','外星人',2 union +select '郭德纲',50,'相声','中国',3 union +select '黄铮',41,'拼多多','中国',2 + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 + +select top 3 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo + order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 + +select S_T_NO as 类型,count(*) as 人数,avg(S_Age) as 平均年龄 from StarInfo + group by S_T_NO + + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 + +select top 1 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo + where S_T_NO = 1 + order by S_AGE desc + + diff --git "a/2021 04 02 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..180832bbb9627951fe5be026e83f6633f38c46ab --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery1.sql" @@ -0,0 +1,61 @@ +use master +go +create database GoodsDB +on( +name='GoodsDB', +filename='D:\GoodsDB_ldf', +size=5, +maxsize=100, +filegrowth=5% + + +) +log on ( +name='GoodsDB_log', +filename='D:\GoodsDB_log_mdf', +size=5, +maxsize=100, +filegrowth=5% + +) +go +use GoodsDB +go +create table GoodsType +( +TypeID int primary key identity(1,1) not null, +TypeName nvarchar(20) not null +) +create table GoodsInfo +( +GoodsID int primary key identity(1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int references GoodsType(TypeID) + +) +insert into GoodsType values('服装内衣'),('鞋包服'),('手机数码') +insert into GoodsInfo values('提花小西装', '红色','菲曼琪', '300', 1), +('无袖背心',' 白色', '阿依莲', '700', 1), +('低帮休闲鞋', '红色', '菲曼琪', '900', 2), +('提花小西装', '红色','菲曼琪', '300', 1), +('中跟单鞋' ,'绿色' ,'哥弟 ','400 ',2), + ('平底鞋', '白色', '阿依莲', '200', 2), +(' 迷你照相机', '红色','尼康', '500', 3), +('硬盘', '黑色', '希捷', '600', 3), +(' 显卡', '黑色', '技嘉','800', 3) + + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + +select MAX(GoodsMoney) from GoodsInfo +select GoodsName 商品名称,GoodsMoney 商品价格 from GoodsInfo inner join GoodsType on GoodsInfo.TypeID=GoodsType.TypeID where GoodsMoney=(select MAX(GoodsMoney) from GoodsInfo ) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select GoodsInfo.TypeID 商品编号,MAX(GoodsMoney)最高价格,min(GoodsMoney)最低价格 ,avg(GoodsMoney)平均价格 from GoodsInfo inner join GoodsType on GoodsInfo.TypeID=GoodsType.TypeID group by GoodsInfo.TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney in(300 , 600) \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery2.sql" "b/2021 04 02 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1fb1473ae8ebc218ff35dcacb679e8218f9c4f00 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery2.sql" @@ -0,0 +1,288 @@ + + + + +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + +-- 练习题目: +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +SELECT StudentName,B.StudentId,B.Score 数学,A.Score 语文 FROM +(select CourseId,Score,StudentId from StudentCourseScore WHERE CourseId='2')A INNER JOIN +(select CourseId,Score,StudentId from StudentCourseScore WHERE CourseId='1')B INNER JOIN +StudentInfo ON B.StudentId=StudentInfo.id +ON A.studentid=B.StudentId +WHERE A.Score60 +-- 3.查询在 成绩 表存在成绩的学生信息 + +select * from StudentCourseScore S LEFT join StudentInfo ST on S.StudentId=ST.Id + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +select StudentInfo.Id 学生编号,StudentInfo.StudentName 学生姓名,count(*) 选课总数, sum(Score)总分 from StudentCourseScore + inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id +group by StudentInfo.Id,StudentInfo.StudentName + +-- 4.1 查有成绩的学生信息 + +select * from StudentCourseScore inner join StudentInfo on StudentCourseScore.Id=StudentInfo.Id + +-- 5.查询「李」姓老师的数量 + +SELECT * FROM Teachers WHERE TeacherName LIKE '李%' + +-- 6.查询学过「张三」老师授课的同学的信息 + +select * from StudentCourseScore inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id inner join Teachers on StudentCourseScore.CourseId=Teachers.Id where Teachers.Id=1 + +-- 7.查询没有学全所有课程的同学的信息 +select distinct(s.StudentId),StudentInfo.StudentName, s.*,sc1.score,sc2.score,sc3.score from StudentCourseScore s +left join StudentCourseScore sc1 on s.studentid = sc1.studentid and sc1.CourseId = '1' +left join StudentCourseScore sc2 on s.studentid = sc2.studentid and sc2.CourseId = '2' +left join StudentCourseScore sc3 on s.studentid = sc3.studentid and sc3.CourseId = '3' +inner join StudentInfo on s.StudentId=StudentInfo.Id +where sc1.score is null or sc2.score is null or sc3.score is null + + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode ,StudentName + having count(CourseId) > 0 +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + + +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode ,StudentName + having count(CourseId)=(select COUNT(CourseName) from CourseInfo) + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select distinct studentName from StudentCourseScore A + inner join CourseInfo B on A.CourseId = B.Id + inner join StudentInfo C on A.StudentId = C.StudentCode + inner join Teachers D on B.TeacherId = D.Id + where TeacherName != '张三' + diff --git "a/2021 04 02 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..11b06ac7e296af040556d3a0a6b74bd192e71ea7 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery3.sql" @@ -0,0 +1,86 @@ +--1、创建数据库HOUSE_DB, +--要求: +--指定数据文件大小:5兆, +--指定文件最大值:50兆, +--文件增长率:1兆 +use master +go + +create database HOUSE_DB +on +( + name = HOUSE_DB, + filename ='D:\HOUSE_DB.mfg', + size = 5MB, + maxsize = 50MB, + filegrowth = 1MB +) +use HOUSE_DB + +--2、创建数据表 +--房屋类型表(HOUSE_TYPE) +--字段名 类型 是否可为空 约束 说明 +--type_id int N 主键,自增长 类型编号 +--type_name varchar(50) N 唯一约束 类型名称 + +create table HOUSE_TYPE +( + type_id int primary key identity , + type_name varchar(20) unique +) + +--房屋信息表(HOUSE) +--字段名 类型 是否可为空 约束 说明 +--house_id int N 主键,自增长 房屋编号 +--house_name varchar(50) N 房屋名称 +--house_price float N 默认值0 房租 +--type_id int N 外键依赖HOUSE_TYPE表 房屋类型 + +create table HOUSE +( + house_id int primary key identity , + house_name varchar(50) , + house_price float default('0'), + type_id int references HOUSE_TYPE(type_id) +) + +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 + +insert into HOUSE_TYPE(type_name) +select '小户型' union +select '经济型' union +select '别墅' + +insert into HOUSE(house_name,house_price,type_id) +select '蔡','5000','3' union +select '东','10000','2' union +select '生','50000','1' + +--4、添加查询 +--查询所有房屋信息 + +select * from HOUSE A + inner join HOUSE_TYPE B on A.type_id = B.type_id + +--使用模糊查询包含”型“字的房屋类型信息 + +select * from HOUSE A + inner join HOUSE_TYPE B on a.type_id = B.type_id + where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 + +select house_name 名字,house_price 租金 from HOUSE + order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 + +select house_name 名称,type_name 类型名称 from HOUSE A + inner join HOUSE_TYPE B on A.type_id = B.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 + +select house_price ,house_name from HOUSE where house_price = (select MAX(house_price) from HOUSE) + diff --git "a/2021 04 02 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery4.sql" "b/2021 04 02 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cd3d67f97d3cb4545623816fb8e8c9fd1d4fcae9 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery4.sql" @@ -0,0 +1,88 @@ +--1、创建明星数据库(StarManagerDB),然后建立两张表,StarType(明星类型表),StarInfo(明星信息表),表结构分别如下: +--StarType(明星类型表) +--字段名 说明 类型 长度 可否为空 约束 +--T_NO 明星类型编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--T_NAME 明星类型 nvarchar 20 + +use master +go + +create database StarManagerDB +go + +use StarManagerDB +go + +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) + +--StarInfo(明星信息表) +--字段名 说明 类型 长度 可否为空 约束 +--S_NO 明星编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--S_NAME 明星姓名 nvarchar 20 否 +--S_AGE 明星年龄 int 否 +--S_HOBBY 特技 nvarchar 20 +--S_NATIVE 明星籍贯 nvarchar 20 默认约束:中国大陆 +--S_T_NO 明星类型编号 int 外键,参照StarType表的主键T_NO + +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20) , + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) + +--2、使用插入语句为两张表添加数据 + +--明星类型表(StarType) +--明星类型编号 明星类型 +--1 体育明星 +--2 IT明星 +--3 相声演员 + +insert into StarType +select '体育明星' union +select 'IT明星' union +select '相声演员' + +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 + +insert into StarInfo(S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) +select '梅西',30,'射门','阿根廷',1 union +select '科比',35,'过人','美国',1 union +select '蔡景现',40,'敲代码','中国',2 union +select '马斯克',36,'造火箭','外星人',2 union +select '郭德纲',50,'相声','中国',3 union +select '黄铮',41,'拼多多','中国',2 + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 + +select top 3 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo + order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 + +select S_T_NO as 类型,count(*) as 人数,avg(S_Age) as 平均年龄 from StarInfo + group by S_T_NO + + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 + +select top 1 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo + where S_T_NO = 1 + order by S_AGE desc + + diff --git "a/2021 04 02 \344\275\234\344\270\232/\347\275\227\344\273\225\345\244\251/SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\347\275\227\344\273\225\345\244\251/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f07bc40ac73ced3e0b2bb32e418629093a968e7a --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\347\275\227\344\273\225\345\244\251/SQLQuery3.sql" @@ -0,0 +1,61 @@ +create database GoodsDB +go +use GoodsDB +go +create table GoodsType +( +TypeID int not null primary key identity , +TypeName nvarchar(20) not null +) +go + +create table GoodsInfo +( +GoodsID int not null primary key identity, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20) not null, +GoodsMoney money not null, +TypeID int foreign key references GoodsType(TypeID) +) +go + +insert into GoodsType(TypeName) values ('服装内衣'),('鞋包配饰'),('手机数码') +go + +insert into GoodsInfo(GoodsName ,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +values('提花小西装','红色','菲曼琪','300','1'), + + ('百搭短裤','绿色',' 哥弟','100','1'), + + (' 无袖背心','白色 ','阿依莲','700','1'), + + ('低帮休闲鞋 ','红色','菲曼琪','900','2'), + + ('中跟单鞋','绿色','哥弟','400','2'), + + ('平底鞋',' 白色',' 阿依莲','200','2'), + + ('迷你照相机','红色','尼康','500','3'), + + ('硬盘 ','黑色','希捷','600','3'), + + ('显卡 ','黑色 ',' 技嘉','800','3') + +go + +select * from GoodsType +select max(GoodsMoney) from GoodsInfo + + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName, GoodsColor ,GoodsMoney from GoodsInfo where GoodsMoney=(select max (GoodsMoney) from GoodsInfo) + +--4、按商品类型编号 分组查询 商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select TypeID,max(GoodsMoney) 最高,min(GoodsMoney) 最低 ,avg(GoodsMoney) 平均 from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>300 and GoodsMoney<600 + diff --git "a/2021 04 02 \344\275\234\344\270\232/\347\275\227\344\273\225\345\244\251/SQLQuery5.sql" "b/2021 04 02 \344\275\234\344\270\232/\347\275\227\344\273\225\345\244\251/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f8ad48c8b4dc33428b4624854b35f942c248070f --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\347\275\227\344\273\225\345\244\251/SQLQuery5.sql" @@ -0,0 +1,55 @@ +create database HOUSE_DB +go +use HOUSE_DB +go + +create table HOUSE_TYPE +( +type_id int not null primary key identity , +type_name varchar(50) not null unique +) +go + +create table HOUSE +( +house_id int not null primary key identity , +house_name varchar(50) not null, +house_price float not null default ('0'), +type_id int not null foreign key references HOUSE_TYPE(type_id) +) + +go + + +select * from HOUSE_TYPE + +insert into HOUSE_TYPE(type_name) values ('小户型'),('经济型'),('别墅') +go +insert into HOUSE(house_name,house_price,type_id) values('皇家','10000','3'), + ('大宅','1000','2'), + ('小窝','100','1') +insert into HOUSE(house_name,house_price,type_id) values('天帝型','100000','3') + +select * from HOUSE_TYPE +select max (house_price) from HOUSE + + +--查询所有房屋信息 + +select * from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 + +select * from HOUSE where house_name like '%%型' + +--查询出房屋的名称和租金,并且按照租金降序排序 + +select house_name,house_price from HOUSE group by house_price,house_name order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 + +select house_name,type_name from HOUSE_TYPE e inner join HOUSE r on e.type_id=r.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 + +select house_price,house_name from HOUSE where house_price=(select max (house_price) from HOUSE) \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\347\275\227\344\273\225\345\244\251/SQLQuery6.sql" "b/2021 04 02 \344\275\234\344\270\232/\347\275\227\344\273\225\345\244\251/SQLQuery6.sql" new file mode 100644 index 0000000000000000000000000000000000000000..11c3e1a3d1ef47478d808eba705ef98595195ba6 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\347\275\227\344\273\225\345\244\251/SQLQuery6.sql" @@ -0,0 +1,62 @@ +create database StarManagerDB +go +use StarManagerDB +go + +create table StarType +( +T_NO int primary key identity , +T_NAME nvarchar(20) +) +go + + +create table StarInfo +( +S_NO int not null primary key identity , +S_NAME nvarchar(20) not null, +S_AGE int null , +S_HOBBY nvarchar(20) not null , +S_NATIVE nvarchar(20) default ('中国大陆'), +S_T_NO int foreign key references StarType(T_NO ) +) +go + +insert into StarType(T_NAME) values ('体育明星'), ('IT明星'), ('相声演员') + +go + +insert into StarInfo(S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) values('梅西','30','射门','阿根廷','1'), + + ('科比','35','过人','美国','1'), + + ('蔡景现','40','敲代码','中国','2'), + + ('马斯克','36','造火箭','外星人','2'), + + ('郭德纲','50','相声','中国','3'), + + ('黄铮','34','拼多多','中国','1') +go + + + +select * from StarType +select * from StarInfo + + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 + +select top 3 S_AGE 年龄,S_HOBBY 特技,S_NATIVE 籍贯信息 , S_NAME 姓名 + from StarInfo order by S_AGE DESC + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 + +select count(T_NO) 分组的明星人数,avg(S_AGE) 平均年龄 from + StarType e inner join StarInfo r on e.T_NO=r.S_T_NO group by e.T_NO + having count(T_NO)>=2 + + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 + +select top 1 S_AGE,S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarType e inner join StarInfo r on e.T_NO=r.S_T_NO where T_NO=1 order by S_AGE desc \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\347\275\227\346\265\252\345\255\220/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\347\275\227\346\265\252\345\255\220/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..06cc25e6af418e57a4b6d0f91c9daeaf692c75e6 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\347\275\227\346\265\252\345\255\220/SQLQuery1.sql" @@ -0,0 +1,461 @@ + +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers +-- select * from StudentInfo + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo +-- select * from Teachers +-- select * from StudentInfo + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore +-- select * from CourseInfo +-- select * from Teachers +-- select * from StudentInfo + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + + +-- 练习题目: + + + select * from StudentCourseScore + select * from CourseInfo + select * from Teachers + select * from StudentInfo + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +select * from +(select StudentId,SCore,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.Id=SI.Id where CourseId=1) A +full join StudentInfo C on C.Id=A.Id, +(select StudentId,SCore,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.Id=SI.Id where CourseId=2) B +where A.Score=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 diff --git "a/2021 04 02 \344\275\234\344\270\232/\347\275\227\346\265\252\345\255\220/\345\244\215\344\271\240\351\242\230 SQLQuery2.sql" "b/2021 04 02 \344\275\234\344\270\232/\347\275\227\346\265\252\345\255\220/\345\244\215\344\271\240\351\242\230 SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1b1bb7b6b41a46e09922e81aa5b694227f72d72d --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\347\275\227\346\265\252\345\255\220/\345\244\215\344\271\240\351\242\230 SQLQuery2.sql" @@ -0,0 +1,57 @@ +use master +go + +create database GoodsDB +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity not null, + TypeName nvarchar(20) not null +) +go + +create table GoodsInfo +( + GoodsID int not null primary key identity, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) +go + +insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') + +insert into GoodsInfo values('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋 ','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) +go + +select * from GoodsType +select * from GoodsInfo + +--查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName,GoodsColor,GoodsMoney +from GoodsInfo GI inner join GoodsType GT on GI.TypeID=Gt.TypeID +order by GoodsMoney desc + +--按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeName,max(GoodsMoney)最高价格,min(GoodsMoney)最低价格,avg(GoodsMoney)平均价格 +from GoodsInfo GI inner join GoodsType GT on GI.TypeID=Gt.TypeID +group by GI.TypeID,TypeName + +--查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * +from GoodsInfo +where GoodsColor='红色' and GoodsMoney >300 and GoodsMoney <600 diff --git "a/2021 04 02 \344\275\234\344\270\232/\347\275\227\346\265\252\345\255\220/\345\244\215\344\271\240\351\242\230SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\347\275\227\346\265\252\345\255\220/\345\244\215\344\271\240\351\242\230SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..09c5291241523b1a51199faa085479c9ae04e85c --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\347\275\227\346\265\252\345\255\220/\345\244\215\344\271\240\351\242\230SQLQuery3.sql" @@ -0,0 +1,69 @@ +use master +go + +create database HOUSE_DB +on +( + name=HOUSE_DB_mbf, + filename='D:\HOUSE_DB.mbf', + size=5, + filegrowth=1MB +) +log on +( + name=HOUSE_DB_dbf, + filename='D:\HOUSE_DB_log.lbf', + size=5, + filrgrowth=1MB +) +go + +create table HOUSE_TYPE +( + type_id int not null primary key identity, + type_name varchar(50) not null +) +go + +create table HOUSE +( + house_id int not null primary key identity, + house_name varchar(50) not null, + house_price float not null default(0), + type_id int not null references HOUSE_TYPE(type_id) +) +go + +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') +go + +--HOUSE表中添加至少3条数据,不能全都为同一类型 +insert into HOUSE values +('房1',1000,1), +('房2',2000,2), +('房3',3000,3) + + +--查询所有房屋信息 +select * +from HOUSE HO inner join HOUSE_TYPE HT on HO.type_id=HT.type_id + +--使用模糊查询包含”型“字的房屋类型信息 +select * +from HOUSE HO inner join HOUSE_TYPE HT on HO.type_id=HT.type_id +where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select type_name,house_price +from HOUSE HO inner join HOUSE_TYPE HT on HO.type_id=HT.type_id +order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name,type_name +from HOUSE HO inner join HOUSE_TYPE HT on HO.type_id=HT.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select house_name,house_price +from HOUSE HO inner join HOUSE_TYPE HT on HO.type_id=HT.type_id +where house_price=(select max(house_price) from HOUSE) \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\347\275\227\346\265\252\345\255\220/\345\244\215\344\271\240\351\242\230SQLQuery5.sql" "b/2021 04 02 \344\275\234\344\270\232/\347\275\227\346\265\252\345\255\220/\345\244\215\344\271\240\351\242\230SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2f33e86d4a693166f43f724f5487e919c15f8388 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\347\275\227\346\265\252\345\255\220/\345\244\215\344\271\240\351\242\230SQLQuery5.sql" @@ -0,0 +1,60 @@ +use master +go + +create database StarManagerDB +go + +use StarManagerDB +go + +create table StarType +( + T_NO int not null primary key identity, + T_NAME nvarchar(20) +) +go + +create table StarInfo +( + S_NO int not null primary key identity, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) +go + +insert into StarType values +('体育明星'), +('IT明星'), +('相声演员') + +insert into StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) +go + +--查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名 +select top 3 S_NAME,S_HOBBY,S_NATIVE +from StarInfo SI inner join StarType ST on SI.S_T_NO=ST.T_NO +order by S_AGE desc + +--按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select count(S_T_NO)明星人数,avg(S_AGE) 平均年龄 +from StarInfo SI inner join StarType ST on SI.S_T_NO=ST.T_NO +group by T_NAME +having count(S_T_NO)>2 + + +select * from StarInfo +select * from StarType +--查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select S_NAME,S_HOBBY,S_NATIVE +from StarInfo SI inner join StarType ST on SI.S_T_NO=ST.T_NO +where S_T_NO=1 + diff --git "a/2021 04 02 \344\275\234\344\270\232/\350\221\243\344\270\226\351\276\231/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\350\221\243\344\270\226\351\276\231/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..914c1778e5309c46297012ceee2e3010ac8637dd --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\350\221\243\344\270\226\351\276\231/SQLQuery1.sql" @@ -0,0 +1,86 @@ +--1、创建商品数据库(GoodsDB),然后建立两张表,GoodsType(商品类型表),GoodsInfo(商品信息表),表结构分别如下: +--商品类型表(GoodsType) +--字段名 说明 类型 长度 可否为空 约束 +--TypeID 商品类型编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--TypeName 商品类型名称 nvarchar 20 否 + +create database GoodsDB +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) + +--商品信息表(GoodsInfo) +--字段名 说明 类型 长度 可否为空 约束 +--GoodsID 商品编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--GoodsName 商品名称 nvarchar 20 否 +--GoodsColor 商品颜色 nvarchar 20 否 +--GoodsBrand 商品品牌 nvarchar 20 +--GoodsMoney 商品价格 money 否 +--TypeID 商品类型编号 int 外键,参照GoodsType中的TypeID + +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +--2、使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 + +insert into GoodsType(TypeName) +select '服装内衣' union +select '鞋包配饰' union +select '手机数码' + +--商品信息表(GoodsType) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 + +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +select '提花小西装','红色','菲曼琪',300,1 union +select '百搭短裤','绿色','哥弟',100,1 union +select '无袖背心','白色','阿依莲',700,1 union +select '低帮休闲鞋','红色','菲曼琪',900,2 union +select '中跟单鞋','绿色','哥弟',400,2 union +select '平底鞋','白色','阿依莲',200,2 union +select '迷你照相机','红色','尼康',500,3 union +select '硬盘','黑色','希捷',600,3 union +select '显卡','黑色','技嘉',800,3 + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo + where GoodsMoney = (select MAX(GoodsMoney) from GoodsInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select MAX(GoodsMoney) 最高价格,MIN(GoodsMoney) 最低价格,AVG(GoodsMoney) 平均价 from GoodsInfo + group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select * from GoodsInfo + where GoodsColor = '红色' and GoodsMoney between 300 and 600 diff --git "a/2021 04 02 \344\275\234\344\270\232/\350\221\243\344\270\226\351\276\231/SQLQuery2.sql" "b/2021 04 02 \344\275\234\344\270\232/\350\221\243\344\270\226\351\276\231/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..294edaf23874fd6f25ec8c3b411c367d2a23211f --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\350\221\243\344\270\226\351\276\231/SQLQuery2.sql" @@ -0,0 +1,458 @@ +-- 练习题目: + + + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + +select * from + (select * from StudentCourseScore where courseId = 1) as A + inner join StudentInfo s on s.Id = A.Id, + (select * from StudentCourseScore where CourseId = 2) as B + where A.Score > B.Score and A.StudentId = B.StudentId + + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 + +select StudentId from StudentCourseScore + where CourseId = 1 or CourseId = 2 + group by StudentId + HAVING COUNT(distinct CourseId) = 2 + + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) + +select * from + (select * from StudentCourseScore where CourseId = 2) A + left join (select * from StudentCourseScore where CourseId = 1) B on A.StudentId = B.StudentId + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 + +select * from + (select * from StudentCourseScore where CourseId = 1) A + left join (select * from StudentCourseScore where CourseId = 2) B on A.StudentId = B.StudentId + where B.CourseId is null + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 + +select Studentcode 学生编号,StudentName 学生姓名,AVG(Score) 平均成绩 from StudentCourseScore + inner join StudentInfo on StudentCourseScore.StudentId = StudentInfo.StudentCode + group by Studentcode,StudentName + HAVING AVG(Score) >= 60 + +-- 3.查询在 成绩 表存在成绩的学生信息 + +select * from StudentCourseScore + left join StudentInfo on StudentCourseScore.StudentId = StudentInfo.StudentCode + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +select StudentCode ,StudentName ,COUNT(CourseId) ,SUM(Score) from StudentCourseScore scs + right join StudentInfo si on scs.StudentId = si.StudentCode + group by StudentCode,StudentName + +-- 4.1 查有成绩的学生信息 + +select * from StudentCourseScore scs + inner join StudentInfo si on scs.StudentId = si.StudentCode + +-- 5.查询「李」姓老师的数量 + +select COUNT(TeacherName) from Teachers + where TeacherName like '李%' + +-- 6.查询学过 「张三」老师授课 的 同学的信息 + +select * from StudentCourseScore A + inner join CourseInfo B on A.CourseId = B.Id + inner join StudentInfo C on A.StudentId = C.StudentCode + inner join Teachers D on B.TeacherId = D.Id + where TeacherName = '张三' + +-- 7.查询没有学全所有课程的同学的信息 +select * from CourseInfo +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers + +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode,StudentName + HAVING COUNT(CourseId) < (select COUNT(CourseName) from CourseInfo) + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode ,StudentName + having count(CourseId) > 0 + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode ,StudentName + having count(CourseId)=(select COUNT(CourseName) from CourseInfo) + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select distinct studentName from StudentCourseScore A + inner join CourseInfo B on A.CourseId = B.Id + inner join StudentInfo C on A.StudentId = C.StudentCode + inner join Teachers D on B.TeacherId = D.Id + where TeacherName != '张三' + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 + +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\350\221\243\344\270\226\351\276\231/SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\350\221\243\344\270\226\351\276\231/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..bc0eff663147abc5dde96231a9b1eba649b05c1a --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\350\221\243\344\270\226\351\276\231/SQLQuery3.sql" @@ -0,0 +1,86 @@ +--1、创建数据库HOUSE_DB, +--要求: +--指定数据文件大小:5兆, +--指定文件最大值:50兆, +--文件增长率:1兆 +use master +go + +create database HOUSE_DB +on +( + name = HOUSE_DB, + filename ='D:\HOUSE_DB.mfg', + size = 5MB, + maxsize = 50MB, + filegrowth = 1MB +) +use HOUSE_DB + +--2、创建数据表 +--房屋类型表(HOUSE_TYPE) +--字段名 类型 是否可为空 约束 说明 +--type_id int N 主键,自增长 类型编号 +--type_name varchar(50) N 唯一约束 类型名称 + +create table HOUSE_TYPE +( + type_id int primary key identity , + type_name varchar(20) unique +) + +--房屋信息表(HOUSE) +--字段名 类型 是否可为空 约束 说明 +--house_id int N 主键,自增长 房屋编号 +--house_name varchar(50) N 房屋名称 +--house_price float N 默认值0 房租 +--type_id int N 外键依赖HOUSE_TYPE表 房屋类型 + +create table HOUSE +( + house_id int primary key identity , + house_name varchar(50) , + house_price float default('0'), + type_id int references HOUSE_TYPE(type_id) +) + +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 + +insert into HOUSE_TYPE(type_name) +select '小户型' union +select '经济型' union +select '别墅' + +insert into HOUSE(house_name,house_price,type_id) +select '蔡','5000','3' union +select '东','10000','2' union +select '生','50000','1' + +--4、添加查询 +--查询所有房屋信息 + +select * from HOUSE A + inner join HOUSE_TYPE B on A.type_id = B.type_id + +--使用模糊查询包含”型“字的房屋类型信息 + +select * from HOUSE A + inner join HOUSE_TYPE B on a.type_id = B.type_id + where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 + +select house_name 名字,house_price 租金 from HOUSE + order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 + +select house_name 名称,type_name 类型名称 from HOUSE A + inner join HOUSE_TYPE B on A.type_id = B.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 + +select house_price ,house_name from HOUSE where house_price = (select MAX(house_price) from HOUSE) + diff --git "a/2021 04 02 \344\275\234\344\270\232/\350\221\243\344\270\226\351\276\231/SQLQuery4.sql" "b/2021 04 02 \344\275\234\344\270\232/\350\221\243\344\270\226\351\276\231/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c6f9245f44dd18ee1c60cf0f97494dc62aef1744 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\350\221\243\344\270\226\351\276\231/SQLQuery4.sql" @@ -0,0 +1,88 @@ +--1、创建明星数据库(StarManagerDB),然后建立两张表,StarType(明星类型表),StarInfo(明星信息表),表结构分别如下: +--StarType(明星类型表) +--字段名 说明 类型 长度 可否为空 约束 +--T_NO 明星类型编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--T_NAME 明星类型 nvarchar 20 + +use master +go + +create database StarManagerDB +go + +use StarManagerDB +go + +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) + +--StarInfo(明星信息表) +--字段名 说明 类型 长度 可否为空 约束 +--S_NO 明星编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--S_NAME 明星姓名 nvarchar 20 否 +--S_AGE 明星年龄 int 否 +--S_HOBBY 特技 nvarchar 20 +--S_NATIVE 明星籍贯 nvarchar 20 默认约束:中国大陆 +--S_T_NO 明星类型编号 int 外键,参照StarType表的主键T_NO + +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20) , + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) + +--2、使用插入语句为两张表添加数据 + +--明星类型表(StarType) +--明星类型编号 明星类型 +--1 体育明星 +--2 IT明星 +--3 相声演员 + +insert into StarType +select '体育明星' union +select 'IT明星' union +select '相声演员' + +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 + +insert into StarInfo(S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) +select '梅西',30,'射门','阿根廷',1 union +select '科比',35,'过人','美国',1 union +select '蔡景现',40,'敲代码','中国',2 union +select '马斯克',36,'造火箭','外星人',2 union +select '郭德纲',50,'相声','中国',3 union +select '黄铮',41,'拼多多','中国',2 + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 + +select top 3 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo + order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 + +select S_T_NO as 类型,count(*) as 人数,avg(S_Age) as 平均年龄 from StarInfo + group by S_T_NO + + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 + +select top 1 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo + where S_T_NO = 1 + order by S_AGE desc + + diff --git "a/2021 04 02 \344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ba7fd89937d3d5c45406b9b859ae454a9fe8c9e4 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery1.sql" @@ -0,0 +1,55 @@ +create database GoodsDB +on +( + + name='GoodsDB', + filename='D:\GoodsDB.mdf' +) +log on +( + + name='GoodsDB_log', + filename='D:\GoodsDB_log.ldf' + + +) +go +use GoodsDB +create table GoodsType +( + TypeID int not null primary key identity, + TypeName nvarchar(20) not null + +) +go +create table GoodsInfo +( + GoodsID int not null primary key identity, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) + + +) + +insert into GoodsType values ('服装内衣'),('鞋包配饰'),('手机数码') +select * from GoodsType +insert into GoodsInfo values + ('提花小西装',' 红色', '菲曼琪', '300', '1'), + ('百搭短裤' ,'绿色' ,'哥弟' ,'100' ,'1'), + ('无袖背心', '白色', '阿依莲', '700', '1'), + ('低帮休闲鞋' ,'红色' ,'菲曼琪', '900' ,'2'), + ('中跟单鞋', '绿色', '哥弟', '400', '2'), + ('平底鞋', '白色' ,'阿依莲' ,'200',' 2'), + ('迷你照相机', '红色' ,'尼康', '500', '3'), + ('硬盘 ','黑色', '希捷', '600' ,'3'), + ('显卡' ,'黑色', '技嘉', '800', '3') + SELECT * from GoodsInfo + + SELECT top 1 GoodsName as 商品名称,GoodsColor as 商品颜色,GoodsMoney as 商品价格 from GoodsInfo order by GoodsMoney desc + + select TypeID,MAX(GoodsMoney) as 最高价格,min(GoodsMoney) as 最低价格,avg(GoodsMoney) as 平均价格 from GoodsInfo group by TypeID + + select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>300 and GoodsMoney<600 diff --git "a/2021 04 02 \344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery2.sql" "b/2021 04 02 \344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2d86defe64c7a8db1f89e4b942c464480cfbb4aa --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery2.sql" @@ -0,0 +1,103 @@ +use ClassicDb +go + + +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore + +select D.* from +(select A.StudentId a,A.CourseId b,A.Score c,B.StudentId d,B.CourseId e,B.Score f from +(select * from StudentCourseScore where CourseId = 1) as A , +(select * from StudentCourseScore where CourseId = 2) as B +where A.Score > B.Score and A.StudentId = B.StudentId) as C +inner join StudentInfo D on C.a = D.StudentCode + + + + +select StudentId,count(CourseId) from StudentCourseScore +where CourseId = 1 or CourseId = 2 +group by StudentId +having count(CourseId) = 2 + + +select * from (select * from StudentCourseScore where CourseId = 1) as A +right join (select * from StudentCourseScore where CourseId = 2) as B on A.StudentId = B.StudentId + + + +select * from (select * from StudentCourseScore where CourseId = 1) as A +left join (select * from StudentCourseScore where CourseId = 2) as B on A.StudentId = B.StudentId +where A.CourseId is null + + +select StudentId as 学生编号,StudentName as 学生姓名,avg(Score) as 平均成绩 from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +group by StudentId,StudentName +having avg(Score) >= 60 + + +select B.* from StudentCourseScore A +left join StudentInfo B on A.StudentId = B.StudentCode + + +select +StudentCode as 学生编号,StudentName as 学生姓名, +count(CourseId) as 选课总数,sum(Score) as 总成绩 +from StudentCourseScore A +right join StudentInfo B on A.StudentId = B.StudentCode +group by StudentCode,StudentName +order by StudentCode + + + +select StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +left join StudentInfo B on A.StudentId = B.StudentCode +group by StudentCode,StudentName,Birthday,Sex,ClassId + + + +select count(*) as 姓老师的数量 from Teachers +group by TeacherName +having TeacherName like ('李%') + + + + +select B.* from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +where A.CourseId = 1 + + + +select StudentId,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +group by StudentId,StudentName,Birthday,Sex,ClassId +having count(CourseId)<3 + + + +select StudentId,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 1 or CourseId = 2 or CourseId = 3 +group by StudentId,StudentName,Birthday,Sex,ClassId + + + +select StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 1 or CourseId = 2 or CourseId = 3 +group by StudentCode,StudentName,Birthday,Sex,ClassId +having count(CourseId) = 3 + + + +select StudentName from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +where A.CourseId != 1 +group by StudentCode,StudentName + diff --git "a/2021 04 02 \344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d05691a686eee14c51b30bd90a7e1483e943d833 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery3.sql" @@ -0,0 +1,42 @@ + +CREATE database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5, + maxsize=50, + filegrowth=1 +) +log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5, + maxsize=50, + filegrowth=1 +) +go +use HOUSE_DB +go +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name varchar(50) not null unique +) +create table HOUSE +( +house_id int primary key identity(1,1), +house_name varchar(50) not null , +house_price float not null default(0), +type_id int references HOUSE_TYPE(type_id) +) +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅' ) +insert into HOUSE values('小红屋',500,'1'),('经济屋',1000,'2'),('别墅屋',5000,'3') +select * from HOUSE_TYPE +select * from HOUSE +select * from HOUSE_TYPE where type_name like '%型%' +select * from HOUSE order by house_price desc +select house_name 房屋名称,type_name 房屋类型名称 from HOUSE A INNER JOIN HOUSE_TYPE B ON A.type_id=B.type_id +select top 1 house_price 租金,house_name 房屋名称 from HOUSE order by house_price desc + diff --git "a/2021 04 02 \344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery4.sql" "b/2021 04 02 \344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3c3ef987b4e8418896f3d20f18c72ae8249f6000 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery4.sql" @@ -0,0 +1,70 @@ +use master +go + +create database StarManagerDB +on +( + name='StarManagerDB', + filename='D:\test\StarManagerDB.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='StarManagerDB_log', + filename='D:\test\StarManagerDB_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +go + +use StarManagerDB +go + +create table StarType +( + T_NO int primary key identity not null , + T_NAME nvarchar(20) , +) +go + +create table StarInfo +( + S_NO int primary key identity not null , + S_NAME nvarchar(20) not null , + S_AGE int not null , + S_HOBBY nvarchar(20) , + S_NATIVE nvarchar(20) default('中国大陆') , + S_T_NO int references StarType(T_NO) +) +go + +insert StarType values +('体育明星'), +('IT明星'), +('相声演员') +go + +insert StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) +go + +select top 3 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo +order by S_AGE desc +go + +select S_T_NO as 类型,count(*) as 人数,avg(S_Age) as 平均年龄 from StarInfo +group by S_T_NO +go + +select top 1 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo +where S_T_NO = 1 +order by S_AGE desc + diff --git "a/2021 04 02 \344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7aab7272aaaae033e13b11ef5913b70d84cde7fe --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery1.sql" @@ -0,0 +1,53 @@ +use master +go +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB。mdf', + size =5, + maxsize=50, + filegrowth=50 +) +log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size =5, + maxsize=50, + filegrowth=50 +) +go +use HOUSE_DB +go +create table HOUSE_TYPE +( + type_id int primary key identity , -- 类型编号 + type_name varchar(50) unique , --类型名称 +) +create table HOUSE +( + house_id int primary key identity, --房屋编号 + house_name varchar(50) , --房屋名称 + house_price float default(0), --房租 + type_id int references HOUSE_TYPE(type_id) --房屋类型 +) + +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +insert into HOUSE_TYPE values ('小户型'),('经济型'),('别墅') +select * from HOUSE_TYPE +--HOUSE表中添加至少3条数据,不能全都为同一类型 +insert into HOUSE values ('小房子','500','2'),('公寓','5000','3'),('瓦房','1000','1') +select * from HOUSE + +--查询所有房屋信息 +select H.house_name 房屋名称 ,house_price 房租 ,type_name 类型名称 from HOUSE H ,HOUSE_TYPE HT where H.type_id=HT.Type_id +--使用模糊查询包含”型“字的房屋类型信息 +select H.house_name 房屋名称 ,house_price 房租 ,type_name 类型名称 from HOUSE H ,HOUSE_TYPE HT where H.type_id=HT.Type_id +and type_name like '%型' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 房屋名称 ,house_price 房租 from HOUSE order by house_price DESC +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select H.house_name 房屋名称 ,type_name 类型名称 from HOUSE H ,HOUSE_TYPE HT where H.type_id=HT.Type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select house_name 房屋名称 ,house_price 房租 from HOUSE where house_price= (select max(house_price) from HOUSE) diff --git "a/2021 04 02 \344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery2.sql" "b/2021 04 02 \344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4f3c14548780adf0f993608a19bd76fafa6fe3c5 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery2.sql" @@ -0,0 +1,265 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore + + +--1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +select * from StudentInfo S ,(select StudentId ,Score from StudentCourseScore where CourseId=1) A , +(select StudentId ,Score from StudentCourseScore where CourseId=2) B +where A.StudentId=B.StudentId and A.Score>B.Score and S.Id=A.StudentId + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select * from (select StudentId , CourseId from StudentCourseScore where CourseId=1 ) A, +(select StudentId , CourseId from StudentCourseScore where CourseId =2) B +where A.StudentId=B.StudentId + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select * from (select StudentId , CourseId from StudentCourseScore where CourseId=1 ) A left join +(select StudentId , CourseId from StudentCourseScore where CourseId =2) B on A.StudentId=B.StudentId + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select * from (select StudentId , CourseId from StudentCourseScore where CourseId=1 ) A left join +(select StudentId , CourseId from StudentCourseScore where CourseId =2) B on A.StudentId=B.StudentId +where B.CourseId is null and B.StudentId is null +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select SS.StudentId 学生编号, StudentName 学生姓名 ,avg(Score) 平均成绩 from StudentCourseScore SS inner join StudentInfo S on SS.StudentId=S.Id +group by SS.StudentId ,StudentName +-- 3.查询在 成绩 表存在成绩的学生信息 +select distinct S.* from StudentCourseScore SS left join StudentInfo S on SS.StudentId=S.Id + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select S.Id 学生编号, StudentName 学生姓名 ,count(CourseId), sum(Score) 总成绩 from StudentCourseScore SS right join StudentInfo S on SS.StudentId=S.Id +group by S.Id,StudentName + + +-- 4.1 查有成绩的学生信息 + +select distinct S.* from StudentCourseScore SS left join StudentInfo S on SS.StudentId=S.Id + + +-- 5.查询「李」姓老师的数量 + +select count(TeacherName) 李姓老师的数量 from Teachers where TeacherName like '李%' + +-- 6.查询学过「张三」老师授课的同学的信息 + +select distinct S.* from StudentCourseScore SS left join StudentInfo S on SS.StudentId=S.Id where CourseId=2 + +-- 7.查询没有学全所有课程的同学的信息 + +select S.* from StudentInfo S ,(select StudentId ,count(CourseId) bb from StudentCourseScore group by StudentId) A +where A.StudentId=S.Id and A.bb<3 + + +-- 8.查询至少有一门课 与学号为" 01 "的同学所学相同的同学的信息 +select * from StudentInfo S ,(select StudentId ,count(CourseId) a from StudentCourseScore group by StudentId having StudentId!=1) A +where A.StudentId= S.Id and A.a>=1 + + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +select * from StudentInfo S ,(select StudentId ,count(CourseId) a from StudentCourseScore group by StudentId having StudentId!=1) A +where A.StudentId= S.Id and A.a=3 + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 +select distinct StudentName 学生姓名 ,TeacherName 老师姓名 from StudentInfo S , Teachers T, CourseInfo C, StudentCourseScore SS +where S.Id=SS.StudentId and SS.CourseId=C.Id and C.TeacherId=T.Id and +T.TeacherName!='张三' + diff --git "a/2021 04 02 \344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..053ee76c6150ca7d90458c591a6eade3a628f9c8 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery3.sql" @@ -0,0 +1,77 @@ +use master +go +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\GoodsDB.mdf', + size =100, + maxsize=3000, + filegrowth=15% +) +log on +( + name='GoodsDB_log', + filename='D:\GoodsDB_log.ldf', + size =100, + maxsize=3000, + filegrowth=15% +) +go +use GoodsDB +go +create table GoodsType +( + TypeID int not null primary key identity , + TypeName nvarchar(20) not null +) + +create table GoodsInfo +( + GoodsID int not null primary key identity , + GoodsName nvarchar(20) not null , + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GOOdsType(TypeID) +) +insert into GoodsType(TypeName) values ('服装内衣'), ('鞋包配饰'), ('手机数码') + +insert into GoodsInfo values ('提花小西装','红色','菲曼琪','300 ','1'), ('百搭短裤','绿色','哥弟','100 ','1'), ('无袖背心','白色','阿依莲 ','700 ','1') +, ('低帮休闲鞋','红色','菲曼琪','900 ','2'), ('中跟单鞋','绿色','哥弟','400 ','2'), ('平底鞋','白色','阿依莲','200 ','2') +, ('迷你照相机','红色','尼康','500 ','3'), ('硬盘','黑色','希捷','600 ','3'), ('显卡','黑色','技嘉','800','3') +select * from GoodsType +select * from GoodsInfo + + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称, GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo where GoodsMoney=(select max(GoodsMoney) from GoodsInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型编号, max(GoodsMoney) 商品最高价格 ,min(GoodsMoney) 最低价格 ,avg(GoodsMoney) 平均价格 from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney >300 and GoodsMoney<600 + + + + + + + + + + + + + + + + + + + + + + + diff --git "a/2021 04 02 \344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery4.sql" "b/2021 04 02 \344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2e14563ec11f6e6ff7fe70fd19741651c91d8455 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery4.sql" @@ -0,0 +1,47 @@ +use master +go +create database StarManagerDB +on +( + name='StarManagerDB', + filename='D:\StarManagerDB.mdf', + size =5, + maxsize=50, + filegrowth=50 +) +log on +( + name='StarManagerDB_log', + filename='D:\StarManagerDB_log.ldf', + size =5, + maxsize=50, + filegrowth=50 +) +go +use StarManagerDB +go +create table StarType +( + T_NO int primary key identity, --明星类型编号 + T_NAME nvarchar(20) --明星类型 +) +create table StarInfo +( + S_NO int primary key identity, --明星编号 + S_NAME nvarchar(20) not null , --明星姓名 + S_AGE int not null, --明星年龄 + S_HOBBY nvarchar(20), --特技 + S_NATIVE nvarchar(20) default('中国大陆'), --明星籍贯 + S_T_NO int references StarType(T_NO) --明星类型编号 +) +insert into StarType values ('体育明星'),('IT明星'),('相声演员') +select * from StarType +insert into StarInfo values ('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1),('蔡景现',40,'敲代码','中国',2),('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3),('黄峥',41,'拼多多','中国',2) +select * from StarInfo +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo S join StarType P on S.S_T_NO=P.T_NO where S_AGE in(select top 3 S_AGE from StarInfo order by S_AGE DESC) +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 明星类型编号 , count(*) 明星人数,avg(S_AGE) 平均年龄 from StarInfo group by S_T_NO having count(*)>=2 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo S join StarType P on S.S_T_NO=P.T_NO where S_AGE =(select top 1 S_AGE from StarInfo where S_T_NO=1 order by S_AGE DESC) diff --git "a/2021 04 02 \344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/50\351\242\230.sql" "b/2021 04 02 \344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/50\351\242\230.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ff71c40cbd0c5ffc51b29f5e9ba4d3380df6119a --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/50\351\242\230.sql" @@ -0,0 +1,294 @@ +use ClassicDb +go + +create database ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + +select D.* from +(select A.StudentId a,A.CourseId b,A.Score c,B.StudentId d,B.CourseId e,B.Score f from +(select * from StudentCourseScore where CourseId = 1) as A , +(select * from StudentCourseScore where CourseId = 2) as B +where A.Score > B.Score and A.StudentId = B.StudentId) as C +inner join StudentInfo D on C.a = D.StudentCode + + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 + +select StudentId,count(CourseId) from StudentCourseScore +where CourseId = 1 or CourseId = 2 +group by StudentId +having count(CourseId) = 2 + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) + +select * from (select * from StudentCourseScore where CourseId = 1) as A +right join (select * from StudentCourseScore where CourseId = 2) as B on A.StudentId = B.StudentId + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 + +select * from (select * from StudentCourseScore where CourseId = 1) as A +left join (select * from StudentCourseScore where CourseId = 2) as B on A.StudentId = B.StudentId + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 + +select StudentId as 学生编号,StudentName as 学生姓名,avg(Score) as 平均成绩 from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +group by StudentId,StudentName +having avg(Score) >= 60 + +-- 3.查询在 成绩表 存在成绩的学生信息 + +select B.* from StudentCourseScore A +left join StudentInfo B on A.StudentId = B.StudentCode + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +select +StudentCode as 学生编号,StudentName as 学生姓名, +count(CourseId) as 选课总数,sum(Score) as 总成绩 +from StudentCourseScore A +right join StudentInfo B on A.StudentId = B.StudentCode +group by StudentCode,StudentName +order by StudentCode + +-- 4.1 查有成绩的学生信息 + +select StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +left join StudentInfo B on A.StudentId = B.StudentCode +group by StudentCode,StudentName,Birthday,Sex,ClassId + +-- 5.查询「李」姓老师的数量 + +select count(*) as 姓老师的数量 from Teachers +group by TeacherName +having TeacherName like ('李%') + + +-- 6.查询学过「张三」老师授课的同学的信息 + +select B.* from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +where A.CourseId = 1 + +-- 7.查询没有学全所有课程的同学的信息 + +select StudentId,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +group by StudentId,StudentName,Birthday,Sex,ClassId +having count(CourseId)<3 + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + +select StudentId,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 1 or CourseId = 2 or CourseId = 3 +group by StudentId,StudentName,Birthday,Sex,ClassId + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + +select StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 1 or CourseId = 2 or CourseId = 3 +group by StudentCode,StudentName,Birthday,Sex,ClassId +having count(CourseId) = 3 + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select StudentName from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +where A.CourseId != 1 +group by StudentCode,StudentName diff --git "a/2021 04 02 \344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..28aaefa3fbef2da2ca7dd4ab0add4c692d422606 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/SQLQuery1.sql" @@ -0,0 +1,57 @@ +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\GoodsDB.mdf', + size=10, + maxsize=100, + filegrowth=10 +) +log on +( + name='GoodsDB_log', + filename='D:\GoodsDB_log.ldf', + size=10, + maxsize=100, + filegrowth=10 +) +go +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity(1,1), + TypeName nvarchar(20) not null, +) +create table GoodsInfo +( + GoodsID int primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) not null, + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) +insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') +insert into GoodsInfo values('小西装',' 红色' ,'菲曼琪' ,'300' ,'1'), +(' 百搭短裤',' 绿色' ,'哥弟' ,'100 ' ,'1'), +('无袖背心',' 白色' ,' 阿依莲' ,'700 ' ,'1'), +('低帮休闲鞋',' 红色' ,'菲曼琪 ' ,'900 ' ,'2'), +('中跟单鞋',' 绿色' ,'哥弟' ,'400 ' ,'2'), +('平底鞋',' 白色' ,'阿依莲' ,'200 ' ,'2'), +('迷你照相机',' 红色' ,'尼康' ,'500 ' ,'3'), +('硬盘',' 黑色' ,'希捷' ,'600 ' ,'3'), +('显卡',' 黑色' ,'技嘉' ,'800 ' ,'3') + +select * from GoodsType +select * from GoodsInfo + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName as 商品名称,GoodsColor as 商品颜色,GoodsMoney 商品价格 from GoodsInfo order by GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID as 商品类型编号, MAX(GoodsMoney) as 最高价格,MIN(GoodsMoney) as 最低价格,AVG(GoodsMoney) as 平均价格 from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsMoney>=300 and GoodsMoney<=600 and GoodsColor='红色' diff --git "a/2021 04 02 \344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5393dc72bcb39abc438e08119d66804871b56e95 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/SQLQuery3.sql" @@ -0,0 +1,67 @@ +--1、创建数据库HOUSE_DB, +--要求: +--指定数据文件大小:5兆, +--指定文件最大值:50兆, +--文件增长率:1兆 + +CREATE database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5, + maxsize=50, + filegrowth=1 +) +log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5, + maxsize=50, + filegrowth=1 +) +go +use HOUSE_DB +go +--2、创建数据表 +--房屋类型表(HOUSE_TYPE) +--字段名 类型 是否可为空 约束 说明 +--type_id int N 主键,自增长 类型编号 +--type_name varchar(50) N 唯一约束 类型名称 +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name varchar(50) not null unique +) +--房屋信息表(HOUSE) +--字段名 类型 是否可为空 约束 说明 +--house_id int N 主键,自增长 房屋编号 +--house_name varchar(50) N 房屋名称 +--house_price float N 默认值0 房租 +--type_id int N 外键依赖HOUSE_TYPE表 房屋类型 +create table HOUSE +( +house_id int primary key identity(1,1), +house_name varchar(50) not null , +house_price float not null default(0), +type_id int references HOUSE_TYPE(type_id) +) +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅' ) +insert into HOUSE values('小红屋',500,'1'),('经济屋',1000,'2'),('别墅屋',5000,'3') +--4、添加查询 +select * from HOUSE_TYPE +select * from HOUSE +--查询所有房屋信息 +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select * from HOUSE order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,type_name 房屋类型名称 from HOUSE A INNER JOIN HOUSE_TYPE B ON A.type_id=B.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price 租金,house_name 房屋名称 from HOUSE order by house_price desc + diff --git "a/2021 04 02 \344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/SQLQuery4.sql" "b/2021 04 02 \344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..58574be4cf3bca450f4160fc986a4c0bf925dc39 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/SQLQuery4.sql" @@ -0,0 +1,92 @@ +--1、创建明星数据库(StarManagerDB),然后建立两张表,StarType(明星类型表),StarInfo(明星信息表),表结构分别如下: + + CREATE DATABASE StarManagerDB + ON + ( + name='StarManagerDB', + filename='D:\StarManagerDB.mdf', + size=5, + maxsize=50, + filegrowth=10 + ) + log on + ( + name='StarManagerDB_log', + filename='D:\StarManagerDB_log.ldf', + size=5, + maxsize=50, + filegrowth=10 + ) + go + use StarManagerDB + go + --StarType(明星类型表) +--字段名 说明 类型 长度 可否为空 约束 +--T_NO 明星类型编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--T_NAME 明星类型 nvarchar 20 +create table StarType +( + T_NO int primary key identity(1,1), + T_NAME nvarchar(20) +) + + +--StarInfo(明星信息表) +--字段名 说明 类型 长度 可否为空 约束 +--S_NO 明星编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--S_NAME 明星姓名 nvarchar 20 否 +--S_AGE 明星年龄 int 否 +--S_HOBBY 特技 nvarchar 20 +--S_NATIVE 明星籍贯 nvarchar 20 默认约束:中国大陆 +--S_T_NO 明星类型编号 int 外键,参照StarType表的主键T_NO + +create table StarInfo +( + S_NO int primary key identity not null , + S_NAME nvarchar(20) not null , + S_AGE int not null , + S_HOBBY nvarchar(20) , + S_NATIVE nvarchar(20) default('中国大陆') , + S_T_NO int references StarType(T_NO) +) + + +--2、使用插入语句为两张表添加数据 + +--明星类型表(StarType) +--明星类型编号 明星类型 +--1 体育明星 +--2 IT明星 +--3 相声演员 + +insert StarType values +('体育明星'), +('IT明星'), +('相声演员') +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 + +insert StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) +select * from StarType +select * from StarInfo +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 名字, S_HOBBY 特技, S_NATIVE 籍贯信息 from StarInfo order by S_AGE desc +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO as 类型,count(*) as 人数,avg(S_Age) as 平均年龄 from StarInfo +group by S_T_NO +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo +where S_T_NO = 1 +order by S_AGE desc \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\350\256\270\350\207\252\346\246\225/50\351\242\230.sql" "b/2021 04 02 \344\275\234\344\270\232/\350\256\270\350\207\252\346\246\225/50\351\242\230.sql" new file mode 100644 index 0000000000000000000000000000000000000000..797cc510dfea4d3b831d71dba04ba0af60a548ce --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\350\256\270\350\207\252\346\246\225/50\351\242\230.sql" @@ -0,0 +1,244 @@ +-- 1.鏌ヨ"鏁板 "璇剧▼姣" 璇枃 "璇剧▼鎴愮哗楂樼殑瀛︾敓鐨勪俊鎭強璇剧▼鍒嗘暟 + +select StudentName,Birthday,A.Score from StudentInfo S +inner join StudentCourseScore A +on S.Id = A.StudentId and A.CourseId = 2 +inner join StudentCourseScore B +on S.Id = B.StudentId and B.CourseId = 1 +where A.Score>B.Score + +-- 1.1 鏌ヨ鍚屾椂瀛樺湪" 鏁板 "璇剧▼鍜" 璇枃 "璇剧▼鐨勬儏鍐 +select * from (select StudentId,Score,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 2) B +inner join +(select StudentId,Score,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 1) A +on A.StudentId = B.StudentId +-- 1.2 鏌ヨ瀛樺湪" 鏁板 "璇剧▼浣嗗彲鑳戒笉瀛樺湪" 璇枃 "璇剧▼鐨勬儏鍐(涓嶅瓨鍦ㄦ椂鏄剧ず涓 null ) + +select * from (select StudentId,Score,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 2) B +left join +(select StudentId,Score,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 1) A +on A.StudentId = B.StudentId +-- 1.3 鏌ヨ涓嶅瓨鍦" 鏁板 "璇剧▼浣嗗瓨鍦" 璇枃 "璇剧▼鐨勬儏鍐 +select * from +(select StudentId,Score from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 1 )A +left join +(select StudentId,Score from StudentCourseScore SC left join StudentInfo SI on SC.StudentId = SI.Id where CourseId = 2) B +on A.StudentId = B.StudentId +-- 2.鏌ヨ骞冲潎鎴愮哗澶т簬绛変簬 60 鍒嗙殑鍚屽鐨勫鐢熺紪鍙峰拰瀛︾敓濮撳悕鍜屽钩鍧囨垚缁 +select StudentId,StudentName,avg(Score) from StudentCourseScore SC inner join StudentInfo SI +on SC.StudentId = SI.Id +group by StudentId,StudentName having avg(Score)>60 + + +-- 3.鏌ヨ鍦 鎴愮哗 琛ㄥ瓨鍦ㄦ垚缁╃殑瀛︾敓淇℃伅 +select * from (select StudentId from StudentCourseScore group by StudentId) SC inner join StudentInfo SI on SC.StudentId = SI.Id + + +-- 4.鏌ヨ鎵鏈夊悓瀛︾殑瀛︾敓缂栧彿銆佸鐢熷鍚嶃侀夎鎬绘暟銆佹墍鏈夎绋嬬殑鎬绘垚缁(娌℃垚缁╃殑鏄剧ず涓 null ) +select * from StudentInfo SI left join (select StudentId from StudentCourseScore group by StudentId) SC on SI.Id = SC.StudentId + + +-- 4.1 鏌ユ湁鎴愮哗鐨勫鐢熶俊鎭 +select * from StudentInfo SI inner join (select StudentId from StudentCourseScore group by StudentId) SC on SI.Id = SC.StudentId + + +-- 5.鏌ヨ銆屾潕銆嶅鑰佸笀鐨勬暟閲 +select * from Teachers where TeacherName like '鏉%' + + +-- 6.鏌ヨ瀛﹁繃銆屽紶涓夈嶈佸笀鎺堣鐨勫悓瀛︾殑淇℃伅 +select * from StudentInfo SI inner join (select StudentId from StudentCourseScore where CourseId = 2 group by StudentId ) SC on SI.Id = SC.StudentId + + +-- 7.鏌ヨ娌℃湁瀛﹀叏鎵鏈夎绋嬬殑鍚屽鐨勪俊鎭 +select * from +(select * from StudentInfo A +right join +(select StudentId from StudentCourseScore group by StudentId having count(StudentId)<3) C +on A.Id = C.StudentId) Q +full join +(select SI.* from StudentInfo SI left join StudentCourseScore SC on SI.Id = SC.StudentId where Score is null) W +on Q.Id=W.Id +-- 8.鏌ヨ鑷冲皯鏈変竴闂ㄨ涓庡鍙蜂负" 01 "鐨勫悓瀛︽墍瀛︾浉鍚岀殑鍚屽鐨勪俊鎭 +select * from StudentInfo SI inner join +(select A.StudentId from +(select StudentId,CourseId from StudentCourseScore) A, +(select StudentId,CourseId from StudentCourseScore where StudentId = 1) B +where A.CourseId = B.CourseId and A.StudentId!=1 +group by A.StudentId) A +on SI.Id = A.StudentId +-- 9.鏌ヨ鍜" 01 "鍙风殑鍚屽瀛︿範鐨勮绋 瀹屽叏鐩稿悓鐨勫叾浠栧悓瀛︾殑淇℃伅 +select * from StudentInfo SI inner join +(select A.StudentId from +(select StudentId,CourseId from StudentCourseScore) A, +(select StudentId,CourseId from StudentCourseScore where StudentId = 1) B +where A.CourseId = B.CourseId and A.StudentId!=1 +group by A.StudentId having count(A.StudentId)>2) A +on SI.Id = A.StudentId +-- 10.鏌ヨ娌″杩"寮犱笁"鑰佸笀璁叉巿鐨勪换涓闂ㄨ绋嬬殑瀛︾敓濮撳悕 + +select * from StudentInfo SI +left join +(select StudentId from StudentCourseScore where CourseId = 2) S +on SI.Id = S.StudentId +where StudentId is null + +-- 11.鏌ヨ涓ら棬鍙婂叾浠ヤ笂涓嶅強鏍艰绋嬬殑鍚屽鐨勫鍙凤紝濮撳悕鍙婂叾骞冲潎鎴愮哗 + + + +-- 12.妫绱" 鏁板 "璇剧▼鍒嗘暟灏忎簬 60锛屾寜鍒嗘暟闄嶅簭鎺掑垪鐨勫鐢熶俊鎭 + + + +-- 13.鎸夊钩鍧囨垚缁╀粠楂樺埌浣庢樉绀烘墍鏈夊鐢熺殑鎵鏈夎绋嬬殑鎴愮哗浠ュ強骞冲潎鎴愮哗 +select * from StudentInfo + + +-- 14.鏌ヨ鍚勭鎴愮哗鏈楂樺垎銆佹渶浣庡垎鍜屽钩鍧囧垎锛 + + + +-- 15.浠ュ涓嬪舰寮忔樉绀猴細璇剧▼ ID锛岃绋 name锛屾渶楂樺垎锛屾渶浣庡垎锛屽钩鍧囧垎锛屽強鏍肩巼锛屼腑绛夌巼锛屼紭鑹巼锛屼紭绉鐜 + +/* + + 鍙婃牸涓>=60锛屼腑绛変负锛70-80锛屼紭鑹负锛80-90锛屼紭绉涓猴細>=90 + + + + 瑕佹眰杈撳嚭璇剧▼鍙峰拰閫変慨浜烘暟锛屾煡璇㈢粨鏋滄寜浜烘暟闄嶅簭鎺掑垪锛岃嫢浜烘暟鐩稿悓锛屾寜璇剧▼鍙峰崌搴忔帓鍒 + + + + 鎸夊悇绉戞垚缁╄繘琛屾帓搴忥紝骞舵樉绀烘帓鍚嶏紝 Score 閲嶅鏃朵繚鐣欏悕娆$┖缂 + +*/ + + + +-- 15.1 鎸夊悇绉戞垚缁╄繘琛屾帓搴忥紝骞舵樉绀烘帓鍚嶏紝 Score 閲嶅鏃跺悎骞跺悕娆 + + + +-- 16.鏌ヨ瀛︾敓鐨勬绘垚缁╋紝骞惰繘琛屾帓鍚嶏紝鎬诲垎閲嶅鏃朵繚鐣欏悕娆$┖缂 + + + +-- 16.1 鏌ヨ瀛︾敓鐨勬绘垚缁╋紝骞惰繘琛屾帓鍚嶏紝鎬诲垎閲嶅鏃朵笉淇濈暀鍚嶆绌虹己 + + + +-- 17.缁熻鍚勭鎴愮哗鍚勫垎鏁版浜烘暟锛氳绋嬬紪鍙凤紝璇剧▼鍚嶇О锛孾100-85]锛孾85-70]锛孾70-60]锛孾60-0] 鍙婃墍鍗犵櫨鍒嗘瘮 + + + +-- 18.鏌ヨ鍚勭鎴愮哗鍓嶄笁鍚嶇殑璁板綍 + + + +-- 19.鏌ヨ姣忛棬璇剧▼琚変慨鐨勫鐢熸暟 + + + +-- 20.鏌ヨ鍑哄彧閫変慨涓ら棬璇剧▼鐨勫鐢熷鍙峰拰濮撳悕 + + + +-- 21.鏌ヨ鐢风敓銆佸コ鐢熶汉鏁 + + + +-- 22.鏌ヨ鍚嶅瓧涓惈鏈夈岄銆嶅瓧鐨勫鐢熶俊鎭 + + + +-- 23.鏌ヨ鍚屽悕鍚屾у鐢熷悕鍗曪紝骞剁粺璁″悓鍚嶄汉鏁 + + + +-- 24.鏌ヨ 1990 骞村嚭鐢熺殑瀛︾敓鍚嶅崟 + + + +-- 25.鏌ヨ姣忛棬璇剧▼鐨勫钩鍧囨垚缁╋紝缁撴灉鎸夊钩鍧囨垚缁╅檷搴忔帓鍒楋紝骞冲潎鎴愮哗鐩稿悓鏃讹紝鎸夎绋嬬紪鍙峰崌搴忔帓鍒 + + + +-- 26.鏌ヨ骞冲潎鎴愮哗澶т簬绛変簬 85 鐨勬墍鏈夊鐢熺殑瀛﹀彿銆佸鍚嶅拰骞冲潎鎴愮哗 + + + +-- 27.鏌ヨ璇剧▼鍚嶇О涓恒屾暟瀛︺嶏紝涓斿垎鏁颁綆浜 60 鐨勫鐢熷鍚嶅拰鍒嗘暟 + + + +-- 28.鏌ヨ鎵鏈夊鐢熺殑璇剧▼鍙婂垎鏁版儏鍐碉紙瀛樺湪瀛︾敓娌℃垚缁╋紝娌¢夎鐨勬儏鍐碉級 + + + +-- 29.鏌ヨ浠讳綍涓闂ㄨ绋嬫垚缁╁湪 70 鍒嗕互涓婄殑濮撳悕銆佽绋嬪悕绉板拰鍒嗘暟 + + + +-- 30.鏌ヨ涓嶅強鏍肩殑璇剧▼ + + + +-- 31.鏌ヨ璇剧▼缂栧彿涓 01 涓旇绋嬫垚缁╁湪 80 鍒嗕互涓婄殑瀛︾敓鐨勫鍙峰拰濮撳悕 + + + +-- 32.姹傛瘡闂ㄨ绋嬬殑瀛︾敓浜烘暟 + + + +-- 33.鎴愮哗涓嶉噸澶嶏紝鏌ヨ閫変慨銆屽紶涓夈嶈佸笀鎵鎺堣绋嬬殑瀛︾敓涓紝鎴愮哗鏈楂樼殑瀛︾敓淇℃伅鍙婂叾鎴愮哗 + + + +--34.鎴愮哗鏈夐噸澶嶇殑鎯呭喌涓嬶紝鏌ヨ閫変慨銆屽紶涓夈嶈佸笀鎵鎺堣绋嬬殑瀛︾敓涓紝鎴愮哗鏈楂樼殑瀛︾敓淇℃伅鍙婂叾鎴愮哗 + + + +-- 35.鏌ヨ涓嶅悓璇剧▼鎴愮哗鐩稿悓鐨勫鐢熺殑瀛︾敓缂栧彿銆佽绋嬬紪鍙枫佸鐢熸垚缁 + + + +-- 36.鏌ヨ姣忛棬鍔熸垚缁╂渶濂界殑鍓嶄袱鍚 + + + +-- 37.缁熻姣忛棬璇剧▼鐨勫鐢熼変慨浜烘暟锛堣秴杩 5 浜虹殑璇剧▼鎵嶇粺璁★級銆 + + + +-- 38.妫绱㈣嚦灏戦変慨涓ら棬璇剧▼鐨勫鐢熷鍙 + + + +-- 39.鏌ヨ閫変慨浜嗗叏閮ㄨ绋嬬殑瀛︾敓淇℃伅 + + + +-- 40.鏌ヨ鍚勫鐢熺殑骞撮緞锛屽彧鎸夊勾浠芥潵绠 + + + +-- 41.鎸夌収鍑虹敓鏃ユ湡鏉ョ畻锛屽綋鍓嶆湀鏃 < 鍑虹敓骞存湀鐨勬湀鏃ュ垯锛屽勾榫勫噺涓 + + + +-- 42.鏌ヨ鏈懆杩囩敓鏃ョ殑瀛︾敓 + + + +-- 43.鏌ヨ涓嬪懆杩囩敓鏃ョ殑瀛︾敓 + + + +-- 44.鏌ヨ鏈湀杩囩敓鏃ョ殑瀛︾敓 + + + +-- 45.鏌ヨ涓嬫湀杩囩敓鏃ョ殑瀛︾敓 \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\350\256\270\350\207\252\346\246\225/\345\244\215\344\271\240\351\242\2301.sql" "b/2021 04 02 \344\275\234\344\270\232/\350\256\270\350\207\252\346\246\225/\345\244\215\344\271\240\351\242\2301.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fad9a153504205e3f59febcd08c697f6e39c0770 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\350\256\270\350\207\252\346\246\225/\345\244\215\344\271\240\351\242\2301.sql" @@ -0,0 +1,39 @@ +create database GoodDB +go +use GoodDB +go + +create table GoodsType +( + TypeID int not null primary key identity , + TypeName nvarchar(20) not null +) + +create table GoodInfo +( + GoodsID int primary key identity not null, + GiidsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeId int foreign key references GoodsType(TypeID) +) +go +insert into GoodsType(TypeName) +select '鏈嶈鍐呰。' union +select '闉嬪寘閰嶉グ' union +select '鎵嬫満鏁扮爜' + +insert into GoodInfo(GiidsName,GoodsColor,GoodsBrand,GoodsMoney,TypeId) +values('鎻愯姳灏忚タ瑁','绾㈣壊','鑿叉浖鐞',300,1),('鐧炬惌鐭¥','缁胯壊','鍝ュ紵',100,1),('鏃犺鑳屽績','鐧借壊','闃夸緷鑾',700,1), +('浣庡府浼戦棽闉','绾㈣壊','鑿叉浖鐞',900,2),('涓窡鍗曢瀷','缁胯壊','鍝ュ紵',400,2),('骞冲簳闉','鐧借壊','闃夸緷鑾',200,2), +('杩蜂綘鐓х浉鏈','绾㈣壊','灏煎悍',500,3),('纭洏','榛戣壊','甯屾嵎',600,3),('鏄惧崱','榛戣壊','鎶鍢',800,3) +go +--3銆佹煡璇环鏍兼渶璐电殑鍟嗗搧鍚嶇О锛屽晢鍝侀鑹插拰鍟嗗搧浠锋牸锛岃姹備娇鐢ㄥ埆鍚嶆樉绀哄垪鍚 +select GiidsName 鍟嗗搧鍚嶇О,GoodsColor 鍟嗗搧棰滆壊,GoodsMoney 鍟嗗搧浠锋牸 from GoodInfo where GoodsMoney = (select max(GoodsMoney) from GoodInfo) + +--4銆佹寜鍟嗗搧绫诲瀷缂栧彿鍒嗙粍鏌ヨ鍟嗗搧鏈楂樹环鏍硷紝鏈浣庝环鏍煎拰骞冲潎浠锋牸锛岃姹備娇鐢ㄥ埆鍚嶆樉绀哄垪鍚 +select TypeID 鍟嗗搧绫诲瀷,max(GoodsMoney) 鏈楂樹环鏍,min(GoodsMoney) 鏈浣庝环鏍 ,avg(GoodsMoney) 骞冲潎浠锋牸 from GoodInfo group by TypeID + +--5銆佹煡璇㈠晢鍝佷俊鎭墍鏈夊垪锛岃姹傚晢鍝侀鑹蹭负绾㈣壊锛屼环鏍煎湪300~600涔嬮棿 +select * from GoodInfo where GoodsColor='绾㈣壊' and GoodsMoney between 300 and 600 \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\350\256\270\350\207\252\346\246\225/\345\244\215\344\271\240\351\242\2302.sql" "b/2021 04 02 \344\275\234\344\270\232/\350\256\270\350\207\252\346\246\225/\345\244\215\344\271\240\351\242\2302.sql" new file mode 100644 index 0000000000000000000000000000000000000000..126eb79c96addc43662cbfdc02ae2507734e8133 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\350\256\270\350\207\252\346\246\225/\345\244\215\344\271\240\351\242\2302.sql" @@ -0,0 +1,50 @@ +create database HOUSE_DB +on +( + name = 'HOUSE_DB', + filename = 'D:\HOUSE_DB.mdf', + size = 5, + maxsize= 50, + filegrowth = 10% +) +log on +( + name= 'HOUSE_DB_log', + filename = 'D:\HOUSE_DB_log.ldf', + size = 5, + maxsize= 50, + filegrowth = 10% +) +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity, + type_name varchar(50) unique +) +create table HOUSE +( + house_id int primary key identity , + house_name varchar(50), + house_price float default (0), + type_id int foreign key references HOUSE_TYPE(type_id) +) +go +insert into HOUSE_TYPE(type_name) +select '灏忔埛鍨' union select '缁忔祹鍨' union select '鍒' + +insert into HOUSE(house_name,house_price,type_id) +values('楸',40,1),('榫',45,2),('铏',60,3) +--go +--鏌ヨ鎵鏈夋埧灞嬩俊鎭 +select * from HOUSE +--浣跨敤妯$硦鏌ヨ鍖呭惈鈥濆瀷鈥滃瓧鐨勬埧灞嬬被鍨嬩俊鎭 +select * from HOUSE inner join HOUSE_TYPE on HOUSE_TYPE.type_id = HOUSE.type_id +where type_name like '%鍨%' +--鏌ヨ鍑烘埧灞嬬殑鍚嶇О鍜岀閲戯紝骞朵笖鎸夌収绉熼噾闄嶅簭鎺掑簭 +select house_name,house_price from HOUSE order by house_price desc +--浣跨敤杩炴帴鏌ヨ锛屾煡璇俊鎭紝鏄剧ず鎴垮眿鍚嶇О鍜屾埧灞嬬被鍨嬪悕绉 +select house_name,type_name from HOUSE inner join HOUSE_TYPE on HOUSE_TYPE.type_id = HOUSE.type_id +--鏌ヨ鎵鏈夋埧灞嬩腑鏈堢鏈楂樼殑鎴垮眿锛屾樉绀烘渶楂樼殑绉熼噾鍜屾埧灞嬪悕绉 +select house_price,house_name from HOUSE where house_price = (select max(house_price) from HOUSE ) \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\350\256\270\350\207\252\346\246\225/\345\244\215\344\271\240\351\242\2303.sql" "b/2021 04 02 \344\275\234\344\270\232/\350\256\270\350\207\252\346\246\225/\345\244\215\344\271\240\351\242\2303.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b8c51f286d86aa87da3b98a0be3babe29cd02145 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\350\256\270\350\207\252\346\246\225/\345\244\215\344\271\240\351\242\2303.sql" @@ -0,0 +1,36 @@ +create database StarManagerDB +use StarManagerDB +go + +create table StarType +( + T_NO int primary key identity , + T_NAME nvarchar(20) +) +create table StarInfo +( + S_NO int primary key identity, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20) , + S_NATIVE nvarchar(20) default ('涓浗澶ч檰'), + S_T_NO int foreign key references StarType(T_NO) +) +go +insert into StarType(T_NAME) +select '浣撹偛鏄庢槦'union +select 'IT鏄庢槦'union +select '鐩稿0婕斿憳' + +insert into StarInfo(S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) +values ('姊呰タ',30,'灏勯棬','闃挎牴寤',1),('绉戞瘮',35,'杩囦汉','缇庡浗',2),('钄℃櫙鐜',40,'鏁蹭唬鐮','涓浗',2) +,('椹厠鏂',36,'閫犵伀绠','澶栨槦浜',2),('閮痉绾',50,'鐩稿0','涓浗',3),('榛勫偿',41,'鎷煎澶','涓浗',2) + +--3銆佹煡璇㈠勾榫勬渶澶х殑3涓槑鏄熺殑濮撳悕锛岀壒鎶鍜岀睄璐俊鎭紝瑕佹眰浣跨敤鍒悕鏄剧ず鍒楀悕銆 +select top 3 S_NAME 濮撳悕,S_HOBBY 绫嶈疮淇℃伅,S_NATIVE 鐗规妧 from StarInfo order by S_AGE desc +--4銆佹寜鏄庢槦绫诲瀷缂栧彿鍒嗙被鏌ヨ鏄庢槦浜烘暟锛屾槑鏄熷钩鍧囧勾榫勶紝鏄剧ず鏄庢槦浜烘暟澶т簬2鐨勫垎缁勪俊鎭紝瑕佹眰浣跨敤鍒悕鏄剧ず鍒楀悕銆 +select S_T_NO,count(*)鏄庢槦浜烘暟,avg(S_AGE)骞冲潎骞撮緞 from StarInfo group by S_T_NO having count(S_T_NO)>1 +--5銆佹煡璇㈡槑鏄熺被鍨嬩负鈥滀綋鑲叉槑鏄熲濅腑骞撮緞鏈澶х殑濮撳悕銆佺壒鎶銆佺睄璐俊鎭紝瑕佹眰鏄剧ず鍒楀埆鍚嶃 +select S_NAME 濮撳悕,S_HOBBY 鐗规妧,S_NATIVE 绫嶈疮 from StarInfo where S_AGE=(select max(S_AGE) from StarInfo where S_T_NO = 1) + +select * from StarInfo \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/50/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/50/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..60e4876554f324bbd32f38c0cdb32507a60e6b57 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/50/SQLQuery1.sql" @@ -0,0 +1,424 @@ + +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + +select * from CourseInfo +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers + + + + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +select * from StudentInfo inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.Id inner join CourseInfo +on CourseInfo.Id= StudentInfo.Id +where +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 + + + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) + + + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 + + + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentCode 学生编号,StudentName 学生姓名,AVG(Score)平均成绩 from StudentInfo inner join StudentCourseScore on StudentInfo.Id = StudentCourseScore.StudentId group by StudentCode,StudentName having AVG(Score)>=60 + +-- 3.查询在 成绩 表存在成绩的学生信息 +select StudentInfo.Id,StudentInfo.StudentCode,StudentInfo.StudentName,StudentInfo.Birthday,StudentInfo.Sex,StudentInfo.ClassId from StudentInfo inner join StudentCourseScore on StudentInfo.Id = StudentCourseScore.StudentId +where Score>0 +UNION +select StudentInfo.Id,StudentInfo.StudentCode,StudentInfo.StudentName,StudentInfo.Birthday,StudentInfo.Sex,StudentInfo.ClassId from StudentInfo inner join StudentCourseScore on StudentInfo.Id = StudentCourseScore.StudentId +where StudentCode=01 and StudentCode=02 and StudentCode=03 and StudentCode=04 and StudentCode=05 and StudentCode=06 and StudentCode=07 + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select StudentCode 学生编号,StudentName 学生姓名,COUNT(CourseId)选课总数,SUM(Score)总成绩 from StudentInfo left join StudentCourseScore on StudentInfo.Id = StudentCourseScore.StudentId group by StudentCode,StudentName + +-- 4.1 查有成绩的学生信息 +select StudentInfo.* from StudentInfo inner join StudentCourseScore on StudentInfo.Id = StudentCourseScore.StudentId +UNION +select StudentInfo.* from StudentInfo inner join StudentCourseScore on StudentInfo.Id = StudentCourseScore.StudentId +where StudentInfo.Id=1 and StudentInfo.Id=2 and StudentInfo.Id=3 and StudentInfo.Id=4 and StudentInfo.Id=5 and StudentInfo.Id=6 and StudentInfo.Id=7 + +-- 5.查询「李」姓老师的数量 +select COUNT(TeacherName) from Teachers where TeacherName like '李%' + + +-- 6.查询学过「张三」老师授课的同学的信息 +select StudentInfo.* from StudentInfo inner join StudentCourseScore on StudentInfo.Id = StudentCourseScore.StudentId +where CourseId='2' + +-- 7.查询没有学全所有课程的同学的信息 +select * from StudentInfo +except +select StudentInfo.* from StudentInfo inner join StudentCourseScore on StudentInfo.Id = StudentCourseScore.StudentId + + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select StudentInfo.* from StudentInfo A,StudentInfo B ????????????/ + + + select * from CourseInfo --课程信息表 + select * from StudentCourseScore --分数表 + select * from StudentInfo --学生信息表 + select * from Teachers --教师信息表 +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + + + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + + + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 +select StudentInfo.* from StudentInfo StudentInfo inner join StudentCourseScore on StudentInfo.Id = StudentCourseScore.StudentId +where CourseId='2' and Score<80 order by Score DESC + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列,按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/SQLQuery.sql" "b/2021 04 02 \344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/SQLQuery.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e14727f81a9631b9e443ba87ca7c4327478f77df --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/SQLQuery.sql" @@ -0,0 +1,61 @@ +create database HOUSE_DB +on +( + name=HOUSE_DB, + filename='D:\HOUSE_DB.mdf', + size=10mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name=HOUSE_DB_log, + filename='D:\HOUSE_DB_log.mdf', + size=10mb, + maxsize=50mb, + filegrowth=10% +) +go +--房屋类型表( +create table HOUSE_TYPE +--字段名 类型 是否可为空 约束 说明 +(type_id int not null primary key identity(1,1), +--int N 主键,自增长 类型编号 +type_name varchar(50) not null +--varchar(50) N 唯一约束 类型名称 +) +insert into HOUSE_TYPE values ('小户型'),('经济型'),('别墅') +select * from HOUSE_TYPE +--房屋信息表 +create table HOUSE +--字段名 类型 是否可为空 约束 说明 +( +house_id int not null primary key identity(1,1), +--int N 主键,自增长 房屋编号 +house_name varchar(50) not null, +--varchar(50) N 房屋名称 +house_price float not null default('0'), +--float N 默认值0 房租 +type_id int not null references HOUSE_TYPE(type_id) +--int N 外键依赖HOUSE_TYPE表 房屋类型 +) +insert into HOUSE values ('林佳元','20','1'),('刘毅','10','2'),('刘金海','25','3') +select * from HOUSE + +SELECT * FROM HOUSE +SELECT * FROM HOUSE_TYPE +--查询所有房屋信息 +select *from House +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE inner join HOUSE on HOUSE_TYPE.type_id=HOUSE.type_id +where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 房屋的名称,house_price 房屋租金 from HOUSE +order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,TYPE_NAME 房屋类型名称 from HOUSE +inner join HOUSE_TYPE on HOUSE_TYPE.type_id=HOUSE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_id, house_price,house_name from HOUSE +inner join HOUSE_TYPE on HOUSE_TYPE.type_id=HOUSE.type_id +order by house_price desc diff --git "a/2021 04 02 \344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..93732b93e171ecafd8d788004a20d87a7d409fc0 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/SQLQuery1.sql" @@ -0,0 +1,65 @@ +create database GoodsDB +on +( +name='GoodsDB', +filename='D:\GoodsDB.mdf', +size=10mb, +maxsize=50mb, +filegrowth=10% +) +log on +( +name='GoodsDB_log', +filename='D:\GoodsDB_log.mdf', +size=10mb, +maxsize=50mb, +filegrowth=10% +) +go +create table GoodsType +( +TypeID int not null primary key identity(1,1), +--商品类型编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +TypeName nvarchar(20) not null +--商品类型名称 nvarchar 20 否 + +) +insert into GoodsType values +('服装内衣'),('鞋包配饰'),('手机数码') +select * from GoodsType +create table GoodsInfo +( +GoodsID int not null primary key identity(1,1), +--商品编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +GoodsName nvarchar(20) not null, +--商品名称 nvarchar 20 否 +GoodsColor nvarchar(20) not null, +--商品颜色 nvarchar 20 否 +GoodsBrand nvarchar(20), +--商品品牌 nvarchar 20 +GoodsMoney money not null, +--商品价格 money 否 +TypeID int references GoodsType(TypeID) +--商品类型编号 int 外键,参照GoodsType中的TypeID + +) +insert into GoodsInfo values +('提花小西装','红色','菲曼琪',300,1),('百搭短裤','绿色','哥弟',100,1),('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2),('中跟单鞋','绿色','哥弟',400,2),('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3),('硬盘','黑色','希捷',600,3),('显卡','黑色','技嘉',800,3) +select * from GoodsInfo + + +select * from GoodsInfo +select * from GoodsType + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + select goodsname 商品名称,goodscolor 商品颜色,goodsmoney 商品价格 from GoodsInfo + inner join GoodsType on GoodsInfo.TypeID=GoodsType.TypeID + where goodsmoney=(select max(GoodsMoney) from GoodsInfo) + --4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + select GoodsInfo.TypeID, max(goodsmoney) 商品最高价格,MIN(goodsmoney ) 最低价格,avg(goodsmoney) 平均价格 from GoodsInfo + group by GoodsInfo.TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo +where GoodsColor='红色' AND GoodsMoney>=300 and GoodsMoney<=600 \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/SQLQuery4.sql" "b/2021 04 02 \344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..48f553406111580e6aeeae7baf48240c320da697 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/SQLQuery4.sql" @@ -0,0 +1,82 @@ +create database StarManagerDB +on +( + name=StarManagerDB, + filename='D:\StarManagerDB.mdf', + size=10mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name=StarManagerDB_log, + filename='D:\StarManagerDB_log.mdf', + size=10mb, + maxsize=50mb, + filegrowth=10% +) +go +create table StarType--(明星类型表) +--字段名 说明 类型 长度 可否为空 约束 +( +T_NO int not null primary key identity(1,1), +--明星类型编号 int 否 主键约束,自增,标识种子和标识增量都是1 +T_NAME NVARCHAR(20) +--明星类型 nvarchar 20 +) +--明星类型表(StarType) +--明星类型编号 明星类型 +--1 体育明星 +--2 IT明星 +--3 相声演员 +insert into StarType values +('体育演员'),('IT明星'),('相声演员') +select * from StarType +create table StarInfo--(明星信息表) +--字段名 说明 类型 长度 可否为空 约束 +( +S_NO int not null primary key identity(1,1), +--明星编号 int 否 主键约束,自增,标识种子和标识增量都是1 +S_NAME nvarchar(20) not null, +--明星姓名 nvarchar 20 否 +S_AGE int not null, +--明星年龄 int 否 +S_HOBBY nvarchar(20), +--特技 nvarchar 20 +S_NATIVE nvarchar(20) default('中国大陆'), +--明星籍贯 nvarchar 20 默认约束:中国大陆 +S_T_NO int references StarType(T_NO) , +--明星类型编号 int 外键,参照StarType表的主键T_NO +) +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 +insert into StarInfo values +('梅西','30','射门','阿根廷','1'),('科比','35','过人','美国','1'), +('蔡景现','40','敲代码','中国','2'),('马斯克','36','造火箭','外星人','2'), +('郭德纲','50','相声','中国','3'),('黄峥','41','拼多多','中国','2') +select * from StarInfo + +select * from StarInfo +SELECT * FROM StarType + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_name 明星的姓名,s_hobby 特技,s_native 籍贯信息 from StarType +inner join StarInfo on StarType.T_NO=StarInfo.S_T_NO +order by S_age desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select T_NO 明星类型,count(*)明星人数,avg(S_age)明星平均年龄 from StarType +INNER join StarInfo on StarInfo.S_T_NO=StarType.T_NO + group by t_no having count(*)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 t_no,t_name,s_name,s_hobby,s_native, max(s_age) from StarType +inner join StarInfo on StarInfo.S_T_NO=StarType.T_NO group by +t_no,t_name,s_name,s_hobby,s_native +having T_NO=1 \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..06cc25e6af418e57a4b6d0f91c9daeaf692c75e6 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/SQLQuery1.sql" @@ -0,0 +1,461 @@ + +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers +-- select * from StudentInfo + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo +-- select * from Teachers +-- select * from StudentInfo + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore +-- select * from CourseInfo +-- select * from Teachers +-- select * from StudentInfo + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + + +-- 练习题目: + + + select * from StudentCourseScore + select * from CourseInfo + select * from Teachers + select * from StudentInfo + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +select * from +(select StudentId,SCore,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.Id=SI.Id where CourseId=1) A +full join StudentInfo C on C.Id=A.Id, +(select StudentId,SCore,SC.Id from StudentCourseScore SC left join StudentInfo SI on SC.Id=SI.Id where CourseId=2) B +where A.Score=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/\345\244\215\344\271\240\351\242\230 SQLQuery2.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/\345\244\215\344\271\240\351\242\230 SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1b1bb7b6b41a46e09922e81aa5b694227f72d72d --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/\345\244\215\344\271\240\351\242\230 SQLQuery2.sql" @@ -0,0 +1,57 @@ +use master +go + +create database GoodsDB +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity not null, + TypeName nvarchar(20) not null +) +go + +create table GoodsInfo +( + GoodsID int not null primary key identity, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) +go + +insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') + +insert into GoodsInfo values('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋 ','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) +go + +select * from GoodsType +select * from GoodsInfo + +--查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName,GoodsColor,GoodsMoney +from GoodsInfo GI inner join GoodsType GT on GI.TypeID=Gt.TypeID +order by GoodsMoney desc + +--按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeName,max(GoodsMoney)最高价格,min(GoodsMoney)最低价格,avg(GoodsMoney)平均价格 +from GoodsInfo GI inner join GoodsType GT on GI.TypeID=Gt.TypeID +group by GI.TypeID,TypeName + +--查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * +from GoodsInfo +where GoodsColor='红色' and GoodsMoney >300 and GoodsMoney <600 diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/\345\244\215\344\271\240\351\242\230SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/\345\244\215\344\271\240\351\242\230SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..09c5291241523b1a51199faa085479c9ae04e85c --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/\345\244\215\344\271\240\351\242\230SQLQuery3.sql" @@ -0,0 +1,69 @@ +use master +go + +create database HOUSE_DB +on +( + name=HOUSE_DB_mbf, + filename='D:\HOUSE_DB.mbf', + size=5, + filegrowth=1MB +) +log on +( + name=HOUSE_DB_dbf, + filename='D:\HOUSE_DB_log.lbf', + size=5, + filrgrowth=1MB +) +go + +create table HOUSE_TYPE +( + type_id int not null primary key identity, + type_name varchar(50) not null +) +go + +create table HOUSE +( + house_id int not null primary key identity, + house_name varchar(50) not null, + house_price float not null default(0), + type_id int not null references HOUSE_TYPE(type_id) +) +go + +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') +go + +--HOUSE表中添加至少3条数据,不能全都为同一类型 +insert into HOUSE values +('房1',1000,1), +('房2',2000,2), +('房3',3000,3) + + +--查询所有房屋信息 +select * +from HOUSE HO inner join HOUSE_TYPE HT on HO.type_id=HT.type_id + +--使用模糊查询包含”型“字的房屋类型信息 +select * +from HOUSE HO inner join HOUSE_TYPE HT on HO.type_id=HT.type_id +where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select type_name,house_price +from HOUSE HO inner join HOUSE_TYPE HT on HO.type_id=HT.type_id +order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name,type_name +from HOUSE HO inner join HOUSE_TYPE HT on HO.type_id=HT.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select house_name,house_price +from HOUSE HO inner join HOUSE_TYPE HT on HO.type_id=HT.type_id +where house_price=(select max(house_price) from HOUSE) \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/\345\244\215\344\271\240\351\242\230SQLQuery5.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/\345\244\215\344\271\240\351\242\230SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2f33e86d4a693166f43f724f5487e919c15f8388 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/\345\244\215\344\271\240\351\242\230SQLQuery5.sql" @@ -0,0 +1,60 @@ +use master +go + +create database StarManagerDB +go + +use StarManagerDB +go + +create table StarType +( + T_NO int not null primary key identity, + T_NAME nvarchar(20) +) +go + +create table StarInfo +( + S_NO int not null primary key identity, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) +go + +insert into StarType values +('体育明星'), +('IT明星'), +('相声演员') + +insert into StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) +go + +--查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名 +select top 3 S_NAME,S_HOBBY,S_NATIVE +from StarInfo SI inner join StarType ST on SI.S_T_NO=ST.T_NO +order by S_AGE desc + +--按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select count(S_T_NO)明星人数,avg(S_AGE) 平均年龄 +from StarInfo SI inner join StarType ST on SI.S_T_NO=ST.T_NO +group by T_NAME +having count(S_T_NO)>2 + + +select * from StarInfo +select * from StarType +--查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select S_NAME,S_HOBBY,S_NATIVE +from StarInfo SI inner join StarType ST on SI.S_T_NO=ST.T_NO +where S_T_NO=1 + diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..40e99fbe36adcb5cc5e31698abebd625f94066f0 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery1.sql" @@ -0,0 +1,66 @@ +use master +go +create database GOODsDB +on +( +name='GOODsDB', +filename='D:\GOODsDB.dmf', +size=5mb, +maxsize=100mb, +filegrowth=10% +) +log on +( +name='GOODsDB_log', +filename='D:\GOODsDB_log.lmf', +size=5mb, +maxsize=100mb, +filegrowth=10% +) +go +use GOODsDB +go +create table GOODsType +( +TypeId int primary key identity(1,1) not null, +TypeName nvarchar(20) not null +) +create table GOODsInfo +( +GOODsID int primary key identity (1,1) not null, +GOODsName nvarchar(20) not null, +GOODsColor nvarchar(20) not null, +GOODsBrand nvarchar(20), +GOODsMoney money not null, +TypeID int references GOODsType(TypeID) +) +insert into GOODsType +select '服装内衣'union +select '鞋子配饰'union +select '手机数码' +insert into GOODsInfo values +('提花小西裤','红色','费曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色 ','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) +select*from GOODsInfo +select*from GOODsType +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GOODsName 商品名称, GOODsColor 商品颜色,max(GOODsMoney) 商品价格 from GOODsInfo +group by GOODsName,GOODsColor,GOODsMoney order by GOODsMoney desc +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select GOODsType.TypeID 商品类型编号,TypeName 商品类型名称,max(GOODsMoney)最高价格,AVg(GOODsMoney)平均价格,min(GOODsMoney)最低价格 from +GOODsInfo inner join GOODsType on GOODsInfo.TypeID=GOODsType.TypeId +group by GOODsType.TypeID,TypeName +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select*from GOODsInfo where GOODsColor='红色' and GOODsMoney>=300 and GOODsMoney<=600 + + + + + diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery2.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..075c69ac879e53c960611e199bc54c994e293319 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery2.sql" @@ -0,0 +1,251 @@ + +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + +select*from StudentInfo +select*from Teachers +select*from StudentCourseScore +select*from CourseInfo +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +select*from StudentCourseScore A inner join StudentCourseScore B on A.StudentId=B.StudentId where A.CourseId>B.CourseId and A.Score>B.Score +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select StudentId 学生编号,count(CourseId) from StudentCourseScore inner join CourseInfo on StudentCourseScore.CourseId =CourseInfo.Id +where CourseName='数学' or CourseName='语文' group by StudentId having count(CourseId) = 2 +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select * from (select * from StudentCourseScore where CourseId = 1) as A +right join (select * from StudentCourseScore where CourseId = 2) as B on A.StudentId = B.StudentId +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select * from (select * from StudentCourseScore where CourseId = 1) as A +left join (select * from StudentCourseScore where CourseId = 2) as B on A.StudentId = B.StudentId +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentId 学生编号,StudentName 学生姓名,AVG(Score)平均成绩 from StudentCourseScore inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id group by StudentId,StudentName having Avg(Score)>=60 +-- 3.查询在 成绩 表存在成绩的学生信息 +select*from StudentCourseScore inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id where Score is not null +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select StudentId 学生编号,StudentName 学生姓名,count(*)选课总数,SUM(Score) 课程的总成绩 from StudentCourseScore right join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id +group by StudentId,StudentName order by StudentId +-- 4.1 查有成绩的学生信息 +select * from StudentCourseScore right join StudentInfo on StudentCourseScore.StudentId= StudentInfo.Id where Score is not null +-- 5.查询「李」姓老师的数量 +select count(*)李姓老师数量 from Teachers where TeacherName like '李%' +-- 6.查询学过「张三」老师授课的同学的信息 +select B.* from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +where A.CourseId = 1 +-- 7.查询没有学全所有课程的同学的信息 +select StudentId,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +group by StudentId,StudentName,Birthday,Sex,ClassId +having count(CourseId)<3 +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select StudentId,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 1 or CourseId = 2 or CourseId = 3 +group by StudentId,StudentName,Birthday,Sex,ClassId +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +select StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 1 or CourseId = 2 or CourseId = 3 +group by StudentCode,StudentName,Birthday,Sex,ClassId +having count(CourseId) = 3 +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 +select StudentName from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +where A.CourseId != 1 +group by StudentCode,StudentName + diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..429e54b76e6555a04ed08697f88ff918ccc61b9f --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery3.sql" @@ -0,0 +1,52 @@ +use MASTER +GO +create database HOUSE_DB +on +( +name='HOUSE_DB', +filename='D:\HOUSE_DB.mdf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +log on +( +name='HOUSE_DB_log', +filename='D:\HOUSE_DB_log.ldf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +go +use HOUSE_DB +go +create table HOUSE_TYPE +( +type_id int primary key identity(1,1) not null, +type_name varchar(50) not null unique +) +create table HOUSE +( +house_id int primary key identity(1,1) not null, +house_name varchar(50) not null, +house_price float default(0) not null, +type_id int references HOUSE_TYPE(type_id) +) +insert into HOUSE_type +select '小户型' union +select '经济型' union +select '别墅' union +select '豪华房车' +insert into HOUSE values +('聚春园','100000.12','3'), +('居民楼','120000.23','1'), +('空中花园','230000','2') +select*from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select*from HOUSE_TYPE where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 房屋名称,house_price 租金 from HOUSE order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,type_name 房屋类型名称 from HOUSE inner join HOUSE_type on HOUSE.type_id=HOUSE_type.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_name 房屋名称,max(house_price)租金 from HOUSE group by house_name,house_price order by house_price desc \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery4.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6fdd0f80aa49003ac349f29dfdf23941dc647630 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery4.sql" @@ -0,0 +1,57 @@ +use master +go +create database StarManagerDB +on +( +name='StarManagerDB', +filename='D:\StarManagerDB.mdf', +size=5mb, +maxsize=100mb, +filegrowth=10% +) + log on +( +name='StarManagerDB_log', +filename='D:\StarManagerDB_log.ldf', +size=5mb, +maxsize=100mb, +filegrowth=10% +) +go +use StarManagerDB +go +create table StarType +( +T_NO int primary key identity(1,1) not null, +T_NAME nvarchar(20) +) +create table StarInfo +( +S_NO int primary key identity(1,1) not null, +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20) , +S_NATIVE nvarchar(20) default('中国大陆'), +S_T_NO int references StarType(T_NO) +) +insert into StarType +select '体育明星' union +select 'IT明星' union +select '相声明星' +insert into StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡锦现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄峥',41,'拼多多','中国',2) +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select TOP 3 max(S_AGE)年龄,S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo +group by S_NAME,S_HOBBY,S_NATIVE,S_AGE ORDER by S_AGE desc +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select count(*)明星人数,AVG(S_AGE) 名星平均年龄 from StarInfo inner join StarType on StarInfo.S_T_NO=StarType .T_NO +group by S_T_NO having count(T_NO)>2 order by S_T_NO +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo inner join StarType on StarInfo.S_T_NO=StarType.T_NO +where T_NAME='体育明星' group by S_NAME,S_HOBBY,S_NATIVE, S_AGE order by S_AGE desc + diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/0402.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/0402.sql" new file mode 100644 index 0000000000000000000000000000000000000000..041d78b460f436a4df73634f754d8e0676f2ffde --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/0402.sql" @@ -0,0 +1,69 @@ +use master +go +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\GoodsDB.mdf', + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) +log on +( + name='GoodsDB_log', + filename='D:\GoodsDB_log.ldf', + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) +go +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity(1,1), + TypeName nvarchar(20) not null +) + +create table GoodsInfo +( + GoodsID int primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) not null, + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') + +insert into GoodsInfo values('提花小西装','红色',' 菲曼琪', 300, 1), +('百搭短裤', '绿色', '哥弟', 100 ,1), +('无袖背心', '白色', '阿依莲', 700, 1), +('低帮休闲鞋',' 红色',' 菲曼琪', 900, 2), +('中跟单鞋', '绿色',' 哥弟', 400 ,2), +('平底鞋',' 白色',' 阿依莲', 200 ,2), +( '迷你照相机',' 红色',' 尼康 ',500, 3), +('硬盘' ,'黑色' ,'希捷', 600 ,3), +('显卡' ,'黑色' ,'技嘉' ,800 ,3) + +select * from GoodsType +select * from GoodsInfo + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsType GT inner join GoodsInfo GI on +GT.TypeID=GI.TypeID where GoodsMoney=(select MAX(GoodsMoney) from GoodsInfo) + +--4、按商品类型编号 分组查询 商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select GT.TypeID,MAX(GoodsMoney) 商品最高价格, MIN(GoodsMoney) 最低价格 ,AVG(GoodsMoney) 平均价格 FROM GoodsInfo GI +inner join GoodsType GT ON GI.TypeID=GT.TypeID GROUP BY GT.TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 + +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/0402\357\274\2102\357\274\211.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/0402\357\274\2102\357\274\211.sql" new file mode 100644 index 0000000000000000000000000000000000000000..93f54e9fcadf3ada0017880bbe616888d1375df6 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/0402\357\274\2102\357\274\211.sql" @@ -0,0 +1,61 @@ +use master +go +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5MB, + maxsize=50MB, + filegrowth=1Mb +) +log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=1Mb +) +go +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity(1,1), + type_name varchar(50) unique not null +) + +create table HOUSE +( + house_id int primary key identity(1,1), + house_name varchar(50) not null, + house_price float default(0) not null, + type_id int references HOUSE_TYPE(type_id) +) + +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') + +insert into HOUSE values('宿舍',1000,1),('旺铺',500000,2),('避暑山庄',100000000,3) + +--查询所有房屋信息 + +select * from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 + +select * from HOUSE H inner join HOUSE_TYPE HT on H.type_id=HT.type_id where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 + +select house_name 房屋的名称,house_price 租金 from HOUSE ORDER BY house_price + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 + +select house_name 房屋的名称,type_name 房屋类型名称 from HOUSE H inner join HOUSE_TYPE HT on H.type_id=HT.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 + +select house_name 房屋的名称,MAX(house_price)租金 from HOUSE +where house_price=(select MAX(house_price) from HOUSE ) group by house_name \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/0402\357\274\2103\357\274\211.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/0402\357\274\2103\357\274\211.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4cdee272b195e482a6e629aa80b7f78af39ea90b --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/0402\357\274\2103\357\274\211.sql" @@ -0,0 +1,61 @@ +use master +go +create database StarManagerDB +on +( + name='StarManagerDB', + filename='D:\StarManagerDB.mdf', + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) +log on +( + name='StarManagerDB_log', + filename='D:\StarManagerDB_log.ldf', + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) +go +use StarManagerDB +go + +create table StarType +( + T_NO int primary key identity(1,1), + T_NAME nvarchar(20) +) + +create table StarInfo +( + S_NO int primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) + +insert into StarType values('体育明星'),('IT明星'),('相声演员') + +insert into StarInfo values('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄峥',41,'拼多多','中国',2) + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 + +select top 3 S_AGE 年龄,S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo order by S_AGE DESC + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 + +select T_NO 类型编号, COUNT(*) 人数,AVG(S_AGE) 平均年龄 from StarType ST INNER JOIN StarInfo SI ON ST.T_NO=SI.S_T_NO +group by T_NO having COUNT(*)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 + +select top 1 S_AGE 年龄,S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo where S_T_NO=1 order by S_AGE DESC + diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/50\351\242\230.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/50\351\242\230.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8ddef7daac9a247c5aab8a5db0071fcacc58d469 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/50\351\242\230.sql" @@ -0,0 +1,248 @@ +select * from CourseInfo +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers + + + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + +select SI. * , SCS.CourseId 课程ID, SCS.Score 课程分数 from (select A.StudentId AD, A.CourseId AC,A.Score AE,B.StudentId BD, B.CourseId BC ,B.Score BS FROM +(select * from StudentCourseScore where CourseId=1) A, +(select * from StudentCourseScore where CourseId=2) B +WHERE A.StudentId=B.StudentId AND A.Score60 +-- 3.查询在 成绩 表存在成绩的学生信息 + +select distinct SI.* from StudentCourseScore SCS inner join CourseInfo CI on SCS.CourseId=CI.Id +inner join StudentInfo SI on SI.Id =SCS.StudentId + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +select StudentCode 学生编号,StudentName 学生姓名,COUNT(*) 选课总数,SUM(Score) 总成绩 +from StudentCourseScore SCS RIGHT join StudentInfo SI on SI.Id =SCS.StudentId group by StudentCode,StudentName +order by StudentCode,StudentName + + +-- 4.1 查有成绩的学生信息 + +select distinct SI.* from StudentCourseScore SCS left join StudentInfo SI on SI.Id =SCS.StudentId + +-- 5.查询「李」姓老师的数量 + +select COUNT(*) 数量 from Teachers where TeacherName like '李%' + +-- 6.查询学过「张三」老师授课的同学的信息 + +select SI. * from StudentCourseScore SCS inner join CourseInfo CI on SCS.CourseId=CI.Id +inner join StudentInfo SI on SI.Id =SCS.StudentId where CourseId=2 +-- 7.查询没有学全所有课程的同学的信息 + +select DISTINCT SI. * from StudentCourseScore SCS RIGHT join CourseInfo CI on SCS.CourseId=CI.Id +LEFT join StudentInfo SI on SI.Id =SCS.StudentId + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + +select DISTINCT SI. * from StudentCourseScore SCS inner join CourseInfo CI on SCS.CourseId=CI.Id +inner join StudentInfo SI on SI.Id =SCS.StudentId WHERE CourseId=1 OR CourseId=2 OR CourseId=3 + + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + +select SI.ID,StudentCode ,StudentName,Birthday,Sex,ClassId from StudentCourseScore SCS RIGHT join CourseInfo CI on SCS.CourseId=CI.Id +LEFT join StudentInfo SI on SI.Id =SCS.StudentId GROUP BY SI.ID,StudentCode ,StudentName,Birthday,Sex,ClassId HAVING COUNT(CourseId)=3 + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + + +select StudentName from StudentCourseScore SCS inner join CourseInfo CI on SCS.CourseId=CI.Id +inner join StudentInfo SI on SI.Id =SCS.StudentId WHERE CourseId !=2 group by StudentName + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\237\246\345\244\251\347\277\224/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\237\246\345\244\251\347\277\224/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c3e3901961163e71c3ba573ca0fdc089b4ecc9f0 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\237\246\345\244\251\347\277\224/SQLQuery1.sql" @@ -0,0 +1,86 @@ +--1、创建商品数据库(GoodsDB),然后建立两张表,GoodsType(商品类型表),GoodsInfo(商品信息表),表结构分别如下: +--商品类型表(GoodsType) +--字段名 说明 类型 长度 可否为空 约束 +--TypeID 商品类型编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--TypeName 商品类型名称 nvarchar 20 否 + +create database GoodsDB +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) + +--商品信息表(GoodsInfo) +--字段名 说明 类型 长度 可否为空 约束 +--GoodsID 商品编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--GoodsName 商品名称 nvarchar 20 否 +--GoodsColor 商品颜色 nvarchar 20 否 +--GoodsBrand 商品品牌 nvarchar 20 +--GoodsMoney 商品价格 money 否 +--TypeID 商品类型编号 int 外键,参照GoodsType中的TypeID + +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +--2、使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 + +insert into GoodsType(TypeName) +select '服装内衣' union +select '鞋包配饰' union +select '手机数码' + +--商品信息表(GoodsType) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 + +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +select '提花小西装','红色','菲曼琪',300,1 union +select '百搭短裤','绿色','哥弟',100,1 union +select '无袖背心','白色','阿依莲',700,1 union +select '低帮休闲鞋','红色','菲曼琪',900,2 union +select '中跟单鞋','绿色','哥弟',400,2 union +select '平底鞋','白色','阿依莲',200,2 union +select '迷你照相机','红色','尼康',500,3 union +select '硬盘','黑色','希捷',600,3 union +select '显卡','黑色','技嘉',800,3 + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo + where GoodsMoney = (select MAX(GoodsMoney) from GoodsInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select MAX(GoodsMoney) 最高价格,MIN(GoodsMoney) 最低价格,AVG(GoodsMoney) 平均价 from GoodsInfo + group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select * from GoodsInfo + where GoodsColor = '红色' and GoodsMoney between 300 and 600 diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\237\246\345\244\251\347\277\224/SQLQuery2.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\237\246\345\244\251\347\277\224/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d9f698e5f1d369312a624b2aebb5c9805f2b6476 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\237\246\345\244\251\347\277\224/SQLQuery2.sql" @@ -0,0 +1,458 @@ +-- 练习题目: + + + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + +select * from + (select * from StudentCourseScore where courseId = 1) as A + inner join StudentInfo s on s.Id = A.Id, + (select * from StudentCourseScore where CourseId = 2) as B + where A.Score > B.Score and A.StudentId = B.StudentId + + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 + +select StudentId from StudentCourseScore + where CourseId = 1 or CourseId = 2 + group by StudentId + HAVING COUNT(distinct CourseId) = 2 + + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) + +select * from + (select * from StudentCourseScore where CourseId = 2) A + left join (select * from StudentCourseScore where CourseId = 1) B on A.StudentId = B.StudentId + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 + +select * from + (select * from StudentCourseScore where CourseId = 1) A + left join (select * from StudentCourseScore where CourseId = 2) B on A.StudentId = B.StudentId + where B.CourseId is null + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 + +select Studentcode 学生编号,StudentName 学生姓名,AVG(Score) 平均成绩 from StudentCourseScore + inner join StudentInfo on StudentCourseScore.StudentId = StudentInfo.StudentCode + group by Studentcode,StudentName + HAVING AVG(Score) >= 60 + +-- 3.查询在 成绩 表存在成绩的学生信息 + +select * from StudentCourseScore + left join StudentInfo on StudentCourseScore.StudentId = StudentInfo.StudentCode + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +select StudentCode ,StudentName ,COUNT(CourseId) ,SUM(Score) from StudentCourseScore scs + right join StudentInfo si on scs.StudentId = si.StudentCode + group by StudentCode,StudentName + +-- 4.1 查有成绩的学生信息 + +select * from StudentCourseScore scs + inner join StudentInfo si on scs.StudentId = si.StudentCode + +-- 5.查询「李」姓老师的数量 + +select COUNT(TeacherName) from Teachers + where TeacherName like '李%' + +-- 6.查询学过 「张三」老师授课 的 同学的信息 + +select * from StudentCourseScore A + inner join CourseInfo B on A.CourseId = B.Id + inner join StudentInfo C on A.StudentId = C.StudentCode + inner join Teachers D on B.TeacherId = D.Id + where TeacherName = '张三' + +-- 7.查询没有学全所有课程的同学的信息 +select * from CourseInfo +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers + +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode,StudentName + HAVING COUNT(CourseId) < (select COUNT(CourseName) from CourseInfo) + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode ,StudentName + having count(CourseId) > 0 + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode ,StudentName + having count(CourseId)=(select COUNT(CourseName) from CourseInfo) + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select distinct studentName from StudentCourseScore A + inner join CourseInfo B on A.CourseId = B.Id + inner join StudentInfo C on A.StudentId = C.StudentCode + inner join Teachers D on B.TeacherId = D.Id + where TeacherName != '张三' + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 + +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\237\246\345\244\251\347\277\224/SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\237\246\345\244\251\347\277\224/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..11b06ac7e296af040556d3a0a6b74bd192e71ea7 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\237\246\345\244\251\347\277\224/SQLQuery3.sql" @@ -0,0 +1,86 @@ +--1、创建数据库HOUSE_DB, +--要求: +--指定数据文件大小:5兆, +--指定文件最大值:50兆, +--文件增长率:1兆 +use master +go + +create database HOUSE_DB +on +( + name = HOUSE_DB, + filename ='D:\HOUSE_DB.mfg', + size = 5MB, + maxsize = 50MB, + filegrowth = 1MB +) +use HOUSE_DB + +--2、创建数据表 +--房屋类型表(HOUSE_TYPE) +--字段名 类型 是否可为空 约束 说明 +--type_id int N 主键,自增长 类型编号 +--type_name varchar(50) N 唯一约束 类型名称 + +create table HOUSE_TYPE +( + type_id int primary key identity , + type_name varchar(20) unique +) + +--房屋信息表(HOUSE) +--字段名 类型 是否可为空 约束 说明 +--house_id int N 主键,自增长 房屋编号 +--house_name varchar(50) N 房屋名称 +--house_price float N 默认值0 房租 +--type_id int N 外键依赖HOUSE_TYPE表 房屋类型 + +create table HOUSE +( + house_id int primary key identity , + house_name varchar(50) , + house_price float default('0'), + type_id int references HOUSE_TYPE(type_id) +) + +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 + +insert into HOUSE_TYPE(type_name) +select '小户型' union +select '经济型' union +select '别墅' + +insert into HOUSE(house_name,house_price,type_id) +select '蔡','5000','3' union +select '东','10000','2' union +select '生','50000','1' + +--4、添加查询 +--查询所有房屋信息 + +select * from HOUSE A + inner join HOUSE_TYPE B on A.type_id = B.type_id + +--使用模糊查询包含”型“字的房屋类型信息 + +select * from HOUSE A + inner join HOUSE_TYPE B on a.type_id = B.type_id + where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 + +select house_name 名字,house_price 租金 from HOUSE + order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 + +select house_name 名称,type_name 类型名称 from HOUSE A + inner join HOUSE_TYPE B on A.type_id = B.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 + +select house_price ,house_name from HOUSE where house_price = (select MAX(house_price) from HOUSE) + diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\237\246\345\244\251\347\277\224/SQLQuery4.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\237\246\345\244\251\347\277\224/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cd3d67f97d3cb4545623816fb8e8c9fd1d4fcae9 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\237\246\345\244\251\347\277\224/SQLQuery4.sql" @@ -0,0 +1,88 @@ +--1、创建明星数据库(StarManagerDB),然后建立两张表,StarType(明星类型表),StarInfo(明星信息表),表结构分别如下: +--StarType(明星类型表) +--字段名 说明 类型 长度 可否为空 约束 +--T_NO 明星类型编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--T_NAME 明星类型 nvarchar 20 + +use master +go + +create database StarManagerDB +go + +use StarManagerDB +go + +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) + +--StarInfo(明星信息表) +--字段名 说明 类型 长度 可否为空 约束 +--S_NO 明星编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--S_NAME 明星姓名 nvarchar 20 否 +--S_AGE 明星年龄 int 否 +--S_HOBBY 特技 nvarchar 20 +--S_NATIVE 明星籍贯 nvarchar 20 默认约束:中国大陆 +--S_T_NO 明星类型编号 int 外键,参照StarType表的主键T_NO + +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20) , + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) + +--2、使用插入语句为两张表添加数据 + +--明星类型表(StarType) +--明星类型编号 明星类型 +--1 体育明星 +--2 IT明星 +--3 相声演员 + +insert into StarType +select '体育明星' union +select 'IT明星' union +select '相声演员' + +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 + +insert into StarInfo(S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) +select '梅西',30,'射门','阿根廷',1 union +select '科比',35,'过人','美国',1 union +select '蔡景现',40,'敲代码','中国',2 union +select '马斯克',36,'造火箭','外星人',2 union +select '郭德纲',50,'相声','中国',3 union +select '黄铮',41,'拼多多','中国',2 + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 + +select top 3 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo + order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 + +select S_T_NO as 类型,count(*) as 人数,avg(S_Age) as 平均年龄 from StarInfo + group by S_T_NO + + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 + +select top 1 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo + where S_T_NO = 1 + order by S_AGE desc + + diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery2.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..075c69ac879e53c960611e199bc54c994e293319 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery2.sql" @@ -0,0 +1,251 @@ + +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + +select*from StudentInfo +select*from Teachers +select*from StudentCourseScore +select*from CourseInfo +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +select*from StudentCourseScore A inner join StudentCourseScore B on A.StudentId=B.StudentId where A.CourseId>B.CourseId and A.Score>B.Score +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select StudentId 学生编号,count(CourseId) from StudentCourseScore inner join CourseInfo on StudentCourseScore.CourseId =CourseInfo.Id +where CourseName='数学' or CourseName='语文' group by StudentId having count(CourseId) = 2 +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select * from (select * from StudentCourseScore where CourseId = 1) as A +right join (select * from StudentCourseScore where CourseId = 2) as B on A.StudentId = B.StudentId +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select * from (select * from StudentCourseScore where CourseId = 1) as A +left join (select * from StudentCourseScore where CourseId = 2) as B on A.StudentId = B.StudentId +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentId 学生编号,StudentName 学生姓名,AVG(Score)平均成绩 from StudentCourseScore inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id group by StudentId,StudentName having Avg(Score)>=60 +-- 3.查询在 成绩 表存在成绩的学生信息 +select*from StudentCourseScore inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id where Score is not null +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select StudentId 学生编号,StudentName 学生姓名,count(*)选课总数,SUM(Score) 课程的总成绩 from StudentCourseScore right join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id +group by StudentId,StudentName order by StudentId +-- 4.1 查有成绩的学生信息 +select * from StudentCourseScore right join StudentInfo on StudentCourseScore.StudentId= StudentInfo.Id where Score is not null +-- 5.查询「李」姓老师的数量 +select count(*)李姓老师数量 from Teachers where TeacherName like '李%' +-- 6.查询学过「张三」老师授课的同学的信息 +select B.* from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +where A.CourseId = 1 +-- 7.查询没有学全所有课程的同学的信息 +select StudentId,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +group by StudentId,StudentName,Birthday,Sex,ClassId +having count(CourseId)<3 +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select StudentId,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 1 or CourseId = 2 or CourseId = 3 +group by StudentId,StudentName,Birthday,Sex,ClassId +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +select StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 1 or CourseId = 2 or CourseId = 3 +group by StudentCode,StudentName,Birthday,Sex,ClassId +having count(CourseId) = 3 +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 +select StudentName from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +where A.CourseId != 1 +group by StudentCode,StudentName + diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery4.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a495d69599ffd2e187a04c2dfe360b53dbd89239 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery4.sql" @@ -0,0 +1,53 @@ +use master +go +create database GoodsDB +on +( + name='list', + filename='D:\GoodsDB.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='list_log', + filename='D:\GoodsDB_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +use GoodsDB +go +create table GoodsType +( +TypeID int primary key not null identity(1,1) , +TypeName nvarchar(20) not null +) +create table GoodsInfo +( +GoodsID int primary key not null identity(1,1) , +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar (20) not null, +GoodsMoney money not null, +TypeID int not null foreign key references GoodsType(TypeID) +) +go +insert into GoodsType(TypeName) values +('服装内衣'),('鞋包配饰'),('手机数码') +select * from GoodsType + +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values +('提花小西装','红色', '菲曼琪',300,1),('百搭短裤','绿色','哥弟',100, 1),('无袖背心', '白色', '阿依莲', 700, 1), +('低帮休闲鞋',' 红色', '菲曼琪', 900, 2),('中跟单鞋', '绿色', '哥弟', 400, 2),('平底鞋', '白色', '阿依莲', 200, 2), +('迷你照相机', '红色', '尼康', 500, 3),('硬盘', '黑色', '希捷', 600, 3),('显卡', '黑色', '技嘉', 800, 3) +select * from GoodsInfo + +go +--查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName,GoodsColor 商品颜色,GoodsMoney from GoodsInfo order by GoodsMoney desc +--按商品类型编号 分组查询 商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型编号,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格 ,avg(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +--查询商品信息 所有列,要求商品颜色为 红色,价格在 300~600 之间 +select * from GoodsInfo where GoodsColor= '红色' and GoodsMoney between 300 and 600 \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery5.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6fdd0f80aa49003ac349f29dfdf23941dc647630 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery5.sql" @@ -0,0 +1,57 @@ +use master +go +create database StarManagerDB +on +( +name='StarManagerDB', +filename='D:\StarManagerDB.mdf', +size=5mb, +maxsize=100mb, +filegrowth=10% +) + log on +( +name='StarManagerDB_log', +filename='D:\StarManagerDB_log.ldf', +size=5mb, +maxsize=100mb, +filegrowth=10% +) +go +use StarManagerDB +go +create table StarType +( +T_NO int primary key identity(1,1) not null, +T_NAME nvarchar(20) +) +create table StarInfo +( +S_NO int primary key identity(1,1) not null, +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20) , +S_NATIVE nvarchar(20) default('中国大陆'), +S_T_NO int references StarType(T_NO) +) +insert into StarType +select '体育明星' union +select 'IT明星' union +select '相声明星' +insert into StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡锦现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄峥',41,'拼多多','中国',2) +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select TOP 3 max(S_AGE)年龄,S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo +group by S_NAME,S_HOBBY,S_NATIVE,S_AGE ORDER by S_AGE desc +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select count(*)明星人数,AVG(S_AGE) 名星平均年龄 from StarInfo inner join StarType on StarInfo.S_T_NO=StarType .T_NO +group by S_T_NO having count(T_NO)>2 order by S_T_NO +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo inner join StarType on StarInfo.S_T_NO=StarType.T_NO +where T_NAME='体育明星' group by S_NAME,S_HOBBY,S_NATIVE, S_AGE order by S_AGE desc + diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery7.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery7.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9541d8c948c9b7975376af38e94e863ac6bae4c4 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery7.sql" @@ -0,0 +1,52 @@ +use master +go +create database HOUSE_DB +on +( + name='list', + filename='D:\HOUSE_DB.mdf', + size=5, + maxsize=50, + filegrowth=1% +) +log on +( + name='list_log', + filename='D:\HOUSE_DB_log.ldf', + size=5, + maxsize=50, + filegrowth=1% +) +use HOUSE_DB +go +create table HOUSE_TYPE +( +type_id int not null primary key identity, +type_name varchar(50) not null unique +) +create table HOUSE +( +house_id int not null primary key identity, +house_name varchar(50) not null, +house_price float not null default(0), +type_id int not null foreign key references HOUSE_TYPE(type_id) +) +go +insert into HOUSE_TYPE(type_name) values +('小户型'),('经济型'),('别墅') +select * from HOUSE_TYPE + +insert into HOUSE(house_name,house_price,type_id) values +('小户型',200,1),('经济型',500,2),('别墅',1000,3) +select * from HOUSE +go +--查询所有房屋信息 +select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select* from HOUSE where house_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 房屋名称,house_price 租金 from HOUSE order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select a.type_name 房屋类型名称,b.house_name 房屋名称 from HOUSE_TYPE a inner join HOUSE b on a.type_id = b.type_id +--查询所有房屋中 月租最高的房屋,显示最高的 租金 和 房屋名称 +select top 1 max(house_price)最高价格, house_name 房屋名称 from HOUSE group by house_name \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\253\230\345\245\225/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\253\230\345\245\225/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f5dda3a376aefb4c72cdb517f878572c221e62e9 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\253\230\345\245\225/SQLQuery1.sql" @@ -0,0 +1,177 @@ +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\GoodsDB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='GoodsDB_log', + filename='D:\GoodsDB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) +go + +create table GoodsInfo +( + GoodsID int primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) +go + +insert into GoodsType values ('服装内衣'), +('鞋包配饰'), +('手机数码') + +select * from GoodsType + +insert into GoodsInfo values +('提花小西装','红色','费曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','费曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3) +go + + +select * from GoodsInfo + + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色, GoodsMoney 价格 from GoodsInfo where GoodsMoney=(select max(GoodsMoney) from GoodsInfo) group by GoodsID,GoodsColor,GoodsName,GoodsMoney +select max(GoodsMoney) from GoodsInfo + + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select max(GoodsMoney)最高价格,min(GoodsMoney)最低价格,avg(GoodsMoney)平均价格 from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\253\230\345\245\225/SQLQuery2.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\253\230\345\245\225/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6787e923c48953cb1ae7b1896c524ef0c4495cb8 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\253\230\345\245\225/SQLQuery2.sql" @@ -0,0 +1,107 @@ +create database HOUSE_DB +on +( + name=house, + filename='E:\Demo\house.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) +log on +( + name=house_log, + filename='E:\Demo\house_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) +go + +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name varchar(50) unique not null +) +go + +create table HOUSE +( + house_id int primary key identity(1,1), + house_name varchar(50) not null, + house_price float default(0) not null, + type_id int references HOUSE_TYPE(type_id) not null +) +go + + +insert into HOUSE_TYPE values ('小户型'), +('经济型'), +('别墅') + + +insert into HOUSE (house_name,house_price,type_id) values ('大平层海景型别墅',5000,3), +('精装三户',2500,1), +('简易三户',2000,2), +('独栋海景',5000,3) + + + +--查询所有房屋信息 +select * from HOUSE full join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id + +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE full join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id +where house_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price DESC + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select HOUSE_TYPE.type_name,HOUSE.house_name from HOUSE +inner join HOUSE_TYPE on HOUSE_TYPE.type_id=HOUSE.type_id + + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select max(house_price) from HOUSE + +select top 1 house_name,house_price from HOUSE +where house_price=(select max(house_price) from HOUSE ) + +select top 1 house_name,house_price from HOUSE +order by house_price DESC + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\253\230\345\245\225/SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\253\230\345\245\225/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4251c93fde5f774dbf0f58874481c41526a1af86 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\253\230\345\245\225/SQLQuery3.sql" @@ -0,0 +1,64 @@ +create database StarManagerDB +on +( + name=house, + filename='E:\Demo\StarManagerDB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) +log on +( + name=house_log, + filename='E:\Demo\StarManagerDB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) +go + +create table StarType +( + T_NO int primary key identity(1,1), + T_NAME nvarchar(20) +) +go + +create table StarInfo +( + S_NO int primary key identity(1,1), + S_NAME nvarchar(20), + S_AGE int, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20), + S_T_NO int references StarType(T_NO) +) +go + + +insert into StarType values ('体育明星'), +('IT明星'), +('相声演员') + +select * from StarType + + +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) values ('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo +order by S_AGE DESC + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 明星类型,count(S_T_NO)明星人数,avg(S_AGE)平均年龄 from StarInfo +group by S_T_NO having count(S_T_NO)>2 + + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo where S_T_NO=1 order by S_AGE DESC + diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\253\230\345\245\225/stdent.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\253\230\345\245\225/stdent.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8aef83ad42455b8086266ca5aa735dbc51681143 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\253\230\345\245\225/stdent.sql" @@ -0,0 +1,531 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + +select * from StudentCourseScore +select * from StudentInfo +select * from CourseInfo +select * from Teachers + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的 学生的信息 及课程分数 + +--select AA.*,SI.StudentName from (select yu.StudentId,yu.CourseId yuCourseId,yu.Score yuScore,shu.CourseId shuCourseId,shu.Score shuScore +--from (SELECT * FROM StudentCourseScore SC WHERE SC.CourseId=1) AS yu, +--(SELECT * FROM StudentCourseScore SC WHERE SC.CourseId=2) AS shu +--WHERE shu.Score > yu.Score AND yu.StudentId = shu.StudentId) AA +--inner join StudentInfo SI ON SI.Id=AA.StudentId + +--select * from (SELECT * FROM StudentCourseScore SC WHERE SC.CourseId=1) AS yu, +--(SELECT * FROM StudentCourseScore SC WHERE SC.CourseId=2) AS shu +--WHERE shu.Score > yu.Score AND yu.StudentId = shu.StudentId + + +SELECT * FROM StudentCourseScore A,StudentCourseScore B +where A.StudentId=B.StudentId AND A.CourseId=2 AND B.CourseId=1 AND A.Score>B.Score + +-- 1.1 查询同时存在" 数学 "2课程和" 语文 "1课程的情况 +select StudentId,count(CourseId) from StudentCourseScore +where CourseId = 1 or CourseId =2 +group by StudentId +having count(CourseId)=2 +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select * from +(select * from StudentCourseScore where CourseId =2)as A +left join(select * from StudentCourseScore where CourseId =1) as B on A.StudentId= B.StudentId + + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select * from +(select * from StudentCourseScore where CourseId =1)as A +left join(select * from StudentCourseScore where CourseId =2) as B on A.StudentId= B.StudentId + + +-- 2.查询 平均成绩大于等于 60 分 的同学的 学生编号和 学生姓名和 平均成绩 +select StudentId,StudentName,AVG(Score)平均成绩 from StudentCourseScore +inner join StudentInfo on StudentInfo.StudentCode=StudentCourseScore.StudentId +group by StudentId,StudentName having AVG(Score)>=60 + + +-- 3.查询在 成绩 表存在成绩的学生信息 +select * from StudentInfo +right join StudentCourseScore on StudentCourseScore.StudentId=StudentInfo.StudentCode + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select StudentInfo.Id 学生编号,StudentName 学生姓名,count(StudentId)选课总数,sum(Score)所有课程的总成绩 from StudentInfo +left join StudentCourseScore on StudentInfo.StudentCode= StudentCourseScore.StudentId +group by StudentInfo.Id,StudentName order by StudentInfo.Id + +-- 4.1 查有成绩的学生信息 +select * from StudentInfo inner join StudentCourseScore on StudentCourseScore.StudentId=StudentInfo.StudentCode + + +-- 5.查询「李」姓老师的数量 +select * from Teachers where TeacherName like '李%' + + +-- 6.查询学过「张三」老师授课的同学的信息 +select * from StudentInfo +inner join StudentCourseScore on StudentCourseScore.StudentId=StudentInfo.StudentCode +where StudentCourseScore.CourseId=2 + + +-- 7.查询没有学全所有课程的同学的信息 +select StudentId,StudentName,Birthday,Sex,ClassId from StudentCourseScore +inner join StudentInfo on StudentInfo.StudentCode=StudentCourseScore.StudentId +group by StudentId,StudentName,Birthday,Sex,ClassId having count(StudentId)<3 + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select distinct StudentId,StudentName from StudentCourseScore +inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.StudentCode +where CourseId=1 or CourseId=2 or CourseId=3 + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +select StudentId from StudentCourseScore group by StudentId having count(StudentId)=3 + + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 +select * from StudentCourseScore +right join StudentInfo on StudentInfo.StudentCode=StudentCourseScore.StudentId + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + +select StudentId,StudentName,avg(Score)平均成绩 from StudentCourseScore sc +inner join StudentInfo B on sc.StudentId = B.StudentCode +Where (Select Count(*) From StudentCourseScore s1 Where s1.StudentId=sc.StudentId And Score<60)>=2 +Group By StudentId ,StudentName + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 +select StudentId,Score from StudentCourseScore +where StudentId=(select * from StudentCourseScore where CourseId=2 and Score<60) + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 +select StudentId 学生号,sum(Score)总成绩,avg(Score)平均成绩 from StudentCourseScore group by StudentId order by avg(Score) DESC + + +-- 14.查询各科成绩最高分、最低分和平均分: +select max(Score)最高分,min(Score)最低分,avg(Score)平均分 from StudentCourseScore group by CourseId + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 +select CourseId 课程名,count(CourseId)每门课程被选修的学生数 from StudentCourseScore group by CourseId + + +-- 20.查询出只选修两门课程的学生学号和姓名 +select StudentId,StudentName from StudentCourseScore +inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.StudentCode +group by StudentId,StudentName having count(StudentId)=2 + +-- 21.查询男生、女生人数 +select sex,count(Sex) from StudentInfo group by Sex + + +-- 22.查询名字中含有「风」字的学生信息 +select * from StudentInfo where StudentName like '%风%' + + +-- 23.查询同名同性学生名单,并统计同名人数 +SELECT StudentName, +COUNT(1) +FROM StudentInfo st +GROUP BY StudentName,Sex HAVING COUNT(1)>1 + + +-- 24.查询 1990 年出生的学生名单 +select * from StudentInfo where datepart(yyyy,Birthday)=1990 + + +-- 25.查询 每门课程的 平均成绩,结果按 平均成绩降序排列,平均成绩相同时,按 课程编号升序排列 +select CourseId,avg(Score) from StudentCourseScore group by CourseId order by avg(Score)DESC,CourseId ASC + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 +select StudentCode,StudentName,avg(Score) from StudentCourseScore +inner join StudentInfo on StudentInfo.StudentCode=StudentCourseScore.StudentId +group by StudentCode,StudentName having avg(Score)>=85 + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 +select StudentName,Score from StudentInfo +inner join StudentCourseScore on StudentCourseScore.StudentId=StudentInfo.StudentCode +where CourseId=2 and Score<60 group by StudentName,Score + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) +select StudentName 姓名,CourseName 科目,Score 成绩 from StudentCourseScore +full join StudentInfo on StudentInfo.StudentCode=StudentCourseScore.StudentId +full join CourseInfo on StudentCourseScore.CourseId=CourseInfo.Id + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 +select StudentName 姓名,CourseId 课程,Score 分数 from StudentCourseScore +inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.StudentCode +where Score>70 +group by StudentName,CourseId,Score + + +-- 30.查询不及格的课程 +select StudentName 姓名,CourseName 课程,Score 分数 from StudentCourseScore +inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.StudentCode +inner join CourseInfo on StudentCourseScore.CourseId=CourseInfo.Id +where Score<60 +group by StudentName,CourseId,CourseName,Score + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 +select StudentId 学号,StudentName 姓名 from StudentCourseScore +inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.StudentCode +where CourseId=1 and Score>=80 +group by StudentId,StudentName + + +-- 32.求每门课程的学生人数 +select CourseId,count(CourseId) from StudentCourseScore group by CourseId + + +-- 33.成绩不重复,查询选修「张三」2老师所授课程的学生中,成绩最高的学生信息及其成绩 + + +select distinct StudentId 学号,Score 成绩 from StudentCourseScore +where score=(select max(Score) from StudentCourseScore where CourseId=2) + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 +select StudentId 学号,Score 成绩 from StudentCourseScore +where score=(select max(Score) from StudentCourseScore where CourseId=2) + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 +SELECT StudentId,CourseId,score, +COUNT(1) +FROM StudentCourseScore st +GROUP BY StudentId,CourseId,Score HAVING COUNT(1)>1 +-- 36.查询每门功成绩最好的前两名 +select CourseId,Score from StudentCourseScore group by CourseId,Score order by Score DESC + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 +select CourseId from StudentCourseScore +group by CourseId having count(CourseId)>5 + + +-- 38.检索至少选修两门课程的学生学号 +select StudentId from StudentCourseScore +group by StudentId having count(StudentId)>=2 + + +-- 39.查询选修了全部课程的学生信息 +select StudentId from StudentCourseScore +group by StudentId having count(StudentId)=3 + + +-- 40.查询各学生的年龄,只按年份来算 +select * from StudentInfo where datediff(YY,GETDATE,Birthday) as shengri + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..180832bbb9627951fe5be026e83f6633f38c46ab --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery1.sql" @@ -0,0 +1,61 @@ +use master +go +create database GoodsDB +on( +name='GoodsDB', +filename='D:\GoodsDB_ldf', +size=5, +maxsize=100, +filegrowth=5% + + +) +log on ( +name='GoodsDB_log', +filename='D:\GoodsDB_log_mdf', +size=5, +maxsize=100, +filegrowth=5% + +) +go +use GoodsDB +go +create table GoodsType +( +TypeID int primary key identity(1,1) not null, +TypeName nvarchar(20) not null +) +create table GoodsInfo +( +GoodsID int primary key identity(1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int references GoodsType(TypeID) + +) +insert into GoodsType values('服装内衣'),('鞋包服'),('手机数码') +insert into GoodsInfo values('提花小西装', '红色','菲曼琪', '300', 1), +('无袖背心',' 白色', '阿依莲', '700', 1), +('低帮休闲鞋', '红色', '菲曼琪', '900', 2), +('提花小西装', '红色','菲曼琪', '300', 1), +('中跟单鞋' ,'绿色' ,'哥弟 ','400 ',2), + ('平底鞋', '白色', '阿依莲', '200', 2), +(' 迷你照相机', '红色','尼康', '500', 3), +('硬盘', '黑色', '希捷', '600', 3), +(' 显卡', '黑色', '技嘉','800', 3) + + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + +select MAX(GoodsMoney) from GoodsInfo +select GoodsName 商品名称,GoodsMoney 商品价格 from GoodsInfo inner join GoodsType on GoodsInfo.TypeID=GoodsType.TypeID where GoodsMoney=(select MAX(GoodsMoney) from GoodsInfo ) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select GoodsInfo.TypeID 商品编号,MAX(GoodsMoney)最高价格,min(GoodsMoney)最低价格 ,avg(GoodsMoney)平均价格 from GoodsInfo inner join GoodsType on GoodsInfo.TypeID=GoodsType.TypeID group by GoodsInfo.TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney in(300 , 600) \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery2.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1fb1473ae8ebc218ff35dcacb679e8218f9c4f00 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery2.sql" @@ -0,0 +1,288 @@ + + + + +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + +-- 练习题目: +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +SELECT StudentName,B.StudentId,B.Score 数学,A.Score 语文 FROM +(select CourseId,Score,StudentId from StudentCourseScore WHERE CourseId='2')A INNER JOIN +(select CourseId,Score,StudentId from StudentCourseScore WHERE CourseId='1')B INNER JOIN +StudentInfo ON B.StudentId=StudentInfo.id +ON A.studentid=B.StudentId +WHERE A.Score60 +-- 3.查询在 成绩 表存在成绩的学生信息 + +select * from StudentCourseScore S LEFT join StudentInfo ST on S.StudentId=ST.Id + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +select StudentInfo.Id 学生编号,StudentInfo.StudentName 学生姓名,count(*) 选课总数, sum(Score)总分 from StudentCourseScore + inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id +group by StudentInfo.Id,StudentInfo.StudentName + +-- 4.1 查有成绩的学生信息 + +select * from StudentCourseScore inner join StudentInfo on StudentCourseScore.Id=StudentInfo.Id + +-- 5.查询「李」姓老师的数量 + +SELECT * FROM Teachers WHERE TeacherName LIKE '李%' + +-- 6.查询学过「张三」老师授课的同学的信息 + +select * from StudentCourseScore inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id inner join Teachers on StudentCourseScore.CourseId=Teachers.Id where Teachers.Id=1 + +-- 7.查询没有学全所有课程的同学的信息 +select distinct(s.StudentId),StudentInfo.StudentName, s.*,sc1.score,sc2.score,sc3.score from StudentCourseScore s +left join StudentCourseScore sc1 on s.studentid = sc1.studentid and sc1.CourseId = '1' +left join StudentCourseScore sc2 on s.studentid = sc2.studentid and sc2.CourseId = '2' +left join StudentCourseScore sc3 on s.studentid = sc3.studentid and sc3.CourseId = '3' +inner join StudentInfo on s.StudentId=StudentInfo.Id +where sc1.score is null or sc2.score is null or sc3.score is null + + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode ,StudentName + having count(CourseId) > 0 +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + + +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode ,StudentName + having count(CourseId)=(select COUNT(CourseName) from CourseInfo) + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select distinct studentName from StudentCourseScore A + inner join CourseInfo B on A.CourseId = B.Id + inner join StudentInfo C on A.StudentId = C.StudentCode + inner join Teachers D on B.TeacherId = D.Id + where TeacherName != '张三' + diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..11b06ac7e296af040556d3a0a6b74bd192e71ea7 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery3.sql" @@ -0,0 +1,86 @@ +--1、创建数据库HOUSE_DB, +--要求: +--指定数据文件大小:5兆, +--指定文件最大值:50兆, +--文件增长率:1兆 +use master +go + +create database HOUSE_DB +on +( + name = HOUSE_DB, + filename ='D:\HOUSE_DB.mfg', + size = 5MB, + maxsize = 50MB, + filegrowth = 1MB +) +use HOUSE_DB + +--2、创建数据表 +--房屋类型表(HOUSE_TYPE) +--字段名 类型 是否可为空 约束 说明 +--type_id int N 主键,自增长 类型编号 +--type_name varchar(50) N 唯一约束 类型名称 + +create table HOUSE_TYPE +( + type_id int primary key identity , + type_name varchar(20) unique +) + +--房屋信息表(HOUSE) +--字段名 类型 是否可为空 约束 说明 +--house_id int N 主键,自增长 房屋编号 +--house_name varchar(50) N 房屋名称 +--house_price float N 默认值0 房租 +--type_id int N 外键依赖HOUSE_TYPE表 房屋类型 + +create table HOUSE +( + house_id int primary key identity , + house_name varchar(50) , + house_price float default('0'), + type_id int references HOUSE_TYPE(type_id) +) + +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 + +insert into HOUSE_TYPE(type_name) +select '小户型' union +select '经济型' union +select '别墅' + +insert into HOUSE(house_name,house_price,type_id) +select '蔡','5000','3' union +select '东','10000','2' union +select '生','50000','1' + +--4、添加查询 +--查询所有房屋信息 + +select * from HOUSE A + inner join HOUSE_TYPE B on A.type_id = B.type_id + +--使用模糊查询包含”型“字的房屋类型信息 + +select * from HOUSE A + inner join HOUSE_TYPE B on a.type_id = B.type_id + where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 + +select house_name 名字,house_price 租金 from HOUSE + order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 + +select house_name 名称,type_name 类型名称 from HOUSE A + inner join HOUSE_TYPE B on A.type_id = B.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 + +select house_price ,house_name from HOUSE where house_price = (select MAX(house_price) from HOUSE) + diff --git "a/2021 04 02 \344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery4.sql" "b/2021 04 02 \344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cd3d67f97d3cb4545623816fb8e8c9fd1d4fcae9 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery4.sql" @@ -0,0 +1,88 @@ +--1、创建明星数据库(StarManagerDB),然后建立两张表,StarType(明星类型表),StarInfo(明星信息表),表结构分别如下: +--StarType(明星类型表) +--字段名 说明 类型 长度 可否为空 约束 +--T_NO 明星类型编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--T_NAME 明星类型 nvarchar 20 + +use master +go + +create database StarManagerDB +go + +use StarManagerDB +go + +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) + +--StarInfo(明星信息表) +--字段名 说明 类型 长度 可否为空 约束 +--S_NO 明星编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--S_NAME 明星姓名 nvarchar 20 否 +--S_AGE 明星年龄 int 否 +--S_HOBBY 特技 nvarchar 20 +--S_NATIVE 明星籍贯 nvarchar 20 默认约束:中国大陆 +--S_T_NO 明星类型编号 int 外键,参照StarType表的主键T_NO + +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20) , + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) + +--2、使用插入语句为两张表添加数据 + +--明星类型表(StarType) +--明星类型编号 明星类型 +--1 体育明星 +--2 IT明星 +--3 相声演员 + +insert into StarType +select '体育明星' union +select 'IT明星' union +select '相声演员' + +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 + +insert into StarInfo(S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) +select '梅西',30,'射门','阿根廷',1 union +select '科比',35,'过人','美国',1 union +select '蔡景现',40,'敲代码','中国',2 union +select '马斯克',36,'造火箭','外星人',2 union +select '郭德纲',50,'相声','中国',3 union +select '黄铮',41,'拼多多','中国',2 + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 + +select top 3 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo + order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 + +select S_T_NO as 类型,count(*) as 人数,avg(S_Age) as 平均年龄 from StarInfo + group by S_T_NO + + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 + +select top 1 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo + where S_T_NO = 1 + order by S_AGE desc + + diff --git "a/20210326\344\275\234\344\270\232/.keep" "b/20210326\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/20210326\344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b113faaa18fc534b819c3464c4388dfb5a0ec6d6 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery2.sql" @@ -0,0 +1,15 @@ + + + +select oi.orderId,orderDate,itemType,itemName,theNumber,theMoney from dbo.orderItem oi inner join dbo.orders O on oi.orderId = o.orderId + + +select oi.orderId,orderDate,itemType,itemName from dbo.orderItem oi inner join dbo.orders o on oi.orderId = o.orderId where theNumber>50 + +select oi.orderId,orderDate,itemType,itemName,theNumber,theMoney,theMoney * theNumber from dbo.orderItem oi inner join dbo.orders o on oi.orderId = o.orderId + +select oi.orderId,orderDate,itemType,itemName,theNumber,theMoney,theMoney * theNumber from dbo.orderItem oi inner join dbo.orders o on oi.orderId =o.orderId where theNumber>5 and theNumber<50 + +select oi.orderId,COUNT(*) from dbo.orderItem oi inner join dbo.orders o on oi.orderId = o.orderId group by oi.orderId + +select oi.orderId,itemType,COUNT(*),sum(theNumber) from dbo.orderItem oi inner join dbo.orders o on oi.orderId = o.orderId group by oi.orderId,itemType order by oi.orderId diff --git "a/20210326\344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery6.sql" "b/20210326\344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery6.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2502aa462e155153480d94a63c3082d88f6f2a87 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery6.sql" @@ -0,0 +1,24 @@ + + + +select ClassId, COUNT(StuSex) from dbo.StuInfo where StuSex = '男' group by ClassId + +select ClassId,StuSex, COUNT(StuSex) from dbo.StuInfo group by ClassId ,StuSex + +select ClassId,COUNT(*) from dbo.StuInfo where StuProvince='福建省' group by ClassId + +select StuProvince,COUNT(*) from dbo.StuInfo where StuProvince is not null and StuProvince!='' group by StuProvince + +select StuProvince, COUNT(StuSex) from dbo.StuInfo where StuSex = '女' and StuProvince is not null and StuProvince!='' group by StuProvince + +select StuProvince,StuSex, COUNT(StuSex) from dbo.StuInfo where StuProvince is not null and StuProvince!='' group by StuProvince,StuSex + +select StuId,SUM(Score),AVG(Score) from dbo.Scores group by StuId + +select StuId,sum(Score) from dbo.Scores group by StuId having sum(Score)>620 + +select CourseId,max(Score), min(score) from dbo.Scores group by CourseId + +select StuId,CourseId,AVG(Score) from dbo.Scores group by StuId,CourseId + + diff --git "a/20210326\344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery7.sql" "b/20210326\344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery7.sql" new file mode 100644 index 0000000000000000000000000000000000000000..79704c922814cd9be65195fa873badc77f4febbc --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery7.sql" @@ -0,0 +1,19 @@ + + + + select UID,uName,sName from dbo.bbsUsers bu inner join dbo.bbsSection bs on bu.UID=bs.sUid + + select UID,uName,tTitle,tMsg,tTime from dbo.bbsUsers bs inner join dbo.bbsTopic bt on bs.UID=bt.tUID where tTime > '2008-09-15' + + select sUid,uName,sName from dbo.bbsSection bs inner join dbo.bbsUsers bu on bs.sUid=bu.UID where uAge<20 + + select UID,uName,tTitle,tMsg,tCount from dbo.bbsUsers bu inner join dbo.bbsTopic bt on bu.UID=bt.tUID where tCount=(select MAX(tCount) from bbsTopic) + + select tSID,tUID,uName,sName,count(tSID)as 发帖总数 from dbo.bbsTopic inner join dbo.bbsSection on bbsTopic.tSID=bbsSection.sID inner join dbo.bbsUsers on bbsTopic.tUID=bbsUsers.UID group by tSID,tUID,uName,sName + + + + + + + diff --git "a/20210326\344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery9.sql" "b/20210326\344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery9.sql" new file mode 100644 index 0000000000000000000000000000000000000000..30eb8059b05e0a3caa92d497a04180f0b8685fb1 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\344\275\225\351\223\255\346\266\233/SQLQuery9.sql" @@ -0,0 +1,17 @@ +select stuName,stuAge,writtenExam,labExam from dbo.StudenIfo inner join dbo.Class01 on StudenIfo.stuNo=Class01.stuNo + +select StudenIfo.stuNo,stuName,writtenExam,labExam from dbo.StudenIfo inner join dbo.Class01 on StudenIfo.stuNo=Class01.stuNo where writtenExam>60 and labExam>60 + +select StudenIfo.stuNo,stuName,writtenExam,labExam from dbo.StudenIfo left join dbo.Class01 on StudenIfo.stuNo=Class01.stuNo + +select stuName,stuAge,writtenExam,labExam from dbo.StudenIfo inner join dbo.Class01 on StudenIfo.stuNo=Class01.stuNo where stuAge>=20 order by writtenExam desc + +select stuSext,AVG(labExam) from dbo.StudenIfo inner join dbo.Class01 on StudenIfo.stuNo=Class01.stuNo group by stuSext + +select stuSext,SUM(writtenExam) from dbo.StudenIfo inner join dbo.Class01 on StudenIfo.stuNo=Class01.stuNo group by stuSext + + + + + + diff --git "a/20210326\344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/DingDan.sql" "b/20210326\344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/DingDan.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ca05636113ff28a647e1e36fa9d20ebe5745a379 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/DingDan.sql" @@ -0,0 +1,98 @@ + use master + go + + create database DingDan + on + ( + name = 'DingDan' , + filename = 'D:\DingDan.mdf', + size = 5MB, + MAXsize = 50MB, + filegrowth = 10% + ) + log on + ( + name = 'DingDan_log' , + filename = 'D:\DingDan_log.ldf', + size = 5MB, + MAXsize = 50MB, + filegrowth = 10% + ) + go + + use DingDan + go + --表1-------------------------------------------------------------------------- + create table OrdersIofo + ( + orderId int primary key identity(1,1), + orderDate datetime + ) + --表2-------------------------------------------------------------------------- + create table OrderItemInfo + ( + ItemiD int primary key identity(1,1), + orderId int references OrdersIofo(orderId), + itemType varchar(10) not null, + itemName varchar(10)not null, + theNumber int, + theMoney money + ) + + insert into OrdersIofo values('2008-01-12'), + ('2008-02-10'), + ('2008-02-15'), + ('2008-03-10') + select * from OrdersIofo + + insert into OrderItemInfo values(1,'文具','笔',72,2), + (1,'文具','尺',10,1), + (1,'体育用品','篮球',1,56), + (2,'文具','笔',36,2), + (2,'文具','固体胶',20,3), + (2,'日常用品','透明胶',2,1), + (2,'体育用品','羽毛球',20,3), + (3,'文具','订书机',20,3), + (3,'文具','订书机',10,3), + (3,'文具','裁纸刀',5,5), + (4,'文具','笔',20,2), + (4,'文具','信纸',50,1), + (4,'日常用品','毛巾',4,5), + (4,'日常用品','透明胶',30,1), + (4,'体育用品','羽毛球',20,3) + select * from OrderItemInfo + select * from OrdersIofo + + --1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 + select OrdersIofo.orderId 订单的编号,orderDate 订单日期,itemType 产品的类别,itemName 订购的产品名称,theNumber 订购数量 ,theMoney 订购单价 from OrderItemInfo + inner join OrdersIofo on OrderItemInfo.orderId = OrdersIofo.orderId + --2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 + select OrdersIofo.orderId 订单的编号,theNumber 订购数量,orderDate 订单日期,itemType 产品的类别,itemName 产品名称 from OrderItemInfo + inner join OrdersIofo on OrderItemInfo.orderId = OrdersIofo.orderId + where theNumber > 50 + --3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价sum(theMoney* theNumber) + select OrdersIofo.orderId 订单的编号,itemType 产品的类别,orderDate 订单日期,itemName 产品名称 ,theNumber 订购数量,theMoney 订购单价,theMoney* theNumber 订购总价 from OrderItemInfo + inner join OrdersIofo on OrderItemInfo.orderId = OrdersIofo.orderId + --4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + select OrdersIofo.orderId 订单的编号,itemType 产品的类别,orderDate 订单日期,itemName 产品名称 ,theNumber 订购数量,theMoney 订购单价,theMoney* theNumber 订购总价 from OrderItemInfo + inner join OrdersIofo on OrderItemInfo.orderId = OrdersIofo.orderId + where theMoney >= 5 and theNumber >= 50 + --5.查询每个订单分别订购了几个产品,例如: + -- 编号 订购产品数 + -- 1 3 + -- 2 4 + select orderId 编号,theNumber 订购产品数 from OrderItemInfo + --6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + + -- 订单编号 产品类别 订购次数 总数量 + + -- 1 文具 2 82 + -- 1 体育用品 1 1 + -- 2 文具 2 56 + -- 2 体育用品 1 2 + -- 2 日常用品 1 20 + select orderId 编号,itemType 产品类别 ,count(theNumber)订购产品数 ,sum(theNumber)总数量 from OrderItemInfo + group by orderId ,itemType + order by orderId + + \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/SQLQuery7.sql" "b/20210326\344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/SQLQuery7.sql" new file mode 100644 index 0000000000000000000000000000000000000000..82c09de79dff3df2dee026bfdea5ecb3e752be2d --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/SQLQuery7.sql" @@ -0,0 +1,23 @@ + + use TestDB + go + --统计每个班的男生数 + select ClassId, count(StuSex) 男生数量 from StuInfo where StuSex = '男' group by ClassId + --统计每个班的男、女生数 + select ClassId,StuSex, count(StuSex) from StuInfo group by StuSex,ClassId + --统计每个班的福建人数 + select ClassId, count(StuProvince)福建 from StuInfo where StuProvince = '福建省'group by ClassId + --统计每个班的各个省的总人数 + select ClassId,StuProvince,count(StuProvince)人数 from StuInfo group by ClassId ,StuProvince order by ClassId + --统计每个省的女生数 + select StuProvince 省份,count(StuProvince)人数 from StuInfo where StuSex = '女' group by StuProvince + --统计每个省的男、女生数 + select StuProvince 省份,StuSex,count(StuSex)人数 from StuInfo group by StuProvince , StuSex + --统计每个学生的考试总分、平均分 + select StuId,sum(Score)总分,avg(Score)平均分 from Scores group by StuId + --统计出考试总分大于620的学生的考试总分 + select StuId,sum(Score)总分 from Scores group by StuId having sum(Score)>620 + --统计出每门考试成绩最高分和最低分 + select CourseId, max(Score)最高分,min(Score)最低分 from Scores group by CourseId + --统计出每个学生的各门成绩的平均分 + select StuId,CourseId,avg(Score) 平均分 from Scores group by CourseId,StuId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/Students03.sql" "b/20210326\344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/Students03.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9ca5977b127ee9ce3fd9a8630113eed6c86b3bf3 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/Students03.sql" @@ -0,0 +1,78 @@ + create database Students03 + on + ( + name = 'Students03', + filename = 'D:\Students03.mdf', + size = 5MB, + maxsize = 50MB, + filegrowth = 10% + ) + log on + ( + name = 'Students03_log', + filename = 'D:\Students03_log.ldf', + size = 5MB, + maxsize = 50MB, + filegrowth = 10% + ) + go + use Students03 + go + + if exists(select * from sys.objects where name = 'StudentInfo') + drop table StudentInfo + --表1----------------------------------------------------------------------------------------------------------------------------- + create table StudentInfo + ( + StuNo varchar(10) primary key , + StuName varchar(10) check(len(StuName)>=2), + StuAge int not null, + StuAddress nvarchar(20), + StuSeat int , + StuSex nvarchar(1) default ('男') check(StuSex='女' or StuSex='男') + ) + select * from StudentInfo + insert into StudentInfo values ('s2501','张秋利',20,'美国硅谷',1,'男'),('s2502','李斯文',18,'湖北武汉',2,'女'), + ('s2503','马文才',22,'湖南长沙',3,'男'),('s2504','欧阳俊雄',21,'湖北武汉',4,'女'), + ('s2505','梅超风',20,'湖北武汉',5,'男'),('s2506','陈旋风',19,'美国硅谷',6,'男'),('s2507','陈风',20,'美国硅谷',7,'女') + --表2----------------------------------------------------------------------------------------------------------------------------- + create table StuExamInfo + ( + ExamNo int primary key identity(1,1), + StuNo varchar(10) references StudentInfo(StuNo), + WrittenExam int, + LadExam int + ) + select * from StuExamInfo + insert into StuExamInfo values ('s2501',50,70),('s2502',60,65),('s2503',86,85), + ('s2504',40,80),('s2505',70,90),('s2506',85,90) + --要求----------------------------------------------------------------------------------------------------------------------------- + + --1.查询学生的姓名,年龄,笔试成绩和机试成绩 + select StuName 姓名,StuAge 年龄,WrittenExam 笔试成绩,LadExam 机试成绩 from StudentInfo + inner join StuExamInfo on StudentInfo.StuNo = StuExamInfo.StuNo + --2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 + select StuName 姓名,StuAge 年龄,WrittenExam 笔试成绩,LadExam 机试成绩 from StudentInfo + inner join StuExamInfo on StudentInfo.StuNo = StuExamInfo.StuNo + where WrittenExam > 60 and LadExam > 60 + --3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 + select StuExamInfo.StuNo 学号,StuName 姓名,WrittenExam 笔试成绩,LadExam 机试成绩 from StudentInfo + left join StuExamInfo on StudentInfo.StuNo= StuExamInfo.StuNo + --4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 + select StuName 姓名,StuAge 年龄,WrittenExam 笔试成绩,LadExam 机试成绩 from StudentInfo + inner join StuExamInfo on StudentInfo.StuNo = StuExamInfo.StuNo + where StuAge >= 20 + order by WrittenExam DESC + --5.查询男女生的机试平均分 + select StuSex 性别 ,avg(LadExam)机试成绩平均分 from StudentInfo + inner join StuExamInfo on StudentInfo.StuNo = StuExamInfo.StuNo + group by StuSex + --6.查询男女生的笔试总分 + select StuSex 性别 ,sum(WrittenExam) 笔试总成绩 from StudentInfo + inner join StuExamInfo on StudentInfo.StuNo = StuExamInfo.StuNo + group by StuSex + + select * from StudentInfo + select * from StuExamInfo + + \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/bbs.sql" "b/20210326\344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/bbs.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8c2ab8ea036dbaaea91c5c53b64708eeed16a976 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\205\260\351\246\250\345\204\277/bbs.sql" @@ -0,0 +1,111 @@ + + use master + go + + create database bbs + on + ( + name='bbs', + filename='D:\bbs.mdf', + size=5MB, + filegrowth=10MB, + maxsize=100MB + ) + log on + ( + name='bbs_log', + filename='D:\bbs_log.ldf', + size=5MB, + filegrowth=10MB, + maxsize=100MB + ) + go + + use bbs + go + + create table bbsUser + ( + UID int identity, + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null + ) + + alter table bbsUser add constraint PK_bbsUser_UID primary key(UID) + alter table bbsUser add constraint UK_bbsUser_uName unique(uName) + --alter table bbsUser add constraint CK_bbsUser_uSex check(uSex='男' or uSex='女') + alter table bbsUser add constraint CK_bbsUser_uSex check(uSex in('男','女')) + --alter table bbsUser add constraint CK_bbsUser_uAge check(uAge>=15 and uAge<=60) + alter table bbsUser add constraint CK_bbsUser_uAge check(uAge between 15 and 60) + alter table bbsUser add constraint CK_bbsUser_uPoint check(uPoint>=0) + + + create table bbsTopic + ( + tID int primary key identity, + tUID int references bbsUser(UID), + tSID int references bbsSection(sID), + tTitle varchar(100) not null, + tMsg varchar(100) not null, + tTime datetime, + tCount int + ) + + create table bbsReply + ( + rID int primary key identity, + rUID int references bbsUser(UID), + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime + ) + + create table bbsSection + ( + sID int identity, + sName varchar(10) not null, + sUid int + ) + alter table bbsSection add constraint PK_bbsSection_sID primary key(sID) + alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUser(UID) + + insert into bbsUser values ('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) + insert into bbsSection values ('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) + insert into bbsTopic values (2,4,'范跑跑','谁是范跑跑','2008-7-8',1) + insert into bbsTopic values (3,1,'.NET','与JAVA的区别是什么呀?','2008-9-1',2),(1,3,'今年夏天最流行什么','有谁知道今年夏天最流行','2008-9-10',0) + insert into bbsReply ( rMsg, rTime, rUID,rTID )values ('范跑跑是',GETDATE(),1,1), + ('.NET与JAVA的区别是',GETDATE(),2,2), + ('今年夏天最流行是',GETDATE(),3,3) + + --1.查询出每个版块的版主编号,版主姓名和版块名称 + select sUid 版主编号,uName 用户名,sName 版块名称 from bbsSection + inner join bbsUser on bbsSection.sUid = bbsUser.UID + --2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 + select tUID 发帖人编号,uName 用户名,tTitle 贴子的标题, tMsg 帖子的内容,tTime 发帖时间 from bbsTopic + inner join bbsUser on bbsTopic.tUID = bbsUser.UID + where tTime > '2008-9-15' + --3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 + select uAge 年龄,sUid 版主编号,uName 用户名,sName 版块名称 from bbsSection + inner join bbsUser on bbsSection.sUid = bbsUser.UID + where uAge < 20 + --4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 + select top 1 max(tCount)回复数量最多 ,tUID 发帖人编号,uName 用户名,tTitle 贴子的标题, tMsg 帖子的内容, tCount 回复数量 from bbsTopic + inner join bbsUser on bbsTopic.tUID = bbsUser.UID + group by tUID,uName,tTitle,tCount, tMsg + order by tCount DESC + --5.在主贴表中查询每个版块中每个用户的发帖总数 + select uName 用户名, sName 版块名称, count(tID)发帖总数 from bbsTopic + inner join bbsUser on bbsTopic.tUID = bbsUser.UID + inner join bbsSection on bbsTopic.tUID = bbsSection.sUid + group by uName ,sName + + select * from bbsUser + select * from bbsSection + select * from bbsTopic + select * from bbsReply + + + + \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1c6047e7a4286f4b00893af66dd48831ff5ffa40 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery1.sql" @@ -0,0 +1,96 @@ +use master + go + + create database bbs + on + ( + name='bbs', + filename='D:\bbs.mdf', + size=5MB, + filegrowth=10MB, + maxsize=100MB + ) + log on + ( + name='bbs_log', + filename='D:\bbs_log.ldf', + size=5MB, + filegrowth=10MB, + maxsize=100MB + ) + go + + use bbs + go + + create table bbsUser + ( + UID int identity, + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null + ) + + alter table bbsUser add constraint PK_bbsUser_UID primary key(UID) + alter table bbsUser add constraint UK_bbsUser_uName unique(uName) + --alter table bbsUser add constraint CK_bbsUser_uSex check(uSex='男' or uSex='女') + alter table bbsUser add constraint CK_bbsUser_uSex check(uSex in('男','女')) + --alter table bbsUser add constraint CK_bbsUser_uAge check(uAge>=15 and uAge<=60) + alter table bbsUser add constraint CK_bbsUser_uAge check(uAge between 15 and 60) + alter table bbsUser add constraint CK_bbsUser_uPoint check(uPoint>=0) + + + create table bbsTopic + ( + tID int primary key identity, + tUID int references bbsUser(UID), + tSID int references bbsSection(sID), + tTitle varchar(100) not null, + tMsg varchar(100) not null, + tTime datetime, + tCount int + ) + + create table bbsReply + ( + rID int primary key identity, + rUID int references bbsUser(UID), + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime + ) + + create table bbsSection + ( + sID int identity, + sName varchar(10) not null, + sUid int + ) + alter table bbsSection add constraint PK_bbsSection_sID primary key(sID) + alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUser(UID) + + insert into bbsUser values ('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) + insert into bbsSection values ('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) + insert into bbsTopic values (2,4,'范跑跑','谁是范跑跑','2008-7-8',1) + insert into bbsTopic values (3,1,'.NET','与JAVA的区别是什么呀?','2008-9-1',2),(1,3,'今年夏天最流行什么','有谁知道今年夏天最流行','2008-9-10',0) + insert into bbsReply ( rMsg, rTime, rUID,rTID )values ('范跑跑是',GETDATE(),1,1), + ('.NET与JAVA的区别是',GETDATE(),2,2), + ('今年夏天最流行是',GETDATE(),3,3) + +--在论坛数据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select *from bbsSection +select *from bbsUser +select sUid 版主编号,uName 用户名,sName 版块名称 from bbsSection inner join bbsUser on bbsSection.sUid=bbsUser.UID +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select *from bbsTopic +select *from bbsUser +select tUID 发帖人编号,uName 发帖人姓名,tTitle 帖子的标题,tMsg 帖子的内容, tTime 发帖时间 from bbsTopic +inner join bbsUser on bbsTopic.tUID=bbsUser.UID where tTime>2008-9-15 +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sUid 版主编号,uName 用户名,sName 版块名称 from bbsSection inner join bbsUser on bbsSection.sUid=bbsUser.UID where uAge<20 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select tUID 发帖人编号,uName 发帖人姓名,tTitle 帖子的标题, tMsg 帖子的内容 ,max(uPoint)回帖数量 from bbsTopic inner join bbsUser on bbsTopic.tUID=bbsUser.UID group by tUID ,uName ,tTitle, tMsg +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select count( tID ) 发帖总数 , sID 版块, UID 用户 from bbsTopic inner join bbsSection on bbsTopic.tSID=bbsSection.sID inner join bbsUser on bbsTopic.tID=bbsUser.UID group by sID,UID \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b8b9ff335be36928a5f8e3da3be84653546608a5 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery2.sql" @@ -0,0 +1,26 @@ +use TestDB +go +--统计每个班的男生数 +select ClassId,COUNT(StuSex) from StuInfo WHERE StuSex='男' group by ClassId +--统计每个班的男、女生数 +select ClassId, StuSex, count(StuSex) from StuInfo group by StuSex ,ClassId + +--统计每个班的福建人数 +select ClassId, count(StuProvince)福建人数 from StuInfo where StuProvince='福建省' group by ClassId +--统计每个班的各个省的总人数 +select ClassId, StuProvince,count(*) from StuInfo group by StuProvince,ClassId +--统计每个省的女生数 +select StuProvince,count(StuSex) from StuInfo where StuSex='女' group by StuProvince + +--统计 每个省的男、女生数 +select StuProvince,StuSex, count(StuSex) from StuInfo group by StuProvince,StuSex order by StuProvince +--统计每个学生的考试总分、平均分 +select StuId, sum(Score) 总分,avg(Score) 平均分 from Scores group by StuId +--统计出考试总分大于620的学生的考试总分 +select StuId,sum(Score) 总分 from Scores group by StuId having sum(Score)>620 +--统计出每门考试成绩最高分和最低分 + +select CourseId ,max(Score)最高分,min(Score)最低分 from Scores group by CourseId + +--统计出每个学生的各门成绩的平均分 +select StuId,CourseId, avg(Score)平均分 from Scores group by StuId,CourseId order by StuId diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b89c63e1e899f9a451b6363c50b2a996dc62d6b6 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery3.sql" @@ -0,0 +1,63 @@ +use master +go +create database Students03 + on + ( + name = 'Students03', + filename = 'D:\Students03.mdf', + size = 5MB, + maxsize = 50MB, + filegrowth = 10% + ) + log on + ( + name = 'Students03_log', + filename = 'D:\Students03_log.ldf', + size = 5MB, + maxsize = 50MB, + filegrowth = 10% + ) + go + use Students03 + go + + create table StudentInfo + ( + StuNo varchar(10) primary key , + StuName varchar(10) check(len(StuName)>=2), + StuAge int not null, + StuAddress nvarchar(20), + StuSeat int , + StuSex nvarchar(1) default ('男') check(StuSex='女' or StuSex='男') + ) + select * from StudentInfo + insert into StudentInfo values ('s2501','张秋利',20,'美国硅谷',1,'男'),('s2502','李斯文',18,'湖北武汉',2,'女'), + ('s2503','马文才',22,'湖南长沙',3,'男'),('s2504','欧阳俊雄',21,'湖北武汉',4,'女'), + ('s2505','梅超风',20,'湖北武汉',5,'男'),('s2506','陈旋风',19,'美国硅谷',6,'男'),('s2507','陈风',20,'美国硅谷',7,'女') + + create table StuExamInfo + ( + ExamNo int primary key identity(1,1), + StuNo varchar(10) references StudentInfo(StuNo), + WrittenExam int, + LadExam int + ) + select * from StuExamInfo + insert into StuExamInfo values ('s2501',50,70),('s2502',60,65),('s2503',86,85), + ('s2504',40,80),('s2505',70,90),('s2506',85,90) +--数据如图片1,使用上次作业的数据 +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select * from StudentInfo +select *from StuExamInfo +select StuName 姓名,StuAge 年龄,WrittenExam 笔试成绩,LadExam 机试成绩 from StudentInfo inner join StuExamInfo on StudentInfo.StuNo=StuExamInfo.StuNo +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select StuName 姓名,StuAge 年龄,WrittenExam 笔试成绩,LadExam 机试成绩 from StudentInfo inner join StuExamInfo on StudentInfo.StuNo=StuExamInfo.StuNo WHERE LadExam>60 AND WrittenExam>6 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,NULL值填充 +select StuName 姓名,StuAge 年龄,WrittenExam 笔试成绩,LadExam 机试成绩 from StudentInfo left join StuExamInfo on StudentInfo.StuNo=StuExamInfo.StuNo GROUP BY StuName ,StuAge ,WrittenExam ,LadExam +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 + +select StuName 姓名,StuAge 年龄,WrittenExam 笔试成绩,LadExam 机试成绩 from StudentInfo inner join StuExamInfo on StudentInfo.StuNo=StuExamInfo.StuNo order by WrittenExam desc +--5.查询男女生的机试平均分 +select StuSex 性别,avg(LadExam) from StuExamInfo inner join StudentInfo on StuExamInfo.StuNo =StudentInfo.StuNo group by StuSex +--6.查询男女生的笔试总分 +select StuSex 性别,avg(WrittenExam) from StuExamInfo inner join StudentInfo on StuExamInfo.StuNo =StudentInfo.StuNo group by StuSex diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery4.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d1b825bc5a3ceebd0d988fadbb7197515fac3a88 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\346\200\235\347\233\210/SQLQuery4.sql" @@ -0,0 +1,88 @@ + use master + go + + create database DingDan + on + ( + name = 'DingDan' , + filename = 'D:\DingDan.mdf', + size = 5MB, + MAXsize = 50MB, + filegrowth = 10% + ) + log on + ( + name = 'DingDan_log' , + filename = 'D:\DingDan_log.ldf', + size = 5MB, + MAXsize = 50MB, + filegrowth = 10% + ) + go + + use DingDan + go + create table OrdersIofo + ( + orderId int primary key identity(1,1), + orderDate datetime + ) + create table OrderItemInfo + ( + ItemiD int primary key identity(1,1), + orderId int references OrdersIofo(orderId), + itemType varchar(10) not null, + itemName varchar(10)not null, + theNumber int, + theMoney money + ) + insert into OrdersIofo values('2008-01-12'), + ('2008-02-10'), + ('2008-02-15'), + ('2008-03-10') + + insert into OrderItemInfo values(1,'文具','笔',72,2), + (1,'文具','尺',10,1), + (1,'体育用品','篮球',1,56), + (2,'文具','笔',36,2), + (2,'文具','固体胶',20,3), + (2,'日常用品','透明胶',2,1), + (2,'体育用品','羽毛球',20,3), + (3,'文具','订书机',20,3), + (3,'文具','订书机',10,3), + (3,'文具','裁纸刀',5,5), + (4,'文具','笔',20,2), + (4,'文具','信纸',50,1), + (4,'日常用品','毛巾',4,5), + (4,'日常用品','透明胶',30,1), + (4,'体育用品','羽毛球',20,3) + +-- 使用上次作业的订单数据库,完成下列题目: + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 + select * from OrderItemInfo + select * from OrdersIofo + select OI.orderId 订单编号, orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量, theMoney 订购单价 from OrderItemInfo OI inner join OrdersIofo on OI.orderId=OrdersIofo.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 + select OI.orderId 订单编号, orderDate 订单日期,itemType 产品类别,itemName 产品名称 from OrderItemInfo OI inner join OrdersIofo on OI.orderId=OrdersIofo.orderId WHERE theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + select OI.orderId 订单编号, orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量, theMoney 订购单价, theMoney*theNumber 订购总价 from OrderItemInfo OI inner join OrdersIofo on OI.orderId=OrdersIofo.orderId +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + select OI.orderId 订单编号, orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量, theMoney 订购单价, theMoney*theNumber 订购总价 from OrderItemInfo OI inner join OrdersIofo on OI.orderId=OrdersIofo.orderId where theMoney>5 and theNumber>5 + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderId 编号 , theNumber 订购产品数 from OrderItemInfo group by orderId,theNumber,itemType +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + + select orderId 编号 ,itemType 产品类别,count(theNumber) 订购次数 ,sum(theNumber) 总数量 from OrderItemInfo group by orderId,itemType order by orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\346\257\205/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\346\257\205/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a7a6a21be056f3aadb542becc55f190be5ba792d --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\346\257\205/SQLQuery1.sql" @@ -0,0 +1,466 @@ +USE [master] +GO +/****** Object: Database [TestDB] Script Date: 2021/3/15 16:11:24 ******/ +CREATE DATABASE [TestDB] + CONTAINMENT = NONE + ON PRIMARY +( NAME = N'TestDB', FILENAME = N'D:\TestDB.mdf' , SIZE = 4288KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) + LOG ON +( NAME = N'TestDB_log', FILENAME = N'D:\TestDB_log.ldf' , SIZE = 1072KB , MAXSIZE = 2048GB , FILEGROWTH = 10%) +GO +ALTER DATABASE [TestDB] SET COMPATIBILITY_LEVEL = 120 +GO +IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled')) +begin +EXEC [TestDB].[dbo].[sp_fulltext_database] @action = 'enable' +end +GO +ALTER DATABASE [TestDB] SET ANSI_NULL_DEFAULT OFF +GO +ALTER DATABASE [TestDB] SET ANSI_NULLS OFF +GO +ALTER DATABASE [TestDB] SET ANSI_PADDING OFF +GO +ALTER DATABASE [TestDB] SET ANSI_WARNINGS OFF +GO +ALTER DATABASE [TestDB] SET ARITHABORT OFF +GO +ALTER DATABASE [TestDB] SET AUTO_CLOSE OFF +GO +ALTER DATABASE [TestDB] SET AUTO_SHRINK OFF +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS ON +GO +ALTER DATABASE [TestDB] SET CURSOR_CLOSE_ON_COMMIT OFF +GO +ALTER DATABASE [TestDB] SET CURSOR_DEFAULT GLOBAL +GO +ALTER DATABASE [TestDB] SET CONCAT_NULL_YIELDS_NULL OFF +GO +ALTER DATABASE [TestDB] SET NUMERIC_ROUNDABORT OFF +GO +ALTER DATABASE [TestDB] SET QUOTED_IDENTIFIER OFF +GO +ALTER DATABASE [TestDB] SET RECURSIVE_TRIGGERS OFF +GO +ALTER DATABASE [TestDB] SET ENABLE_BROKER +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF +GO +ALTER DATABASE [TestDB] SET DATE_CORRELATION_OPTIMIZATION OFF +GO +ALTER DATABASE [TestDB] SET TRUSTWORTHY OFF +GO +ALTER DATABASE [TestDB] SET ALLOW_SNAPSHOT_ISOLATION OFF +GO +ALTER DATABASE [TestDB] SET PARAMETERIZATION SIMPLE +GO +ALTER DATABASE [TestDB] SET READ_COMMITTED_SNAPSHOT OFF +GO +ALTER DATABASE [TestDB] SET HONOR_BROKER_PRIORITY OFF +GO +ALTER DATABASE [TestDB] SET RECOVERY FULL +GO +ALTER DATABASE [TestDB] SET MULTI_USER +GO +ALTER DATABASE [TestDB] SET PAGE_VERIFY CHECKSUM +GO +ALTER DATABASE [TestDB] SET DB_CHAINING OFF +GO +ALTER DATABASE [TestDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) +GO +ALTER DATABASE [TestDB] SET TARGET_RECOVERY_TIME = 0 SECONDS +GO +ALTER DATABASE [TestDB] SET DELAYED_DURABILITY = DISABLED +GO +EXEC sys.sp_db_vardecimal_storage_format N'TestDB', N'ON' +GO +USE [TestDB] +GO +/****** Object: Table [dbo].[ClassInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[ClassInfo]( + [ClassId] [int] IDENTITY(1,1) NOT NULL, + [ClassName] [nvarchar](20) NOT NULL, +PRIMARY KEY CLUSTERED +( + [ClassId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[CourseInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[CourseInfo]( + [CourseId] [int] IDENTITY(1,1) NOT NULL, + [CourseName] [nvarchar](50) NOT NULL, + [CourseCredit] [int] NULL DEFAULT ((1)), +PRIMARY KEY CLUSTERED +( + [CourseId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[Scores] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[Scores]( + [ScoreId] [int] IDENTITY(1,1) NOT NULL, + [StuId] [int] NULL, + [CourseId] [int] NULL, + [Score] [int] NULL DEFAULT ((0)), +PRIMARY KEY CLUSTERED +( + [ScoreId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[StuInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[StuInfo]( + [StuId] [int] IDENTITY(1,1) NOT NULL, + [ClassId] [int] NULL, + [StuName] [nvarchar](10) NOT NULL, + [StuSex] [nvarchar](1) NULL DEFAULT ('男'), + [StuBrithday] [date] NULL, + [StuPhone] [nvarchar](11) NULL, + [StuProvince] [nvarchar](200) NULL, + [CreateDate] [datetime] NULL DEFAULT (getdate()), + [StuAge] [int] NULL, +PRIMARY KEY CLUSTERED +( + [StuId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] ON + +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (1, N'软件1班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (2, N'软件2班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (3, N'软件3班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (4, N'软件4班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (5, N'软件5班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (6, N'软件6班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (7, N'软件7班') +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] ON + +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (1, N'计算机基础', 3) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (2, N'HTML+CSS网页制作', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (3, N'JAVA编程基础', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (4, N'SQL Server数据库基础', 4) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (5, N'C#面向对象编程', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (6, N'Winform桌面应用程序设计', 5) +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[Scores] ON + +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (1, 1, 1, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (2, 1, 2, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (3, 1, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (4, 1, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (5, 2, 1, 60) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (6, 2, 2, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (7, 2, 3, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (8, 2, 4, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (9, 3, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (10, 3, 2, 45) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (11, 3, 3, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (12, 3, 4, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (13, 4, 1, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (14, 4, 2, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (15, 4, 3, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (16, 4, 4, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (17, 5, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (18, 5, 2, 79) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (19, 5, 3, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (20, 5, 4, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (21, 6, 1, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (22, 6, 2, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (23, 6, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (24, 6, 5, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (25, 7, 1, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (26, 7, 2, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (27, 7, 3, 92) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (28, 7, 5, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (29, 8, 1, 58) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (30, 8, 2, 59) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (31, 8, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (32, 8, 5, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (33, 9, 1, 48) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (34, 9, 2, 67) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (35, 9, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (36, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (37, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (38, 1, 1, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (39, 1, 2, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (40, 1, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (41, 1, 4, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (42, 2, 1, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (43, 2, 2, 82) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (44, 2, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (45, 2, 4, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (46, 3, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (47, 3, 2, 50) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (48, 3, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (49, 3, 4, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (50, 4, 1, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (51, 4, 2, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (52, 4, 3, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (53, 4, 4, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (54, 5, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (55, 5, 2, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (56, 5, 3, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (57, 5, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (58, 6, 1, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (59, 6, 2, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (60, 6, 3, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (61, 6, 5, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (62, 7, 1, 89) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (63, 7, 2, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (64, 7, 3, 97) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (65, 7, 5, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (66, 8, 1, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (67, 8, 2, 64) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (68, 8, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (69, 8, 5, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (70, 9, 1, 53) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (71, 9, 2, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (72, 9, 3, 76) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (73, 9, 5, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (74, 9, 5, 61) +GO +SET IDENTITY_INSERT [dbo].[Scores] OFF +GO +SET IDENTITY_INSERT [dbo].[StuInfo] ON + +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (1, 1, N'刘正', N'男', CAST(N'2002-08-02' AS Date), N'13245678121', N'广西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (2, 1, N'黄贵', N'男', CAST(N'2003-07-02' AS Date), N'13345678121', N'江西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (3, 1, N'陈美', N'女', CAST(N'2002-07-22' AS Date), N'13355678125', N'福建省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (4, 2, N'江文', N'男', CAST(N'2001-07-02' AS Date), N'13347678181', N'湖南省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (5, 2, N'钟琪', N'女', CAST(N'2004-01-13' AS Date), N'13345778129', N'安徽省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (6, 3, N'曾小林', N'男', CAST(N'2005-05-15' AS Date), N'13345378563', N'安徽省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (7, 3, N'欧阳天天', N'女', CAST(N'2000-08-19' AS Date), N'13347878121', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (8, 3, N'李逍遥', N'男', CAST(N'1999-09-02' AS Date), N'13345678557', N'广东省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 22) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (9, 4, N'刘德华', N'男', CAST(N'1995-06-11' AS Date), N'15345679557', N'福建省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 26) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (10, 4, N'刘翔', N'男', CAST(N'1996-07-09' AS Date), N'18346679589', N'江西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (11, 4, N'曾小贤', N'男', CAST(N'2003-07-02' AS Date), N'18348979589', N'湖南省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (12, 5, N'刘德华', N'男', CAST(N'2002-07-02' AS Date), N'18348979509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (13, 5, N'陈天翔', N'男', CAST(N'2003-07-02' AS Date), N'18348079509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (14, 5, N'刘能', N'男', CAST(N'2005-08-02' AS Date), N'13245678122', N'广西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (15, 5, N'钟馗', N'男', CAST(N'2004-08-02' AS Date), N'13245678123', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (16, 5, N'钟吴艳', N'女', CAST(N'2002-08-02' AS Date), N'13245678124', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (17, 5, N'刘欢', N'男', CAST(N'2001-07-02' AS Date), N'13245678125', N'湖南省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (18, 5, N'张庭', N'女', CAST(N'2000-07-02' AS Date), N'13245678126', N'江西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (19, 5, N'曹植', N'男', CAST(N'2000-08-02' AS Date), N'13245678127', N'福建省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (20, 5, N'曹操', N'男', CAST(N'2002-08-02' AS Date), N'13245678128', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (21, 5, N'孙尚香', N'女', CAST(N'2003-08-02' AS Date), N'13245678129', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (22, 3, N'老1', N'女', CAST(N'2002-08-02' AS Date), N'13245678130', N'广东省', CAST(N'2021-03-14 17:02:36.347' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (24, 2, N'老2', N'男', CAST(N'2002-08-03' AS Date), N'13345678945', NULL, CAST(N'2021-03-14 17:03:37.733' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (25, 4, N'老3', N'男', NULL, N'13645987545', N'广东省', CAST(N'2021-03-14 17:03:43.307' AS DateTime), NULL) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (28, 5, N'老4', N'男', CAST(N'2006-03-05' AS Date), N'13456987456', NULL, CAST(N'2021-03-14 17:04:03.957' AS DateTime), 15) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (29, 5, N'老5', N'女', CAST(N'1998-04-12' AS Date), N'15978456123', NULL, CAST(N'2021-03-14 17:04:47.103' AS DateTime), 23) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (30, 4, N'老6', N'男', CAST(N'1996-08-06' AS Date), N'18945674561', NULL, CAST(N'2021-03-14 17:05:04.990' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (31, 3, N'老7', N'女', CAST(N'1997-04-06' AS Date), N'18845678912', NULL, CAST(N'2021-03-14 17:05:20.570' AS DateTime), 24) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (32, 2, N'老10', N'女', CAST(N'1998-08-09' AS Date), N'19945645612', NULL, CAST(N'2021-03-14 17:06:08.107' AS DateTime), 23) +GO +SET IDENTITY_INSERT [dbo].[StuInfo] OFF +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__CourseIn__9526E2773AB7BECE] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[CourseInfo] ADD UNIQUE NONCLUSTERED +( + [CourseName] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__StuInfo__2D85FC63AF6FC6FA] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[StuInfo] ADD UNIQUE NONCLUSTERED +( + [StuPhone] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([CourseId]) +REFERENCES [dbo].[CourseInfo] ([CourseId]) +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([StuId]) +REFERENCES [dbo].[StuInfo] ([StuId]) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD FOREIGN KEY([ClassId]) +REFERENCES [dbo].[ClassInfo] ([ClassId]) +ON DELETE SET NULL +GO +ALTER TABLE [dbo].[CourseInfo] WITH CHECK ADD CHECK (([CourseCredit]>=(1) AND [CourseCredit]<=(5))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK ((len([StuPhone])=(11))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK (([StuSex]='女' OR [StuSex]='男')) +GO +USE [master] +GO +ALTER DATABASE [TestDB] SET READ_WRITE +GO + + +select * from Stuinfo +select * from Scores +select * from Courseinfo +select * from Classinfo + +--统计 每个班 的 男生数 +select ClassId,count(Stusex)男生数 from Stuinfo where Stusex='男' group by ClassId +--统计 每个班 的 男、女生数 +select ClassId,Stusex,count(*)人数 from Stuinfo group by ClassId,Stusex +--统计 每个班 的 福建人数 +select ClassId,count(*)福建人数 from Stuinfo where StuProvince='福建省' group by ClassId +--统计 每个班 的 各个省的总人数 +select ClassId,StuProvince 省份,count(*)人数 from Stuinfo group by ClassId,StuProvince order by ClassID +--统计 每个省 的 女生数 +select StuProvince 省份,count(StuSex)女生人数 from Stuinfo where StuSex='女' group by StuProvince +--统计 每个省 的 男、女生数 +select StuProvince,StuSex,count(*)人数 from Stuinfo group by StuProvince,StuSex +--统计 每个学生 的 考试总分、平均分 +select StuId,SUM(Score)考试总分,AVG(Score)平均分 from Scores group by StuId +--统计出 考试总分大于620 的学生 的考试总分 +select StuId,SUM(Score)总分 from Scores group by StuId having SUM(Score)>620 +--统计出 每门考试 成绩最高分和最低分 +select CourseId,MAX(Score)最高分,MIN(Score)最低分 from Scores group by CourseId +--统计出 每个学生 的 各门成绩的平均分 +select StuId,CourseId,AVG(Score)各门成绩的平均分 from Scores group by StuId,CourseId order by StuId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\346\257\205/\347\273\203\344\271\2401.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\346\257\205/\347\273\203\344\271\2401.sql" new file mode 100644 index 0000000000000000000000000000000000000000..26098a0376f776e7c29bb5932f622c7172c1cb64 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\346\257\205/\347\273\203\344\271\2401.sql" @@ -0,0 +1,97 @@ +create database Student +on +( + name=Student, + filename='E:\SQL课堂\Student.mdf', + size=10MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name=Student_log, + filename='E:\SQL课堂\Student_log.ldf', + size=10MB, + maxsize=50MB, + filegrowth=10% +) + +use Student +go + +create table Stuinfo +( + stuNO varchar(5) unique not null, + stuName nvarchar(20), + stuAge int, + stuAddress nvarchar(20), + stuSeat int identity(1,1) unique not null, + stuSex nchar(1) check(stuSex='男' or stuSex='女') +) +go +insert into Stuinfo values ('s2501','张秋利',20,'美国硅谷','女'),('s2502','李斯文',18,'湖北武汉','男'),('s2503','马文才',22,'湖南长沙','女'), +('s2504','欧阳俊雄',21,'湖北武汉','男'),('s2505','梅超风',20,'湖北武汉','女'),('s2506','陈旋风',19,'美国硅谷','女'),('s2507','陈风',20,'美国硅谷','男') + +select 学号=stuNO,stuName as 姓名,stuAge as 年龄,stuAddress as 地址,stuSeat as 座位号,stuSex as 性别 from Stuinfo +select stuName,stuAge,stuAddress from Stuinfo +select stuNO,邮箱=stuName+'@'+stuAddress from Stuinfo +select stuAddress from Stuinfo +select distinct 所有年龄=stuAge from Stuinfo +select * from Stuinfo where stuSeat<=3 +select stuName,stuSeat from Stuinfo where stuSeat<=4 +select top 50 percent * from Stuinfo +select * from Stuinfo where stuAddress='湖北武汉' and stuAge=20 +select * from Stuinfo where stuAddress='湖北武汉' or stuAddress='湖南长沙' +select * from Stuinfo where stuAddress in('湖北武汉' , '湖南长沙') +select * from Stuinfo where stuAge is null +select * from Stuinfo where stuAge is not null +select * from Stuinfo where stuName like '张%' +select * from Stuinfo where stuAddress like '湖%' +select * from Stuinfo where stuName like '张_' +select * from Stuinfo where stuName like '__俊_' +select * from Stuinfo order by stuAge desc +select * from Stuinfo order by stuAge desc,stuSeat asc + + + +create table Scoreinfo +( + examNO int primary key identity(1,1), + stuNO varchar(5) references Stuinfo(stuNO), + writtenExan int, + labExam int, +) +insert into Scoreinfo values('s2501',50,70),('s2502',60,65),('s2503',86,85),('s2504',40,80),('s2505',70,90),('s2506',85,90) + +select 学号=stuNO,writtenExan as 笔试 ,labExam as 机试 from Scoreinfo +select examNO,stuNO,总分=writtenExan+labExam from Scoreinfo +select labExam from Scoreinfo where labExam>=60 and labExam<=80 order by labExam desc +select writtenExan from Scoreinfo where not labExam>=70 and labExam<=90 +select top 1* from Scoreinfo order by writtenExan desc +select top 1* from Scoreinfo order by labExam + +select stuAddress,AVG(stuAge)平均年龄 from dbo.Stuinfo group by stuAddress +select stuSex,SUM(stuAge)年龄总和 from dbo.Stuinfo group by stuSex +select stuAddress,AVG(stuAge)平均年龄,SUM(stuAge)年龄总和 from dbo.Stuinfo group by stuAddress + + +--数据如图片1,使用上次作业的数据 +select * from Stuinfo +select * from Scoreinfo + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 stuName stuAge writtenExan labExam +select stuName 姓名,stuAge 年龄,writtenExan 笔试成绩,labExam 机试成绩 from Scoreinfo inner join Stuinfo on Scoreinfo.stuNO = Stuinfo.stuNO +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select Stuinfo.stuNO 学号,Stuinfo.stuName 姓名,Scoreinfo.writtenExan 笔试成绩,Scoreinfo.labExam 机试成绩 from Scoreinfo inner join Stuinfo on Scoreinfo.stuNO = Stuinfo.stuNO +where Scoreinfo.writtenExan>=60 and Scoreinfo.labExam>=60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select A.stuNO 学号,A.stuName 姓名,B.writtenExan 笔试成绩,B.labExam 机试成绩 from Stuinfo A +left join Scoreinfo B on A.stuNO = B.stuNO +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName 姓名,stuAge 年龄,writtenExan 笔试成绩,labExam 机试成绩 from Stuinfo inner join Scoreinfo on Stuinfo.stuNO = Scoreinfo.stuNO +where stuAge>=20 order by writtenExan DESC +--5.查询男女生的机试平均分 +select stuSex 性别,AVG(labExam)机试平均分 from Stuinfo inner join Scoreinfo on Stuinfo.stuNO = Scoreinfo.stuNO group by stuSex + --结果四舍五入 +--6.查询男女生的笔试总分 +select stuSex 性别,SUM(writtenExan)笔试总分 from Stuinfo inner join Scoreinfo on Stuinfo.stuNO = Scoreinfo.stuNO group by stuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\346\257\205/\347\273\203\344\271\2402.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\346\257\205/\347\273\203\344\271\2402.sql" new file mode 100644 index 0000000000000000000000000000000000000000..459f349147fc8e68fe9d2094de4e006a48f36eb7 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\346\257\205/\347\273\203\344\271\2402.sql" @@ -0,0 +1,111 @@ +create database BBS +on +( + name='BBS', + filename='E:\SQL课堂\BBS.mdf', + size=20, + maxsize=300, + filegrowth=50 +) +log on +( + name='BBS_log', + filename='E:\SQL课堂\BBS_log.ldf', + size=20, + maxsize=300, + filegrowth=50 +) +go +use BBS +go +create table orderItem +( +ItemiD int , +orderId int, +itemType varchar(10), +itemName nvarchar(10), +theNumber int, +theMoney money +) +create table orders +( +orderId int not null primary key , +orderDate datetime +) +insert into orders(orderId,orderDate) values (1,'2008-1-12'),(2,'2008-2-10'),(3,'2008-2-15'),(4,'2008-3-10') +select * from orders +insert into orderItem(ItemiD,orderId,itemType,itemName,theNumber,theMoney) values +(1,1,'文具','笔',72,2), +(2,1,'文具','尺',10,1), +(3,1,'体育用品','篮球',1,56), +(4,2,'文具','笔',36,2), +(5,2,'文具','固体胶',20,3), +(6,2,'日常用品','透明胶',2,1), +(7,2,'体育用品','羽毛球',20,3), +(8,3,'文具','订书机',20,3), +(9,3,'文具','订书钉',10,3), +(10,3,'文具','裁纸刀',5,5), +(11,4,'文具','笔',20,2), +(12,4,'文具','信纸',50,1), +(13,4,'日常用品','毛巾',4,5), +(14,4,'日常用品','透明胶',30,1), +(15,4,'体育用品','羽毛球',20,3) + +select * from orderItem +--1.查询所有订单订购的所有物品数量总和 +select SUM(theNumber) 数量总和 from orderItem +--2.查询 每个订单订购的所有物品的 数量和 以及 平均单价 +select orderId,SUM(theNumber), AVG(theMoney) from orderItem group by orderId having AVG(theMoney)<10 and orderId<3 + +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select orderId ,sum(theNumber),AVG(theMoney) from orderItem where theNumber>50 group by orderId having AVG(theMoney)<10 +--4.查询每种类别的产品分别订购了几次,例如: +-- 文具 9 +-- 体育用品 3 +-- 日常用品 3 +select itemType 类型,COUNT(itemType) 数量 from orderItem group by itemType + +--5.查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 + +select itemType 类型,sum(theNumber)总数,avg(theMoney) 平均单价 from orderItem group by itemType having sum(theNumber)>100 + +--6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: + +-- 产品名称 订购次数 总数量 平均单价 +-- 笔 3 120 2 +select itemName 产品名称 ,COUNT(itemName) 订购次数,sum(theNumber) 总数量 ,avg(theMoney)平均单价 from orderItem group by itemName + + +-------------------------------------------------------- + +--使用上次作业的订单数据库,完成下列题目: +select * from orders +select * from orderItem + +--1.查询所有的订单的 订单的编号ItemiD,订单日期orderDate,订购产品的类别itemType和订购的产品名称itemName,订购数量theNumber和订购单价theMoney +select orders.orderId 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价 from orders inner join orderItem on orders.orderId = orderItem.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orders.orderId 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称 from orders inner join orderItem on orders.orderId = orderItem.orderId +where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderId 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价,SUM(theNumber * theMoney) 订购总价 from orders inner join orderItem on orders.orderId = orderItem.orderId +group by orders.orderId,orderDate,itemType,itemName,theNumber,theMoney +--4.查询单价大于等于3并且数量大于等于20的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderId 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价,SUM(theNumber * theMoney) 订购总价 from orders inner join orderItem on orders.orderId = orderItem.orderId +where theMoney>=3 and theNumber>=20 +group by orders.orderId,orderDate,itemType,itemName,theNumber,theMoney +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderItem.orderId 编号,COUNT(itemName)订购产品数 from orderItem group by orderItem.orderId +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 +select orderItem.orderId 订单编号,itemType 产品类别,COUNT(itemType)订购次数,SUM(theNumber)总数量 from orderItem group by orderItem.orderId,itemType order by orderItem.orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\346\257\205/\347\273\203\344\271\2403.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\346\257\205/\347\273\203\344\271\2403.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c265c8ee6f9e12ce6a5500f5c3d97cd806a5bdd1 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\346\257\205/\347\273\203\344\271\2403.sql" @@ -0,0 +1,136 @@ +create database 论坛 +on +(name='论坛', + filename='E:\SQL论坛\论坛.mdf', + size=20, + maxsize=300, + filegrowth=50 +) +log on +(name='论坛_log', + filename='E:\SQL论坛\论坛_log.ldf', + size=20, + maxsize=300, + filegrowth=50 +) +go +use 论坛 +create table bbsUsers +( +uIDD int primary key identity(1,1), +uName varchar(10) not null unique , + uSex varchar(2) not null check(uSex='男' or uSex='女'), + uAge int not null check(uAge>15 and uAge<60), + uPoint int not null check(uPoint>=0) +) +create table bbsSection( +sIDD int primary key identity(1,1), +sName varchar(10) not null , + sUid int references bbsUsers(uIDD) +) +create table bbsTopic( + tID int primary key identity(1,1), + tUID int references bbsUsers(uIDD), + tSID int references bbsSection(sIDD), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime , + tCount int +) +create table bbsReply( +rID int primary key identity(1,1), + rUID int references bbsUsers(uIDD ), + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime + +) + +--1.现在有3个会员注册成功,请用一次插入多行数据的方法向bbsUsers表种插入3行记录,记录值如下: +-- 小雨点 女 20 0 +-- 逍遥 男 18 4 +-- 七年级生 男 19 2 +insert into bbsUsers(uName,uSex,uAge,uPoint) +select'小雨点','女','20','0'union +select'逍遥','男','18','4'union +select'七年级生','男','19','2' +go + + --2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中,提示查询部分列:select 列名1,列名2 from 表名 + select uName,uPoint into bbsPoint from bbsUsers + go + --3.给论坛开设4个板块 + -- 名称 版主名 + -- 技术交流 小雨点 + -- 读书世界 七年级生 + -- 生活百科 小雨点 + -- 八卦区 七年级生 + insert into bbsSection(sName,sUid) + select'技术交流',3 union + select'读书世界',1 union + select'生活百科',3 union + select'八卦区',1 + go + --4.向主贴和回帖表中添加几条记录 + insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) +select '2','1','范跑跑','谁是范跑跑','2008-7-8','1'union +select '1','3','.NET','与Java的区别是什么呀?','2008-9-1','2'union +select '3','4','今年夏天最流行什么呀?','有谁知道今年夏天最流行什么呀?','2008-9-10','0' + + +-- 回帖: +-- 分别给上面三个主贴添加对应的回帖,回帖的内容,时间,回帖人自定 + +insert into bbsReply(rTID,rMsg,rTime,rUId) +select'1','不知道','2008-9-5','2'union +select'2','不认识','2008-9-10','1'union +select'3','不知道','2008-10-1','3' + +-- 5.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户,请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) + + + +alter table bbsReply drop constraint FK_bbsReply_rUID ----出错 +alter table bbsTopic drop constraint FK_bbsTopic_tUID +alter table bbsSection drop constraint FK_bbsSection_sUid +alter table bbsUsers drop constraint PK_bbsUsers_UID + +delete from bbsUsers where UIDD=2 + +-- 6.因为小雨点发帖较多,将其积分增加10分 + + +update bbsPoint set uPoint=(uPoint + 10) where uName='小雨点' + + +-- 7.因为板块“生活百科”灌水的人太少,现决定取消该板块,即删除(注意主外键) + +delete from bbsReply where rTID=3 +delete from bbsTopic where tSID=4 +delete from bbsSection where sIDD=4 + +-- 8.因回帖积累太多,现需要将所有的回帖删除 + +delete from bbsReply +truncate table bbsReply +drop table bbsReply + + +--在论坛数据库中完成以下题目 +select * from bbsUsers +select * from bbsTopic +select * from bbsSection +select * from bbsPoint +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select sUid 版主编号,uName 版主姓名,sName 版块名称 from bbsSection inner join bbsUsers on bbsSection.sUid = bbsUsers.uIDD +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID 发帖人编号,uName 发帖人姓名,tTitle 帖子的标题,tMsg 帖子的内容,tTime 发帖时间 from bbsTopic inner join bbsUsers on bbsTopic.tUID = bbsUsers.uIDD +where tTime>'2008-8-15' +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sUid 版主编号,uName 版主的名称,sName 版块的名称 from bbsSection inner join bbsUsers on bbsSection.sUid = bbsUsers.uIDD +where bbsUsers.uAge<20 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select tUID 发帖人编号,uName 发帖人姓名,tTitle 主贴标题,tMsg 主贴内容,tCount 回复数量 from bbsTopic inner join bbsUsers on bbsTopic.tUID = bbsUsers.uIDD +where tcount=(select MAX(tcount)from bbsTopic) +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select tSID 版块编号,tUID 用户,count(tID)发帖总数 from bbsTopic inner join bbsUsers on bbsTopic.tUID = bbsUsers.uIDD inner join bbssection on bbsTopic.tUID = bbsUsers.uIDD group by tSID,tUID diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\346\261\211\346\226\207/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\346\261\211\346\226\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c5794f5b86b052370ab87e2ba68fb887de6e5ed1 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\346\261\211\346\226\207/SQLQuery1.sql" @@ -0,0 +1,103 @@ +--建数据库 +create database Dong +on +( + name='Dong', + filename='D:\test\Dong.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% + +) +log on +( + name='Dong_log', + filename='D:\test\Dong_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% + +) +--使用创建出来的数据库 +go +use Dong + +go +create table orders +( +orderId int primary key identity(1,1), +orderDate datetime not null, +) +--在数据库创建表 +go +create table orderItem +( +ItemiD int primary key identity(1,1), +orderId int references orders(orderId), +itemType varchar(20) not null, +itemName varchar(20) not null, +theNumber int not null, +theMoney money not null, +) +--测试查询 orders orderItem 表 +go +select * from orders +select * from orderItem +--添加日期 +go +insert into orders(orderDate) values('2008-01-12'),('2008-02-10'),('2008-02-15'),('2008-03-10') +--添加数据 +go +insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values +(1,'文具','笔',72,2), +(1,'文具','尺',10,1), +(1,'体育用品','篮球',1,56), +(2,'文具','笔',36,2), +(2,'文具','固体胶',20,3), +(2,'日常用品','透明胶',2,1), +(2,'体育用品','羽毛球',20,3), +(3,'文具','订书机',20,3), +(3,'文具','订书针',10,3), +(3,'文具','裁纸刀',5,5), +(4,'文具','笔',20,2), +(4,'文具','信纸',50,1), +(4,'日常用品','毛巾',4,5), +(4,'日常用品','透明胶',30,1), +(4,'体育用品','羽毛球',20,3) + + +--使用上次作业的订单数据库,完成下列题目: + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +GO +select orders.orderId 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量,theMoney 单价 from orderItem inner join orders on orders.orderId = orderItem.orderId + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +go +select orders.orderId 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称 from orderItem inner join orders on orders.orderId = orderItem.orderId where theNumber>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +go +select orders.orderId 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量,theMoney 单价,sum(theNumber*theMoney)订购总价 from orderItem inner join orders on orderItem.orderId = orders.orderId group by theMoney,orders.orderId,orderDate,itemType,itemName,theNumber,theMoney order by orders.orderId + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +go +select orders.orderId 订单编号,orderDate 订单日期,itemType 订购产品,itemName 产品名称,theNumber 订购数量,theMoney 订购单价,sum(theMoney*theNumber)订购总价 from orders inner join orderItem on orders.orderId=orderItem.orderId group by theMoney,orders.orderId,orderDate,itemType,itemName,theNumber,theMoney having theMoney>4 and theNumber>=3 + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +go +select orders.orderId 编号,theNumber 订购产品数 from orderItem inner join orders on orderItem.orderId=orders.orderId + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 +select orders.orderId 订单编号, itemType 产品类别,COUNT(*)订购次数,sum(theNumber)总数量 from orderItem inner join orders on orders.orderId = orderItem.orderId group by orders.orderId,itemType order by orders.orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\346\261\211\346\226\207/SQLQuery10.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\346\261\211\346\226\207/SQLQuery10.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6acb0e8dd6b46d95edf3d0aaec5312f76b860e84 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\346\261\211\346\226\207/SQLQuery10.sql" @@ -0,0 +1,79 @@ +create database Student +on +( + name='Stuinfo', + filename='D:\test\Stuinfo.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% + +) +log on +( + name='Stuinfo_log', + filename='D:\test\Stuinfo_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% + +) +go +use Student + +go + +create table Stuinfo +( + stuNO varchar(5) unique not null, + stuName nvarchar(20), + stuAge int, + stuAddress nvarchar(20), + stuSeat int identity(1,1) unique not null, + stuSex nchar(1) check(stuSex='男' or stuSex='女') +) + +go + +create table Scoreinfo +( + examNO int primary key identity(1,1), + stuNO varchar(5) references Stuinfo(stuNO), + writtenExan int, + labExam int, +) + +go +insert into Scoreinfo values('s2501',50,70),('s2502',60,65),('s2503',86,85),('s2504',40,80),('s2505',70,90),('s2506',85,90) + +go +insert into Stuinfo values ('s2501','张秋利','20','美国硅谷','女'),('s2502','李斯文',18,'湖北武汉','男'),('s2503','马文才',22,'湖南长沙','女'), +('s2504','欧阳俊雄',21,'湖北武汉','男'),('s2505','梅超风',20,'湖北武汉','女'),('s2506','陈旋风',19,'美国硅谷','女'),('s2507','陈风',20,'美国硅谷','男') + +go +select * from Stuinfo +select * from Scoreinfo + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +go +select stuName 姓名,stuAge 年龄,writtenExan 笔试成绩,labExam 机试成绩 from Stuinfo inner join Scoreinfo ON Stuinfo.stuNO =Scoreinfo.stuNO + + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +go +select Scoreinfo.stuNO 学号,stuName 名字,writtenExan 笔试成绩,labExam 机试成绩 from Stuinfo inner join Scoreinfo on Scoreinfo.stuNO =Stuinfo.stuNO where writtenExan>60 and labExam>60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +go +select Stuinfo.stuNO 学号,stuName 名字,writtenExan 笔试成绩,labExam 机试成绩 from Stuinfo left join Scoreinfo on Scoreinfo.stuNO = Stuinfo.stuNO + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +go +select stuName 姓名,stuAge 年龄,writtenExan 笔试成绩,labExam 机试成绩 from Stuinfo inner join Scoreinfo on Scoreinfo.stuNO = Stuinfo.stuNO where stuAge>20 order by writtenExan desc + +--5.查询男女生的机试平均分 +go +select stuSex 性别,AVG(labExam)机试平均分 from Stuinfo inner join Scoreinfo on Scoreinfo.stuNO = Stuinfo.stuNO group by stuSex + +--6.查询男女生的笔试总分 +go +select stuSex 性别,SUM(writtenExan)笔试平均分 from Stuinfo inner join Scoreinfo on Scoreinfo.stuNO = Stuinfo.stuNO group by stuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\346\261\211\346\226\207/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\346\261\211\346\226\207/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..205bee802840e186a5af8a56077745483f78d203 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\346\261\211\346\226\207/SQLQuery2.sql" @@ -0,0 +1,111 @@ +use master +create database bbs +on +( + name='bbs', + filename='D:\test\bbs.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='bbs_log', + filename='D:\test\bbs_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go +use bbs +go +create table bbsUser +( + uID int identity(1,1) not null, + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) + + + +--添加约束 +-- alter table 表名 add constraint 约束名 约束类型 +go +alter table bbsUser add constraint PK_bbsUser_uID primary key(uID) +alter table bbsUser add constraint UK_bbsUser_uName unique(uName) +alter table bbsUser add constraint CK_bbsUser_uSex check(uSex='男' or uSex='女') +alter table bbsUser add constraint CK_bbsUser_uAge check(uAge>=15 and uAge<=60) +alter table bbsUser add constraint CK_bbsUser_uPoint check(uPoint>=0) + +go +alter table bbsSection add constraint PK_bbsSection_sID primary key(sID) +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUser(uID) + +go +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int references bbsUser(uID), + tSID int references bbsSection(sID), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) +go +create table bbsReply +( + rID int identity(1,1) primary key, + rUID int references bbsUser(uID), + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +go +insert into bbsUser (uName,uSex,uAge,uPoint) values ('小雨点','女','20','0') +,('逍遥','男','18','4'),('七年级生','男','19','2') + +go +select uName,uPoint into bbsPoint from bbsUser + +go +insert into bbsSection (sName,sUid) values ('技术交流',1), +('读书世界',3),('生活百科',1),('八卦区',3) + +go +insert into bbsTopic (tUID,tSID,tTitle,tMsg,tTime,tCount) values ('2','4','范跑跑','谁是范跑跑','2008-7-8','1'), +('3','1','.NET','谁是范跑跑','2008-9-1','2'),('1','3','.今年夏天最流行什么呀?','有谁知道今年夏天最流行','2008-9-10','0') + +go +insert into bbsReply (rUID,rMsg,rTime) values ('1','范跑跑是谁啊','2021-1-1'), +('2','名字不一样','2021-1-2'),('3','EMO','2021-1-3') + +go +select * from bbsUser +select * from bbsSection +select * from bbsTopic +select * from bbsReply + +--在论坛数据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 +go +select sUid 版主编号,uName 版主姓名,sName 版块名称 from bbsUser inner join bbsSection on bbsUser.uID= bbsSection.sUid + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +go +select uID 发帖人编号,uName 发贴人编号,tTitle 帖子标题,tMsg 帖子内容,tTime 发帖时间 from bbsTopic inner join bbsUser on bbsTopic.tSID=bbsUser.uID where tTime>'2008-9-1' + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +go +select sID 版主编号,uName 版主名称,sName 版块吧名称,uAge 年龄 from bbsUser inner join bbsSection on bbsUser.uID=bbsSection.sUid where uAge<20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +go +select uID发帖人编号,uName 发帖人名字,tTitle 主贴标题,tMsg 主题内容,tCount 回复数量 from bbsTopic inner join bbsUser on bbsTopic.tSID=bbsUser.uID where tCount= (select max(tCount) from bbsTopic) + +--5.在主贴表中查询每个版块中每个用户的发帖总数 +go +select tsid 版块编号,count(tSID) 发帖总数 from bbsTopic group by tUID,tSID \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\346\261\211\346\226\207/SQLQuery8.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\346\261\211\346\226\207/SQLQuery8.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a6b3f2fe312b9a23d61e04b2dac9c47bf71bffeb --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\346\261\211\346\226\207/SQLQuery8.sql" @@ -0,0 +1,465 @@ +USE [master] +GO +/****** Object: Database [TestDB] Script Date: 2021/3/15 16:11:24 ******/ +CREATE DATABASE [TestDB] + CONTAINMENT = NONE + ON PRIMARY +( NAME = N'TestDB', FILENAME = N'D:\TestDB.mdf' , SIZE = 4288KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) + LOG ON +( NAME = N'TestDB_log', FILENAME = N'D:\TestDB_log.ldf' , SIZE = 1072KB , MAXSIZE = 2048GB , FILEGROWTH = 10%) +GO +ALTER DATABASE [TestDB] SET COMPATIBILITY_LEVEL = 120 +GO +IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled')) +begin +EXEC [TestDB].[dbo].[sp_fulltext_database] @action = 'enable' +end +GO +ALTER DATABASE [TestDB] SET ANSI_NULL_DEFAULT OFF +GO +ALTER DATABASE [TestDB] SET ANSI_NULLS OFF +GO +ALTER DATABASE [TestDB] SET ANSI_PADDING OFF +GO +ALTER DATABASE [TestDB] SET ANSI_WARNINGS OFF +GO +ALTER DATABASE [TestDB] SET ARITHABORT OFF +GO +ALTER DATABASE [TestDB] SET AUTO_CLOSE OFF +GO +ALTER DATABASE [TestDB] SET AUTO_SHRINK OFF +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS ON +GO +ALTER DATABASE [TestDB] SET CURSOR_CLOSE_ON_COMMIT OFF +GO +ALTER DATABASE [TestDB] SET CURSOR_DEFAULT GLOBAL +GO +ALTER DATABASE [TestDB] SET CONCAT_NULL_YIELDS_NULL OFF +GO +ALTER DATABASE [TestDB] SET NUMERIC_ROUNDABORT OFF +GO +ALTER DATABASE [TestDB] SET QUOTED_IDENTIFIER OFF +GO +ALTER DATABASE [TestDB] SET RECURSIVE_TRIGGERS OFF +GO +ALTER DATABASE [TestDB] SET ENABLE_BROKER +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF +GO +ALTER DATABASE [TestDB] SET DATE_CORRELATION_OPTIMIZATION OFF +GO +ALTER DATABASE [TestDB] SET TRUSTWORTHY OFF +GO +ALTER DATABASE [TestDB] SET ALLOW_SNAPSHOT_ISOLATION OFF +GO +ALTER DATABASE [TestDB] SET PARAMETERIZATION SIMPLE +GO +ALTER DATABASE [TestDB] SET READ_COMMITTED_SNAPSHOT OFF +GO +ALTER DATABASE [TestDB] SET HONOR_BROKER_PRIORITY OFF +GO +ALTER DATABASE [TestDB] SET RECOVERY FULL +GO +ALTER DATABASE [TestDB] SET MULTI_USER +GO +ALTER DATABASE [TestDB] SET PAGE_VERIFY CHECKSUM +GO +ALTER DATABASE [TestDB] SET DB_CHAINING OFF +GO +ALTER DATABASE [TestDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) +GO +ALTER DATABASE [TestDB] SET TARGET_RECOVERY_TIME = 0 SECONDS +GO +ALTER DATABASE [TestDB] SET DELAYED_DURABILITY = DISABLED +GO +EXEC sys.sp_db_vardecimal_storage_format N'TestDB', N'ON' +GO +USE [TestDB] +GO +/****** Object: Table [dbo].[ClassInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[ClassInfo]( + [ClassId] [int] IDENTITY(1,1) NOT NULL, + [ClassName] [nvarchar](20) NOT NULL, +PRIMARY KEY CLUSTERED +( + [ClassId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[CourseInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[CourseInfo]( + [CourseId] [int] IDENTITY(1,1) NOT NULL, + [CourseName] [nvarchar](50) NOT NULL, + [CourseCredit] [int] NULL DEFAULT ((1)), +PRIMARY KEY CLUSTERED +( + [CourseId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[Scores] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[Scores]( + [ScoreId] [int] IDENTITY(1,1) NOT NULL, + [StuId] [int] NULL, + [CourseId] [int] NULL, + [Score] [int] NULL DEFAULT ((0)), +PRIMARY KEY CLUSTERED +( + [ScoreId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[StuInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[StuInfo]( + [StuId] [int] IDENTITY(1,1) NOT NULL, + [ClassId] [int] NULL, + [StuName] [nvarchar](10) NOT NULL, + [StuSex] [nvarchar](1) NULL DEFAULT ('男'), + [StuBrithday] [date] NULL, + [StuPhone] [nvarchar](11) NULL, + [StuProvince] [nvarchar](200) NULL, + [CreateDate] [datetime] NULL DEFAULT (getdate()), + [StuAge] [int] NULL, +PRIMARY KEY CLUSTERED +( + [StuId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] ON + +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (1, N'软件1班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (2, N'软件2班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (3, N'软件3班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (4, N'软件4班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (5, N'软件5班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (6, N'软件6班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (7, N'软件7班') +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] ON + +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (1, N'计算机基础', 3) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (2, N'HTML+CSS网页制作', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (3, N'JAVA编程基础', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (4, N'SQL Server数据库基础', 4) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (5, N'C#面向对象编程', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (6, N'Winform桌面应用程序设计', 5) +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[Scores] ON + +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (1, 1, 1, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (2, 1, 2, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (3, 1, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (4, 1, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (5, 2, 1, 60) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (6, 2, 2, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (7, 2, 3, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (8, 2, 4, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (9, 3, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (10, 3, 2, 45) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (11, 3, 3, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (12, 3, 4, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (13, 4, 1, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (14, 4, 2, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (15, 4, 3, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (16, 4, 4, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (17, 5, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (18, 5, 2, 79) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (19, 5, 3, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (20, 5, 4, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (21, 6, 1, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (22, 6, 2, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (23, 6, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (24, 6, 5, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (25, 7, 1, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (26, 7, 2, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (27, 7, 3, 92) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (28, 7, 5, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (29, 8, 1, 58) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (30, 8, 2, 59) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (31, 8, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (32, 8, 5, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (33, 9, 1, 48) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (34, 9, 2, 67) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (35, 9, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (36, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (37, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (38, 1, 1, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (39, 1, 2, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (40, 1, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (41, 1, 4, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (42, 2, 1, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (43, 2, 2, 82) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (44, 2, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (45, 2, 4, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (46, 3, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (47, 3, 2, 50) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (48, 3, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (49, 3, 4, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (50, 4, 1, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (51, 4, 2, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (52, 4, 3, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (53, 4, 4, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (54, 5, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (55, 5, 2, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (56, 5, 3, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (57, 5, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (58, 6, 1, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (59, 6, 2, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (60, 6, 3, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (61, 6, 5, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (62, 7, 1, 89) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (63, 7, 2, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (64, 7, 3, 97) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (65, 7, 5, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (66, 8, 1, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (67, 8, 2, 64) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (68, 8, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (69, 8, 5, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (70, 9, 1, 53) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (71, 9, 2, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (72, 9, 3, 76) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (73, 9, 5, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (74, 9, 5, 61) +GO +SET IDENTITY_INSERT [dbo].[Scores] OFF +GO +SET IDENTITY_INSERT [dbo].[StuInfo] ON + +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (1, 1, N'刘正', N'男', CAST(N'2002-08-02' AS Date), N'13245678121', N'广西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (2, 1, N'黄贵', N'男', CAST(N'2003-07-02' AS Date), N'13345678121', N'江西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (3, 1, N'陈美', N'女', CAST(N'2002-07-22' AS Date), N'13355678125', N'福建省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (4, 2, N'江文', N'男', CAST(N'2001-07-02' AS Date), N'13347678181', N'湖南省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (5, 2, N'钟琪', N'女', CAST(N'2004-01-13' AS Date), N'13345778129', N'安徽省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (6, 3, N'曾小林', N'男', CAST(N'2005-05-15' AS Date), N'13345378563', N'安徽省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (7, 3, N'欧阳天天', N'女', CAST(N'2000-08-19' AS Date), N'13347878121', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (8, 3, N'李逍遥', N'男', CAST(N'1999-09-02' AS Date), N'13345678557', N'广东省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 22) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (9, 4, N'刘德华', N'男', CAST(N'1995-06-11' AS Date), N'15345679557', N'福建省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 26) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (10, 4, N'刘翔', N'男', CAST(N'1996-07-09' AS Date), N'18346679589', N'江西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (11, 4, N'曾小贤', N'男', CAST(N'2003-07-02' AS Date), N'18348979589', N'湖南省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (12, 5, N'刘德华', N'男', CAST(N'2002-07-02' AS Date), N'18348979509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (13, 5, N'陈天翔', N'男', CAST(N'2003-07-02' AS Date), N'18348079509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (14, 5, N'刘能', N'男', CAST(N'2005-08-02' AS Date), N'13245678122', N'广西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (15, 5, N'钟馗', N'男', CAST(N'2004-08-02' AS Date), N'13245678123', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (16, 5, N'钟吴艳', N'女', CAST(N'2002-08-02' AS Date), N'13245678124', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (17, 5, N'刘欢', N'男', CAST(N'2001-07-02' AS Date), N'13245678125', N'湖南省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (18, 5, N'张庭', N'女', CAST(N'2000-07-02' AS Date), N'13245678126', N'江西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (19, 5, N'曹植', N'男', CAST(N'2000-08-02' AS Date), N'13245678127', N'福建省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (20, 5, N'曹操', N'男', CAST(N'2002-08-02' AS Date), N'13245678128', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (21, 5, N'孙尚香', N'女', CAST(N'2003-08-02' AS Date), N'13245678129', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (22, 3, N'老1', N'女', CAST(N'2002-08-02' AS Date), N'13245678130', N'广东省', CAST(N'2021-03-14 17:02:36.347' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (24, 2, N'老2', N'男', CAST(N'2002-08-03' AS Date), N'13345678945', NULL, CAST(N'2021-03-14 17:03:37.733' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (25, 4, N'老3', N'男', NULL, N'13645987545', N'广东省', CAST(N'2021-03-14 17:03:43.307' AS DateTime), NULL) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (28, 5, N'老4', N'男', CAST(N'2006-03-05' AS Date), N'13456987456', NULL, CAST(N'2021-03-14 17:04:03.957' AS DateTime), 15) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (29, 5, N'老5', N'女', CAST(N'1998-04-12' AS Date), N'15978456123', NULL, CAST(N'2021-03-14 17:04:47.103' AS DateTime), 23) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (30, 4, N'老6', N'男', CAST(N'1996-08-06' AS Date), N'18945674561', NULL, CAST(N'2021-03-14 17:05:04.990' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (31, 3, N'老7', N'女', CAST(N'1997-04-06' AS Date), N'18845678912', NULL, CAST(N'2021-03-14 17:05:20.570' AS DateTime), 24) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (32, 2, N'老10', N'女', CAST(N'1998-08-09' AS Date), N'19945645612', NULL, CAST(N'2021-03-14 17:06:08.107' AS DateTime), 23) +GO +SET IDENTITY_INSERT [dbo].[StuInfo] OFF +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__CourseIn__9526E2773AB7BECE] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[CourseInfo] ADD UNIQUE NONCLUSTERED +( + [CourseName] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__StuInfo__2D85FC63AF6FC6FA] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[StuInfo] ADD UNIQUE NONCLUSTERED +( + [StuPhone] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([CourseId]) +REFERENCES [dbo].[CourseInfo] ([CourseId]) +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([StuId]) +REFERENCES [dbo].[StuInfo] ([StuId]) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD FOREIGN KEY([ClassId]) +REFERENCES [dbo].[ClassInfo] ([ClassId]) +ON DELETE SET NULL +GO +ALTER TABLE [dbo].[CourseInfo] WITH CHECK ADD CHECK (([CourseCredit]>=(1) AND [CourseCredit]<=(5))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK ((len([StuPhone])=(11))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK (([StuSex]='女' OR [StuSex]='男')) +GO +USE [master] +GO +ALTER DATABASE [TestDB] SET READ_WRITE +GO + +select * from ClassInfo +select * from CourseInfo +select * from Scores +select * from StuInfo + +--统计每个班的男生数 +select ClassId 班级,COUNT(StuSex)男生人数 from StuInfo where StuSex='男' group by ClassId +--统计每个班的男、女生数 +select ClassId 班级, StuSex 性别,COUNT(StuSex)人数 from StuInfo group by ClassId , StuSex order by ClassId +--统计每个班的福建人数 +select ClassId 班级, COUNT(StuProvince)福建省人数 from StuInfo group by ClassId +--统计每个班的各个省的总人数 +select ClassId 班级,StuProvince 省份,COUNT(*)总人数 from StuInfo group by ClassId , StuProvince order by ClassId +--统计每个省的女生数 +select StuProvince 省份,COUNT(StuSex)女生人数 from StuInfo where StuSex='女' group by StuProvince +--统计每个省的男、女生数 +select StuProvince 省份, StuSex 性别,COUNT(*)人数 from StuInfo group by StuProvince,StuSex order by StuProvince +--统计每个学生的考试总分、平均分 +select StuId 学生ID , SUM(score)总分,AVG(score)平均分 from Scores group by StuId +--统计出考试总分大于620的学生的考试总分 +select StuId 学生ID,SUM(score)总分 from Scores group by StuId HAVING SUM(score)>620 +--统计出每门考试成绩最高分和最低分 +select CourseID 科目,max(score)最高分,min(score)最低分 from Scores group by CourseID +--统计出每个学生的各门成绩的平均分 +select StuId 学生ID,CourseId 科目 , Avg(score)平均分 from Scores group by StuId,CourseId order by StuId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\351\207\221\346\265\267/SQLQuery6.sql03.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\351\207\221\346\265\267/SQLQuery6.sql03.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ecbe1ed59d3689138b3072ae5d64abc766b18769 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\351\207\221\346\265\267/SQLQuery6.sql03.sql" @@ -0,0 +1,98 @@ +use master +go +create database bbs +on +( + name='bbs', + filename='D:\bbs.mdf', + size=10, + maxsize=200, + filegrowth=20 +) +log on +( + name='bbs_log', + filename='D:\bbs_log.ldf ', + size=10, + maxsize=200, + filegrowth=20 +) +use bbs + create table bbsUsers + ( + UIDD int identity(1,1) not null , + uName nvarchar(10) not null, + uSex varchar(2) not null , + uAge int not null, + uPoint int not null + ) + alter table bbsUsers add constraint PK_bbsUsers_UIDD primary key(UIDD ) +alter table bbsUsers add constraint UK_bbsUsers_uName unique(uName) +alter table bbsUsers add constraint CK_bbsUsers_uSex check(uSex in('女','男')) +alter table bbsUsers add constraint CK_bbsUsers_uAge check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint CK_bbsUsers_uPoint check(uPoint>=0) + create table bbsTopic + ( + tID int primary key identity(1,1), + tUID int references bbsUsers(UIDD), + tSID int references bbsSection(sIDD), + tTitle nvarchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int + ) + create table bbsReply + ( + rID int primary key identity(1,1), + rUID int references bbsUsers(UIDD) , + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime, + ) + create table bbsSection + ( + sIDD int identity(1,1), + sName nvarchar(10), + sUid int , + ) + alter table bbsSection add constraint PK_bbsSection_sIDD primary key(sIDD) + alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUsers(UIDD) + + insert into bbsUsers(uName,uSex,uAge,uPoint) values ('小雨点','女','20','0'),('逍遥 ','男','18','4'),('七年级生','男','19','2') + + create table bbsPoint + ( + ID nvarchar(10) , + point int + ) + + -- + insert into bbsPoint(ID,point) select uName,uPoint from bbsUsers + + + -- + select uName,uPoint into bbsUsers2 from bbsUsers + + + insert into bbsSection(sName,sUid) values ('技术交流 ','1'),(' 读书世界 ','3'),('生活百科','1'),('八卦区 ','3') + insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values ('2','4','范跑跑 ','谁是范跑跑','2008-7-8 ','1'), + ('3','1','.NET','与JAVA的区别是什么呀?','2008-9-1','2'),('1','3','今年夏天最流行什么','有谁知道今年夏天最流行什么呀?',' 2008-9-10','0') + + insert into bbsReply(rUID,rMsg, rTime) values ('1','你是','2020.1.2'),('3','爱狗','2020.2.15'),('2','还是能','2020.5.25') + select * from bbsSection + select * from bbsUsers + select * from bbsTopic + select * from bbsReply + +--1.查询出每个版块的版块编号,版主姓名和版块名称 +select uName 版主姓名,sIDD 版块编号,sName 版块名称 from bbsSection C inner join bbsUsers B on C.sUid=B.UIDD +--2.查询出主贴的发帖时间在2008-8-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID 发帖人编号, uName 发帖人姓名,tTitle 帖子的标题 ,tMsg 帖子的内容 ,tTime 发帖时间 from bbsTopic BT inner join bbsUsers BU on BT.tUID =BU.UIDD where tTime>'2008-8-15' +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select uName 版主姓名,sUid 版主编号,sName 版块名称 from bbsSection C inner join bbsUsers B on C.sUid=B.UIDD where uAge >18 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select tUID 发帖人编号,uName 发帖人姓名,tTitle 主贴标题 ,tMsg 主贴内容,tCount 回复数量 from bbsTopic C inner join bbsUsers B on C.tUID =B.UIDD where tCount= (select max(tCount)from bbsTopic) +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select sName 板块名称,uName 用户名称 ,sum(tCount) 发帖总数 from bbsTopic inner join bbsSection on sIDD=tSID +inner join bbsUsers on tUID =UIDD group by sName ,uName + diff --git "a/20210326\344\275\234\344\270\232/\345\217\266\347\234\237/SQLQuery1.sql01.sql" "b/20210326\344\275\234\344\270\232/\345\217\266\347\234\237/SQLQuery1.sql01.sql" new file mode 100644 index 0000000000000000000000000000000000000000..06eb097a057e872f511577acde151335d8fc4a0b --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\217\266\347\234\237/SQLQuery1.sql01.sql" @@ -0,0 +1,78 @@ +create database Student02 +on +( + name='Student02', + filename='D:\Student02.mdf', + size=20, + maxsize=300, + filegrowth=50 +) +log on +( + name='Student02_log', + filename='D:\Student02_log.ldf', + size=20, + maxsize=300, + filegrowth=50 +) +go +use Student02 +go +create table StudenIfo +( + stuNo int identity(2501,1) primary key, + stuName nvarchar(20) not null unique, + stuAge int null , + studdRess nvarchar(20) not null, + stuSeat int , + stuSext int check(stuSext in('1','0')) default('0') +) +insert into StudenIfo(stuName,stuAge,studdRess,stuSeat,stuSext ) values('张秋利','20','美国硅谷','1','1'),('李斯文','18','湖北武汉','2','0'),('马文才','22','湖南长沙','3','1'), +('欧阳俊雄','21','湖北武汉','4','0'),('梅超凤','20','湖北武汉','5','1'),('陈炫风','20','美国硅谷','6','1'),('陈风','20','美国硅谷','7','0') + +create table Class01 +( + examNo int identity(1,1) primary key, + stuNo char(20) null, + writtenExam int , + labExam int +) +insert into Class01(stuNo,writtenExam,labExam) values ('2501','50','70'),('2502','60','65'),('2503','86','85'),('2503','40','80'),('2504','70','90'),('2505','85','90') + +select * from StudenIfo +select * from Class01 +--数据如图片1,使用上次作业的数据 + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 + +select stuName 姓名, stuAge 年龄, writtenExam 笔试成绩,labExam 机试成绩 from StudenIfo si +inner join Class01 cl on si.stuNo = cl.stuNo + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 + +select si.stuNo,stuName ,writtenExam ,labExam from StudenIfo si + inner join Class01 cl on si.stuNo = cl.stuNo + where writtenExam > 60 and labExam > 60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 + +select si.stuNo ,stuName ,writtenExam ,labExam from StudenIfo si + LEFT join Class01 cl on si.stuNo = cl.stuNo + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 + +select stuName,stuAge,writtenExam,labExam from StudenIfo si + inner join Class01 cl on si.stuNo = cl.stuNo + where stuAge >= 20 order by writtenExam DESC + +--5.查询男女生的机试平均分 + +select stuSext ,AVG(labExam) from StudenIfo si + inner join Class01 cl on si.stuNo = cl.stuNo + group by stuSext + +--6.查询男女生的笔试总分 + +select stuSext,SUM(writtenExam) from StudenIfo si + inner join Class01 cl on si.stuNo = cl.stuNo + group by stuSext \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\217\266\347\234\237/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\345\217\266\347\234\237/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b11968d0d53023715b2581e1200d70a4ad223089 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\217\266\347\234\237/SQLQuery2.sql" @@ -0,0 +1,166 @@ + +use master +go + +create database bbs +on +( + name='bbs', + filename='D:\bbs.mdf' +) +log on +( + name='bbs_log', + filename='D:\bbs_log.ldf' +) + + +use bbs +go + +create table bbsUsers +( + UID int identity(1,1), + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) + + +create table bbsSection +( + sID int identity(1,1) , + sName varchar(10) not null , + sUid int +) + +create table bbsTopic +( + tId int identity(1,1), + tUID int, + tSID int, + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) + + +create table bbsReply +( + rId int identity(1,1), + rUId int, + rTID int, + rMsg text not null, + rTime datetime +) + + +alter table bbsUsers add constraint PK_bbsUsers_UID primary key (UID) +alter table bbsUsers add constraint UQ_bbsUsers_uName unique (uName) +alter table bbsUsers add constraint CK_bbsUsers_uSex check(uSex='男' or uSex='女') +alter table bbsUsers add constraint CK_bbsUsers_uAge check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint CK_bbsUsers_uPoint check(uPoint>=0) + +alter table bbsSection add constraint PK_bbsSection_sID primary key (sID) +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUID) references bbsUsers(UID) + +alter table bbsTopic add constraint PK_bbsTopic_tID primary key (tID) +alter table bbsTopic add constraint FK_bbsTopic_tUID foreign key(tUID) references bbsUsers(UID) +alter table bbsTopic add constraint FK_bbsTopic_tSID foreign key(tSID) references bbsSection(sID) + +alter table bbsReply add constraint PK_bbsReply_rID primary key (rID) +alter table bbsReply add constraint FK_bbsReply_rUID foreign key(rUID) references bbsUsers(UID) +alter table bbsReply add constraint Fk_bbsReply_rTID foreign key(rTID) references bbsTopic(tID) + +-- 1.现在有3个会员注册成功,请用一次插入多行数据的方法向bbsUsers表种插入3行记录,记录值如下: +-- 小雨点 女 20 0 +-- 逍遥 男 18 4 +-- 七年级生 男 19 2 + +insert into bbsUsers(uName,uSex,uAge,uPoint) +select'小雨点','女','20','0'union +select'逍遥','男','18','4'union +select'七年级生','男','19','2' + +-- 2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中,提示查询部分列:select 列名1,列名2 from 表名 + +select uName,uPoint into bbsPoint from bbsUsers + + + +-- 3.给论坛开设4个板块 +-- 名称 版主名 +-- 技术交流 小雨点 +-- 读书世界 七年级生 +-- 生活百科 小雨点 +-- 八卦区 七年级生 + +insert into bbsSection(sName,sUid) +select'技术交流','3'union +select'读书世界','1'union +select'生活百科','3'union +select'八卦区','1' + + +-- 4.向主贴和回帖表中添加几条记录 + +-- 主贴: + +-- 发帖人 板块名 帖子标题 帖子内容 发帖时间 回复数量 +-- 逍遥 八卦区 范跑跑 谁是范跑跑 2008-7-8 1 +-- 七年级生 技术交流 .NET 与JAVA的区别是什么呀? 2008-9-1 2 +-- 小雨点 生活百科 今年夏天最流行什么 有谁知道今年夏天最流行 2008-9-10 0 +-- 什么呀? + +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) +select '2','1','范跑跑','谁是范跑跑','2008-7-8','1'union +select '1','3','.NET','与Java的区别是什么呀?','2008-9-1','2'union +select '3','4','今年夏天最流行什么呀?','有谁知道今年夏天最流行什么呀?','2008-9-10','0' + + +-- 回帖: +-- 分别给上面三个主贴添加对应的回帖,回帖的内容,时间,回帖人自定 + +insert into bbsReply(rTID,rMsg,rTime,rUId) +select'1','不知道','2008-9-5','2'union +select'2','不认识','2008-9-10','1'union +select'3','不知道','2008-10-1','3' + + + select * from bbsTopic + select * from bbsReply + select * from bbsUsers + select * from bbsSection +-- 在论坛数据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 + +select sUid 版主编号,uName 版主名称,sName 板块名称 from bbsUsers bu + inner join bbsSection bs on bu.UID = bs.sUid + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 + +select tUID 发帖人编号,uName 发帖人姓名,tTitle 帖子标题,tMsg 帖子内容,tTime 发帖时间 from bbsUsers bu + inner join bbsTopic bt on bu.UID = bt.tUID + where tTime > '2008-09-15' + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 + +select sID 版主编号,uName 版主名称,sName 板块名称 from bbsUsers bu + inner join bbsSection bs on bu.UID = bs.sUid + where uAge < 20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 + +select tUID 发帖人编号,uName 发帖人姓名,tTitle 帖子标题,tMsg 帖子内容,tCount 回复数量 from bbsUsers bu + inner join bbsTopic bt on bu.UID = bt.tUID + where tCount = (select MAX(tCount) from bbsTopic) + +--5.在主贴表中查询每个版块中每个用户的发帖总数 + +select sName 板块名称,uName 发帖人名称,COUNT(UID) 发帖总数 from bbsTopic bt + RIGHT join bbsSection bs on bt.tSID = bs.sID + inner join bbsUsers bu on bt.tUID = bu.UID + group by sID ,UID,uName,sName + diff --git "a/20210326\344\275\234\344\270\232/\345\217\266\347\234\237/SQLQuery3.sql02.sql" "b/20210326\344\275\234\344\270\232/\345\217\266\347\234\237/SQLQuery3.sql02.sql" new file mode 100644 index 0000000000000000000000000000000000000000..72e853f09fee7151e3afc037b2d9c79c4a89df6f --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\217\266\347\234\237/SQLQuery3.sql02.sql" @@ -0,0 +1,88 @@ +use master +go +create database orderr +on +( + name='orderr', + filename= 'D:\orderr.mdf', + size=10, + maxsize=100, + filegrowth=15% +) +log on +( + name='orderr_log', + filename= 'D:\orderr_log.ldf', + size=10, + maxsize=100, + filegrowth=15% +) +go +use orderr +go +create table orders +( + orderId int primary key identity, + orderDate datetime default(getdate()) +) +create table orderItem +( + ItemiD int primary key identity, + orderId int references orders(orderId) , + itemType nvarchar(10), + itemName nvarchar(10), + theNumber int , + theMoney money +) +insert into orders values(default),(default),(default),(default) +select * from orders +insert into orderItem values ('1','文具','笔','72','2'),('1','文具','尺','10','1'),('1','体育用品','蓝球','1','56'),('2','文具','笔','36','2'),('2','文具','固体胶','20','3'), +('2','日常用品','透明胶','2','1'),('2','体育用品','羽毛球','20','3'),('3','文具','订书机','20','3'),('3','文具','订书针','10','3'),('3','文具','裁纸刀','5','5'), +('4','文具','笔','20','2'),('4','文具','信纸','50','1'),('4','日常用品','毛巾','4','5'),('4','日常用品','透明胶','30','1'),('4','体育用品','羽毛球','20','3') + + +select * from orderItem +select * from orders +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 + +select OI.orderId 订单编号 , orderDate 订单日期 , itemType 产品类型 , itemName 产品名称 ,theNumber 订购数量 , theMoney 订单单价 from orderItem OI + inner join orders O on OI.orderId =O.orderId + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 + +select OI.orderId 订单编号 , orderDate 订单日期 , itemType 产品类型 , itemName 产品名称 from orderItem OI + inner join orders O on OI.orderId =O.orderId where theNumber>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +select OI.orderId 订单编号 , orderDate 订单日期 , itemType 产品类型 , itemName 产品名称 ,theNumber 订购数量 , theMoney 订单单价 from orderItem OI + inner join orders O on OI.orderId =O.orderId + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +select oi.orderId 订单编号,orderDate 订单时期,itemType 产品类型,itemName 产品名称,theNumber 订购数量,theMoney 订购单价 from orderItem oi + inner join orders os on oi.orderId = os.orderId + where theMoney >= 5 and theNumber >=50 + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 + +select oi.orderId 订单编号,COUNT(ItemiD) 订购产品数 from orderItem oi + inner join orders os on oi.orderId = os.orderId + group by oi.orderId + +--6.查询 每个订单 里的 每个类别 的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + +select oi.orderId 订单编号,itemType 产品类别,COUNT(itemName) 订购次数,SUM(theNumber) 总数量 from orderItem oi + inner join orders os on oi.orderId = os.orderId + group by oi.itemType,oi.orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\217\266\351\231\210\350\276\211/SQLQuery1.sql01.sql" "b/20210326\344\275\234\344\270\232/\345\217\266\351\231\210\350\276\211/SQLQuery1.sql01.sql" new file mode 100644 index 0000000000000000000000000000000000000000..06eb097a057e872f511577acde151335d8fc4a0b --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\217\266\351\231\210\350\276\211/SQLQuery1.sql01.sql" @@ -0,0 +1,78 @@ +create database Student02 +on +( + name='Student02', + filename='D:\Student02.mdf', + size=20, + maxsize=300, + filegrowth=50 +) +log on +( + name='Student02_log', + filename='D:\Student02_log.ldf', + size=20, + maxsize=300, + filegrowth=50 +) +go +use Student02 +go +create table StudenIfo +( + stuNo int identity(2501,1) primary key, + stuName nvarchar(20) not null unique, + stuAge int null , + studdRess nvarchar(20) not null, + stuSeat int , + stuSext int check(stuSext in('1','0')) default('0') +) +insert into StudenIfo(stuName,stuAge,studdRess,stuSeat,stuSext ) values('张秋利','20','美国硅谷','1','1'),('李斯文','18','湖北武汉','2','0'),('马文才','22','湖南长沙','3','1'), +('欧阳俊雄','21','湖北武汉','4','0'),('梅超凤','20','湖北武汉','5','1'),('陈炫风','20','美国硅谷','6','1'),('陈风','20','美国硅谷','7','0') + +create table Class01 +( + examNo int identity(1,1) primary key, + stuNo char(20) null, + writtenExam int , + labExam int +) +insert into Class01(stuNo,writtenExam,labExam) values ('2501','50','70'),('2502','60','65'),('2503','86','85'),('2503','40','80'),('2504','70','90'),('2505','85','90') + +select * from StudenIfo +select * from Class01 +--数据如图片1,使用上次作业的数据 + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 + +select stuName 姓名, stuAge 年龄, writtenExam 笔试成绩,labExam 机试成绩 from StudenIfo si +inner join Class01 cl on si.stuNo = cl.stuNo + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 + +select si.stuNo,stuName ,writtenExam ,labExam from StudenIfo si + inner join Class01 cl on si.stuNo = cl.stuNo + where writtenExam > 60 and labExam > 60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 + +select si.stuNo ,stuName ,writtenExam ,labExam from StudenIfo si + LEFT join Class01 cl on si.stuNo = cl.stuNo + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 + +select stuName,stuAge,writtenExam,labExam from StudenIfo si + inner join Class01 cl on si.stuNo = cl.stuNo + where stuAge >= 20 order by writtenExam DESC + +--5.查询男女生的机试平均分 + +select stuSext ,AVG(labExam) from StudenIfo si + inner join Class01 cl on si.stuNo = cl.stuNo + group by stuSext + +--6.查询男女生的笔试总分 + +select stuSext,SUM(writtenExam) from StudenIfo si + inner join Class01 cl on si.stuNo = cl.stuNo + group by stuSext \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\217\266\351\231\210\350\276\211/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\345\217\266\351\231\210\350\276\211/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b11968d0d53023715b2581e1200d70a4ad223089 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\217\266\351\231\210\350\276\211/SQLQuery2.sql" @@ -0,0 +1,166 @@ + +use master +go + +create database bbs +on +( + name='bbs', + filename='D:\bbs.mdf' +) +log on +( + name='bbs_log', + filename='D:\bbs_log.ldf' +) + + +use bbs +go + +create table bbsUsers +( + UID int identity(1,1), + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) + + +create table bbsSection +( + sID int identity(1,1) , + sName varchar(10) not null , + sUid int +) + +create table bbsTopic +( + tId int identity(1,1), + tUID int, + tSID int, + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) + + +create table bbsReply +( + rId int identity(1,1), + rUId int, + rTID int, + rMsg text not null, + rTime datetime +) + + +alter table bbsUsers add constraint PK_bbsUsers_UID primary key (UID) +alter table bbsUsers add constraint UQ_bbsUsers_uName unique (uName) +alter table bbsUsers add constraint CK_bbsUsers_uSex check(uSex='男' or uSex='女') +alter table bbsUsers add constraint CK_bbsUsers_uAge check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint CK_bbsUsers_uPoint check(uPoint>=0) + +alter table bbsSection add constraint PK_bbsSection_sID primary key (sID) +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUID) references bbsUsers(UID) + +alter table bbsTopic add constraint PK_bbsTopic_tID primary key (tID) +alter table bbsTopic add constraint FK_bbsTopic_tUID foreign key(tUID) references bbsUsers(UID) +alter table bbsTopic add constraint FK_bbsTopic_tSID foreign key(tSID) references bbsSection(sID) + +alter table bbsReply add constraint PK_bbsReply_rID primary key (rID) +alter table bbsReply add constraint FK_bbsReply_rUID foreign key(rUID) references bbsUsers(UID) +alter table bbsReply add constraint Fk_bbsReply_rTID foreign key(rTID) references bbsTopic(tID) + +-- 1.现在有3个会员注册成功,请用一次插入多行数据的方法向bbsUsers表种插入3行记录,记录值如下: +-- 小雨点 女 20 0 +-- 逍遥 男 18 4 +-- 七年级生 男 19 2 + +insert into bbsUsers(uName,uSex,uAge,uPoint) +select'小雨点','女','20','0'union +select'逍遥','男','18','4'union +select'七年级生','男','19','2' + +-- 2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中,提示查询部分列:select 列名1,列名2 from 表名 + +select uName,uPoint into bbsPoint from bbsUsers + + + +-- 3.给论坛开设4个板块 +-- 名称 版主名 +-- 技术交流 小雨点 +-- 读书世界 七年级生 +-- 生活百科 小雨点 +-- 八卦区 七年级生 + +insert into bbsSection(sName,sUid) +select'技术交流','3'union +select'读书世界','1'union +select'生活百科','3'union +select'八卦区','1' + + +-- 4.向主贴和回帖表中添加几条记录 + +-- 主贴: + +-- 发帖人 板块名 帖子标题 帖子内容 发帖时间 回复数量 +-- 逍遥 八卦区 范跑跑 谁是范跑跑 2008-7-8 1 +-- 七年级生 技术交流 .NET 与JAVA的区别是什么呀? 2008-9-1 2 +-- 小雨点 生活百科 今年夏天最流行什么 有谁知道今年夏天最流行 2008-9-10 0 +-- 什么呀? + +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) +select '2','1','范跑跑','谁是范跑跑','2008-7-8','1'union +select '1','3','.NET','与Java的区别是什么呀?','2008-9-1','2'union +select '3','4','今年夏天最流行什么呀?','有谁知道今年夏天最流行什么呀?','2008-9-10','0' + + +-- 回帖: +-- 分别给上面三个主贴添加对应的回帖,回帖的内容,时间,回帖人自定 + +insert into bbsReply(rTID,rMsg,rTime,rUId) +select'1','不知道','2008-9-5','2'union +select'2','不认识','2008-9-10','1'union +select'3','不知道','2008-10-1','3' + + + select * from bbsTopic + select * from bbsReply + select * from bbsUsers + select * from bbsSection +-- 在论坛数据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 + +select sUid 版主编号,uName 版主名称,sName 板块名称 from bbsUsers bu + inner join bbsSection bs on bu.UID = bs.sUid + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 + +select tUID 发帖人编号,uName 发帖人姓名,tTitle 帖子标题,tMsg 帖子内容,tTime 发帖时间 from bbsUsers bu + inner join bbsTopic bt on bu.UID = bt.tUID + where tTime > '2008-09-15' + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 + +select sID 版主编号,uName 版主名称,sName 板块名称 from bbsUsers bu + inner join bbsSection bs on bu.UID = bs.sUid + where uAge < 20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 + +select tUID 发帖人编号,uName 发帖人姓名,tTitle 帖子标题,tMsg 帖子内容,tCount 回复数量 from bbsUsers bu + inner join bbsTopic bt on bu.UID = bt.tUID + where tCount = (select MAX(tCount) from bbsTopic) + +--5.在主贴表中查询每个版块中每个用户的发帖总数 + +select sName 板块名称,uName 发帖人名称,COUNT(UID) 发帖总数 from bbsTopic bt + RIGHT join bbsSection bs on bt.tSID = bs.sID + inner join bbsUsers bu on bt.tUID = bu.UID + group by sID ,UID,uName,sName + diff --git "a/20210326\344\275\234\344\270\232/\345\217\266\351\231\210\350\276\211/SQLQuery3.sql02.sql" "b/20210326\344\275\234\344\270\232/\345\217\266\351\231\210\350\276\211/SQLQuery3.sql02.sql" new file mode 100644 index 0000000000000000000000000000000000000000..72e853f09fee7151e3afc037b2d9c79c4a89df6f --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\217\266\351\231\210\350\276\211/SQLQuery3.sql02.sql" @@ -0,0 +1,88 @@ +use master +go +create database orderr +on +( + name='orderr', + filename= 'D:\orderr.mdf', + size=10, + maxsize=100, + filegrowth=15% +) +log on +( + name='orderr_log', + filename= 'D:\orderr_log.ldf', + size=10, + maxsize=100, + filegrowth=15% +) +go +use orderr +go +create table orders +( + orderId int primary key identity, + orderDate datetime default(getdate()) +) +create table orderItem +( + ItemiD int primary key identity, + orderId int references orders(orderId) , + itemType nvarchar(10), + itemName nvarchar(10), + theNumber int , + theMoney money +) +insert into orders values(default),(default),(default),(default) +select * from orders +insert into orderItem values ('1','文具','笔','72','2'),('1','文具','尺','10','1'),('1','体育用品','蓝球','1','56'),('2','文具','笔','36','2'),('2','文具','固体胶','20','3'), +('2','日常用品','透明胶','2','1'),('2','体育用品','羽毛球','20','3'),('3','文具','订书机','20','3'),('3','文具','订书针','10','3'),('3','文具','裁纸刀','5','5'), +('4','文具','笔','20','2'),('4','文具','信纸','50','1'),('4','日常用品','毛巾','4','5'),('4','日常用品','透明胶','30','1'),('4','体育用品','羽毛球','20','3') + + +select * from orderItem +select * from orders +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 + +select OI.orderId 订单编号 , orderDate 订单日期 , itemType 产品类型 , itemName 产品名称 ,theNumber 订购数量 , theMoney 订单单价 from orderItem OI + inner join orders O on OI.orderId =O.orderId + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 + +select OI.orderId 订单编号 , orderDate 订单日期 , itemType 产品类型 , itemName 产品名称 from orderItem OI + inner join orders O on OI.orderId =O.orderId where theNumber>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +select OI.orderId 订单编号 , orderDate 订单日期 , itemType 产品类型 , itemName 产品名称 ,theNumber 订购数量 , theMoney 订单单价 from orderItem OI + inner join orders O on OI.orderId =O.orderId + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +select oi.orderId 订单编号,orderDate 订单时期,itemType 产品类型,itemName 产品名称,theNumber 订购数量,theMoney 订购单价 from orderItem oi + inner join orders os on oi.orderId = os.orderId + where theMoney >= 5 and theNumber >=50 + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 + +select oi.orderId 订单编号,COUNT(ItemiD) 订购产品数 from orderItem oi + inner join orders os on oi.orderId = os.orderId + group by oi.orderId + +--6.查询 每个订单 里的 每个类别 的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + +select oi.orderId 订单编号,itemType 产品类别,COUNT(itemName) 订购次数,SUM(theNumber) 总数量 from orderItem oi + inner join orders os on oi.orderId = os.orderId + group by oi.itemType,oi.orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\217\266\351\242\200/ABC.sql" "b/20210326\344\275\234\344\270\232/\345\217\266\351\242\200/ABC.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5406a2a3b26b08b70f5d0048ed4892152fb5d0a9 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\217\266\351\242\200/ABC.sql" @@ -0,0 +1,112 @@ +use master +go + +create database ABC +on +( + name='ABC', + filename='D:\test\ABC.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='ABC_log', + filename='D:\test\ABC_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +go +use ABC +go + +create table orders +( + orderId int primary key identity , + orderDate datetime +) +go + +create table orderItem +( + ItemiD int primary key identity , + orderId int references orders(orderId), + itemType varchar(10), + itemName varchar(10), + theNumber int , + theMoney int +) +go + +insert orders values +('2008-01-12'),('2008-02-10'), +('2008-02-15'),('2008-03-10') +go + +insert orderItem values +('1','文具','笔',72,2), +('1','文具','尺',10,1), +('1','体育用品','篮球',1,56), +('2','文具','笔',36,2), +('2','文具','固体胶',20,3), +('2','日常用品','透明胶',2,1), +('2','体育用品','羽毛球',20,3), +('3','文具','订书机',20,3), +('3','文具','订书针',10,3), +('3','文具','裁纸刀',5,5), +('4','文具','笔',20,2), +('4','文具','信纸',50,1), +('4','日常用品','毛巾',4,5), +('4','日常用品','透明胶',30,1), +('4','体育用品','羽毛球',20,3) +go + +select SUM(theNumber)as 总数 from dbo.orderItem +go + +select SUM(theNumber) as 数量 ,AVG(theMoney) as 平均单价 from dbo.orderItem where orderId<3 group by orderId having AVG(theMoney)<10 +go + +select SUM(theNumber) as 数量,AVG(theMoney) as 平均单价 from dbo.orderItem where theNumber<50 group by orderId having AVG(theMoney)<10 +go + +select '文具:' as 类型,COUNT(itemType) 数量 from orderItem where itemType='文具' union +select '体育用品:' as 类型,COUNT(itemType) 数量 from orderItem where itemType='体育用品' union +select '日常用品:' as 类型, COUNT(itemType) 数量 from orderItem where itemType='日常用品' +go + +select itemType,SUM(theNumber) as 数量,AVG(theMoney) as 平均单价 from dbo.orderItem where theNumber>100 group by itemType +go + +select itemName as 产品名称,COUNT(itemName) as 订购次数,SUM(theNumber) as 总数量,AVG(theMoney) as 平均单价 from dbo.orderItem group by itemName +go + +select orders.orderId,orderDate,itemType,itemName,theNumber,theMoney from orderItem +inner join orders on orderItem.orderId = orders.orderId +go + +select orderDate,itemType,itemName from orderItem +inner join orders on orderItem.orderId = orders.orderId +where theNumber>50 +go + +select orders.orderId,orderDate,itemType,itemName,theNumber,theMoney * theNumber as 订购总价 from orderItem +inner join orders on orderItem.orderId = orders.orderId +go + +select orders.orderId,orderDate,itemType,itemName,theNumber,theMoney * theNumber as 订购总价 from orderItem +inner join orders on orderItem.orderId = orders.orderId +where theMoney > 5 and theNumber > 50 +go + +select orders.orderId as 编号,count(*) as 订购产品数 from orderItem +inner join orders on orderItem.orderId = orders.orderId +group by orders.orderId + +select orders.orderId as 订单编号,itemType as 产品类别,count(*)as 订购次数,sum(theNumber) as 总数量 from orderItem +inner join orders on orderItem.orderId = orders.orderId +group by orders.orderId,itemType +order by orders.orderId + diff --git "a/20210326\344\275\234\344\270\232/\345\217\266\351\242\200/BBS.sql" "b/20210326\344\275\234\344\270\232/\345\217\266\351\242\200/BBS.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7f2357982a9485b531b5131c4a4536ed6481e852 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\217\266\351\242\200/BBS.sql" @@ -0,0 +1,176 @@ +use master +go + +create database bbs +on +( + name='bbs', + filename='D:\test\bbs.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='bbs_log', + filename='D:\test\bbs_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) + +go +use bbs +go + +create table bbsUsers +( + UuId int identity(1,1), + UName varchar(10) not null , + USex varchar(2) , + UAge int , + UPoint int not null +) +go + +alter table bbsUsers add constraint PK_UuId primary key (UuId) +go +alter table bbsUsers add constraint UQ_UName unique (UName) +go +alter table bbsUsers add constraint CK_USex check(USex='男'or USex='女') +go +alter table bbsUsers add constraint CK_UAge check(UAge>=15 and UAge<=60) +go +alter table bbsUsers add constraint CK_UPoint check(Upoint>=0) +go + +create table bbsSection +( + Ssid int identity(1,1) not null, + SName varchar(10) not null, + SuId int, +) +go + +alter table bbsSection add constraint PK_Ssid primary key (Ssid) +go +alter table bbsSection add constraint FK_SuId foreign key(SuId) references bbsUsers(UuId) +go + +create table bbsTopic +( + TtId int primary key identity(1,1), + TuId int references bbsUsers(Uuid), + TsId int references bbsSection(SsId), + TTitlr varchar(100) not null, + TMsg text, + TTime datetime, + TCount int +) +go + +create table bbsReply +( + RrId int primary key identity(1,1), + RuId int references bbsUsers(UuId), + RtId int references bbsTopic(TtId), + RMsg text not null, + RTime datetime, +) +go + +insert bbsUsers (UName,USex,UAge,UPoint) +select '小雨点','女',20,0 union +select '逍遥','男',18,4 union +select '七年级生','男',19,2 +go + +select UName ,UPoint into bbsPoint from bbsUsers +go + +insert into bbsSection(SName,SuId)values +('技术交流',2), +('读书世界',3), +('生活百科',2), +('八卦区',3) +go + +insert into bbsTopic(TuId,TsId,TTitlr,TMsg,TTime,TCount)values +(3,4,'范跑跑','谁是范跑跑','2008-7-8','1'), +(2,1,'.NET ','与JAVA的区别是什么呀?','2008-9-1','2'), +(4,3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?','2008-9-10','0'), +(3,3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?','2008-9-10','0'), +(4,1,'.NET ','与JAVA的区别是什么呀?','2008-9-1','0'), +(2,4,'范跑跑','范跑跑谁是???','2008-7-8','0') +go + +insert into bbsReply(RuId,RMsg,RTime,RtId)values +('2','范跑跑 ','2008-7-8','1'), +('3','NET ','2008-9-1','2'), +('4','.NET ','2008-9-10','2') +go + +update bbsUsers set UPoint=UPoint+10 where UuId=3 + +truncate table bbsReply +go + +select TsId,COUNT(TtId) as 发帖总数 from bbsTopic group by TsId +go + +select RtId, COUNT(RrId) as 回帖总数量 from bbsReply group by RtId +go + +select TuId,COUNT(TtId) as 发帖总数 from bbsTopic group by TuId +go + +select TuId,SUM(TCount) as 回复数量总和 from bbsTopic group by TuId +go + +select TsId,AVG(TCount) as 平均回复数量 from bbsTopic group by TsId having AVG(TCount)>3 +go + +select * from bbsUsers where UPoint=(select MAX(UPoint) from bbsUsers) +go + +select * from bbsTopic where TTitlr like('%快乐%') or TMsg like('%快乐%') +go + +select * from bbsUsers where UAge>=15 and UAge<=20 and UPoint>=10 +go + +select * from bbsUsers where UName like('小%') and UName like('__大%') +go + +select TTitlr as 标题, TMsg as 内容 from bbsTopic where TTime>='2008-9-10 12:00:00' and TCount>=10 + go + + select TuId ,TCount from bbsTopic where TTitlr like ('%!') + go + + select UuId,UName,SName from bbsTopic + inner join bbsUsers on bbsTopic.TuId = bbsUsers.UuId + inner join bbsSection on bbsTopic.TsId = bbsSection.Ssid + go + + select UuId,UName,TTitlr,TMsg,TTime from bbsTopic + inner join bbsUsers on bbsTopic.TuId = bbsUsers.UuId +where TTime >= '2008-09-15' +go + + select UuId,UName,SName from bbsTopic + inner join bbsUsers on bbsTopic.TuId = bbsUsers.UuId + inner join bbsSection on bbsTopic.TsId = bbsSection.Ssid + where UAge<20 + go + + select top 1 UuId,UName,TTitlr,TMsg,TCount from bbsTopic + inner join bbsUsers on bbsTopic.TuId = bbsUsers.UuId + order by TCount desc + go + + + select SName,UName,count(TCount) from bbsTopic + inner join bbsSection on bbsTopic.TsId = bbsSection.Ssid + inner join bbsUsers on bbsUsers.UuId = bbsTopic.TuId + group by UuId,TsId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\217\266\351\242\200/Student.sql" "b/20210326\344\275\234\344\270\232/\345\217\266\351\242\200/Student.sql" new file mode 100644 index 0000000000000000000000000000000000000000..541f42ced9221c615d2353d8f0b25ab96f5b9d45 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\217\266\351\242\200/Student.sql" @@ -0,0 +1,93 @@ +use master +go + +create database Dwu +on +( +name='Dwu', +filename='D:\test\Dwu.mdf', +size=5mb, +maxsize=100mb, +filegrowth=10mb +) +log on +( +name='Dwu_log', +filename='D:\test\Dwu_log.ldf', +size=5mb, +maxsize=100mb, +filegrowth=10mb +) + +go +use Dwu +go + +create table stuinfo +( +stuNo char(5) primary key not null, +stuName nvarchar(20) not null, +stuAge char(3) not null, +stuAddress nvarchar(200), +stuSeat int identity(1,1)not null, +stuSex char(1) check(stuSex in (1,0)) default(1) not null +) +go + +insert into stuinfo(stuNo,stuName,stuAge,stuAddress,stuSex) values +('s2501','张秋利',20,'美国硅谷',1), +('s2502','李斯文',18,'湖北武汉',0), +('s2503','马文才',22,'湖南长沙',1), +('s2504','欧阳俊雄',21,'湖北武汉',0), +('s2505','梅超风',20,'湖北武汉',1), +('s2506','陈旋风',19,'美国硅谷',1), +('s2507','陈凤',20,'美国硅谷',0) +go + +create table stuexam +( +examNo int primary key identity(1,1), +stuNo char(5) references stuinfo(stuNo) not null, +writtenExam int not null, +labExam int not null +) +go + +insert stuexam(stuNo,writtenExam,labExam)values +('s2501',50,70), +('s2502',60,65), +('s2503',86,85), +('s2504',40,80), +('s2505',70,90), +('s2506',85,90) +go + +select stuName,stuAge,writtenExam,labExam from stuexam +inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +go + + +select stuName,stuAge,writtenExam,labExam from stuexam +inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +where writtenExam > 60 and labExam > 60 +go + +select stuName,stuAge,writtenExam,labExam from stuinfo +left join stuexam on stuexam.stuNo = stuinfo.stuNo +go + +select stuName,stuAge,writtenExam,labExam from stuexam +inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +where stuAge >= 20 +order by writtenExam desc +go + +select stuSex,avg(labExam) from stuexam +inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +group by stuSex +go + +select stuSex,sum(writtenExam) from stuexam +inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +group by stuSex +go \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0dc15c543ca9d9bf5250079d78d19fce23b86f7e --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery1.sql" @@ -0,0 +1,92 @@ +use master +go +create database Stundent +on +( + name='Stundent', + filename='D:\新建文件夹.mdf', + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) +log on +( + name='Stundent_log', + filename='D:\新建文件夹_1og.ldf', + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) +go +use Stundent +go +create table stuinfo--学生信息表 +( + stuNO varchar(10), + stuName nvarchar(20), + stuAge int, + stuAddress nvarchar(100), + stuSeat char(10), + StuSex nvarchar(1) check(StuSex in(1,0)), + ) +use Stundent +go +create table stuexam--学生成绩表 +( + examNO char(10), + stuNO varchar(10), + writtenExam int, + labExam int, +) +--插入表数据 +insert into stuinfo(stuNO,stuName,stuAge,stuAddress,stuSeat,StuSex) +values('s2501','张秋丽',20,'美国硅谷',1,1) +insert into stuinfo(stuNO,stuName,stuAge,stuAddress,stuSeat,StuSex) +values('s2502','李斯文',18,'湖北武汉',2,0) +insert into stuinfo(stuNO,stuName,stuAge,stuAddress,stuSeat,StuSex) +values('s2503','马文才',22,'湖南长沙',3,1) +insert into stuinfo(stuNO,stuName,stuAge,stuAddress,stuSeat,StuSex) +values('s2504','欧阳俊雄',21,'湖北武汉',4,0) +insert into stuinfo(stuNO,stuName,stuAge,stuAddress,stuSeat,StuSex) +values('s2505','梅超风',20,'湖北武汉',5,1) +insert into stuinfo(stuNO,stuName,stuAge,stuAddress,stuSeat,StuSex) +values('s2506','陈旋风',19,'美国硅谷',6,1) +insert into stuinfo(stuNO,stuName,stuAge,stuAddress,stuSeat,StuSex) +values('s2507','陈风',20,'美国硅谷',7,0) + + +insert into stuexam(examNO,stuNO,writtenExam,labExam) +values(1,'s2501',50,70) +insert into stuexam(examNO,stuNO,writtenExam,labExam) +values(2,'s2502',60,65) +insert into stuexam(examNO,stuNO,writtenExam,labExam) +values(3,'s2503',86,85) +insert into stuexam(examNO,stuNO,writtenExam,labExam) +values(4,'s2504',40,80) +insert into stuexam(examNO,stuNO,writtenExam,labExam) +values(5,'s2505',70,90) +insert into stuexam(examNO,stuNO,writtenExam,labExam) +values(6,'s2506',85,90) + +select * from stuinfo +select * from stuexam + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName 姓名,stuAge 年龄,writtenExam 笔试成绩,labExam 机试成绩 from stuinfo S inner join stuexam B on S.stuNO = B.stuNO + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select S.stuNO 学号, stuName 姓名,writtenExam 笔试成绩,labExam 机试成绩 from stuinfo S inner join stuexam B on S.stuNO = B.stuNO +where writtenExam>60 and labExam>60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select S.stuNO 学号,stuName 姓名,writtenExam 笔试成绩,labExam 机试成绩 from stuinfo S left join stuexam B on S.stuNO = B.stuNO + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName 姓名,stuAge 年龄,writtenExam 笔试成绩,labExam 机试成绩 from stuinfo S join stuexam B on S.stuNO = B.stuNO +where stuAge>=20 order by writtenExam DESC + +--5.查询男女生的机试平均分 +select StuSex 性别,avg(labExam)机试平均分 from stuinfo S inner join stuexam B on S.stuNO = B.stuNO group by StuSex + +--6.查询男女生的笔试总分 +select StuSex 性别,sum(writtenExam)笔试总分 from stuinfo S inner join stuexam B on S.stuNO = B.stuNO group by StuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..15efc080f5bb28ee19fe1770d0332779c4930890 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery2.sql" @@ -0,0 +1,90 @@ +use master +go +create database biao +on +( +name='biao', +filename='D:\新建文件夹4.mdf', +size=6mb, +maxsize=100mb, +filegrowth=10mb +) +log on +( +name='biao_log', +filename='D:\新建文件夹4_log.ldf', +size=6mb, +maxsize=100mb, +filegrowth=10mb +) +go +use biao +go +create table orders +( +orderId int primary key identity, +orderDate datetime default(getDate()) +) + + +use biao +go +create table orderItem +( +ItemiD int identity, +orderId int references orders(orderId), +itemType nvarchar(6), +itemName nvarchar(5), +theNumber int, +theMoney money +) + +insert into orders values(GETDATE()),(GETDATE()),(GETDATE()),(GETDATE()) +insert into orderItem values(1,'文具','笔',72,2), +(1,'文具','尺',10,1), +(1,'体育用品','篮球',1,56), +(2,'文具','笔',36,2), +(2,'文具','固体胶',20,3), +(2,'日常用品','透明胶',2,1), +(2,'体育用品','羽毛球',20,3), +(3,'文具','订书机',20,3), +(3,'文具','订书针',10,3), +(3,'文具','裁纸刀',5,5), +(4,'文具','笔',20,2), +(4,'文具','信纸',50,1), +(4,'日常用品','毛巾',4,5), +(4,'日常用品','透明胶',30,1), +(4,'体育用品','羽毛球',20,3) + +select * from orders +select * from orderItem + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select O.orderId 订单编号,orderDate 订单日期,itemType 订购产品类别,itemName 订购的产品名称,theNumber 订购数量,theMoney 订购单价 from orders O inner join orderItem X on O.orderId = X.orderId + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select O.orderId 订单编号,orderDate 订单日期,itemType 订购产品类别,itemName 订购的产品名称 from orders O inner join orderItem X on O.orderId = X.orderId +where theNumber>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select O.orderId 订单编号,orderDate 订单日期,itemType 订购产品类别,itemName 订购的产品名称,theNumber 订购数量,theMoney 订购单价,theNumber*theMoney 订购总价 from orders O inner join orderItem X on O.orderId = X.orderId + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select O.orderId 订单编号,orderDate 订单日期,itemType 订购产品类别,itemName 订购的产品名称,theNumber 订购数量,theMoney 订购单价 from orders O inner join orderItem X on O.orderId = X.orderId +where theMoney>5 and theNumber>50 + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select O.orderId 编号,count(*) 订购产品数 from orders O inner join orderItem X on O.orderId = X.orderId group by O.orderId + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: +-- 订单编号 产品类别 订购次数 总数量 +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + +select O.orderId 订单编号,itemType 产品类别,count(*)订购次数,sum(theNumber)总数量 from orders O inner join orderItem X on O.orderId = X.orderId group by itemType,O.orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9a8f410d2150bdf131a469f77199e4bb9aaf3980 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery3.sql" @@ -0,0 +1,150 @@ +use master +go +create database bbs +on +( + name='bbs', + filename='D:\新建文件夹7.mdf', + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) +log on +( + name='bbs_log', + filename='D:\新建文件夹7_1og.ldf', + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) +go +use bbs +go +create table bbsUsers +( + bbUID int identity, + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null + ) +go +use bbs +go +create table bbsSection +( + bbsID int identity, + sName varchar(10) not null, + sUid int references bbsUsers(bbUID) + ) +go +use bbs +go +create table bbsTopic +( + tID int primary key identity, + tUID int references bbsUsers(bbUID), + tSID int references bbsSection(bbsID), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int + ) +go +use bbs +go +create table bbsReply +( + rID int primary key identity, + rUID int references bbsUsers(bbUID), + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime + ) + --添加约束 +alter table bbsUsers add constraint PK_bbsUsers_bbUID primary key(bbUID) + +alter table bbsUsers add constraint UK_bbsUsers_uName unique(uName) + +alter table bbsUsers add constraint CK_bbsUsers_uSex check(uSex='男'or uSex='女') + +alter table bbsUsers add constraint CK_bbsUsers_uAge check(uAge>=15 and uAge<=60) + +alter table bbsUsers add constraint CK_bbsUsers_uPoint check(uPoint>=0) + +alter table bbsSection add constraint PK_bbsSection_bbsID primary key(bbsID) + +alter table bbsSection add constraint FK_bbsUsers_bbUID foreign key(bbUID) references bbsUsers(bbUID) + +insert into bbsUsers(uName,uSex,uAge,uPoint) +values('小雨点','女',20,0), + ('逍遥','男',18,4), + ('七年级生','男',19,2) +--二2. +select uName,uPoint into bbsPoint from bbsUsers + +--二3. +insert into bbsSection(sName,sUid) +values('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) + +--二4. +--主贴表(bbsTopic) +--主贴编号 tID int 主键primary key 标识列,identity +--发帖人编号 tUID int 外键 renferences 表(列) 引用用户信息表的用户编号 +--版块编号 tSID int 外键 primary key 引用版块表的版块编号 (标明该贴子属于哪个版块) +--贴子的标题 tTitle varchar(100) 不能为空 +--帖子的内容 tMsg text 不能为空 +--发帖时间 tTime datetime +--回复数量 tCount int +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) +values(2,4,'范跑跑','谁是范跑跑',2008-7-8,1), + (3,1,'.NET','与JAVA的区别是什么呀?',2008-9-1,2), + (1,3,'今年夏天最流行什么',' 有谁知道今年夏天最流行什么呀?',2008-9-10,0) + +--回帖:分别给上面三个主贴添加对应的回帖,回帖的内容,时间,回帖人自定 + +--回帖表(bbsReply) +--回贴编号 rID int 主键primary key 标识列,identity +--回帖人编号 rUID int 外键 renferences 引用用户信息表的用户编号 +--对应主贴编号 rTID int 外键 renferences 引用主贴表的主贴编号(标明该贴子属于哪个主贴) +--回帖的内容 rMsg text 不能为空 +--回帖时间 rTime datetime + +insert into bbsReply(rUID,rTID,rMsg,rTime) +values(1,3,'地震跑路人',2020-1-1), +(1,2,'区别很大',2020-2-1), +(2,2,'流行敲代码',2020-3-1) +--二.5.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户,请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) + +alter table dbo.bbsTopic drop constraint FK__bbsTopic__tUID__1A14E395 +alter table dbo.bbsReply drop constraint FK__bbsReply__rUID__1DE57479 + +delete from bbsUsers where uName='逍遥' + +--二.6.因为小雨点发帖较多,将其积分增加10分 + +update bbsUsers set uPoint=10 where uName='小雨点' + + + +select * from bbsUsers +select * from bbsSection +select * from bbsTopic +select * from bbsReply + +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select bbUID 版主编号, uName 版主姓名,sName 版块名称 from bbsUsers S inner join bbsSection B on S.bbUID = B.bbsID + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select bbUID 发帖人编号, uName 发帖人姓名,tTitle 帖子的标题,tMsg 帖子的内容,tTime 发帖时间 from bbsUsers S inner join bbsTopic T on S.bbUID = T.tUID where tTime>'2008-9-15' + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select bbUID 版主编号, uName 版主的名称,sName 版块名称 from bbsUsers S inner join bbsSection B on S.bbUID = B.bbsID where uAge<20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select bbUID 发帖人编号, uName 发帖人姓名,tTitle 主贴标题,tMsg 主贴内容,tCount 回复数量 from bbsUsers S inner join bbsTopic T on S.bbUID = T.tUID +where tCount=(select max(tCount) from bbsTopic) + +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select tSID 版块,tUID 用户,count(*)发帖总数 from bbsTopic group by tSID,tUID order by tSID + \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery4.sql" "b/20210326\344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fa6502da840957ab801ca8a6c6c648d957fa0b45 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\220\264\344\276\235\346\266\265/SQLQuery4.sql" @@ -0,0 +1,478 @@ +USE [master] +GO +/****** Object: Database [TestDB] Script Date: 2021/3/15 16:11:24 ******/ +CREATE DATABASE [TestDB] + CONTAINMENT = NONE + ON PRIMARY +( NAME = N'TestDB', FILENAME = N'D:\TestDB.mdf' , SIZE = 4288KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) + LOG ON +( NAME = N'TestDB_log', FILENAME = N'D:\TestDB_log.ldf' , SIZE = 1072KB , MAXSIZE = 2048GB , FILEGROWTH = 10%) +GO +ALTER DATABASE [TestDB] SET COMPATIBILITY_LEVEL = 120 +GO +IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled')) +begin +EXEC [TestDB].[dbo].[sp_fulltext_database] @action = 'enable' +end +GO +ALTER DATABASE [TestDB] SET ANSI_NULL_DEFAULT OFF +GO +ALTER DATABASE [TestDB] SET ANSI_NULLS OFF +GO +ALTER DATABASE [TestDB] SET ANSI_PADDING OFF +GO +ALTER DATABASE [TestDB] SET ANSI_WARNINGS OFF +GO +ALTER DATABASE [TestDB] SET ARITHABORT OFF +GO +ALTER DATABASE [TestDB] SET AUTO_CLOSE OFF +GO +ALTER DATABASE [TestDB] SET AUTO_SHRINK OFF +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS ON +GO +ALTER DATABASE [TestDB] SET CURSOR_CLOSE_ON_COMMIT OFF +GO +ALTER DATABASE [TestDB] SET CURSOR_DEFAULT GLOBAL +GO +ALTER DATABASE [TestDB] SET CONCAT_NULL_YIELDS_NULL OFF +GO +ALTER DATABASE [TestDB] SET NUMERIC_ROUNDABORT OFF +GO +ALTER DATABASE [TestDB] SET QUOTED_IDENTIFIER OFF +GO +ALTER DATABASE [TestDB] SET RECURSIVE_TRIGGERS OFF +GO +ALTER DATABASE [TestDB] SET ENABLE_BROKER +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF +GO +ALTER DATABASE [TestDB] SET DATE_CORRELATION_OPTIMIZATION OFF +GO +ALTER DATABASE [TestDB] SET TRUSTWORTHY OFF +GO +ALTER DATABASE [TestDB] SET ALLOW_SNAPSHOT_ISOLATION OFF +GO +ALTER DATABASE [TestDB] SET PARAMETERIZATION SIMPLE +GO +ALTER DATABASE [TestDB] SET READ_COMMITTED_SNAPSHOT OFF +GO +ALTER DATABASE [TestDB] SET HONOR_BROKER_PRIORITY OFF +GO +ALTER DATABASE [TestDB] SET RECOVERY FULL +GO +ALTER DATABASE [TestDB] SET MULTI_USER +GO +ALTER DATABASE [TestDB] SET PAGE_VERIFY CHECKSUM +GO +ALTER DATABASE [TestDB] SET DB_CHAINING OFF +GO +ALTER DATABASE [TestDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) +GO +ALTER DATABASE [TestDB] SET TARGET_RECOVERY_TIME = 0 SECONDS +GO +ALTER DATABASE [TestDB] SET DELAYED_DURABILITY = DISABLED +GO +EXEC sys.sp_db_vardecimal_storage_format N'TestDB', N'ON' +GO +USE [TestDB] +GO +/****** Object: Table [dbo].[ClassInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[ClassInfo]( + [ClassId] [int] IDENTITY(1,1) NOT NULL, + [ClassName] [nvarchar](20) NOT NULL, +PRIMARY KEY CLUSTERED +( + [ClassId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[CourseInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[CourseInfo]( + [CourseId] [int] IDENTITY(1,1) NOT NULL, + [CourseName] [nvarchar](50) NOT NULL, + [CourseCredit] [int] NULL DEFAULT ((1)), +PRIMARY KEY CLUSTERED +( + [CourseId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[Scores] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[Scores]( + [ScoreId] [int] IDENTITY(1,1) NOT NULL, + [StuId] [int] NULL, + [CourseId] [int] NULL, + [Score] [int] NULL DEFAULT ((0)), +PRIMARY KEY CLUSTERED +( + [ScoreId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[StuInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[StuInfo]( + [StuId] [int] IDENTITY(1,1) NOT NULL, + [ClassId] [int] NULL, + [StuName] [nvarchar](10) NOT NULL, + [StuSex] [nvarchar](1) NULL DEFAULT ('男'), + [StuBrithday] [date] NULL, + [StuPhone] [nvarchar](11) NULL, + [StuProvince] [nvarchar](200) NULL, + [CreateDate] [datetime] NULL DEFAULT (getdate()), + [StuAge] [int] NULL, +PRIMARY KEY CLUSTERED +( + [StuId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] ON + +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (1, N'软件1班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (2, N'软件2班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (3, N'软件3班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (4, N'软件4班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (5, N'软件5班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (6, N'软件6班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (7, N'软件7班') +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] ON + +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (1, N'计算机基础', 3) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (2, N'HTML+CSS网页制作', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (3, N'JAVA编程基础', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (4, N'SQL Server数据库基础', 4) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (5, N'C#面向对象编程', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (6, N'Winform桌面应用程序设计', 5) +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[Scores] ON + +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (1, 1, 1, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (2, 1, 2, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (3, 1, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (4, 1, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (5, 2, 1, 60) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (6, 2, 2, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (7, 2, 3, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (8, 2, 4, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (9, 3, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (10, 3, 2, 45) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (11, 3, 3, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (12, 3, 4, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (13, 4, 1, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (14, 4, 2, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (15, 4, 3, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (16, 4, 4, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (17, 5, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (18, 5, 2, 79) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (19, 5, 3, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (20, 5, 4, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (21, 6, 1, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (22, 6, 2, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (23, 6, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (24, 6, 5, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (25, 7, 1, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (26, 7, 2, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (27, 7, 3, 92) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (28, 7, 5, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (29, 8, 1, 58) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (30, 8, 2, 59) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (31, 8, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (32, 8, 5, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (33, 9, 1, 48) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (34, 9, 2, 67) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (35, 9, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (36, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (37, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (38, 1, 1, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (39, 1, 2, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (40, 1, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (41, 1, 4, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (42, 2, 1, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (43, 2, 2, 82) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (44, 2, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (45, 2, 4, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (46, 3, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (47, 3, 2, 50) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (48, 3, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (49, 3, 4, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (50, 4, 1, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (51, 4, 2, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (52, 4, 3, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (53, 4, 4, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (54, 5, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (55, 5, 2, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (56, 5, 3, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (57, 5, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (58, 6, 1, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (59, 6, 2, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (60, 6, 3, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (61, 6, 5, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (62, 7, 1, 89) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (63, 7, 2, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (64, 7, 3, 97) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (65, 7, 5, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (66, 8, 1, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (67, 8, 2, 64) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (68, 8, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (69, 8, 5, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (70, 9, 1, 53) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (71, 9, 2, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (72, 9, 3, 76) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (73, 9, 5, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (74, 9, 5, 61) +GO +SET IDENTITY_INSERT [dbo].[Scores] OFF +GO +SET IDENTITY_INSERT [dbo].[StuInfo] ON + +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (1, 1, N'刘正', N'男', CAST(N'2002-08-02' AS Date), N'13245678121', N'广西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (2, 1, N'黄贵', N'男', CAST(N'2003-07-02' AS Date), N'13345678121', N'江西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (3, 1, N'陈美', N'女', CAST(N'2002-07-22' AS Date), N'13355678125', N'福建省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (4, 2, N'江文', N'男', CAST(N'2001-07-02' AS Date), N'13347678181', N'湖南省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (5, 2, N'钟琪', N'女', CAST(N'2004-01-13' AS Date), N'13345778129', N'安徽省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (6, 3, N'曾小林', N'男', CAST(N'2005-05-15' AS Date), N'13345378563', N'安徽省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (7, 3, N'欧阳天天', N'女', CAST(N'2000-08-19' AS Date), N'13347878121', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (8, 3, N'李逍遥', N'男', CAST(N'1999-09-02' AS Date), N'13345678557', N'广东省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 22) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (9, 4, N'刘德华', N'男', CAST(N'1995-06-11' AS Date), N'15345679557', N'福建省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 26) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (10, 4, N'刘翔', N'男', CAST(N'1996-07-09' AS Date), N'18346679589', N'江西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (11, 4, N'曾小贤', N'男', CAST(N'2003-07-02' AS Date), N'18348979589', N'湖南省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (12, 5, N'刘德华', N'男', CAST(N'2002-07-02' AS Date), N'18348979509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (13, 5, N'陈天翔', N'男', CAST(N'2003-07-02' AS Date), N'18348079509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (14, 5, N'刘能', N'男', CAST(N'2005-08-02' AS Date), N'13245678122', N'广西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (15, 5, N'钟馗', N'男', CAST(N'2004-08-02' AS Date), N'13245678123', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (16, 5, N'钟吴艳', N'女', CAST(N'2002-08-02' AS Date), N'13245678124', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (17, 5, N'刘欢', N'男', CAST(N'2001-07-02' AS Date), N'13245678125', N'湖南省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (18, 5, N'张庭', N'女', CAST(N'2000-07-02' AS Date), N'13245678126', N'江西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (19, 5, N'曹植', N'男', CAST(N'2000-08-02' AS Date), N'13245678127', N'福建省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (20, 5, N'曹操', N'男', CAST(N'2002-08-02' AS Date), N'13245678128', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (21, 5, N'孙尚香', N'女', CAST(N'2003-08-02' AS Date), N'13245678129', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (22, 3, N'老1', N'女', CAST(N'2002-08-02' AS Date), N'13245678130', N'广东省', CAST(N'2021-03-14 17:02:36.347' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (24, 2, N'老2', N'男', CAST(N'2002-08-03' AS Date), N'13345678945', NULL, CAST(N'2021-03-14 17:03:37.733' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (25, 4, N'老3', N'男', NULL, N'13645987545', N'广东省', CAST(N'2021-03-14 17:03:43.307' AS DateTime), NULL) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (28, 5, N'老4', N'男', CAST(N'2006-03-05' AS Date), N'13456987456', NULL, CAST(N'2021-03-14 17:04:03.957' AS DateTime), 15) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (29, 5, N'老5', N'女', CAST(N'1998-04-12' AS Date), N'15978456123', NULL, CAST(N'2021-03-14 17:04:47.103' AS DateTime), 23) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (30, 4, N'老6', N'男', CAST(N'1996-08-06' AS Date), N'18945674561', NULL, CAST(N'2021-03-14 17:05:04.990' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (31, 3, N'老7', N'女', CAST(N'1997-04-06' AS Date), N'18845678912', NULL, CAST(N'2021-03-14 17:05:20.570' AS DateTime), 24) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (32, 2, N'老10', N'女', CAST(N'1998-08-09' AS Date), N'19945645612', NULL, CAST(N'2021-03-14 17:06:08.107' AS DateTime), 23) +GO +SET IDENTITY_INSERT [dbo].[StuInfo] OFF +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__CourseIn__9526E2773AB7BECE] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[CourseInfo] ADD UNIQUE NONCLUSTERED +( + [CourseName] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__StuInfo__2D85FC63AF6FC6FA] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[StuInfo] ADD UNIQUE NONCLUSTERED +( + [StuPhone] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([CourseId]) +REFERENCES [dbo].[CourseInfo] ([CourseId]) +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([StuId]) +REFERENCES [dbo].[StuInfo] ([StuId]) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD FOREIGN KEY([ClassId]) +REFERENCES [dbo].[ClassInfo] ([ClassId]) +ON DELETE SET NULL +GO +ALTER TABLE [dbo].[CourseInfo] WITH CHECK ADD CHECK (([CourseCredit]>=(1) AND [CourseCredit]<=(5))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK ((len([StuPhone])=(11))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK (([StuSex]='女' OR [StuSex]='男')) +GO +USE [master] +GO +ALTER DATABASE [TestDB] SET READ_WRITE +GO + +select * from StuInfo +select * from ClassInfo +select * from CourseInfo +select * from Scores + + + +--统计每个班的 男生数 +select ClassId 班级,count(*) 男生数 from StuInfo where StuSex='男' group by ClassId order by ClassId + +--统计每个班的男、女生数 + +select ClassId 班级,StuSex 姓别, count(*)数量 from StuInfo group by ClassId, StuSex order by ClassId + +--统计每个班的 福建人数 +select ClassId 班级,count(*)福建人数 from StuInfo where StuProvince='福建省' group by ClassId + +--统计 每个班的 各个省的 总人数 +select ClassId 班级,StuProvince 省份,count(*)总人数 from StuInfo group by ClassId ,StuProvince + +--统计 每个省的 女生数 +select StuProvince 省份,count(*)女生数 from StuInfo where StuSex='女' group by StuProvince + +--统计每个省的男、女生数 +select StuProvince 省份, StuSex 性别 ,count(StuId)人数 from StuInfo group by StuProvince,StuSex + +--统计每个学生的考试总分、平均分 +select StuId 学生,sum(Score)总分,avg(Score)平均分 from Scores group by StuId + +--统计出考试总分大于620的学生的考试总分 +select StuId 学生,sum(Score)总分 from Scores group by StuId having sum(Score)>620 + +--统计出每门考试成绩 的 最高分和最低分 +select CourseId 科目,max(Score)最高分,min(Score)最低分 from Scores group by CourseId + +--统计出每个学生 的 各门成绩的平均分 +select StuId 学生,CourseId 科目,avg(Score)平均分 from Scores group by StuId,CourseId + \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\220\264\346\230\237/\344\275\234\344\270\2321.sql" "b/20210326\344\275\234\344\270\232/\345\220\264\346\230\237/\344\275\234\344\270\2321.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1c6d7b5769ffd6a99bace5b5c412e729933ce6da --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\220\264\346\230\237/\344\275\234\344\270\2321.sql" @@ -0,0 +1,15 @@ +use Student +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName,stuAge,wittenexam,labexam from Stuinfo002 inner join fraction on Stuinfo002.stuNo = fraction.stuNo + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select Stuinfo002.stuNo,stuName,wittenexam,labexam from Stuinfo002 inner join fraction on Stuinfo002.stuNo = fraction.stuNo where wittenexam>60 and labexam>60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select Stuinfo002.stuNo,stuName,wittenexam,labexam from Stuinfo002 inner join fraction on Stuinfo002.stuNo = fraction.stuNo where wittenexam>60 and labexam>60 +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName,stuAge,wittenexam,labexam from Stuinfo002 inner join fraction on Stuinfo002.stuNo = fraction.stuNo where stuAge>=20 order by wittenexam desc +--5.查询男女生的机试平均分 +select stuSex 性别,avg(labexam)机试平均分 from Stuinfo002 inner join fraction on Stuinfo002.stuNo = fraction.stuNo group by stuSex +--6.查询男女生的笔试总分 +select stuSex 性别,sum(wittenexam)总分 from Stuinfo002 inner join fraction on Stuinfo002.stuNo = fraction.stuNo group by stuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\220\264\346\230\237/\344\275\234\344\270\2322.sql" "b/20210326\344\275\234\344\270\232/\345\220\264\346\230\237/\344\275\234\344\270\2322.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3923a21256bb559ae4d8d4372f65bbb696b209bd --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\220\264\346\230\237/\344\275\234\344\270\2322.sql" @@ -0,0 +1,91 @@ + +create database a +on +( + name='a', + filename='D:\bank\ATM1.mdf', + size=5, + maxsize=10, + filegrowth=15% +) +log on +( + name='a_log', + filename='D:\bank\ATM1_log.ldf', + size=5, + maxsize=10, + filegrowth=15% +) +go +use a +go + +create table orders +( +--订单编号(orderId 主键) 订购日期(orderDate) + orderId int primary key, + orderDate datetime default(getdate()) +) +--订购项目表(orderItem),列为: +--项目编号(ItemiD)订单编号(orderId)产品类别(itemType) +--产品名称(itemName) 订购数量(theNumber) 订购单价(theMoney) +create table orderItem +( +--订单编号(orderId 主键) 订购日期(orderDate) + ItemiD int identity(1,1), + orderId int, + itemType varchar(20), + itemName varchar(20), + theNumber int, + theMoney int +) + +select * from orders +select * from orderItem +insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) +select 1,'文具','笔',72,2 union +select 1,'文具','尺',10,1 union +select 1,'体育用品','篮球',1,56 union +select 2,'文具','笔',36,2 union +select 2,'文具','固体胶',20,3 union +select 2,'日常用品','透明胶',2,1 union +select 2,'体育用品','羽毛球',20,3 union +select 3,'文具','订书机',20,3 union +select 3,'文具','订书针',10,3 union +select 3,'文具','栽纸刀',5,5 union +select 4,'文具','笔',20,2 union +select 4,'文具','信纸',50,1 union +select 4,'日常用品','毛巾',4,5 union +select 4,'日常用品','透明胶',30,1 union +select 4,'日常用品','透明胶',52,6 union +select 4,'体育用品','羽毛球',20,3 + +insert into orders(orderId,orderDate) +select 1,'2008-01-12' union +select 2,'2008-02-10' union +select 3,'2008-02-15' union +select 4,'2008-03-10' +use a +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select ItemiD,orderDate,itemType,itemName,theNumber,theMoney from orders inner join orderItem on orders.orderId =orderItem.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select ItemiD,orderDate,itemType,itemName,theNumber from orders inner join orderItem on orders.orderId =orderItem.orderId where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select ItemiD,orderDate,itemType,itemName,theNumber,theMoney,theNumber*theMoney 订购总价 from orders inner join orderItem on orders.orderId = orderItem.orderId +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderDate,itemType,itemName,theNumber,theMoney,theNumber*theMoney 订购总价 from orders inner join orderItem on orders.orderId = orderItem.orderId where theMoney>5 and theNumber>50 +--5.查询每个订单分别订购了几个产品,例如: +select orderId 订单编号 ,count(theNumber)订购产品数 from orderItem group by orderId + -- 编号 订购产品数 + 1 3 + 2 4 + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: +select orderId 订单编号,itemName 产品名称,count(theNumber) 订购次数,sum(theNumber)总数量 from orderItem group by orderId,itemName + 订单编号 产品类别 订购次数 总数量 + + 1 文具 2 82 + 1 体育用品 1 1 + 2 文具 2 56 + 2 体育用品 1 2 + 2 日常用品 1 20 \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\220\264\346\230\237/\344\275\234\344\270\2323.sql" "b/20210326\344\275\234\344\270\232/\345\220\264\346\230\237/\344\275\234\344\270\2323.sql" new file mode 100644 index 0000000000000000000000000000000000000000..04877d959d51eeedab03b9fc6589d2ac65ff6732 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\220\264\346\230\237/\344\275\234\344\270\2323.sql" @@ -0,0 +1,101 @@ + +use bbs +go +create table bbsUsers--用户信息表 +( + UID01 int primary key identity(1,1), + uName varchar(10) unique not null, + uSex varchar(2) check(uSex in('女','男')) not null, + uAge int check(uAge>=15 and uAge<=60) not null, + uPoint int check(uPoint>=0) not null +) +insert into bbsUsers values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) +alter table bbsUsers add telephone varchar(20) check(len(telephone)=11) +update bbsUsers set telephone='12345678911' where uName='小雨点' +update bbsUsers set telephone='12345678912' where uName='七年级生' +alter table bbsUsers add constraint UK_bbsUsers_telephone unique(telephone) +alter table bbsUsers add constraint CK_bbsUsers_telephone check(len(telephone)=11) +alter table bbsUsers drop constraint CK__bbsUsers__uPoint__1367E606 +update bbsUsers set uName='小雪' where uName='小雨点' + + +truncate table bbsUsers2 +delete from bbsUsers2 + +select * from bbsUsers + + + + +create table bbsSection--版块表 + + +( + sID01 int primary key identity(1,1), + sName varchar(10) not null, + sUid int references bbsUsers(UID01) +) +alter table bbsSection add bbzName varchar(10) +insert into bbsSection(sName,bbzName) values('技术交流','小雨点'),('读书世界','七年级生'),('生活百科','小雨点'),('八卦区','七年级生') +select * from bbsSection + + +select sID01 版主的编号,bbzName 版主的名称,sName 版块的名称 from bbsSection inner join bbsUsers on bbsSection.sID01 = bbsUsers.UID01 where uAge<20 + +create table bbsTopic--主贴表 +( + tID int primary key identity(1,1), + tUID int references bbsUsers(UID01), + tSID int references bbsSection(sID01), + tTitle varchar(100) not null, + tMsg text not null, + tTime date, + tCount int +) +alter table bbsTopic add fatieName varchar(10) +alter table bbsTopic add bbzName varchar(10) + +drop table bbsTopic + +insert into bbsTopic values(1,null,'范跑跑!','谁是范跑跑','2008-7-8',3,'逍遥','八卦区'),(1,null,'范跑跑!','谁是范跑跑','2008-10-8',1,'逍遥','八卦区'), (2,null,'.NET','与JAVA的区别是什么呀?','2008-9-1',2,'七年级生','技术交流'),(3,null,'今年夏天最流行什么',' 有谁知道今年夏天最流行什么呀?','2008-9-10',1,'小雨点','生活百科') +select * from bbsTopic + + +select tUID 发帖人编号,tTitle 帖子的标题,uName 发帖人姓名,tMsg 帖子的内容,tTime 发帖时间 from bbsTopic inner join bbsPoint on bbsTopic.tUID= bbsPoint.UID01 where tTime>'2008-9-15' + + + +create table bbsReply--回帖表 +( + rID int primary key identity(1,1), + rUID int references bbsUsers(UID01), + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime +) +alter table bbsReply add huitieName varchar(10) not null +insert into bbsReply values(1,null,'范跑跑',2008-7-8 ,'1'),(2,null,'JAVA',2008-9-1 ,'2'),(2,null,'夏天',2008-9-10 ,'3') +alter table bbsReply alter column rMsg varchar(200) + +select * from bbsReply + +create table bbsPoint +( + UID01 int primary key identity(1,1), + uName varchar(10) unique not null, + uPoint int check(uPoint>=0) not null +) +insert into bbsPoint select uName,uPoint from bbsUsers +select * from bbsPoint + +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select sID01,bbzName,sName from bbsSection group by sID01,bbzName,sName +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID 发帖人编号,tTitle 帖子的标题,uName 发帖人姓名,tMsg 帖子的内容,tTime 发帖时间 from bbsTopic inner join bbsPoint on bbsTopic.tUID= bbsPoint.UID01 where tTime>'2008-9-15' +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sID01 版主的编号,bbzName 版主的名称,sName 版块的名称 from bbsSection inner join bbsUsers on bbsSection.sID01 = bbsUsers.UID01 where uAge<20 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select max(tCount) from bbsTopic +select rUID 发帖人编号,uName 发帖人姓名,tTitle 帖子的标题,tCount 回复数量, tMsg 帖子的内容 from bbsReply inner join bbsTopic on bbsReply.rUID =bbsTopic.tUID inner join bbsUsers on bbsReply.rUID= bbsUsers.UID01 where tCount=(select max(tCount) from bbsTopic) +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select tSID 每个版块,count (tUID) 每个用户 from bbsTopic group by tSID ,tUID \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8843dea2c34a5eb0d80e1822a8690b9b6d7e84b5 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery1.sql" @@ -0,0 +1,101 @@ +use master +go + +create database student +on +( + name='student', + filename='D:\bbs.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) + +log on +( + name='student_log', + filename='D:\bbs_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +go + + + +use student +go + +create table studentinfo +( + stuNO varchar(50) primary key not null, + stuName varchar(10) unique not null, + stuAge int, + stuAddress nvarchar(20), + stuSeat int not null, + stuSex varchar(2) check(stuSex='0' or stuSex='1') not null +) + +go + +create table achievement +( + examNO int primary key identity(1,1) not null, + stuNO varchar(50) references studentinfo(stuNO), + writtenExam int not null, + labExam int not null +) + + + +insert into studentinfo(stuNO,stuName,stuAge,stuAddress,stuSeat,stuSex) +select 's2501','张秋利','20','美国硅谷','1','1' union +select 's2502','李斯文','18','湖北武汉','2','0' union +select 's2503','马文才','22','湖南长沙','3','1' union +select 's2504','欧阳俊雄','21','湖北武汉','4','0' union +select 's2505','梅超风','20','湖北武汉','5','1' union +select 's2506','陈旋风','19','美国硅谷','6','1' union +select 's2507','梅超风2','20','美国硅谷','7','0' +go + + + +select * from studentinfo + +insert into achievement +select 's2501', 50, 70 union +select 's2502', 60, 65 union +select 's2503', 86, 85 union +select 's2504', 40, 80 union +select 's2505', 70, 90 union +select 's2506', 85, 90 + +go + + + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select StuName ,StuAge ,writtenExam ,labExam from StudentInfo +inner join achievement on StudentInfo.StuNo = achievement.StuNo + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select StuName,writtenExam ,labExam from StudentInfo +inner join achievement on StudentInfo.StuNo = StudentInfo.StuNo +where labExam>60 and writtenExam>60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select StudentInfo.StuNo 学号,StuName 姓名,writtenExam 笔试成绩,labExam 机试成绩 from StudentInfo +left join achievement on StudentInfo.StuNo= StudentInfo.StuNo + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select StuName,StuAge,writtenExam,labExam from StudentInfo +inner join achievement on StudentInfo.stuNo = StudentInfo.stuNo +where stuAge>=20 order by writtenExam desc + +--5.查询男女生的机试平均分 +select StuSex,avg(labExam)平均分 from StudentInfo +inner join achievement on StudentInfo.stuNo = StudentInfo.stuNo group by stuSex + +--6.查询男女生的笔试总分 +select StuSex,SUM(writtenExam)笔试总分 from StudentInfo +inner join achievement on StudentInfo.stuNo = StudentInfo.stuNo group by stuSex diff --git "a/20210326\344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery10.sql" "b/20210326\344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery10.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d85a8a90b1660508bb7b6f0c679c81b954c5dfb6 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery10.sql" @@ -0,0 +1,33 @@ +select*from StuInfo +select*from ClassInfo +select*from CourseInfo +select*from Scores +--统计每个班的男生数 +select ClassId,count(*) StuSex from StuInfo where StuSex='男' group by ClassId + +--统计每个班的男、女生数 +select ClassId ,StuSex, count(StuSex) StuSex from StuInfo group by ClassId ,StuSex order by ClassId + +--统计每个班的福建人数 +select ClassId,count(StuProvince) StuProvince from StuInfo where StuProvince='福建省' group by ClassId + +--统计每个班的各个省的总人数 +select ClassId ,StuProvince, count(StuProvince) StuProvince from StuInfo group by ClassId ,StuProvince order by ClassId + +--统计每个省的女生数 +select StuProvince, count(StuSex) StuSex from StuInfo where StuSex='女' group by StuProvince + +--统计每个省的男、女生数 +select StuProvince,StuSex, count(StuSex) StuSex from StuInfo group by StuProvince ,StuSex order by StuProvince + +--统计每个学生的考试总分、平均分 +select StuId ,AVG(Score),SUM(Score) StuId from Scores group by StuId + +--统计出考试总分大于620的学生的考试总分 +select StuId,SUM(Score) StuID from Scores group by StuId having SUM(Score)>620 + +--统计出每门考试成绩最高分和最低分 +select CourseId,max(Score) ,min(Score) from Scores group by CourseId + +--统计出每个学生的各门成绩的平均分 +select StuId,CourseId,AVG(Score) from Scores GROUP BY StuId,CourseId ORDER BY StuId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fb5bf2b4cf82ee94984f40a7834908acba06a5aa --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery2.sql" @@ -0,0 +1,116 @@ +use master +go +create database webshop +on +( + name='webshop', + filename='D:\webshop.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) + +log on +( + name='webshop_log', + filename='D:\webshop_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +go + + +use webshop +go + +create table orders +( + orderID int primary key identity(1,1) , + orderDate datetime +) + + +go + +create table orderItem +( + ItemiD int primary key identity(1,1) , + orderId int, + itemType varchar(20), + itemName nvarchar(10), + theNumber int, + theMoney money +) + +set identity_insert orders ON--打开 +insert into orders(orderId,orderDate) values ('1','2008-1-12'),('2','2008-2-10'),('3','2008-2-15'),('4','2008-3-10') +select * from orders + +set identity_insert orderItem ON--打开 +insert into orderItem(ItemiD,orderId,itemType,itemName,theNumber,theMoney) +select '1','1','文具','笔','72','2' union +select '2','1','文具','尺','11','1' union +select '3','1','体育用品','篮球','1','56' union +select '4','2','文具','笔','36','2' union +select '5','2','文具','固体胶','20','3' union +select '6','2','日常用品','透明胶','20','1' union +select '7','2','体育用品','羽毛球','20','3' union +select '8','3','文具','订书机','20','3' union +select '9','3','文具','订书钉','10','3' union +select '10','3','文具','裁纸刀','5','5' union +select '11','4','文具','笔','20','2' union +select '12','4','文具','信纸','50','1' union +select '13','4','日常用品','毛巾','4','5' union +select '14','4','日常用品','透明胶','30','1' union +select '15','4','体育用品','羽毛球','20','3' +select * from orderItem + + + + +select * from orders +select * from orderItem + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select orderDate ,itemType ,itemName,theNumber,theMoney from orderItem +inner join orders on orderItem.orderId = orders.orderId + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orderDate ,itemType, itemName from orders +inner join orderItem on orders.orderId = orderItem.orderId +where theNumber>50 + + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderId,orderDate,itemType,itemName,theNumber,theMoney * theNumber 订购总价 from orderItem +inner join orders on orderItem.orderId = orders.orderId + + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderDate ,itemType, itemName,theNumber,theMoney,theMoney * theNumber 订购总价 from orders +inner join orderItem on orders.orderId = orderItem.orderId +where theMoney>=5 and theNumber>=50 + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orders.orderId 编号,count(*) as 订购产品数 from orderItem +inner join orders on orderItem.orderId = orders.orderId +group by orders.orderId + + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 +select orders.orderId as 订单编号,itemType as 产品类别,count(*)as 订购次数,sum(theNumber) as 总数量 from orderItem +inner join orders on orderItem.orderId = orders.orderId +group by orders.orderId,itemType +order by orders.orderId diff --git "a/20210326\344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a717e9d9c38c411de315d013c3ac05df3c2af1ec --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\221\250\346\230\237\345\256\207/SQLQuery3.sql" @@ -0,0 +1,183 @@ +create database BBS +on +(name='BBS', + filename='D:\BSS.mdf', + size=20, + maxsize=300, + filegrowth=50 +) +log on +(name='BBS_log', + filename='D:\BSS_log.ldf', + size=20, + maxsize=300, + filegrowth=50 +) +go +use BBS +create table bbsUsers +( +uIDD int primary key identity(1,1), +uName varchar(10) not null unique , +uSex varchar(2) not null check(uSex='男' or uSex='女'), +uAge int not null check(uAge>15 and uAge<60), +uPoint int not null check(uPoint>=0) +) + +create table bbsTopic( +tID int primary key identity(1,1), +tUID int references bbsUsers(uIDD), +tSID int references bbsSection(sIDD), +tTitle varchar(100) not null, +tMsg text not null, +tTime datetime , +tCount int +) + +create table bbsReply( +rID int primary key identity(1,1), +rUID int references bbsUsers(uIDD ), +rTID int references bbsTopic(tID), +rMsg text not null, +rTime datetime +uuuu) + +create table bbsSection( +sIDD int primary key identity(1,1), +sName varchar(10) not null , + sUid int references bbsUsers(uIDD) +) + +select*from bbsUsers +select*from bbsTopic +select*from bbsReply +select*from bbsSection + +--1.现在有3个会员注册成功,请用一次插入多行数据的方法向bbsUsers表种插入3行记录,记录值如下: +-- 小雨点 女 20 0 +-- 逍遥 男 18 4 +-- 七年级生 男 19 2 +insert into bbsUsers(uName,uSex,uAge,uPoint) +select'小雨点','女','20','0'union +select'逍遥','男','18','4'union +select'七年级生','男','19','2' +go + + --2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中,提示查询部分列:select 列名1,列名2 from 表名 + select uName,uPoint into bbsPoint from bbsUsers + go + --3.给论坛开设4个板块 + -- 名称 版主名 + -- 技术交流 小雨点 + -- 读书世界 七年级生 + -- 生活百科 小雨点 + -- 八卦区 七年级生 + insert into bbsSection(sName,sUid) + select'技术交流',3 union + select'读书世界',1 union + select'生活百科',3 union + select'八卦区',1 + go + --4.向主贴和回帖表中添加几条记录 + insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) +select '2','1','范跑跑','谁是范跑跑','2008-7-8','1'union +select '1','3','.NET','与Java的区别是什么呀?','2008-9-1','2'union +select '3','4','今年夏天最流行什么呀?','有谁知道今年夏天最流行什么呀?','2008-9-10','0' + + +-- 回帖: +-- 分别给上面三个主贴添加对应的回帖,回帖的内容,时间,回帖人自定 + +insert into bbsReply(rTID,rMsg,rTime,rUId) +select'1','不知道','2008-9-5','2'union +select'2','不认识','2008-9-10','1'union +select'3','不知道','2008-10-1','3' + +-- 5.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户,请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) + + + +alter table bbsReply drop constraint FK_bbsReply_rUID +alter table bbsTopic drop constraint FK_bbsTopic_tUID +alter table bbsSection drop constraint FK_bbsSection_sUid +alter table bbsUsers drop constraint PK_bbsUsers_UID + +delete from bbsUsers where UIDD=2 + +-- 6.因为小雨点发帖较多,将其积分增加10分 + + +update bbsUsers set uPoint=(uPoint + 10) where uName='小雨点' + + +-- 7.因为板块“生活百科”灌水的人太少,现决定取消该板块,即删除(注意主外键) + +delete from bbsReply where rTID=3 +delete from bbsTopic where tSID=4 +delete from bbsSection where sIDD=4 + +-- 8.因回帖积累太多,现需要将所有的回帖删除 + +delete from bbsReply +truncate table bbsReply +drop table bbsReply + + +--1.在主贴表中统计每个板块的发帖总数 +select tSID 板块, count(*)发帖总数 from bbsTopic group by tSID + +--2.在回帖表中统计每个主贴的回贴总数量 +select rID 回帖编号,count(*)回贴总数量 from bbsReply group by rID + +--3.在主贴中统计每个用户的发的主贴的总数 +select tSID ,count(*)主贴的总数 from bbsTopic group by tSID + +--4.在主贴中统计每个用户发的主贴的回复数量总和 +select SUM(tCount)回复数量总和 from bbsTopic + +--5.在主贴表中查询每个板块的主贴的平均回复数量大于3的板块的平均回复数量 +select AVG(tCount)回复数量大于3的板块的平均回复数量 from bbsTopic group by tSID having AVG(tCount)>3 + +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select UName,USex,Upoint from bbsUsers where uPoint=(select max(uPoint)最高积分 from bbsUsers) + +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select * from bbsTopic where tTitle like('%快乐%') + +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +select * from bbsUsers where uAge>=15 and uAge<=20 and uPoint>10 + +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select * from bbsUsers where uName like ('小%') and uName like ('__大') + +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 +select * from bbsTopic where tTime>='2008-9-10 12:00:00' and tCount>10 + +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tUID , tCount from bbsTopic where tTitle like '%!' + +--1.查询出每个版块的版主编号,版主姓名和版块名称 + select uIDD,UName,sIDD from bbsUsers + inner join bbsSection on bbsUsers.uIDD = bbsSection.sIDD + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select uIDD,uName,tTitle,tMsg,tTime from bbsUsers +inner join bbsTopic on bbsUsers.uIDD=bbsTopic.tUID +where tTime >= '2008-09-15' + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select uIDD,UName,sName from bbsUsers + inner join bbsSection on bbsUsers.uIDD = bbsSection.sIDD + where UAge<20 + + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select top 1 uIDD,uName,tTitle,tMsg,tCount from bbsUsers +inner join bbsTopic on bbsUsers.uIDD = bbsTopic.tUID +order by TCount desc + + +--5.在主贴表中查询每个版块中每个用户的发帖总数 + select sName,uName,count(tCount) from bbsTopic + inner join bbsUsers on bbsTopic.uIDD = bbsTopic.tUID + group by uIDD,tUID \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/Student.sql" "b/20210326\344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/Student.sql" new file mode 100644 index 0000000000000000000000000000000000000000..762adf50535070e4e4991e505f7aa2429190a3b7 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/Student.sql" @@ -0,0 +1,47 @@ + +--统计每个班的男生数 +select '男生的数量',count(StuSex) from StuInfo group by StuSex having StuSex='男' + + +--统计每个班的男、女生数 + +select ClassId,'男生的数量',count(StuSex) from StuInfo where StuSex='男' group by ClassId +union +select ClassId,'女生的数量',count(StuSex) from StuInfo where StuSex='女' group by ClassId + + + + --统计每个班的男、女生数 + +select ClassId,Stusex,count(StuSex) from StuInfo group by ClassId,StuSex + + + --统计每个班的福建人数 +select ClassId,count(StuProvince)福建省的人数 from StuInfo group by ClassId,StuProvince having StuProvince='福建省' + + +--统计每个班的各个省的总人数 +select ClassId,StuProvince,count(StuProvince) from StuInfo group by ClassId,StuProvince order by ClassId + + +--统计每个省的女生数 +select StuProvince,count(StuSex)女生人数 from StuInfo where StuSex='女' group by StuProvince + + +--统计每个省的男、女生数 +select StuProvince,StuSex,count(StuSex)人数 from StuInfo group by StuProvince,StuSex + + +--统计每个学生的考试总分、平均分 +select StuId 学号,sum(Score)总分,avg(Score)平均分 from Scores group by StuId + +--统计出考试总分大于620的学生的考试总分 +select StuId 学号,sum(Score)总分 from Scores group by StuId having sum(Score)>620 + + +--统计出每门考试成绩最高分和最低分 +select CourseId,max(Score)最高分,min(Score)最低分 from Scores group by CourseId + + +--统计出每个学生的各门成绩的平均分 +select StuId,CourseId,avg(score) from Scores group by StuId,CourseId order by StuId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/bbs.sql" "b/20210326\344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/bbs.sql" new file mode 100644 index 0000000000000000000000000000000000000000..21ffe32b5c87a68a65a82b50a9d85a9f76f4b1cf --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/bbs.sql" @@ -0,0 +1,123 @@ +use master +create database bbs +on +( + name='bbs', + filename='D:\Demo\bbs.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='bbs_log', + filename='D:\Demo\bbs_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go +use bbs +create table bbsUser +( + uID int identity(1,1) not null, + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) + +create table bbsSection +( + sID int identity(1,1) not null , + sName varchar(10) not null, + sUid int + +) +--添加约束 +-- alter table 表名 add constraint 约束名 约束类型 +alter table bbsUser add constraint PK_bbsUser_uID primary key(uID) +alter table bbsUser add constraint UK_bbsUser_uName unique(uName) +alter table bbsUser add constraint CK_bbsUser_uSex check(uSex='男' or uSex='女') +alter table bbsUser add constraint CK_bbsUser_uAge check(uAge>=15 and uAge<=60) +alter table bbsUser add constraint CK_bbsUser_uPoint check(uPoint>=0) + +alter table bbsSection add constraint PK_bbsSection_sID primary key(sID) +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUser(uID) + + +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int references bbsUser(uID), + tSID int references bbsSection(sID), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) +create table bbsReply +( + rID int identity(1,1) primary key, + rUID int references bbsUser(uID), + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +insert into bbsUser (uName,uSex,uAge,uPoint) values ('小雨点','女','20','0') +,('逍遥','男','18','4'),('七年级生','男','19','2') + +select uName,uPoint into bbsPoint from bbsUser + +insert into bbsSection (sName,sUid) values ('技术交流',1), +('读书世界',3),('生活百科',1),('八卦区',3) + +insert into bbsTopic (tUID,tSID,tTitle,tMsg,tTime,tCount) values ('2','4','范跑跑','谁是范跑跑','2008-7-8','1'), +('3','1','.NET','谁是范跑跑','2008-9-1','2'),('1','3','.今年夏天最流行什么呀?','有谁知道今年夏天最流行','2008-9-10','0') + +insert into bbsReply (rUID,rMsg,rTime) values ('1','范跑跑是赵家俊小名','2021-1-1'), +('2','名字不一样','2021-1-2'),('3','EMO','2021-1-3') + + +select * from bbsUser +select * from bbsSection +select * from bbsTopic +select * from bbsReply + + +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select sUid,uName,sName from bbsUser inner join bbsSection on bbsUser.uID=bbsSection.sUid + + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 + +select uID,uName,tTitle,tMsg,tTime from bbsTopic inner join bbsUser on bbsTopic.tSID=bbsUser.uID where tTime>'2008-9-1' + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sID,uName,sName,uAge from bbsUser inner join bbsSection on bbsUser.uID=bbsSection.sUid where uAge<20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select max(tCount) from bbsTopic +select uID,uName,tTitle,tMsg,tCount from bbsTopic inner join bbsUser on bbsTopic.tSID=bbsUser.uID where tCount= (select max(tCount) from bbsTopic) + +--5.在主贴表中查询每个版块中每个用户的发帖总数 + +select tsid,count(tSID)from bbsTopic group by tUID,tSID + + + + + + + + + + + + + + + + + diff --git "a/20210326\344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/shopping.sql" "b/20210326\344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/shopping.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e29a48552eefb3638ba52e988884c5df6f2ad86c --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/shopping.sql" @@ -0,0 +1,101 @@ +use master +go + +create database shopping +on +( + name='shopping', + filename='D:\Demo\shopping.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='shopping_log', + filename='D:\Demo\shopping_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use shopping +go + +create table orders +( + orderId int primary key identity(1,1) , + orderDate datetime default(getdate()) +) + +create table orderItem +( + ItemiD int primary key identity(1,1), + orderId int references orders(orderId) not null, + itemType nvarchar(4) check(itemType='文具' or itemType='日常用品' or itemType='体育用品' ) not null, + itemName nvarchar(4) not null, + theNumber int not null, + theMoney money not null +) + +insert into orders values (default),(default),(default),(default) + + +insert into orderItem (orderId,itemType,itemName,theNumber,theMoney) values (1 ,'文具',' 笔' ,72, 2) +,(1,'文具','尺',10,1) +,(1,'体育用品','篮球',1,56) +,(2,'文具','笔',36,2) +,(2,'文具','固体胶',20,3) +,(2,'日常用品','透明胶',2,1) +,(2,'体育用品','羽毛球',20,3) +,(3,'文具','订书机',20,3) +,(3,'文具','订书针',10,3) +,(3,'文具','裁纸刀',5,5) +,(4,'文具','笔',20,2) +,(4,'文具','信纸',50,1) +,(4,'日常用品','毛巾',4,5) +,(4,'日常用品','透明胶',30,1) +,(4,'体育用品','羽毛球',20,3) + + + +select * from orderItem +select * from orders + + + +--使用上次作业的订单数据库,完成下列题目: + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select orders.orderId,orderDate,itemType,itemName,theNumber,theMoney from orders inner join orderItem on orders.orderId=orderItem.orderId + + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orders.orderId,orderDate,itemType,itemName from orders inner join orderItem on orders.orderId=orderItem.orderId where theNumber>=50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderId,orderDate,itemType,itemName,theNumber,theMoney,sum(theMoney*theNumber)订购总价 from orders inner join orderItem on orders.orderId=orderItem.orderId group by theMoney,orders.orderId,orderDate,itemType,itemName,theNumber,theMoney + + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderId,orderDate,itemType,itemName,theNumber,theMoney,sum(theMoney*theNumber)订购总价 from orders inner join orderItem on orders.orderId=orderItem.orderId group by theMoney,orders.orderId,orderDate,itemType,itemName,theNumber,theMoney having theMoney>4 and theNumber>=3 + + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orders.orderId,theNumber from orderItem inner join orders on orderItem.orderId=orders.orderId + +--6.查询 每个订单里的 每个类别的产品 分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + +select orderId,itemType,count(theNumber) from orderItem group by orderId,itemType order by orderId diff --git "a/20210326\344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/stuInfo.sql" "b/20210326\344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/stuInfo.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9bc9386cfbe69b61faf374cb72107f0cae3d435a --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\225\206\350\265\242\346\227\255/stuInfo.sql" @@ -0,0 +1,99 @@ +create database Student +on +( + name='Stuinfo', + filename='D:\Demo\Stuinfo.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% + +) +log on +( + name='Stuinfo_log', + filename='D:\Demo\Stuinfo_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% + +) +go +use Student + +create table Stuinfo +( + stuNO varchar(5) unique not null, + stuName nvarchar(20), + stuAge int, + stuAddress nvarchar(20), + stuSeat int identity(1,1) unique not null, + stuSex nchar(1) check(stuSex='男' or stuSex='女') +) +go + +create table Scoreinfo +( + examNO int primary key identity(1,1), + stuNO varchar(5) references Stuinfo(stuNO), + writtenExan int, + labExam int, +) + +insert into Stuinfo values ('s2501','张秋利','20','美国硅谷','女'),('s2502','李斯文',18,'湖北武汉','男'),('s2503','马文才',22,'湖南长沙','女'), +('s2504','欧阳俊雄',21,'湖北武汉','男'),('s2505','梅超风',20,'湖北武汉','女'),('s2506','陈旋风',19,'美国硅谷','女'),('s2507','陈风',20,'美国硅谷','男') + + +insert into Scoreinfo values('s2501',50,70),('s2502',60,65),('s2503',86,85),('s2504',40,80),('s2505',70,90),('s2506',85,90) + + + +select * from Scoreinfo +select * from Stuinfo + + + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName,stuAge,writtenExan,labExam from Stuinfo inner join Scoreinfo on Scoreinfo.stuNO =Stuinfo.stuNO + + + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select Scoreinfo.stuNO,stuName,writtenExan,labExam from Stuinfo inner join Scoreinfo on Scoreinfo.stuNO =Stuinfo.stuNO where writtenExan>60 and labExam>60 + + + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select Scoreinfo.stuNO,stuName,writtenExan,labExam from Stuinfo left join Scoreinfo on Scoreinfo.stuNO =Stuinfo.stuNO + + + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName,stuAge,writtenExan,labExam from Stuinfo inner join Scoreinfo on Scoreinfo.stuNO =Stuinfo.stuNO where stuAge>20 order by writtenExan desc + + +--5.查询男女生的机试平均分 +select stuSex,avg(labExam)机试平均分 from Stuinfo inner join Scoreinfo on Scoreinfo.stuNO =Stuinfo.stuNO group by stuSex + + +--6.查询男女生的笔试总分 + +select stuSex,sum(writtenExan)笔试平均分 from Stuinfo inner join Scoreinfo on Scoreinfo.stuNO =Stuinfo.stuNO group by stuSex + + + + + + + + + + + + + + + + + + + diff --git "a/20210326\344\275\234\344\270\232/\345\274\240\345\256\217/SQLQuery5.sql" "b/20210326\344\275\234\344\270\232/\345\274\240\345\256\217/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3d91e067bce4320f8faa1b7914d41cc8f6b665a5 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\274\240\345\256\217/SQLQuery5.sql" @@ -0,0 +1,117 @@ +use master +go + +create database bbs +on +( + name='bbs', + filename='D:\bbs.mdf', + size=10mb, + maxsize=100mb, + filegrowth=10mb +) + +log on +( + name='bbs_log', + filename='D:\bbs_log.mdf', + size=10mb, + maxsize=100mb, + filegrowth=10mb +) +go + +use bbs + +go + +create table bbsUsers +( + UID int primary key identity(1,1), + uName varchar(10) unique not null, + uSex varchar(2) check(uSex='男' or uSex='女') not null, + uAge int check(uAge>15 and uAge<60) not null, + uPoint int check(uPoint>=0) not null +) + +create table bbsSection +( + sID int primary key identity(1,1), + sName varchar(10) not null, + sUid int foreign key references bbsUsers(UID), +) + +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int references bbsUsers(UID), + tSID int references bbsSection(sID), + tTitle varchar(100) not null, + tMsg text not null, + Time datetime, + tCount int +) + +create table bbsReply +( + rID int primary key identity(1,1), + rUID int references bbsUsers (UID), + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +--1.现在有3个会员注册成功,请用一次插入多行数据的方法向bbsUsers表种插入3行记录,记录值如下: +-- 小雨点 女 20 0 +-- 逍遥 男 18 4 +-- 七年级生 男 19 2 +insert into bbsUsers values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) +select * from bbsUsers + + + +--2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中,提示查询部分列:select 列名1,列名2 from 表名 +select uName,uPoint into bbsPoint from bbsUsers +insert into bbsPoint select uName,uPoint from bbsUsers +select * from bbsPoint + +-- 读书世界 七年级生 +-- 生活百科 小雨点 +-- 八卦区 七年级生 +insert into bbsSection values('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) +select * from bbsSection +select * from bbsPoint + +--4.向主贴和回帖表中添加几条记录 + + --主贴: + + --发帖人 板块名 帖子标题 帖子内容 发帖时间 回复数量 + --逍遥 八卦区 范跑跑 谁是范跑跑 2008-7-8 1 + --七年级生 技术交流 .NET 与JAVA的区别是什么呀? 2008-9-1 2 + --小雨点 生活百科 今年夏天最流行什么 ’有谁知道今年夏天最流行‘ 2008-9-10 0 + -- 什么呀? +insert into bbsTopic values(2,4,'范跑跑','谁是范跑跑',' 2008-7-8 ',1),(6,1,'NET','与JAVA的区别是什么呀?','2008-9-1',2) +insert into bbsTopic values(4,3,'今年夏天最流行什么 ','有谁知道今年夏天最流行',' 2008-9-10',0) + +--insert into bbsTopic values (2,5,'范跑跑','谁是范跑跑','2008-7-8',1) +--insert into bbsTopic values (3,2,'.NET','与JAVA的区别是什么呀?','2008-9-1',2),(1,4,'今年夏天最流行什么','有谁知道今年夏天最流行','2008-9-10',0) +select * from bbsTopic + + --回帖: + -- 分别给上面三个主贴添加对应的回帖,回帖的内容,时间,回帖人自定 + +--在论坛数据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select UID,uName,uPoint from bbsUsers group by UID +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID,tTitle,tMsg,Time from bbsTopic where Time>2008-9-15 +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select UID,uName from bbsUsers where uAge<20 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select tUID,tTitle,tMsg,tCount from bbsTopic order by tCount DESC +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select tID,tUID,sum(tCount) from bbsTopic group by tID,tUID + + + diff --git "a/20210326\344\275\234\344\270\232/\345\274\240\345\256\217/SQLQuery6.sql" "b/20210326\344\275\234\344\270\232/\345\274\240\345\256\217/SQLQuery6.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cc7ca85cc819b63c76844e1963c7396fd13c301d --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\274\240\345\256\217/SQLQuery6.sql" @@ -0,0 +1,96 @@ +use master +go + +create database DEMO +on +( + name='DEMO' + filename='D:\bank\Demo3.mdf', + size=10mb, + maxsize=100mb, + filegrowth=10mb, +) + +log on +( + name='DEMO_log' + filename='D:\bank\Demo3_log.ldf', + size=10mb, + maxsize=100mb, + filegrowth=10mb, +) +go + +use DEMO +go + +create table orders +( + orderId int primary key identity(1,1), + orderDate datetime default(getdate()), +) + +create table orderItem +( + ItemiD int primary key identity(1,1), + orderId int foreign key references orders(orderId), + itemType nvarchar(10) not null, + itemName nvarchar(20) not null, + theNumber varchar(20) not null, + theMoney money not null, +) + +insert into orders(orderId,orderDate) values(1,2008-01-12),(2,2008-02-10),(3,2008-02-15),(4,2008-03-10) +insert into orders(ItemiD,orderId,itemType,itemName,theNumber,theMoney) values(1,1,'文具','笔','72',2) + +select * from orders +select * from orderItem + + +--1.查询所有订单订购的所有物品数量总和 +select sum(orderId) from orders + +--2.查询 每个订单订购的所有物品的 数量和 以及 平均单价 订单编号小于3的,平均单价小于10 +select orderItem,avg(theMoney)平均单价,sum(theNumber) from orderItem where orderId<3 group by orderId having avg(theMoney)<10 + +--3.查询 每个订单订购的所有物品 数量和 以及 平均单价 平均单价小于10并且总数量大于 50 +select orderItem,avg(theMoney)平均单价 from orderItem where avg(theMoney)<10 group by theMoney having avg(theNumber)>50 + +--4.查询每种类别的产品分别订购了几次, +select + +--5.查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 +select itemType,avg(theMoney)平均单价 from orderItem where sum(theNumber)>100 + +--6.查询每种产品的订购次数,订购总数量和订购的平均单价 +select itemName,sum(orderId) ,avg(theMoney)from orderItem group by itemName + +--使用上次作业的订单数据库,完成下列题目: + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select * from orderItem + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orderId,itemType,itemName from orderItem where orderId>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select *,sum(theMoney) from orderItem + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select *,sum(theMoney) from orderItem where theMoney>5 and theNumber>50 + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderId,theNumber from orderItem group by orderId + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: +select orderId,itemType,sum(theNumber),orderId from orderItem group by orderId,itemType + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\274\240\345\256\217/SQLQuery7.sql" "b/20210326\344\275\234\344\270\232/\345\274\240\345\256\217/SQLQuery7.sql" new file mode 100644 index 0000000000000000000000000000000000000000..63df0ab15610cc89c11b7890ff7d8dfbd72c205a --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\274\240\345\256\217/SQLQuery7.sql" @@ -0,0 +1,80 @@ +create database Student +on +( + name='Student', + filename='D:\Student.mdf', + size=10mb, + maxsize=100mb, + filegrowth=10mb + ) + +log on +( + name='Student_log', + filename='D:\Student_log.ldf', + size=10mb, + maxsize=100mb, + filegrowth=10mb + ) +go +use Student +go + +create table ClassInfo +( + ClassId int primary key identity(1,1), + ClassName int unique not null, + OpenTime int not null, + ClassDescribe text +) + +go +use Student +go + +create table StudentInfo +( + StudentId int primary key identity(1,1), + StudentName varchar(10) check(StudentName>2) unique, + StudentSex varchar(2) check(StudentSex=' ' or StudentSex='1') default(' ') not null, + StudentAge int check(StudentAge>15 or StudentAge<40) not null, + StudentAddress text default('湖北武汉'), + StudentClassId int references ClassInfo(ClassId), +) + +create table CourseInfo +( + CourseId int primary key identity(1,1), + CourseName varchar(20) unique not null, + CoueseDescribe text + +) + +create table PerformanceInfo +( + PerformanceId int primary key identity(1,1), + PerformanceStudentNumber int references StudentInfo(StudentId) not null, + PerformanceClassNumber int references CourseInfo(CourseId) not null, + performance int check(performance>0 or performance<100) +) + + +--数据如图片1,使用上次作业的数据 + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select StudentName,StudentAge from StudentInfo + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select StudentName from StudentInfo where elam>60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select StudentName from StudentInfo + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select StudentName,StudentAge from StudentInfo where StudentAge>20 order by elam DESC + +--5.查询男女生的机试平均分 +select StudentSex,avg(elam) from StudentInfo group by StudentSex + +--6.查询男女生的笔试总分 +select StudentSex,sum(elam) from StudentInfo StudentSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7869cfc4a796bcd0d011de12fff91b100aebbcb9 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery1.sql" @@ -0,0 +1,67 @@ +use master +go + +create database Studen +on +( + name=Studen, + filename='D:\Studen.mdf', + size=10MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name=Studen_log, + filename='D:\Studen_log.ldf', + size=10MB, + maxsize=50MB, + filegrowth=10% +) + +use Studen +go + +create table Stuinfo +( + stuNO varchar(5) unique not null, + stuName nvarchar(20), + stuAge int, + stuAddress nvarchar(20), + stuSeat int identity(1,1) unique not null, + stuSex nchar(1) check(stuSex='男' or stuSex='女') +) +go + +insert into Stuinfo values ('s2501','张秋利',20,'美国硅谷','女'),('s2502','李斯文',18,'湖北武汉','男'),('s2503','马文才',22,'湖南长沙','女'), +('s2504','欧阳俊雄',21,'湖北武汉','男'),('s2505','梅超风',20,'湖北武汉','女'),('s2506','陈旋风',19,'美国硅谷','女'),('s2507','陈风',20,'美国硅谷','男') + +create table Scoreinfo +( + examNO int primary key identity(1,1), + stuNO varchar(5) references Stuinfo(stuNO), + writtenExan int, + labExam int, +) + +insert into Scoreinfo values('s2501',50,70),('s2502',60,65),('s2503',86,85),('s2504',40,80),('s2505',70,90),('s2506',85,90) + +select StuName,stuAge,writtenExan,labExam from StuInfo +inner join Scoreinfo on Scoreinfo.stuNO = Stuinfo.stuNO + +select StuInfo .stuNO,StuName,writtenExan,labExam from StuInfo +inner join Scoreinfo on Scoreinfo.stuNO = Stuinfo.stuNO where writtenExan>'60' and labExam>'60' + +select StuInfo .stuNO,StuName,writtenExan,labExam from StuInfo +left join Scoreinfo on Scoreinfo.stuNO = Stuinfo.stuNO + +select StuName,stuAge,writtenExan,labExam from StuInfo +inner join Scoreinfo on Scoreinfo.stuNO = Stuinfo.stuNO where stuAge>='20' order by writtenExan DESC + +select stuSex,avg(labExam) from StuInfo +inner join Scoreinfo on Scoreinfo.stuNO = Stuinfo.stuNO +group by stuSex + +select stuSex,sum(writtenExan) from StuInfo +inner join Scoreinfo on Scoreinfo.stuNO = Stuinfo.stuNO +group by stuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9cbb2c748ca46e70e909c4a9deef498ab5a384c2 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery2.sql" @@ -0,0 +1,34 @@ +use TestDB +go + + +--统计每个班的男生数 +select classid,count(stusex) from StuInfo group by classid,stusex having stusex='男' + +--统计每个班的男、女生数 +select classid,stusex,count(stusex)人数 from StuInfo group by classid,stusex +select * from StuInfo +--统计每个班的福建人数 +select classid,stuProvince,count(StuProvince)人数 from StuInfo group by classid,StuProvince having StuProvince='福建省' +select * from StuInfo + +--统计每个班的各个省的总人数 +select classid,stuProvince,count(*)人数 from StuInfo group by ClassId,StuProvince + +--统计每个省的女生数 +select StuProvince,stusex,count(*)人数 from StuInfo group by StuProvince,StuSex having StuSex='女' + +--统计每个省的男、女生数 +select StuProvince,StuSex,count(StuSex)人数 from StuInfo group by StuProvince,StuSex order by StuProvince + +--统计每个学生的考试总分、平均分 +select stuid,sum(score)总分,avg(Score)平均分 from Scores group by StuId + +--统计出考试总分大于620的学生的考试总分 +select stuid,sum(Score)总分 from Scores group by StuId having sum(Score)>620 + +--统计出每门考试成绩最高分和最低分 +select CourseId,max(score)最高分,min(score)最低分 from Scores group by CourseId + +--统计出每个学生的各门成绩的平均分 +select stuid,CourseId,avg(Score)平均分 from Scores group by StuId,CourseId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ec8b87e585a8a76680f334d99e54c332790b941f --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery3.sql" @@ -0,0 +1,46 @@ +use master +go + +create database zuoye +go + +use zuoye +go +create table orders +( + orderid int primary key identity(1,1), + orderDate datetime +) + +create table orderItem +( + itemID int primary key identity(1,1), + orderid int references orders(orderid), + itemType varchar(10), + itemName varchar(20), + theNumber int, + theMoney decimal(37,2) +) +insert into orders values('2008-01-12'),('2008-02-10'),('2008-02-15'),('2008-03-10') +insert into orderItem values(1,'文具','笔','72','2'),(1,'文具','尺','10','1'),(1,'体育用品','篮球','1','56'),(2,'文具','笔','36','2'), +(2,'文具','固体胶','20','3'),(2,'日常用品','透明胶','2','1'),(2,'体育用品','羽毛球','20','3'),(3,'文具','订书机','20','3'),(3,'文具','订书机','10','3'), +(3,'文具','裁纸刀','5','5'),(4,'文具','笔','20','2'),(4,'文具','信纸','50','1'),(4,'日常用品','毛巾','4','5'),(4,'日常用品','透明球','30','1'), +(4,'体育用品','羽毛球','20','3') + +select itemID,orderDate,itemType,itemName,theNumber,theMoney from orderItem +inner join orders on orderItem.orderid = orders.orderid + +select theNumber,itemID,orderDate,itemType,itemName from orderItem +inner join orders on orderItem.orderid = orders.orderid where theNumber>'50' + +select itemID,orderDate,itemType,itemName,theMoney,(theNumber*theMoney)订购总价 from orderItem +inner join orders on orderItem.orderid = orders.orderid + +select itemID,orderDate,itemType,itemName,theMoney,(theNumber*theMoney)订购总价 from orderItem +inner join orders on orderItem.orderid = orders.orderid where theMoney>='5' and theNumber>='50' + +select orderid,count(orderid) 订购产品数 from orderItem +group by orderid + +select orderid,itemType,count(orderid) 订购次数,sum(theNumber) from orderItem +group by orderid,itemType \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery4.sql" "b/20210326\344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b890845fccd20a323ed0bd3fa26712a8a686da39 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\274\240\351\221\253/SQLQuery4.sql" @@ -0,0 +1,88 @@ +create database bbs + +go +use bbs +go + +create table bbsUsers +( + bbbsUID int identity(1,1), + uName varchar(10) not null, + uSex varchar(2) not null , + uAge int not null , + uPoint int not null +) +go + +alter table bbsUsers add constraint PK_bbbsUID primary key (bbbsUID) +alter table bbsUsers add constraint UK_uName unique(uName) +alter table bbsUsers add constraint CK_uSex check(uSex in('男','女')) +alter table bbsUsers add constraint CK_uAge check(uAge>14 and uAge<61 ) +alter table bbsUsers add constraint CK_uPoint check(len(uPoint)>=0) +go + +create table bbsSection +( + bbssID int identity(1,1), + sName varchar(10) not null, + sUid int +) +go + +alter table bbsSection add constraint PK_bbssID primary key (bbssID) +go + +alter table bbsSection add constraint FK_sUid foreign key (sUid) references bbsUsers(bbbsUID) +go + +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int foreign key references bbsUsers(bbbsUID), + tSID int foreign key references bbsSection(bbssID), + tTitle varchar(100) not null, + tMsg text, + tTime datetime default(getdate()), + tCount int +) +go + + +create table bbsReply +( + rID int primary key identity(1,1), + rUID int foreign key references bbsUsers(bbbsUID), + rTID int foreign key references bbsTopic(tID), + rMsg text NOT NULL, + rTime datetime default(getdate()) +) +go + +insert into bbsUsers(uName,uSex,uAge,uPoint) values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) +select uName,uPoint into bbsPoint from bbsUsers +insert into bbsSection (sName,sUid) values('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) + +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values(2 ,4,'范跑跑','谁是范跑跑 ',2008-7-8,1) +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values(3 ,1,'.NET','与JAVA的区别是什么呀? ',2008-9-1,2) +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values(1 ,3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀 ',2008-9-10,0) + +insert into bbsReply(rUID,rTID,rMsg,rTime) values(1 ,1,'不认识',2008-7-11) +insert into bbsReply(rUID,rTID,rMsg,rTime) values(1 ,2,'没有区别',2008-9-11) +insert into bbsReply(rUID,rTID,rMsg,rTime) values(2 ,2,'请百度',2008-9-12) + +select sUid,uName,sName +from bbsUsers BU inner join bbsSection BS on BS.bbssID=BU.bbbsUID + +select tTime,tUID,uName,tTitle,tMsg +from bbsTopic inner join bbsUsers on bbsTopic.tUID=bbsUsers.bbbsUID where tTime>'2008-9-15' + +select sUid,uName,sName,uAge +from bbsUsers BU inner join bbsSection BS on BS.bbssID=BU.bbbsUID where uAge<20 + +select rUID,uName,tTitle,tMsg,tCount +from bbsTopic inner join bbsReply on bbsReply.rUID=bbsTopic.tUID +inner join bbsUsers on bbsUsers.bbbsUID=bbsTopic.tUID + +select uName,count(tUID) +from bbsTopic inner join bbsUsers on bbsUsers.bbbsUID=bbsTopic.tUID +group by tUID,bbsUsers.uName diff --git "a/20210326\344\275\234\344\270\232/\345\276\220\344\277\212\351\271\217/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\345\276\220\344\277\212\351\271\217/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a5ee43624011566c4eabb2fc5a452ad206ce5790 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\276\220\344\277\212\351\271\217/SQLQuery1.sql" @@ -0,0 +1,472 @@ +USE [master] +GO +/****** Object: Database [TestDB] Script Date: 2021/3/15 16:11:24 ******/ +CREATE DATABASE [TestDB] + CONTAINMENT = NONE + ON PRIMARY +( NAME = N'TestDB', FILENAME = N'D:\TestDB.mdf' , SIZE = 4288KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) + LOG ON +( NAME = N'TestDB_log', FILENAME = N'D:\TestDB_log.ldf' , SIZE = 1072KB , MAXSIZE = 2048GB , FILEGROWTH = 10%) +GO +ALTER DATABASE [TestDB] SET COMPATIBILITY_LEVEL = 120 +GO +IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled')) +begin +EXEC [TestDB].[dbo].[sp_fulltext_database] @action = 'enable' +end +GO +ALTER DATABASE [TestDB] SET ANSI_NULL_DEFAULT OFF +GO +ALTER DATABASE [TestDB] SET ANSI_NULLS OFF +GO +ALTER DATABASE [TestDB] SET ANSI_PADDING OFF +GO +ALTER DATABASE [TestDB] SET ANSI_WARNINGS OFF +GO +ALTER DATABASE [TestDB] SET ARITHABORT OFF +GO +ALTER DATABASE [TestDB] SET AUTO_CLOSE OFF +GO +ALTER DATABASE [TestDB] SET AUTO_SHRINK OFF +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS ON +GO +ALTER DATABASE [TestDB] SET CURSOR_CLOSE_ON_COMMIT OFF +GO +ALTER DATABASE [TestDB] SET CURSOR_DEFAULT GLOBAL +GO +ALTER DATABASE [TestDB] SET CONCAT_NULL_YIELDS_NULL OFF +GO +ALTER DATABASE [TestDB] SET NUMERIC_ROUNDABORT OFF +GO +ALTER DATABASE [TestDB] SET QUOTED_IDENTIFIER OFF +GO +ALTER DATABASE [TestDB] SET RECURSIVE_TRIGGERS OFF +GO +ALTER DATABASE [TestDB] SET ENABLE_BROKER +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF +GO +ALTER DATABASE [TestDB] SET DATE_CORRELATION_OPTIMIZATION OFF +GO +ALTER DATABASE [TestDB] SET TRUSTWORTHY OFF +GO +ALTER DATABASE [TestDB] SET ALLOW_SNAPSHOT_ISOLATION OFF +GO +ALTER DATABASE [TestDB] SET PARAMETERIZATION SIMPLE +GO +ALTER DATABASE [TestDB] SET READ_COMMITTED_SNAPSHOT OFF +GO +ALTER DATABASE [TestDB] SET HONOR_BROKER_PRIORITY OFF +GO +ALTER DATABASE [TestDB] SET RECOVERY FULL +GO +ALTER DATABASE [TestDB] SET MULTI_USER +GO +ALTER DATABASE [TestDB] SET PAGE_VERIFY CHECKSUM +GO +ALTER DATABASE [TestDB] SET DB_CHAINING OFF +GO +ALTER DATABASE [TestDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) +GO +ALTER DATABASE [TestDB] SET TARGET_RECOVERY_TIME = 0 SECONDS +GO +ALTER DATABASE [TestDB] SET DELAYED_DURABILITY = DISABLED +GO +EXEC sys.sp_db_vardecimal_storage_format N'TestDB', N'ON' +GO +USE [TestDB] +GO +/****** Object: Table [dbo].[ClassInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[ClassInfo]( + [ClassId] [int] IDENTITY(1,1) NOT NULL, + [ClassName] [nvarchar](20) NOT NULL, +PRIMARY KEY CLUSTERED +( + [ClassId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[CourseInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[CourseInfo]( + [CourseId] [int] IDENTITY(1,1) NOT NULL, + [CourseName] [nvarchar](50) NOT NULL, + [CourseCredit] [int] NULL DEFAULT ((1)), +PRIMARY KEY CLUSTERED +( + [CourseId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[Scores] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[Scores]( + [ScoreId] [int] IDENTITY(1,1) NOT NULL, + [StuId] [int] NULL, + [CourseId] [int] NULL, + [Score] [int] NULL DEFAULT ((0)), +PRIMARY KEY CLUSTERED +( + [ScoreId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[StuInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[StuInfo]( + [StuId] [int] IDENTITY(1,1) NOT NULL, + [ClassId] [int] NULL, + [StuName] [nvarchar](10) NOT NULL, + [StuSex] [nvarchar](1) NULL DEFAULT ('男'), + [StuBrithday] [date] NULL, + [StuPhone] [nvarchar](11) NULL, + [StuProvince] [nvarchar](200) NULL, + [CreateDate] [datetime] NULL DEFAULT (getdate()), + [StuAge] [int] NULL, +PRIMARY KEY CLUSTERED +( + [StuId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] ON + +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (1, N'软件1班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (2, N'软件2班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (3, N'软件3班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (4, N'软件4班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (5, N'软件5班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (6, N'软件6班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (7, N'软件7班') +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] ON + +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (1, N'计算机基础', 3) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (2, N'HTML+CSS网页制作', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (3, N'JAVA编程基础', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (4, N'SQL Server数据库基础', 4) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (5, N'C#面向对象编程', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (6, N'Winform桌面应用程序设计', 5) +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[Scores] ON + +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (1, 1, 1, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (2, 1, 2, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (3, 1, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (4, 1, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (5, 2, 1, 60) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (6, 2, 2, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (7, 2, 3, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (8, 2, 4, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (9, 3, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (10, 3, 2, 45) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (11, 3, 3, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (12, 3, 4, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (13, 4, 1, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (14, 4, 2, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (15, 4, 3, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (16, 4, 4, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (17, 5, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (18, 5, 2, 79) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (19, 5, 3, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (20, 5, 4, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (21, 6, 1, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (22, 6, 2, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (23, 6, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (24, 6, 5, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (25, 7, 1, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (26, 7, 2, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (27, 7, 3, 92) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (28, 7, 5, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (29, 8, 1, 58) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (30, 8, 2, 59) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (31, 8, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (32, 8, 5, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (33, 9, 1, 48) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (34, 9, 2, 67) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (35, 9, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (36, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (37, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (38, 1, 1, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (39, 1, 2, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (40, 1, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (41, 1, 4, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (42, 2, 1, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (43, 2, 2, 82) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (44, 2, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (45, 2, 4, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (46, 3, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (47, 3, 2, 50) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (48, 3, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (49, 3, 4, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (50, 4, 1, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (51, 4, 2, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (52, 4, 3, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (53, 4, 4, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (54, 5, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (55, 5, 2, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (56, 5, 3, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (57, 5, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (58, 6, 1, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (59, 6, 2, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (60, 6, 3, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (61, 6, 5, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (62, 7, 1, 89) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (63, 7, 2, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (64, 7, 3, 97) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (65, 7, 5, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (66, 8, 1, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (67, 8, 2, 64) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (68, 8, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (69, 8, 5, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (70, 9, 1, 53) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (71, 9, 2, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (72, 9, 3, 76) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (73, 9, 5, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (74, 9, 5, 61) +GO +SET IDENTITY_INSERT [dbo].[Scores] OFF +GO +SET IDENTITY_INSERT [dbo].[StuInfo] ON + +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (1, 1, N'刘正', N'男', CAST(N'2002-08-02' AS Date), N'13245678121', N'广西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (2, 1, N'黄贵', N'男', CAST(N'2003-07-02' AS Date), N'13345678121', N'江西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (3, 1, N'陈美', N'女', CAST(N'2002-07-22' AS Date), N'13355678125', N'福建省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (4, 2, N'江文', N'男', CAST(N'2001-07-02' AS Date), N'13347678181', N'湖南省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (5, 2, N'钟琪', N'女', CAST(N'2004-01-13' AS Date), N'13345778129', N'安徽省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (6, 3, N'曾小林', N'男', CAST(N'2005-05-15' AS Date), N'13345378563', N'安徽省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (7, 3, N'欧阳天天', N'女', CAST(N'2000-08-19' AS Date), N'13347878121', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (8, 3, N'李逍遥', N'男', CAST(N'1999-09-02' AS Date), N'13345678557', N'广东省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 22) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (9, 4, N'刘德华', N'男', CAST(N'1995-06-11' AS Date), N'15345679557', N'福建省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 26) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (10, 4, N'刘翔', N'男', CAST(N'1996-07-09' AS Date), N'18346679589', N'江西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (11, 4, N'曾小贤', N'男', CAST(N'2003-07-02' AS Date), N'18348979589', N'湖南省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (12, 5, N'刘德华', N'男', CAST(N'2002-07-02' AS Date), N'18348979509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (13, 5, N'陈天翔', N'男', CAST(N'2003-07-02' AS Date), N'18348079509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (14, 5, N'刘能', N'男', CAST(N'2005-08-02' AS Date), N'13245678122', N'广西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (15, 5, N'钟馗', N'男', CAST(N'2004-08-02' AS Date), N'13245678123', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (16, 5, N'钟吴艳', N'女', CAST(N'2002-08-02' AS Date), N'13245678124', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (17, 5, N'刘欢', N'男', CAST(N'2001-07-02' AS Date), N'13245678125', N'湖南省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (18, 5, N'张庭', N'女', CAST(N'2000-07-02' AS Date), N'13245678126', N'江西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (19, 5, N'曹植', N'男', CAST(N'2000-08-02' AS Date), N'13245678127', N'福建省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (20, 5, N'曹操', N'男', CAST(N'2002-08-02' AS Date), N'13245678128', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (21, 5, N'孙尚香', N'女', CAST(N'2003-08-02' AS Date), N'13245678129', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (22, 3, N'老1', N'女', CAST(N'2002-08-02' AS Date), N'13245678130', N'广东省', CAST(N'2021-03-14 17:02:36.347' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (24, 2, N'老2', N'男', CAST(N'2002-08-03' AS Date), N'13345678945', NULL, CAST(N'2021-03-14 17:03:37.733' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (25, 4, N'老3', N'男', NULL, N'13645987545', N'广东省', CAST(N'2021-03-14 17:03:43.307' AS DateTime), NULL) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (28, 5, N'老4', N'男', CAST(N'2006-03-05' AS Date), N'13456987456', NULL, CAST(N'2021-03-14 17:04:03.957' AS DateTime), 15) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (29, 5, N'老5', N'女', CAST(N'1998-04-12' AS Date), N'15978456123', NULL, CAST(N'2021-03-14 17:04:47.103' AS DateTime), 23) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (30, 4, N'老6', N'男', CAST(N'1996-08-06' AS Date), N'18945674561', NULL, CAST(N'2021-03-14 17:05:04.990' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (31, 3, N'老7', N'女', CAST(N'1997-04-06' AS Date), N'18845678912', NULL, CAST(N'2021-03-14 17:05:20.570' AS DateTime), 24) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (32, 2, N'老10', N'女', CAST(N'1998-08-09' AS Date), N'19945645612', NULL, CAST(N'2021-03-14 17:06:08.107' AS DateTime), 23) +GO +SET IDENTITY_INSERT [dbo].[StuInfo] OFF +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__CourseIn__9526E2773AB7BECE] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[CourseInfo] ADD UNIQUE NONCLUSTERED +( + [CourseName] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__StuInfo__2D85FC63AF6FC6FA] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[StuInfo] ADD UNIQUE NONCLUSTERED +( + [StuPhone] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([CourseId]) +REFERENCES [dbo].[CourseInfo] ([CourseId]) +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([StuId]) +REFERENCES [dbo].[StuInfo] ([StuId]) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD FOREIGN KEY([ClassId]) +REFERENCES [dbo].[ClassInfo] ([ClassId]) +ON DELETE SET NULL +GO +ALTER TABLE [dbo].[CourseInfo] WITH CHECK ADD CHECK (([CourseCredit]>=(1) AND [CourseCredit]<=(5))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK ((len([StuPhone])=(11))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK (([StuSex]='女' OR [StuSex]='男')) +GO +USE [master] +GO +ALTER DATABASE [TestDB] SET READ_WRITE +GO +select * from CourseInfo +select * from ClassInfo +select * from Scores +select * from StuInfo + +--统计每个班的男生数 +select ClassId 班级, count(StuSex) 男生人数 from StuInfo where StuSex='男' group by ClassId +--统计每个班的男、女生数 +select ClassId 班级,StuSex 性别 ,count(StuSex) from StuInfo group by ClassId ,StuSex +--统计每个班的福建人数 +select ClassId 班级, count(StuProvince) 福建省人数 from StuInfo where StuProvince='福建省' group by ClassId +--统计每个班的各个省的总人数 +select ClassId 班级, StuProvince 省份,count(StuProvince) 人数 from StuInfo where StuProvince is not null and not StuProvince='' group by ClassId ,StuProvince order by ClassId +--统计每个省的女生数 +select StuProvince 省份,count(StuSex) 人数 from StuInfo where StuSex='女' and StuProvince is not null and not StuProvince='' group by StuProvince +--统计每个省的男、女生数 +select StuProvince 省份,StuSex 性别 ,count(StuSex) from StuInfo where StuProvince is not null and not StuProvince='' group by StuProvince ,StuSex order by StuProvince +--统计每个学生的考试总分、平均分 +select StuId 学生编号, sum(Score) 总分 , avg(Score) 平均分数 from Scores group by StuId +--统计出考试总分大于620的学生的考试总分 +select StuId 学生编号, sum(Score) 总分 from Scores group by StuId having sum(Score)>620 +--统计出每门考试成绩最高分和最低分 +select CourseId 学生编号, max(Score) 最高分,min(Score) 最低分 from Scores group by CourseId order by CourseId +--统计出每个学生的各门成绩的平均分 +select StuId 学生编号,CourseId 科目, avg(Score) 平均分数 from Scores group by StuId ,CourseId + + + + + + + + diff --git "a/20210326\344\275\234\344\270\232/\345\276\220\344\277\212\351\271\217/SQLQuery1.sql01.sql" "b/20210326\344\275\234\344\270\232/\345\276\220\344\277\212\351\271\217/SQLQuery1.sql01.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cb9ab36d5b20cea494f03c5221f08fe371f2d666 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\276\220\344\277\212\351\271\217/SQLQuery1.sql01.sql" @@ -0,0 +1,58 @@ +create database Student02 +on +( + name='Student02', + filename='D:\Student02.mdf', + size=20, + maxsize=300, + filegrowth=50 +) +log on +( + name='Student02_log', + filename='D:\Student02_log.ldf', + size=20, + maxsize=300, + filegrowth=50 +) +go +use Student02 +go +create table StudenIfo +( + stuNo int identity(2501,1) primary key, + stuName nvarchar(20) not null unique, + stuAge int null , + studdRess nvarchar(20) not null, + stuSeat int , + stuSext int check(stuSext in('1','0')) default('0') +) +insert into StudenIfo(stuName,stuAge,studdRess,stuSeat,stuSext ) values('张秋利','20','美国硅谷','1','1'),('李斯文','18','湖北武汉','2','0'),('马文才','22','湖南长沙','3','1'), +('欧阳俊雄','21','湖北武汉','4','0'),('梅超凤','20','湖北武汉','5','1'),('陈炫风','20','美国硅谷','6','1'),('陈风','20','美国硅谷','7','0') + +create table Class01 +( + examNo int identity(1,1) primary key, + stuNo char(20) null, + writtenExam int , + labExam int +) +insert into Class01(stuNo,writtenExam,labExam) values ('2501','50','70'),('2502','60','65'),('2503','86','85'),('2503','40','80'),('2504','70','90'),('2505','85','90') + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName 姓名 ,stuAge 年龄,WrittenExam 笔试成绩,labExam 机试成绩 from StudenIfo S inner join Class01 C on S.stuNo=C.stuNo +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select stuName 姓名 ,stuAge 年龄,WrittenExam 笔试成绩,labExam 机试成绩 from StudenIfo S inner join Class01 C on S.stuNo=C.stuNo where WrittenExam >60 and labExam>60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select examNo 学号, stuName 姓名 ,stuAge 年龄,WrittenExam 笔试成绩,labExam 机试成绩 from StudenIfo S left join Class01 C on S.stuNo=C.stuNo + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName 姓名 ,stuAge 年龄,WrittenExam 笔试成绩,labExam 机试成绩 from StudenIfo S inner join Class01 C on S.stuNo=C.stuNo where stuAge >=20 order by WrittenExam DESC +--5.查询男女生的机试平均分 +select stuSext 性别, AVG(labExam) 机试成绩 from StudenIfo S left join Class01 C on S.stuNo=C.stuNo group by stuSext +--6.查询男女生的笔试总分 +select stuSext 性别, sum(WrittenExam) 笔试总分 from StudenIfo S left join Class01 C on S.stuNo=C.stuNo group by stuSext + +select * from StudenIfo +select * from Class01 \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\276\220\344\277\212\351\271\217/SQLQuery3.sql02.sql" "b/20210326\344\275\234\344\270\232/\345\276\220\344\277\212\351\271\217/SQLQuery3.sql02.sql" new file mode 100644 index 0000000000000000000000000000000000000000..077612902dffc3ca797f594ee390532ab4a5c58b --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\276\220\344\277\212\351\271\217/SQLQuery3.sql02.sql" @@ -0,0 +1,66 @@ +use master +go +create database orderr +on +( + name='orderr', + filename= 'D:\orderr.mdf', + size=10, + maxsize=100, + filegrowth=15% +) +log on +( + name='orderr_log', + filename= 'D:\orderr_log.ldf', + size=10, + maxsize=100, + filegrowth=15% +) +go +use orderr +go +create table orders +( + orderId int primary key identity, + orderDate datetime default(getdate()) +) +create table orderItem +( + ItemiD int primary key identity, + orderId int references orders(orderId) , + itemType nvarchar(10), + itemName nvarchar(10), + theNumber int , + theMoney money +) +insert into orders values(default),(default),(default),(default) +select * from orders +insert into orderItem values ('1','文具','笔','72','2'),('1','文具','尺','10','1'),('1','体育用品','蓝球','1','56'),('2','文具','笔','36','2'),('2','文具','固体胶','20','3'), +('2','日常用品','透明胶','2','1'),('2','体育用品','羽毛球','20','3'),('3','文具','订书机','20','3'),('3','文具','订书针','10','3'),('3','文具','裁纸刀','5','5'), +('4','文具','笔','20','2'),('4','文具','信纸','50','1'),('4','日常用品','毛巾','4','5'),('4','日常用品','透明胶','30','1'),('4','体育用品','羽毛球','20','3') +select * from orderItem +select * from orders +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select OI.orderId 订单编号 , orderDate 订单日期 , itemType 产品类型 , itemName 产品名称 ,theNumber 订购数量 , theMoney 订单单价 from orderItem OI inner join orders O on OI.orderId =O.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select OI.orderId 订单编号 , orderDate 订单日期 , itemType 产品类型 , itemName 产品名称 from orderItem OI inner join orders O on OI.orderId =O.orderId where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select OI.orderId 订单编号 , orderDate 订单日期 , itemType 产品类型 , itemName 产品名称 ,theNumber 订购数量 , theMoney 订单单价,theNumber* theMoney 订购总价 from orderItem OI inner join orders O on OI.orderId =O.orderId +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select OI.orderId 订单编号 , orderDate 订单日期 , itemType 产品类型 , itemName 产品名称 ,theNumber 订购数量 , theMoney 订单单价,theNumber* theMoney 订购总价 from orderItem OI inner join orders O on OI.orderId =O.orderId +where theMoney>5 and theNumber>50 +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +select OI.orderId 订单编号 ,sum(theNumber) 订购产品数 from orderItem OI inner join orders O on OI.orderId =O.orderId group by OI.orderId + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: +select OI.orderId 订单编号 ,itemType 产品类型 ,count(itemType) 订购次数 ,sum(theNumber) 总数量 from orderItem OI inner join orders O on OI.orderId =O.orderId group by itemType ,OI.orderId + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\276\220\344\277\212\351\271\217/SQLQuery6.sql03.sql" "b/20210326\344\275\234\344\270\232/\345\276\220\344\277\212\351\271\217/SQLQuery6.sql03.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ecbe1ed59d3689138b3072ae5d64abc766b18769 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\276\220\344\277\212\351\271\217/SQLQuery6.sql03.sql" @@ -0,0 +1,98 @@ +use master +go +create database bbs +on +( + name='bbs', + filename='D:\bbs.mdf', + size=10, + maxsize=200, + filegrowth=20 +) +log on +( + name='bbs_log', + filename='D:\bbs_log.ldf ', + size=10, + maxsize=200, + filegrowth=20 +) +use bbs + create table bbsUsers + ( + UIDD int identity(1,1) not null , + uName nvarchar(10) not null, + uSex varchar(2) not null , + uAge int not null, + uPoint int not null + ) + alter table bbsUsers add constraint PK_bbsUsers_UIDD primary key(UIDD ) +alter table bbsUsers add constraint UK_bbsUsers_uName unique(uName) +alter table bbsUsers add constraint CK_bbsUsers_uSex check(uSex in('女','男')) +alter table bbsUsers add constraint CK_bbsUsers_uAge check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint CK_bbsUsers_uPoint check(uPoint>=0) + create table bbsTopic + ( + tID int primary key identity(1,1), + tUID int references bbsUsers(UIDD), + tSID int references bbsSection(sIDD), + tTitle nvarchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int + ) + create table bbsReply + ( + rID int primary key identity(1,1), + rUID int references bbsUsers(UIDD) , + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime, + ) + create table bbsSection + ( + sIDD int identity(1,1), + sName nvarchar(10), + sUid int , + ) + alter table bbsSection add constraint PK_bbsSection_sIDD primary key(sIDD) + alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUsers(UIDD) + + insert into bbsUsers(uName,uSex,uAge,uPoint) values ('小雨点','女','20','0'),('逍遥 ','男','18','4'),('七年级生','男','19','2') + + create table bbsPoint + ( + ID nvarchar(10) , + point int + ) + + -- + insert into bbsPoint(ID,point) select uName,uPoint from bbsUsers + + + -- + select uName,uPoint into bbsUsers2 from bbsUsers + + + insert into bbsSection(sName,sUid) values ('技术交流 ','1'),(' 读书世界 ','3'),('生活百科','1'),('八卦区 ','3') + insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values ('2','4','范跑跑 ','谁是范跑跑','2008-7-8 ','1'), + ('3','1','.NET','与JAVA的区别是什么呀?','2008-9-1','2'),('1','3','今年夏天最流行什么','有谁知道今年夏天最流行什么呀?',' 2008-9-10','0') + + insert into bbsReply(rUID,rMsg, rTime) values ('1','你是','2020.1.2'),('3','爱狗','2020.2.15'),('2','还是能','2020.5.25') + select * from bbsSection + select * from bbsUsers + select * from bbsTopic + select * from bbsReply + +--1.查询出每个版块的版块编号,版主姓名和版块名称 +select uName 版主姓名,sIDD 版块编号,sName 版块名称 from bbsSection C inner join bbsUsers B on C.sUid=B.UIDD +--2.查询出主贴的发帖时间在2008-8-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID 发帖人编号, uName 发帖人姓名,tTitle 帖子的标题 ,tMsg 帖子的内容 ,tTime 发帖时间 from bbsTopic BT inner join bbsUsers BU on BT.tUID =BU.UIDD where tTime>'2008-8-15' +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select uName 版主姓名,sUid 版主编号,sName 版块名称 from bbsSection C inner join bbsUsers B on C.sUid=B.UIDD where uAge >18 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select tUID 发帖人编号,uName 发帖人姓名,tTitle 主贴标题 ,tMsg 主贴内容,tCount 回复数量 from bbsTopic C inner join bbsUsers B on C.tUID =B.UIDD where tCount= (select max(tCount)from bbsTopic) +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select sName 板块名称,uName 用户名称 ,sum(tCount) 发帖总数 from bbsTopic inner join bbsSection on sIDD=tSID +inner join bbsUsers on tUID =UIDD group by sName ,uName + diff --git "a/20210326\344\275\234\344\270\232/\346\226\271\350\215\243\346\230\237/SQLQuery11.sql" "b/20210326\344\275\234\344\270\232/\346\226\271\350\215\243\346\230\237/SQLQuery11.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e9049c0c2391ac67ab518b7c78fc4e11fcfa85bd --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\226\271\350\215\243\346\230\237/SQLQuery11.sql" @@ -0,0 +1,126 @@ +use master +go + +create database ABC +on +( + name='ABC', + filename='D:\ABC.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='ABC_log', + filename='D:\ABC_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +go +use ABC +go + +create table orders +( + orderId int primary key identity , + orderDate datetime +) +go + +create table orderItem +( + ItemiD int primary key identity , + orderId int references orders(orderId), + itemType varchar(10), + itemName varchar(10), + theNumber int , + theMoney int +) +go + +insert orders values +('2008-01-12'),('2008-02-10'), +('2008-02-15'),('2008-03-10') +go + +insert orderItem values +('1','文具','笔',72,2), +('1','文具','尺',10,1), +('1','体育用品','篮球',1,56), +('2','文具','笔',36,2), +('2','文具','固体胶',20,3), +('2','日常用品','透明胶',2,1), +('2','体育用品','羽毛球',20,3), +('3','文具','订书机',20,3), +('3','文具','订书针',10,3), +('3','文具','裁纸刀',5,5), +('4','文具','笔',20,2), +('4','文具','信纸',50,1), +('4','日常用品','毛巾',4,5), +('4','日常用品','透明胶',30,1), +('4','体育用品','羽毛球',20,3) +go + +select SUM(theNumber)as 总数 from dbo.orderItem +go + +select SUM(theNumber) as 数量 ,AVG(theMoney) as 平均单价 from dbo.orderItem where orderId<3 group by orderId having AVG(theMoney)<10 +go + +select SUM(theNumber) as 数量,AVG(theMoney) as 平均单价 from dbo.orderItem where theNumber<50 group by orderId having AVG(theMoney)<10 +go + +select '文具:' as 类型,COUNT(itemType) 数量 from orderItem where itemType='文具' union +select '体育用品:' as 类型,COUNT(itemType) 数量 from orderItem where itemType='体育用品' union +select '日常用品:' as 类型, COUNT(itemType) 数量 from orderItem where itemType='日常用品' +go + +select itemType,SUM(theNumber) as 数量,AVG(theMoney) as 平均单价 from dbo.orderItem where theNumber>100 group by itemType +go + +select itemName as 产品名称,COUNT(itemName) as 订购次数,SUM(theNumber) as 总数量,AVG(theMoney) as 平均单价 from dbo.orderItem group by itemName +go +--使用上次作业的订单数据库,完成下列题目: + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select * from orders +select * from orderItem +SELECT ItemiD,orderDate,itemType,itemName,theNumber,theMoney from orderItem +inner join orders on orderItem.orderId=orders.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +SELECT ItemiD,orderDate,itemType,itemName,theNumber,theMoney from orderItem +inner join orders on orderItem.orderId=orders.orderId where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +SELECT ItemiD,orderDate,itemType,itemName,theNumber,theMoney, theNumber*theMoney 总价 from orderItem +inner join orders on orderItem.orderId=orders.orderId + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +SELECT ItemiD,orderDate,itemType,itemName,theNumber,theMoney from orderItem +inner join orders on orderItem.orderId=orders.orderId where theNumber>50 and theMoney>1 +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select ItemiD,COUNT(orderId) from orderItem group by I + +select orders.orderId as 编号,count(*) as 订购产品数 from orderItem +inner join orders on orderItem.orderId = orders.orderId +group by orders.orderId +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + +select * from orderItem +inner join orders on orderItem.orderId=orders.orderId +select orders.orderId as 订单编号,itemType as 产品类别,count(*)as 订购次数,sum(theNumber) as 总数量 from orderItem +inner join orders on orderItem.orderId = orders.orderId +group by orders.orderId,itemType +order by orders.orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\226\271\350\215\243\346\230\237/SQLQuery112.sql" "b/20210326\344\275\234\344\270\232/\346\226\271\350\215\243\346\230\237/SQLQuery112.sql" new file mode 100644 index 0000000000000000000000000000000000000000..541f42ced9221c615d2353d8f0b25ab96f5b9d45 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\226\271\350\215\243\346\230\237/SQLQuery112.sql" @@ -0,0 +1,93 @@ +use master +go + +create database Dwu +on +( +name='Dwu', +filename='D:\test\Dwu.mdf', +size=5mb, +maxsize=100mb, +filegrowth=10mb +) +log on +( +name='Dwu_log', +filename='D:\test\Dwu_log.ldf', +size=5mb, +maxsize=100mb, +filegrowth=10mb +) + +go +use Dwu +go + +create table stuinfo +( +stuNo char(5) primary key not null, +stuName nvarchar(20) not null, +stuAge char(3) not null, +stuAddress nvarchar(200), +stuSeat int identity(1,1)not null, +stuSex char(1) check(stuSex in (1,0)) default(1) not null +) +go + +insert into stuinfo(stuNo,stuName,stuAge,stuAddress,stuSex) values +('s2501','张秋利',20,'美国硅谷',1), +('s2502','李斯文',18,'湖北武汉',0), +('s2503','马文才',22,'湖南长沙',1), +('s2504','欧阳俊雄',21,'湖北武汉',0), +('s2505','梅超风',20,'湖北武汉',1), +('s2506','陈旋风',19,'美国硅谷',1), +('s2507','陈凤',20,'美国硅谷',0) +go + +create table stuexam +( +examNo int primary key identity(1,1), +stuNo char(5) references stuinfo(stuNo) not null, +writtenExam int not null, +labExam int not null +) +go + +insert stuexam(stuNo,writtenExam,labExam)values +('s2501',50,70), +('s2502',60,65), +('s2503',86,85), +('s2504',40,80), +('s2505',70,90), +('s2506',85,90) +go + +select stuName,stuAge,writtenExam,labExam from stuexam +inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +go + + +select stuName,stuAge,writtenExam,labExam from stuexam +inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +where writtenExam > 60 and labExam > 60 +go + +select stuName,stuAge,writtenExam,labExam from stuinfo +left join stuexam on stuexam.stuNo = stuinfo.stuNo +go + +select stuName,stuAge,writtenExam,labExam from stuexam +inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +where stuAge >= 20 +order by writtenExam desc +go + +select stuSex,avg(labExam) from stuexam +inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +group by stuSex +go + +select stuSex,sum(writtenExam) from stuexam +inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +group by stuSex +go \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\226\271\350\215\243\346\230\237/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\346\226\271\350\215\243\346\230\237/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3645a53567dfad85f9b3dd807bc3b566d1418fb9 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\226\271\350\215\243\346\230\237/SQLQuery3.sql" @@ -0,0 +1,123 @@ +use master +go +create database BBS +on +(name='BBS', + filename='D:\BSS.mdf', + size=20, + maxsize=300, + filegrowth=50 +) +log on +(name='BBS_log', + filename='D:\BSS_log.ldf', + size=20, + maxsize=300, + filegrowth=50 +) +go +use BBS +create table bbsUsers +( +uIDD int primary key identity(1,1), +uName varchar(10) not null unique , + uSex varchar(2) not null check(uSex='男' or uSex='女'), + uAge int not null check(uAge>15 and uAge<60), + uPoint int not null check(uPoint>=0) +) + +create table bbsTopic( + tID int primary key identity(1,1), + tUID int references bbsUsers(uIDD), + tSID int references bbsSection(sIDD), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime , + tCount int +) +create table bbsReply( +rID int primary key identity(1,1), + rUID int references bbsUsers(uIDD ), + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime + +) +create table bbsSection( +sIDD int primary key identity(1,1), +sName varchar(10) not null , + sUid int references bbsUsers(uIDD) +) +--1.现在有3个会员注册成功,请用一次插入多行数据的方法向bbsUsers表种插入3行记录,记录值如下: +-- 小雨点 女 20 0 +-- 逍遥 男 18 4 +-- 七年级生 男 19 2 +insert into bbsUsers(uName,uSex,uAge,uPoint) +select'小雨点','女','20','0'union +select'逍遥','男','18','4'union +select'七年级生','男','19','2' +go + + --2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中,提示查询部分列:select 列名1,列名2 from 表名 + select uName,uPoint into bbsPoint from bbsUsers + go + --3.给论坛开设4个板块 + -- 名称 版主名 + -- 技术交流 小雨点 + -- 读书世界 七年级生 + -- 生活百科 小雨点 + -- 八卦区 七年级生 + insert into bbsSection(sName,sUid) + select'技术交流',3 union + select'读书世界',1 union + select'生活百科',3 union + select'八卦区',1 + go + truncate table bbsSection + --4.向主贴和回帖表中添加几条记录 + insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) +select '2','1','范跑跑','谁是范跑跑','2008-7-8','1'union +select '1','3','.NET','与Java的区别是什么呀?','2008-9-1','2'union +select '3','4','今年夏天最流行什么呀?','有谁知道今年夏天最流行什么呀?','2008-9-10','0' + + +-- 回帖: +-- 分别给上面三个主贴添加对应的回帖,回帖的内容,时间,回帖人自定 + +insert into bbsReply(rTID,rMsg,rTime,rUId) +select'1','不知道','2008-9-5','2'union +select'2','不认识','2008-9-10','1'union +select'3','不知道','2008-10-1','3' + + +-- 6.因为小雨点发帖较多,将其积分增加10分 + + +update bbsUsers set uPoint=(uPoint + 10) where uName='小雨点' + +--在论坛数据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 + select uIDD ,uName,sName from bbsSection + inner join bbsUsers on bbsSection.sUid=bbsUsers.uIDD + + select * from bbsReply + select * from bbsSection + select * from bbsTopic + select * from bbsUsers + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 + select tID,uName,tTitle,tMsg,tTime from bbsTopic + inner join bbsUsers on bbsTopic.tID =bbsUsers.uIDD where bbsTopic.tTime>'2008-9-01' +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 + select sIDD,uName,sName from bbsUsers + inner join bbsSection on bbsUsers.uIDD=bbsSection.sIDD where bbsUsers.uAge<20 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 + select top 1 tID,uName,tTitle,tMsg,tCount from bbsTopic + inner join bbsUsers on bbsTopic.tUID=bbsUsers.uIDD + order by tCount desc +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select sName,uName,COUNT(tID) from bbsTopic +inner join bbsUsers on bbsTopic.tUID=bbsUsers.uIDD +inner join bbsSection on bbsSection.sIDD=bbsTopic.tSID +group by tSID, uIDD + diff --git "a/20210326\344\275\234\344\270\232/\346\226\275\346\261\237\345\263\260/SQLQuery1.sql01.sql" "b/20210326\344\275\234\344\270\232/\346\226\275\346\261\237\345\263\260/SQLQuery1.sql01.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8753d3b8e0c21383d5085f000a49a70197a09948 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\226\275\346\261\237\345\263\260/SQLQuery1.sql01.sql" @@ -0,0 +1,78 @@ +create database Student02 +on +( + name='Student02', + filename='D:\Student02.mdf', + size=20, + maxsize=300, + filegrowth=50 +) +log on +( + name='Student02_log', + filename='D:\Student02_log.ldf', + size=20, + maxsize=300, + filegrowth=50 +) +go +use Student02 +go +create table StudenIfo +( + stuNo int identity(2501,1) primary key, + stuName nvarchar(20) not null unique, + stuAge int null , + studdRess nvarchar(20) not null, + stuSeat int , + stuSext int check(stuSext in('1','0')) default('0') +) +insert into StudenIfo(stuName,stuAge,studdRess,stuSeat,stuSext ) values('寮犵鍒','20','缇庡浗纭呰胺','1','1'),('鏉庢柉鏂','18','婀栧寳姝︽眽','2','0'),('椹枃鎵','22','婀栧崡闀挎矙','3','1'), +('娆ч槼淇婇泟','21','婀栧寳姝︽眽','4','0'),('姊呰秴鍑','20','婀栧寳姝︽眽','5','1'),('闄堢偒椋','20','缇庡浗纭呰胺','6','1'),('闄堥','20','缇庡浗纭呰胺','7','0') + +create table Class01 +( + examNo int identity(1,1) primary key, + stuNo char(20) null, + writtenExam int , + labExam int +) +insert into Class01(stuNo,writtenExam,labExam) values ('2501','50','70'),('2502','60','65'),('2503','86','85'),('2503','40','80'),('2504','70','90'),('2505','85','90') + +select * from StudenIfo +select * from Class01 +--鏁版嵁濡傚浘鐗1,浣跨敤涓婃浣滀笟鐨勬暟鎹 + +--1.鏌ヨ瀛︾敓鐨勫鍚嶏紝骞撮緞锛岀瑪璇曟垚缁╁拰鏈鸿瘯鎴愮哗 + +select stuName 濮撳悕, stuAge 骞撮緞, writtenExam 绗旇瘯鎴愮哗,labExam 鏈鸿瘯鎴愮哗 from StudenIfo si +inner join Class01 cl on si.stuNo = cl.stuNo + +--2.鏌ヨ绗旇瘯鍜屾満璇曟垚缁╅兘鍦60鍒嗕互涓婄殑瀛︾敓鐨勫鍙凤紝濮撳悕锛岀瑪璇曟垚缁╁拰鏈鸿瘯鎴愮哗 + +select si.stuNo,stuName ,writtenExam ,labExam from StudenIfo si + inner join Class01 cl on si.stuNo = cl.stuNo + where writtenExam > 60 and labExam > 60 + +--3.鏌ヨ鎵鏈夊鐢熺殑瀛﹀彿锛屽鍚嶏紝绗旇瘯鎴愮哗锛屾満璇曟垚缁╋紝娌℃湁鍙傚姞鑰冭瘯鐨勫鐢熺殑鎴愮哗浠ULL鍊煎~鍏 + +select si.stuNo ,stuName ,writtenExam ,labExam from StudenIfo si + LEFT join Class01 cl on si.stuNo = cl.stuNo + +--4.鏌ヨ骞撮緞鍦20浠ヤ笂锛堝寘鎷20锛夌殑瀛︾敓鐨勫鍚嶏紝骞撮緞锛岀瑪璇曟垚缁╁拰鏈鸿瘯鎴愮哗锛屽苟鎸夌瑪璇曟垚缁╅檷搴忔帓鍒 + +select stuName,stuAge,writtenExam,labExam from StudenIfo si + inner join Class01 cl on si.stuNo = cl.stuNo + where stuAge >= 20 order by writtenExam DESC + +--5.鏌ヨ鐢峰コ鐢熺殑鏈鸿瘯骞冲潎鍒 + +select stuSext ,AVG(labExam) from StudenIfo si + inner join Class01 cl on si.stuNo = cl.stuNo + group by stuSext + +--6.鏌ヨ鐢峰コ鐢熺殑绗旇瘯鎬诲垎 + +select stuSext,SUM(writtenExam) from StudenIfo si + inner join Class01 cl on si.stuNo = cl.stuNo + group by stuSext \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\226\275\346\261\237\345\263\260/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\346\226\275\346\261\237\345\263\260/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/20210326\344\275\234\344\270\232/\346\226\275\346\261\237\345\263\260/SQLQuery3.sql02.sql" "b/20210326\344\275\234\344\270\232/\346\226\275\346\261\237\345\263\260/SQLQuery3.sql02.sql" new file mode 100644 index 0000000000000000000000000000000000000000..eb1b991f107c0ecaf61ac8d5bd1172e1870198eb --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\226\275\346\261\237\345\263\260/SQLQuery3.sql02.sql" @@ -0,0 +1,164 @@ +use master +go + +create database bbs +on +( + name='bbs', + filename='D:\bbs.mdf' +) +log on +( + name='bbs_log', + filename='D:\bbs_log.ldf' +) + + +use bbs +go + +create table bbsUsers +( + UID int identity(1,1), + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) + + +create table bbsSection +( + sID int identity(1,1) , + sName varchar(10) not null , + sUid int +) + +create table bbsTopic +( + tId int identity(1,1), + tUID int, + tSID int, + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) + + +create table bbsReply +( + rId int identity(1,1), + rUId int, + rTID int, + rMsg text not null, + rTime datetime +) + + +alter table bbsUsers add constraint PK_bbsUsers_UID primary key (UID) +alter table bbsUsers add constraint UQ_bbsUsers_uName unique (uName) +alter table bbsUsers add constraint CK_bbsUsers_uSex check(uSex='鐢' or uSex='濂') +alter table bbsUsers add constraint CK_bbsUsers_uAge check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint CK_bbsUsers_uPoint check(uPoint>=0) + +alter table bbsSection add constraint PK_bbsSection_sID primary key (sID) +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUID) references bbsUsers(UID) + +alter table bbsTopic add constraint PK_bbsTopic_tID primary key (tID) +alter table bbsTopic add constraint FK_bbsTopic_tUID foreign key(tUID) references bbsUsers(UID) +alter table bbsTopic add constraint FK_bbsTopic_tSID foreign key(tSID) references bbsSection(sID) + +alter table bbsReply add constraint PK_bbsReply_rID primary key (rID) +alter table bbsReply add constraint FK_bbsReply_rUID foreign key(rUID) references bbsUsers(UID) +alter table bbsReply add constraint Fk_bbsReply_rTID foreign key(rTID) references bbsTopic(tID) + +-- 1.鐜板湪鏈3涓細鍛樻敞鍐屾垚鍔燂紝璇风敤涓娆℃彃鍏ュ琛屾暟鎹殑鏂规硶鍚慴bsUsers琛ㄧ鎻掑叆3琛岃褰曪紝璁板綍鍊煎涓嬶細 +-- 灏忛洦鐐 濂 20 0 +-- 閫嶉仴 鐢 18 4 +-- 涓冨勾绾х敓 鐢 19 2 + +insert into bbsUsers(uName,uSex,uAge,uPoint) +select'灏忛洦鐐','濂','20','0'union +select'閫嶉仴','鐢','18','4'union +select'涓冨勾绾х敓','鐢','19','2' + +-- 2.灏哹bsUsers琛ㄤ腑鐨勭敤鎴峰悕鍜岀Н鍒嗕袱鍒楀浠藉埌鏂拌〃bbsPoint琛ㄤ腑锛屾彁绀烘煡璇㈤儴鍒嗗垪:select 鍒楀悕1锛屽垪鍚2 from 琛ㄥ悕 + +select uName,uPoint into bbsPoint from bbsUsers + + + +-- 3.缁欒鍧涘紑璁4涓澘鍧 +-- 鍚嶇О 鐗堜富鍚 +-- 鎶鏈氦娴 灏忛洦鐐 +-- 璇讳功涓栫晫 涓冨勾绾х敓 +-- 鐢熸椿鐧剧 灏忛洦鐐 +-- 鍏崷鍖 涓冨勾绾х敓 + +insert into bbsSection(sName,sUid) +select'鎶鏈氦娴','3'union +select'璇讳功涓栫晫','1'union +select'鐢熸椿鐧剧','3'union +select'鍏崷鍖','1' + + +-- 4.鍚戜富璐村拰鍥炲笘琛ㄤ腑娣诲姞鍑犳潯璁板綍 + +-- 涓昏创锛 + +-- 鍙戝笘浜 鏉垮潡鍚 甯栧瓙鏍囬 甯栧瓙鍐呭 鍙戝笘鏃堕棿 鍥炲鏁伴噺 +-- 閫嶉仴 鍏崷鍖 鑼冭窇璺 璋佹槸鑼冭窇璺 2008-7-8 1 +-- 涓冨勾绾х敓 鎶鏈氦娴 .NET 涓嶫AVA鐨勫尯鍒槸浠涔堝憖锛 2008-9-1 2 +-- 灏忛洦鐐 鐢熸椿鐧剧 浠婂勾澶忓ぉ鏈娴佽浠涔 鏈夎皝鐭ラ亾浠婂勾澶忓ぉ鏈娴佽 2008-9-10 0 +-- 浠涔堝憖锛 + +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) +select '2','1','鑼冭窇璺','璋佹槸鑼冭窇璺','2008-7-8','1'union +select '1','3','.NET','涓嶫ava鐨勫尯鍒槸浠涔堝憖锛','2008-9-1','2'union +select '3','4','浠婂勾澶忓ぉ鏈娴佽浠涔堝憖锛','鏈夎皝鐭ラ亾浠婂勾澶忓ぉ鏈娴佽浠涔堝憖锛','2008-9-10','0' + + +-- 鍥炲笘锛 +-- 鍒嗗埆缁欎笂闈笁涓富璐存坊鍔犲搴旂殑鍥炲笘锛屽洖甯栫殑鍐呭锛屾椂闂达紝鍥炲笘浜鸿嚜瀹 + +insert into bbsReply(rTID,rMsg,rTime,rUId) +select'1','涓嶇煡閬','2008-9-5','2'union +select'2','涓嶈璇','2008-9-10','1'union +select'3','涓嶇煡閬','2008-10-1','3' + + + select * from bbsTopic + select * from bbsReply + select * from bbsUsers + select * from bbsSection +-- 鍦ㄨ鍧涙暟鎹簱涓畬鎴愪互涓嬮鐩 +--1.鏌ヨ鍑烘瘡涓増鍧楃殑鐗堜富缂栧彿锛岀増涓诲鍚嶅拰鐗堝潡鍚嶇О + +select sUid 鐗堜富缂栧彿,uName 鐗堜富鍚嶇О,sName 鏉垮潡鍚嶇О from bbsUsers bu + inner join bbsSection bs on bu.UID = bs.sUid + +--2.鏌ヨ鍑轰富璐寸殑鍙戝笘鏃堕棿鍦2008-9-15浠ュ悗鐨勪富璐寸殑鍙戝笘浜虹紪鍙凤紝鍙戝笘浜哄鍚嶏紝甯栧瓙鐨勬爣棰橈紝甯栧瓙鐨勫唴瀹瑰拰鍙戝笘鏃堕棿 + +select tUID 鍙戝笘浜虹紪鍙,uName 鍙戝笘浜哄鍚,tTitle 甯栧瓙鏍囬,tMsg 甯栧瓙鍐呭,tTime 鍙戝笘鏃堕棿 from bbsUsers bu + inner join bbsTopic bt on bu.UID = bt.tUID + where tTime > '2008-09-15' + +--3.鏌ヨ鍑哄勾榫勫湪20浠ヤ笅鐨勭増涓荤殑缂栧彿锛岀増涓荤殑鍚嶇О鍜岀増鍧楃殑鍚嶇О + +select sID 鐗堜富缂栧彿,uName 鐗堜富鍚嶇О,sName 鏉垮潡鍚嶇О from bbsUsers bu + inner join bbsSection bs on bu.UID = bs.sUid + where uAge < 20 + +--4.鏌ヨ鍑哄洖澶嶆暟閲忔渶澶氱殑涓昏创鐨勫彂甯栦汉缂栧彿锛屽彂甯栦汉濮撳悕锛屼富璐存爣棰橈紝涓昏创鍐呭鍜屽洖澶嶆暟閲 + +select tUID 鍙戝笘浜虹紪鍙,uName 鍙戝笘浜哄鍚,tTitle 甯栧瓙鏍囬,tMsg 甯栧瓙鍐呭,tCount 鍥炲鏁伴噺 from bbsUsers bu + inner join bbsTopic bt on bu.UID = bt.tUID + where tCount = (select MAX(tCount) from bbsTopic) + +--5.鍦ㄤ富璐磋〃涓煡璇㈡瘡涓増鍧椾腑姣忎釜鐢ㄦ埛鐨勫彂甯栨绘暟 + +select sName 鏉垮潡鍚嶇О,uName 鍙戝笘浜哄悕绉,COUNT(UID) 鍙戝笘鎬绘暟 from bbsTopic bt + RIGHT join bbsSection bs on bt.tSID = bs.sID + inner join bbsUsers bu on bt.tUID = bu.UID + group by sID ,UID,uName,sName \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/Student.sql" "b/20210326\344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/Student.sql" new file mode 100644 index 0000000000000000000000000000000000000000..762adf50535070e4e4991e505f7aa2429190a3b7 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/Student.sql" @@ -0,0 +1,47 @@ + +--统计每个班的男生数 +select '男生的数量',count(StuSex) from StuInfo group by StuSex having StuSex='男' + + +--统计每个班的男、女生数 + +select ClassId,'男生的数量',count(StuSex) from StuInfo where StuSex='男' group by ClassId +union +select ClassId,'女生的数量',count(StuSex) from StuInfo where StuSex='女' group by ClassId + + + + --统计每个班的男、女生数 + +select ClassId,Stusex,count(StuSex) from StuInfo group by ClassId,StuSex + + + --统计每个班的福建人数 +select ClassId,count(StuProvince)福建省的人数 from StuInfo group by ClassId,StuProvince having StuProvince='福建省' + + +--统计每个班的各个省的总人数 +select ClassId,StuProvince,count(StuProvince) from StuInfo group by ClassId,StuProvince order by ClassId + + +--统计每个省的女生数 +select StuProvince,count(StuSex)女生人数 from StuInfo where StuSex='女' group by StuProvince + + +--统计每个省的男、女生数 +select StuProvince,StuSex,count(StuSex)人数 from StuInfo group by StuProvince,StuSex + + +--统计每个学生的考试总分、平均分 +select StuId 学号,sum(Score)总分,avg(Score)平均分 from Scores group by StuId + +--统计出考试总分大于620的学生的考试总分 +select StuId 学号,sum(Score)总分 from Scores group by StuId having sum(Score)>620 + + +--统计出每门考试成绩最高分和最低分 +select CourseId,max(Score)最高分,min(Score)最低分 from Scores group by CourseId + + +--统计出每个学生的各门成绩的平均分 +select StuId,CourseId,avg(score) from Scores group by StuId,CourseId order by StuId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/bbs.sql" "b/20210326\344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/bbs.sql" new file mode 100644 index 0000000000000000000000000000000000000000..21ffe32b5c87a68a65a82b50a9d85a9f76f4b1cf --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/bbs.sql" @@ -0,0 +1,123 @@ +use master +create database bbs +on +( + name='bbs', + filename='D:\Demo\bbs.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='bbs_log', + filename='D:\Demo\bbs_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go +use bbs +create table bbsUser +( + uID int identity(1,1) not null, + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) + +create table bbsSection +( + sID int identity(1,1) not null , + sName varchar(10) not null, + sUid int + +) +--添加约束 +-- alter table 表名 add constraint 约束名 约束类型 +alter table bbsUser add constraint PK_bbsUser_uID primary key(uID) +alter table bbsUser add constraint UK_bbsUser_uName unique(uName) +alter table bbsUser add constraint CK_bbsUser_uSex check(uSex='男' or uSex='女') +alter table bbsUser add constraint CK_bbsUser_uAge check(uAge>=15 and uAge<=60) +alter table bbsUser add constraint CK_bbsUser_uPoint check(uPoint>=0) + +alter table bbsSection add constraint PK_bbsSection_sID primary key(sID) +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUser(uID) + + +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int references bbsUser(uID), + tSID int references bbsSection(sID), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) +create table bbsReply +( + rID int identity(1,1) primary key, + rUID int references bbsUser(uID), + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +insert into bbsUser (uName,uSex,uAge,uPoint) values ('小雨点','女','20','0') +,('逍遥','男','18','4'),('七年级生','男','19','2') + +select uName,uPoint into bbsPoint from bbsUser + +insert into bbsSection (sName,sUid) values ('技术交流',1), +('读书世界',3),('生活百科',1),('八卦区',3) + +insert into bbsTopic (tUID,tSID,tTitle,tMsg,tTime,tCount) values ('2','4','范跑跑','谁是范跑跑','2008-7-8','1'), +('3','1','.NET','谁是范跑跑','2008-9-1','2'),('1','3','.今年夏天最流行什么呀?','有谁知道今年夏天最流行','2008-9-10','0') + +insert into bbsReply (rUID,rMsg,rTime) values ('1','范跑跑是赵家俊小名','2021-1-1'), +('2','名字不一样','2021-1-2'),('3','EMO','2021-1-3') + + +select * from bbsUser +select * from bbsSection +select * from bbsTopic +select * from bbsReply + + +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select sUid,uName,sName from bbsUser inner join bbsSection on bbsUser.uID=bbsSection.sUid + + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 + +select uID,uName,tTitle,tMsg,tTime from bbsTopic inner join bbsUser on bbsTopic.tSID=bbsUser.uID where tTime>'2008-9-1' + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sID,uName,sName,uAge from bbsUser inner join bbsSection on bbsUser.uID=bbsSection.sUid where uAge<20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select max(tCount) from bbsTopic +select uID,uName,tTitle,tMsg,tCount from bbsTopic inner join bbsUser on bbsTopic.tSID=bbsUser.uID where tCount= (select max(tCount) from bbsTopic) + +--5.在主贴表中查询每个版块中每个用户的发帖总数 + +select tsid,count(tSID)from bbsTopic group by tUID,tSID + + + + + + + + + + + + + + + + + diff --git "a/20210326\344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/shopping.sql" "b/20210326\344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/shopping.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e29a48552eefb3638ba52e988884c5df6f2ad86c --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/shopping.sql" @@ -0,0 +1,101 @@ +use master +go + +create database shopping +on +( + name='shopping', + filename='D:\Demo\shopping.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='shopping_log', + filename='D:\Demo\shopping_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use shopping +go + +create table orders +( + orderId int primary key identity(1,1) , + orderDate datetime default(getdate()) +) + +create table orderItem +( + ItemiD int primary key identity(1,1), + orderId int references orders(orderId) not null, + itemType nvarchar(4) check(itemType='文具' or itemType='日常用品' or itemType='体育用品' ) not null, + itemName nvarchar(4) not null, + theNumber int not null, + theMoney money not null +) + +insert into orders values (default),(default),(default),(default) + + +insert into orderItem (orderId,itemType,itemName,theNumber,theMoney) values (1 ,'文具',' 笔' ,72, 2) +,(1,'文具','尺',10,1) +,(1,'体育用品','篮球',1,56) +,(2,'文具','笔',36,2) +,(2,'文具','固体胶',20,3) +,(2,'日常用品','透明胶',2,1) +,(2,'体育用品','羽毛球',20,3) +,(3,'文具','订书机',20,3) +,(3,'文具','订书针',10,3) +,(3,'文具','裁纸刀',5,5) +,(4,'文具','笔',20,2) +,(4,'文具','信纸',50,1) +,(4,'日常用品','毛巾',4,5) +,(4,'日常用品','透明胶',30,1) +,(4,'体育用品','羽毛球',20,3) + + + +select * from orderItem +select * from orders + + + +--使用上次作业的订单数据库,完成下列题目: + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select orders.orderId,orderDate,itemType,itemName,theNumber,theMoney from orders inner join orderItem on orders.orderId=orderItem.orderId + + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orders.orderId,orderDate,itemType,itemName from orders inner join orderItem on orders.orderId=orderItem.orderId where theNumber>=50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderId,orderDate,itemType,itemName,theNumber,theMoney,sum(theMoney*theNumber)订购总价 from orders inner join orderItem on orders.orderId=orderItem.orderId group by theMoney,orders.orderId,orderDate,itemType,itemName,theNumber,theMoney + + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderId,orderDate,itemType,itemName,theNumber,theMoney,sum(theMoney*theNumber)订购总价 from orders inner join orderItem on orders.orderId=orderItem.orderId group by theMoney,orders.orderId,orderDate,itemType,itemName,theNumber,theMoney having theMoney>4 and theNumber>=3 + + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orders.orderId,theNumber from orderItem inner join orders on orderItem.orderId=orders.orderId + +--6.查询 每个订单里的 每个类别的产品 分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + +select orderId,itemType,count(theNumber) from orderItem group by orderId,itemType order by orderId diff --git "a/20210326\344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/stuInfo.sql" "b/20210326\344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/stuInfo.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9bc9386cfbe69b61faf374cb72107f0cae3d435a --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/stuInfo.sql" @@ -0,0 +1,99 @@ +create database Student +on +( + name='Stuinfo', + filename='D:\Demo\Stuinfo.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% + +) +log on +( + name='Stuinfo_log', + filename='D:\Demo\Stuinfo_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% + +) +go +use Student + +create table Stuinfo +( + stuNO varchar(5) unique not null, + stuName nvarchar(20), + stuAge int, + stuAddress nvarchar(20), + stuSeat int identity(1,1) unique not null, + stuSex nchar(1) check(stuSex='男' or stuSex='女') +) +go + +create table Scoreinfo +( + examNO int primary key identity(1,1), + stuNO varchar(5) references Stuinfo(stuNO), + writtenExan int, + labExam int, +) + +insert into Stuinfo values ('s2501','张秋利','20','美国硅谷','女'),('s2502','李斯文',18,'湖北武汉','男'),('s2503','马文才',22,'湖南长沙','女'), +('s2504','欧阳俊雄',21,'湖北武汉','男'),('s2505','梅超风',20,'湖北武汉','女'),('s2506','陈旋风',19,'美国硅谷','女'),('s2507','陈风',20,'美国硅谷','男') + + +insert into Scoreinfo values('s2501',50,70),('s2502',60,65),('s2503',86,85),('s2504',40,80),('s2505',70,90),('s2506',85,90) + + + +select * from Scoreinfo +select * from Stuinfo + + + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName,stuAge,writtenExan,labExam from Stuinfo inner join Scoreinfo on Scoreinfo.stuNO =Stuinfo.stuNO + + + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select Scoreinfo.stuNO,stuName,writtenExan,labExam from Stuinfo inner join Scoreinfo on Scoreinfo.stuNO =Stuinfo.stuNO where writtenExan>60 and labExam>60 + + + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select Scoreinfo.stuNO,stuName,writtenExan,labExam from Stuinfo left join Scoreinfo on Scoreinfo.stuNO =Stuinfo.stuNO + + + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName,stuAge,writtenExan,labExam from Stuinfo inner join Scoreinfo on Scoreinfo.stuNO =Stuinfo.stuNO where stuAge>20 order by writtenExan desc + + +--5.查询男女生的机试平均分 +select stuSex,avg(labExam)机试平均分 from Stuinfo inner join Scoreinfo on Scoreinfo.stuNO =Stuinfo.stuNO group by stuSex + + +--6.查询男女生的笔试总分 + +select stuSex,sum(writtenExan)笔试平均分 from Stuinfo inner join Scoreinfo on Scoreinfo.stuNO =Stuinfo.stuNO group by stuSex + + + + + + + + + + + + + + + + + + + diff --git "a/20210326\344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/SQLsever1.sql" "b/20210326\344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/SQLsever1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7d277ba971f78ab14fc3fca56d5e34069c77b3e4 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/SQLsever1.sql" @@ -0,0 +1,111 @@ +use master +go + +create database ABC +on +( + name='ABC', + filename='D:\test\ABC.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='ABC_log', + filename='D:\test\ABC_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +use ABC +go + +create table orders +( + orderId int primary key identity , + orderDate datetime +) +go + +create table orderItem +( + ItemiD int primary key identity , + orderId int references orders(orderId), + itemType varchar(10), + itemName varchar(10), + theNumber int , + theMoney int +) +go + +insert orders values +('2008-01-12'),('2008-02-10'), +('2008-02-15'),('2008-03-10') +go + +insert orderItem values +('1','文具','笔',72,2), +('1','文具','尺',10,1), +('1','体育用品','篮球',1,56), +('2','文具','笔',36,2), +('2','文具','固体胶',20,3), +('2','日常用品','透明胶',2,1), +('2','体育用品','羽毛球',20,3), +('3','文具','订书机',20,3), +('3','文具','订书针',10,3), +('3','文具','裁纸刀',5,5), +('4','文具','笔',20,2), +('4','文具','信纸',50,1), +('4','日常用品','毛巾',4,5), +('4','日常用品','透明胶',30,1), +('4','体育用品','羽毛球',20,3) +go + +select SUM(theNumber)as 总数 from dbo.orderItem +go + +select SUM(theNumber) as 数量 ,AVG(theMoney) as 平均单价 from dbo.orderItem where orderId<3 group by orderId having AVG(theMoney)<10 +go + +select SUM(theNumber) as 数量,AVG(theMoney) as 平均单价 from dbo.orderItem where theNumber<50 group by orderId having AVG(theMoney)<10 +go + +select '文具:' as 类型,COUNT(itemType) 数量 from orderItem where itemType='文具' union +select '体育用品:' as 类型,COUNT(itemType) 数量 from orderItem where itemType='体育用品' union +select '日常用品:' as 类型, COUNT(itemType) 数量 from orderItem where itemType='日常用品' +go + +select itemType,SUM(theNumber) as 数量,AVG(theMoney) as 平均单价 from dbo.orderItem where theNumber>100 group by itemType +go + +select itemName as 产品名称,COUNT(itemName) as 订购次数,SUM(theNumber) as 总数量,AVG(theMoney) as 平均单价 from dbo.orderItem group by itemName +go + +select orders.orderId,orderDate,itemType,itemName,theNumber,theMoney from orderItem +inner join orders on orderItem.orderId = orders.orderId +go + +select orderDate,itemType,itemName from orderItem +inner join orders on orderItem.orderId = orders.orderId +where theNumber>50 +go + +select orders.orderId,orderDate,itemType,itemName,theNumber,theMoney * theNumber as 订购总价 from orderItem +inner join orders on orderItem.orderId = orders.orderId +go + +select orders.orderId,orderDate,itemType,itemName,theNumber,theMoney * theNumber as 订购总价 from orderItem +inner join orders on orderItem.orderId = orders.orderId +where theMoney > 5 and theNumber > 50 +go + +select orders.orderId as 编号,count(*) as 订购产品数 from orderItem +inner join orders on orderItem.orderId = orders.orderId +group by orders.orderId + +select orders.orderId as 订单编号,itemType as 产品类别,count(*)as 订购次数,sum(theNumber) as 总数量 from orderItem +inner join orders on orderItem.orderId = orders.orderId +group by orders.orderId,itemType +order by orders.orderId + diff --git "a/20210326\344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/SQLsever2.sql" "b/20210326\344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/SQLsever2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2292faa1b09757f66f95b20a3cbbc312d46bf5a7 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/SQLsever2.sql" @@ -0,0 +1,175 @@ +use master +go + +create database bbs +on +( + name='bbs', + filename='D:\test\bbs.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='bbs_log', + filename='D:\test\bbs_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) + +use bbs +go + +create table bbsUsers +( + UuId int identity(1,1), + UName varchar(10) not null , + USex varchar(2) , + UAge int , + UPoint int not null +) +go + +alter table bbsUsers add constraint PK_UuId primary key (UuId) +go +alter table bbsUsers add constraint UQ_UName unique (UName) +go +alter table bbsUsers add constraint CK_USex check(USex='男'or USex='女') +go +alter table bbsUsers add constraint CK_UAge check(UAge>=15 and UAge<=60) +go +alter table bbsUsers add constraint CK_UPoint check(Upoint>=0) +go + +create table bbsSection +( + Ssid int identity(1,1) not null, + SName varchar(10) not null, + SuId int, +) +go + +alter table bbsSection add constraint PK_Ssid primary key (Ssid) +go +alter table bbsSection add constraint FK_SuId foreign key(SuId) references bbsUsers(UuId) +go + +create table bbsTopic +( + TtId int primary key identity(1,1), + TuId int references bbsUsers(Uuid), + TsId int references bbsSection(SsId), + TTitlr varchar(100) not null, + TMsg text, + TTime datetime, + TCount int +) +go + +create table bbsReply +( + RrId int primary key identity(1,1), + RuId int references bbsUsers(UuId), + RtId int references bbsTopic(TtId), + RMsg text not null, + RTime datetime, +) +go + +insert bbsUsers (UName,USex,UAge,UPoint) +select '小雨点','女',20,0 union +select '逍遥','男',18,4 union +select '七年级生','男',19,2 +go + +select UName ,UPoint into bbsPoint from bbsUsers +go + +insert into bbsSection(SName,SuId)values +('技术交流',2), +('读书世界',3), +('生活百科',2), +('八卦区',3) +go + +insert into bbsTopic(TuId,TsId,TTitlr,TMsg,TTime,TCount)values +(3,4,'范跑跑','谁是范跑跑','2008-7-8','1'), +(2,1,'.NET ','与JAVA的区别是什么呀?','2008-9-1','2'), +(4,3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?','2008-9-10','0'), +(3,3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?','2008-9-10','0'), +(4,1,'.NET ','与JAVA的区别是什么呀?','2008-9-1','0'), +(2,4,'范跑跑','范跑跑谁是???','2008-7-8','0') +go + +insert into bbsReply(RuId,RMsg,RTime,RtId)values +('2','范跑跑 ','2008-7-8','1'), +('3','NET ','2008-9-1','2'), +('4','.NET ','2008-9-10','2') +go + +update bbsUsers set UPoint=UPoint+10 where UuId=3 + +truncate table bbsReply +go + +select TsId,COUNT(TtId) as 发帖总数 from bbsTopic group by TsId +go + +select RtId, COUNT(RrId) as 回帖总数量 from bbsReply group by RtId +go + +select TuId,COUNT(TtId) as 发帖总数 from bbsTopic group by TuId +go + +select TuId,SUM(TCount) as 回复数量总和 from bbsTopic group by TuId +go + +select TsId,AVG(TCount) as 平均回复数量 from bbsTopic group by TsId having AVG(TCount)>3 +go + +select * from bbsUsers where UPoint=(select MAX(UPoint) from bbsUsers) +go + +select * from bbsTopic where TTitlr like('%快乐%') or TMsg like('%快乐%') +go + +select * from bbsUsers where UAge>=15 and UAge<=20 and UPoint>=10 +go + +select * from bbsUsers where UName like('小%') and UName like('__大%') +go + +select TTitlr as 标题, TMsg as 内容 from bbsTopic where TTime>='2008-9-10 12:00:00' and TCount>=10 + go + + select TuId ,TCount from bbsTopic where TTitlr like ('%!') + go + + select UuId,UName,SName from bbsTopic + inner join bbsUsers on bbsTopic.TuId = bbsUsers.UuId + inner join bbsSection on bbsTopic.TsId = bbsSection.Ssid + go + + select UuId,UName,TTitlr,TMsg,TTime from bbsTopic + inner join bbsUsers on bbsTopic.TuId = bbsUsers.UuId +where TTime >= '2008-09-15' +go + + select UuId,UName,SName from bbsTopic + inner join bbsUsers on bbsTopic.TuId = bbsUsers.UuId + inner join bbsSection on bbsTopic.TsId = bbsSection.Ssid + where UAge<20 + go + + select top 1 UuId,UName,TTitlr,TMsg,TCount from bbsTopic + inner join bbsUsers on bbsTopic.TuId = bbsUsers.UuId + order by TCount desc + go + + + select SName,UName,count(TCount) from bbsTopic + inner join bbsSection on bbsTopic.TsId = bbsSection.Ssid + inner join bbsUsers on bbsUsers.UuId = bbsTopic.TuId + group by UuId,TsId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/SQLsever3.sql" "b/20210326\344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/SQLsever3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a7dc60f7cf1de6f09617949caeca1d443145c55f --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/SQLsever3.sql" @@ -0,0 +1,91 @@ +use master +go + +create database Dwu +on +( +name='Dwu', +filename='D:\test\Dwu.mdf', +size=5mb, +maxsize=100mb, +filegrowth=10mb +) +log on +( +name='Dwu_log', +filename='D:\test\Dwu_log.ldf', +size=5mb, +maxsize=100mb, +filegrowth=10mb +) +use Dwu +go + +create table stuinfo +( +stuNo char(5) primary key not null, +stuName nvarchar(20) not null, +stuAge char(3) not null, +stuAddress nvarchar(200), +stuSeat int identity(1,1)not null, +stuSex char(1) check(stuSex in (1,0)) default(1) not null +) +go + +insert into stuinfo(stuNo,stuName,stuAge,stuAddress,stuSex) values +('s2501','张秋利',20,'美国硅谷',1), +('s2502','李斯文',18,'湖北武汉',0), +('s2503','马文才',22,'湖南长沙',1), +('s2504','欧阳俊雄',21,'湖北武汉',0), +('s2505','梅超风',20,'湖北武汉',1), +('s2506','陈旋风',19,'美国硅谷',1), +('s2507','陈凤',20,'美国硅谷',0) +go + +create table stuexam +( +examNo int primary key identity(1,1), +stuNo char(5) references stuinfo(stuNo) not null, +writtenExam int not null, +labExam int not null +) +go + +insert stuexam(stuNo,writtenExam,labExam)values +('s2501',50,70), +('s2502',60,65), +('s2503',86,85), +('s2504',40,80), +('s2505',70,90), +('s2506',85,90) +go + +select stuName,stuAge,writtenExam,labExam from stuexam +inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +go + + +select stuName,stuAge,writtenExam,labExam from stuexam +inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +where writtenExam > 60 and labExam > 60 +go + +select stuName,stuAge,writtenExam,labExam from stuinfo +left join stuexam on stuexam.stuNo = stuinfo.stuNo +go + +select stuName,stuAge,writtenExam,labExam from stuexam +inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +where stuAge >= 20 +order by writtenExam desc +go + +select stuSex,avg(labExam) from stuexam +inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +group by stuSex +go + +select stuSex,sum(writtenExam) from stuexam +inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +group by stuSex +go \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..53b021d53b7fc7336abe5471ecee3d731c47a46d --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/SQLQuery1.sql" @@ -0,0 +1,461 @@ +USE [master] +GO +/****** Object: Database [TestDB] Script Date: 2021/3/15 16:11:24 ******/ +CREATE DATABASE [TestDB] + CONTAINMENT = NONE + ON PRIMARY +( NAME = N'TestDB', FILENAME = N'D:\TestDB.mdf' , SIZE = 4288KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) + LOG ON +( NAME = N'TestDB_log', FILENAME = N'D:\TestDB_log.ldf' , SIZE = 1072KB , MAXSIZE = 2048GB , FILEGROWTH = 10%) +GO +ALTER DATABASE [TestDB] SET COMPATIBILITY_LEVEL = 120 +GO +IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled')) +begin +EXEC [TestDB].[dbo].[sp_fulltext_database] @action = 'enable' +end +GO +ALTER DATABASE [TestDB] SET ANSI_NULL_DEFAULT OFF +GO +ALTER DATABASE [TestDB] SET ANSI_NULLS OFF +GO +ALTER DATABASE [TestDB] SET ANSI_PADDING OFF +GO +ALTER DATABASE [TestDB] SET ANSI_WARNINGS OFF +GO +ALTER DATABASE [TestDB] SET ARITHABORT OFF +GO +ALTER DATABASE [TestDB] SET AUTO_CLOSE OFF +GO +ALTER DATABASE [TestDB] SET AUTO_SHRINK OFF +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS ON +GO +ALTER DATABASE [TestDB] SET CURSOR_CLOSE_ON_COMMIT OFF +GO +ALTER DATABASE [TestDB] SET CURSOR_DEFAULT GLOBAL +GO +ALTER DATABASE [TestDB] SET CONCAT_NULL_YIELDS_NULL OFF +GO +ALTER DATABASE [TestDB] SET NUMERIC_ROUNDABORT OFF +GO +ALTER DATABASE [TestDB] SET QUOTED_IDENTIFIER OFF +GO +ALTER DATABASE [TestDB] SET RECURSIVE_TRIGGERS OFF +GO +ALTER DATABASE [TestDB] SET ENABLE_BROKER +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF +GO +ALTER DATABASE [TestDB] SET DATE_CORRELATION_OPTIMIZATION OFF +GO +ALTER DATABASE [TestDB] SET TRUSTWORTHY OFF +GO +ALTER DATABASE [TestDB] SET ALLOW_SNAPSHOT_ISOLATION OFF +GO +ALTER DATABASE [TestDB] SET PARAMETERIZATION SIMPLE +GO +ALTER DATABASE [TestDB] SET READ_COMMITTED_SNAPSHOT OFF +GO +ALTER DATABASE [TestDB] SET HONOR_BROKER_PRIORITY OFF +GO +ALTER DATABASE [TestDB] SET RECOVERY FULL +GO +ALTER DATABASE [TestDB] SET MULTI_USER +GO +ALTER DATABASE [TestDB] SET PAGE_VERIFY CHECKSUM +GO +ALTER DATABASE [TestDB] SET DB_CHAINING OFF +GO +ALTER DATABASE [TestDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) +GO +ALTER DATABASE [TestDB] SET TARGET_RECOVERY_TIME = 0 SECONDS +GO +ALTER DATABASE [TestDB] SET DELAYED_DURABILITY = DISABLED +GO +EXEC sys.sp_db_vardecimal_storage_format N'TestDB', N'ON' +GO +USE [TestDB] +GO +/****** Object: Table [dbo].[ClassInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[ClassInfo]( + [ClassId] [int] IDENTITY(1,1) NOT NULL, + [ClassName] [nvarchar](20) NOT NULL, +PRIMARY KEY CLUSTERED +( + [ClassId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[CourseInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[CourseInfo]( + [CourseId] [int] IDENTITY(1,1) NOT NULL, + [CourseName] [nvarchar](50) NOT NULL, + [CourseCredit] [int] NULL DEFAULT ((1)), +PRIMARY KEY CLUSTERED +( + [CourseId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[Scores] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[Scores]( + [ScoreId] [int] IDENTITY(1,1) NOT NULL, + [StuId] [int] NULL, + [CourseId] [int] NULL, + [Score] [int] NULL DEFAULT ((0)), +PRIMARY KEY CLUSTERED +( + [ScoreId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[StuInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[StuInfo]( + [StuId] [int] IDENTITY(1,1) NOT NULL, + [ClassId] [int] NULL, + [StuName] [nvarchar](10) NOT NULL, + [StuSex] [nvarchar](1) NULL DEFAULT ('男'), + [StuBrithday] [date] NULL, + [StuPhone] [nvarchar](11) NULL, + [StuProvince] [nvarchar](200) NULL, + [CreateDate] [datetime] NULL DEFAULT (getdate()), + [StuAge] [int] NULL, +PRIMARY KEY CLUSTERED +( + [StuId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] ON + +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (1, N'软件1班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (2, N'软件2班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (3, N'软件3班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (4, N'软件4班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (5, N'软件5班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (6, N'软件6班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (7, N'软件7班') +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] ON + +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (1, N'计算机基础', 3) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (2, N'HTML+CSS网页制作', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (3, N'JAVA编程基础', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (4, N'SQL Server数据库基础', 4) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (5, N'C#面向对象编程', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (6, N'Winform桌面应用程序设计', 5) +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[Scores] ON + +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (1, 1, 1, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (2, 1, 2, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (3, 1, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (4, 1, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (5, 2, 1, 60) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (6, 2, 2, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (7, 2, 3, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (8, 2, 4, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (9, 3, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (10, 3, 2, 45) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (11, 3, 3, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (12, 3, 4, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (13, 4, 1, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (14, 4, 2, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (15, 4, 3, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (16, 4, 4, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (17, 5, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (18, 5, 2, 79) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (19, 5, 3, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (20, 5, 4, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (21, 6, 1, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (22, 6, 2, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (23, 6, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (24, 6, 5, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (25, 7, 1, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (26, 7, 2, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (27, 7, 3, 92) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (28, 7, 5, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (29, 8, 1, 58) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (30, 8, 2, 59) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (31, 8, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (32, 8, 5, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (33, 9, 1, 48) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (34, 9, 2, 67) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (35, 9, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (36, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (37, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (38, 1, 1, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (39, 1, 2, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (40, 1, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (41, 1, 4, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (42, 2, 1, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (43, 2, 2, 82) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (44, 2, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (45, 2, 4, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (46, 3, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (47, 3, 2, 50) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (48, 3, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (49, 3, 4, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (50, 4, 1, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (51, 4, 2, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (52, 4, 3, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (53, 4, 4, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (54, 5, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (55, 5, 2, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (56, 5, 3, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (57, 5, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (58, 6, 1, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (59, 6, 2, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (60, 6, 3, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (61, 6, 5, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (62, 7, 1, 89) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (63, 7, 2, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (64, 7, 3, 97) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (65, 7, 5, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (66, 8, 1, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (67, 8, 2, 64) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (68, 8, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (69, 8, 5, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (70, 9, 1, 53) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (71, 9, 2, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (72, 9, 3, 76) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (73, 9, 5, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (74, 9, 5, 61) +GO +SET IDENTITY_INSERT [dbo].[Scores] OFF +GO +SET IDENTITY_INSERT [dbo].[StuInfo] ON + +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (1, 1, N'刘正', N'男', CAST(N'2002-08-02' AS Date), N'13245678121', N'广西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (2, 1, N'黄贵', N'男', CAST(N'2003-07-02' AS Date), N'13345678121', N'江西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (3, 1, N'陈美', N'女', CAST(N'2002-07-22' AS Date), N'13355678125', N'福建省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (4, 2, N'江文', N'男', CAST(N'2001-07-02' AS Date), N'13347678181', N'湖南省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (5, 2, N'钟琪', N'女', CAST(N'2004-01-13' AS Date), N'13345778129', N'安徽省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (6, 3, N'曾小林', N'男', CAST(N'2005-05-15' AS Date), N'13345378563', N'安徽省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (7, 3, N'欧阳天天', N'女', CAST(N'2000-08-19' AS Date), N'13347878121', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (8, 3, N'李逍遥', N'男', CAST(N'1999-09-02' AS Date), N'13345678557', N'广东省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 22) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (9, 4, N'刘德华', N'男', CAST(N'1995-06-11' AS Date), N'15345679557', N'福建省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 26) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (10, 4, N'刘翔', N'男', CAST(N'1996-07-09' AS Date), N'18346679589', N'江西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (11, 4, N'曾小贤', N'男', CAST(N'2003-07-02' AS Date), N'18348979589', N'湖南省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (12, 5, N'刘德华', N'男', CAST(N'2002-07-02' AS Date), N'18348979509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (13, 5, N'陈天翔', N'男', CAST(N'2003-07-02' AS Date), N'18348079509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (14, 5, N'刘能', N'男', CAST(N'2005-08-02' AS Date), N'13245678122', N'广西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (15, 5, N'钟馗', N'男', CAST(N'2004-08-02' AS Date), N'13245678123', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (16, 5, N'钟吴艳', N'女', CAST(N'2002-08-02' AS Date), N'13245678124', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (17, 5, N'刘欢', N'男', CAST(N'2001-07-02' AS Date), N'13245678125', N'湖南省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (18, 5, N'张庭', N'女', CAST(N'2000-07-02' AS Date), N'13245678126', N'江西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (19, 5, N'曹植', N'男', CAST(N'2000-08-02' AS Date), N'13245678127', N'福建省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (20, 5, N'曹操', N'男', CAST(N'2002-08-02' AS Date), N'13245678128', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (21, 5, N'孙尚香', N'女', CAST(N'2003-08-02' AS Date), N'13245678129', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (22, 3, N'老1', N'女', CAST(N'2002-08-02' AS Date), N'13245678130', N'广东省', CAST(N'2021-03-14 17:02:36.347' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (24, 2, N'老2', N'男', CAST(N'2002-08-03' AS Date), N'13345678945', NULL, CAST(N'2021-03-14 17:03:37.733' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (25, 4, N'老3', N'男', NULL, N'13645987545', N'广东省', CAST(N'2021-03-14 17:03:43.307' AS DateTime), NULL) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (28, 5, N'老4', N'男', CAST(N'2006-03-05' AS Date), N'13456987456', NULL, CAST(N'2021-03-14 17:04:03.957' AS DateTime), 15) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (29, 5, N'老5', N'女', CAST(N'1998-04-12' AS Date), N'15978456123', NULL, CAST(N'2021-03-14 17:04:47.103' AS DateTime), 23) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (30, 4, N'老6', N'男', CAST(N'1996-08-06' AS Date), N'18945674561', NULL, CAST(N'2021-03-14 17:05:04.990' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (31, 3, N'老7', N'女', CAST(N'1997-04-06' AS Date), N'18845678912', NULL, CAST(N'2021-03-14 17:05:20.570' AS DateTime), 24) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (32, 2, N'老10', N'女', CAST(N'1998-08-09' AS Date), N'19945645612', NULL, CAST(N'2021-03-14 17:06:08.107' AS DateTime), 23) +GO +SET IDENTITY_INSERT [dbo].[StuInfo] OFF +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__CourseIn__9526E2773AB7BECE] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[CourseInfo] ADD UNIQUE NONCLUSTERED +( + [CourseName] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__StuInfo__2D85FC63AF6FC6FA] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[StuInfo] ADD UNIQUE NONCLUSTERED +( + [StuPhone] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([CourseId]) +REFERENCES [dbo].[CourseInfo] ([CourseId]) +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([StuId]) +REFERENCES [dbo].[StuInfo] ([StuId]) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD FOREIGN KEY([ClassId]) +REFERENCES [dbo].[ClassInfo] ([ClassId]) +ON DELETE SET NULL +GO +ALTER TABLE [dbo].[CourseInfo] WITH CHECK ADD CHECK (([CourseCredit]>=(1) AND [CourseCredit]<=(5))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK ((len([StuPhone])=(11))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK (([StuSex]='女' OR [StuSex]='男')) +GO +USE [master] +GO +ALTER DATABASE [TestDB] SET READ_WRITE +GO +select * from stuinfo +select * from scores +--1.统计每个班的男生数 +select classid 班级,count(classid) 男生总数 from stuinfo where stusex='男' group by classid +--2.统计每个班的男、女生数 +select classid 班级,count(classid) 该班级性别人数,stusex 性别 from stuinfo group by classid,stusex order by classid +--3.统计每个班的福建人数 +select classid 班级,count(classid)人数 from stuinfo where stuprovince='福建省' group by classid +--4.统计每个班的各个省的总人数 +select classid,count(classid)总人数,stuprovince from stuinfo group by classid,stuprovince order by classid +--5.统计每个省的女生数 +select classid,stuprovince from stuinfo where stusex='女' group by classid,stuprovince order by classid +--6.统计每个省的男、女生数 +select stuprovince,count(stusex) 人数 from stuinfo group by stuprovince +--7.统计每个学生的考试总分、平均分 +select stuid,sum(score)总分,avg(score)平均分 from scores group by stuid +--8.统计出考试总分大于620的学生的考试总分 +select stuid,sum(score)总分 from scores group by stuid having sum(score)>620 +--9.统计出每门考试成绩最高分和最低分 +select courseid,max(score)最高分,min(score)最低分 from scores group by courseid +--10.统计出每个学生的各门成绩的平均分 +select stuid,Courseid ,avg(score) 平均分 from scores group by stuid,Courseid order by stuid \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d348d994b4bfa62ded1fc1977348bb9e8c35d5ab --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/SQLQuery2.sql" @@ -0,0 +1,75 @@ +create database orders +on +( + name = 'order', + size = 5, + maxsize = 500, + filename = 'D:\新建文件夹3\order.mdf', + filegrowth = 10% +) +log on +( + name = 'order_LOG', + size = 5, + maxsize = 500, + filename = 'D:\新建文件夹3\order.ldf', + filegrowth = 10% +) +use orders +go +create table orders --订单表 +( + orderId int primary key, --订单编号 + orderDate smalldatetime --订购日期 +) +go +create table orderItem --订购项目表 +( + itemName nvarchar(10) not null, --产品名称 + theNumber int not null, --订购数量 + theMoney money not null, --订购单价 + itemType nvarchar(4) not null,--产品类别 + orderId int references orders(orderId) not null, --订单编号 + ItemiD int identity(1,1) primary key, --项目编号 +) +insert into orders(orderId,orderDate) values(1,'2008-01-12'),(2,'2008-02-10'),(3,'2008-02-15'),(4,'2008-03-10') +insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) +select 1,'文具','笔',72,2 union +select 1,'文具','尺',10,1 union +select 1, '体育用品','篮球',1,56 union +select 2,'文具','笔',36,2 union +select 2,'文具','固体胶',20,3 union +select 2,'日常用品','透明胶',2,1 union +select 2,'体育用品','羽毛球',20,3 union +select 3,'文具','订书机',20,3 union +select 3,'文具','订书机',10,3 union +select 3,'文具','裁缝刀',5,5 union +select 4,'文具','笔',20,2 union +select 4,'文具','信纸',50,1 union +select 4,'日常用品','毛巾',4,5 union +select 4,'日常用品','透明胶',30,1 union +select 4,'体育用品','羽毛球',20,3 +select * from orders +select * from orderItem +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select orderItem.orderId,orderDate,itemType,itemName,theNumber,theMoney from orders left join orderItem on orders.orderId=orderItem.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orderItem.orderId,orderDate,itemType,itemName,theNumber from orders left join orderItem on orders.orderId=orderItem.orderId where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderItem.orderId 订单的编号,orderDate 订单日期,itemType 订购产品的类别,theNumber 订购数量,theMoney 订购单价,theNumber*theMoney 订购总价 from orderItem left join orders on orders.orderId=orderItem.orderId +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderItem.orderId 订单编号, orderDate 订单日期,itemType 订购产品的类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价,theNumber*theMoney 订购总价 from orderItem left join orders on orders.orderId=orderItem.orderId where theMoney>5 and theNumber >=50 +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderId 编号,count(itemType) 订购产品数 from orderItem group by orderId +select orders.orderId 编号,count(itemType) 订购产品数 from orderItem left join orders on orderItem.orderId=orders.orderId group by orders.orderId +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: +-- 订单编号 产品类别 订购次数 总数量 +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 +select Distinct orderId 编号,itemType 产品类型,count(theNumber)订购产品数,sum(theNumber)总数量 from orderItem group by orderId ,itemType order by orderId asc \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/SQLQuery8.sql" "b/20210326\344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/SQLQuery8.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0c5c8cbb3a4470cae32fb42af015d55a49a8c151 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/SQLQuery8.sql" @@ -0,0 +1,60 @@ +create database Student +on +( + name='Stuinfo', + filename='D:\新建文件夹.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% + +) +log on +( + name='Stuinfo_log', + filename='D:\新建文件夹.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +use Student +go + +create table Stuinfo +( + stuNO varchar(5) unique not null, --学生学号 + stuName nvarchar(20), --学生姓名 + stuAge int, --学生年龄 + stuAddress nvarchar(20), --学生住址 + stuSeat int identity(1,1) unique not null, --学生座位号 + stuSex nchar(1) check(stuSex='男' or stuSex='女') --学生性别 +) +go + +create table Scoreinfo +( + examNO int primary key identity(1,1), --考试座位号 + stuNO varchar(5) references Stuinfo(stuNO), --学生学号 + writtenExan int, --笔试成绩 + labExam int, --机试成绩 + ) + +go +insert into Stuinfo values ('s2501','张秋利','20','美国硅谷','女'),('s2502','李斯文',18,'湖北武汉','男'),('s2503','马文才',22,'湖南长沙','女'), +('s2504','欧阳俊雄',21,'湖北武汉','男'),('s2505','梅超风',20,'湖北武汉','女'),('s2506','陈旋风',19,'美国硅谷','女'),('s2507','陈风',20,'美国硅谷','男') + +insert into Scoreinfo values('s2501',50,70),('s2502',60,65),('s2503',86,85),('s2504',40,80),('s2505',70,90),('s2506',85,90) +select * from Stuinfo +select * from Scoreinfo +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName,stuAge,writtenExan,labExam from Stuinfo inner join Scoreinfo on Stuinfo.stuNO=Scoreinfo.stuNO +select stuName,stuAge,writtenExan,labExam from Stuinfo left join Scoreinfo on Stuinfo.stuNO=Scoreinfo.stuNO +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select Stuinfo.stuNO,stuName,writtenExan,labExam from Stuinfo left join Scoreinfo on Stuinfo.stuNO=Scoreinfo.stuNO where writtenExan>60 and labExam>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select Stuinfo.stuNO,stuName,writtenExan,labExam from Stuinfo left join Scoreinfo on Stuinfo.stuNO=Scoreinfo.stuNO +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName,stuAge,writtenExan,labExam from Stuinfo right join Scoreinfo on Stuinfo.stuNO=Scoreinfo.stuNO where stuAge>=20 order by stuAge desc +--5.查询男女生的机试平均分 +select stuSex 性别, avg(labExam)机试平均分 from Stuinfo left join Scoreinfo on Stuinfo.stuNO=Scoreinfo.stuNO group by stuSex +--6.查询男女生的笔试总分 +select stuSex 性别, sum(labExam)机试平均分 from Stuinfo left join Scoreinfo on Stuinfo.stuNO=Scoreinfo.stuNO group by stuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/bbs.sql" "b/20210326\344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/bbs.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0c43dfc374d382488e8a75c3981da8cc43a090c6 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\235\216\346\230\214\345\256\235/bbs.sql" @@ -0,0 +1,88 @@ +create database bbs1 +on +( +name = 'bbs1', +size = 5, +maxsize = 500, +filename = 'D:\新建文件夹2.mdf', +filegrowth = 10 +) +log on +( +name = 'bbs1_log', +size = 5, +maxsize = 500, +filename = 'D:\新建文件夹2.ldf', +filegrowth = 10% +) +use bbs1 +go +create table bbsUsers --用户信息表 +( +UID int identity (1,1), --用户编号 +uName varchar(10) not null , --用户名 +uSex varchar(2) not null, --性别 +uAge int not null, --年龄 +uPoint int not null, --积分 +) +go +create table bbsSection --板块表 +( + sID int identity, --板块编号 + sName varchar(10) not null,--板块名称 + sUid int, --版主编号 +) +go +alter table bbsUsers add constraint PK_UID primary key(UID); +alter table bbsUsers add constraint UQ_uName unique(uName); +alter table bbsUsers add constraint CK_uSex check(uSex = '男' or uSex = '女'); +alter table bbsUsers add constraint CK_uAge check(uAge>=15 and uAge<=60); +alter table bbsUsers add constraint CK_uPoint check(uPoint>=0); +alter table bbsSection add constraint CK_sID primary key (sID); +alter table bbsSection add constraint FK_sUid foreign key(sUid) references bbsUsers(UID) +go +create table bbsTopic --主贴表 +( +tID int primary key identity, --主贴编号 +tUID int references bbsUsers(UID),--发帖人编号 +tSID int references bbsSection(sID),--板块编号 +tTitle varchar(100) not null,--贴纸标题 +tMsg text not null,--帖子内容 +tTime datetime ,--发帖时间 +tCount int--回复数量 +) +alter table bbsTopic alter column tMsg varchar(60) +go +create table bbsReply --回贴表 +( +rID int primary key identity,--回帖编号 + rUID int references bbsUsers(UID),--回帖人编号 + rTID int references bbsTopic(tID),--对应主贴编号 + rMsg text not null,--回帖内容 + rTime datetime --回帖时间 +) +go +insert into bbsUsers(uName,uSex,uAge,uPoint) values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) +insert into bbsSection (sName,sUid) values('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values(2 ,4,'范跑跑','谁是范跑跑 ','2008-7-8',1) +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values(3 ,1,'.NET','与JAVA的区别是什么呀? ','2008-9-1',2) +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values(1 ,3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀 ','2008-9-10',2) +insert into bbsReply(rTID,rMsg,rTime) values(1,'不认识',2008-7-11) +insert into bbsReply(rTID,rMsg,rTime) values(2,'没有区别',2008-9-11) +insert into bbsReply(rTID,rMsg,rTime) values(2,'请百度',2008-9-12) + +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select sUid'版主编号',uName'版主姓名',sName'板块名称' from bbsSection inner join bbsUsers on bbsSection.sUid=bbsUsers.UID +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID,uName,tTitle,tMsg,tTime from bbsUsers left join bbsTopic on bbsUsers.UID=bbsTopic.tID where tTime>'2008-9-8' +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sUid'版主编号',uName'版主姓名',sName'板块名称',uAge 年龄 from bbsSection inner join bbsUsers on bbsSection.sUid=bbsUsers.UID where uAge<20 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select top 1 tUID,uName,tTitle,tMsg,tCount from bbsUsers left join bbsTopic on bbsUsers.UID=bbsTopic.tID order by tCount desc +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select uName 用户姓名,sName 板块名,tSID 板块编号,count(tuid)该用户发帖总数 from bbsTopic left join bbsUsers on bbsUsers.UID=bbsTopic.tID +left join bbsSection on bbsTopic.tSID=bbsSection.sID group by tSID,uName,sName +select * from bbsSection +select * from bbsUsers +select * from bbsReply +select * from bbsTopic \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c5794f5b86b052370ab87e2ba68fb887de6e5ed1 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery1.sql" @@ -0,0 +1,103 @@ +--建数据库 +create database Dong +on +( + name='Dong', + filename='D:\test\Dong.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% + +) +log on +( + name='Dong_log', + filename='D:\test\Dong_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% + +) +--使用创建出来的数据库 +go +use Dong + +go +create table orders +( +orderId int primary key identity(1,1), +orderDate datetime not null, +) +--在数据库创建表 +go +create table orderItem +( +ItemiD int primary key identity(1,1), +orderId int references orders(orderId), +itemType varchar(20) not null, +itemName varchar(20) not null, +theNumber int not null, +theMoney money not null, +) +--测试查询 orders orderItem 表 +go +select * from orders +select * from orderItem +--添加日期 +go +insert into orders(orderDate) values('2008-01-12'),('2008-02-10'),('2008-02-15'),('2008-03-10') +--添加数据 +go +insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values +(1,'文具','笔',72,2), +(1,'文具','尺',10,1), +(1,'体育用品','篮球',1,56), +(2,'文具','笔',36,2), +(2,'文具','固体胶',20,3), +(2,'日常用品','透明胶',2,1), +(2,'体育用品','羽毛球',20,3), +(3,'文具','订书机',20,3), +(3,'文具','订书针',10,3), +(3,'文具','裁纸刀',5,5), +(4,'文具','笔',20,2), +(4,'文具','信纸',50,1), +(4,'日常用品','毛巾',4,5), +(4,'日常用品','透明胶',30,1), +(4,'体育用品','羽毛球',20,3) + + +--使用上次作业的订单数据库,完成下列题目: + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +GO +select orders.orderId 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量,theMoney 单价 from orderItem inner join orders on orders.orderId = orderItem.orderId + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +go +select orders.orderId 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称 from orderItem inner join orders on orders.orderId = orderItem.orderId where theNumber>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +go +select orders.orderId 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量,theMoney 单价,sum(theNumber*theMoney)订购总价 from orderItem inner join orders on orderItem.orderId = orders.orderId group by theMoney,orders.orderId,orderDate,itemType,itemName,theNumber,theMoney order by orders.orderId + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +go +select orders.orderId 订单编号,orderDate 订单日期,itemType 订购产品,itemName 产品名称,theNumber 订购数量,theMoney 订购单价,sum(theMoney*theNumber)订购总价 from orders inner join orderItem on orders.orderId=orderItem.orderId group by theMoney,orders.orderId,orderDate,itemType,itemName,theNumber,theMoney having theMoney>4 and theNumber>=3 + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +go +select orders.orderId 编号,theNumber 订购产品数 from orderItem inner join orders on orderItem.orderId=orders.orderId + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 +select orders.orderId 订单编号, itemType 产品类别,COUNT(*)订购次数,sum(theNumber)总数量 from orderItem inner join orders on orders.orderId = orderItem.orderId group by orders.orderId,itemType order by orders.orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery10.sql" "b/20210326\344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery10.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6acb0e8dd6b46d95edf3d0aaec5312f76b860e84 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery10.sql" @@ -0,0 +1,79 @@ +create database Student +on +( + name='Stuinfo', + filename='D:\test\Stuinfo.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% + +) +log on +( + name='Stuinfo_log', + filename='D:\test\Stuinfo_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% + +) +go +use Student + +go + +create table Stuinfo +( + stuNO varchar(5) unique not null, + stuName nvarchar(20), + stuAge int, + stuAddress nvarchar(20), + stuSeat int identity(1,1) unique not null, + stuSex nchar(1) check(stuSex='男' or stuSex='女') +) + +go + +create table Scoreinfo +( + examNO int primary key identity(1,1), + stuNO varchar(5) references Stuinfo(stuNO), + writtenExan int, + labExam int, +) + +go +insert into Scoreinfo values('s2501',50,70),('s2502',60,65),('s2503',86,85),('s2504',40,80),('s2505',70,90),('s2506',85,90) + +go +insert into Stuinfo values ('s2501','张秋利','20','美国硅谷','女'),('s2502','李斯文',18,'湖北武汉','男'),('s2503','马文才',22,'湖南长沙','女'), +('s2504','欧阳俊雄',21,'湖北武汉','男'),('s2505','梅超风',20,'湖北武汉','女'),('s2506','陈旋风',19,'美国硅谷','女'),('s2507','陈风',20,'美国硅谷','男') + +go +select * from Stuinfo +select * from Scoreinfo + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +go +select stuName 姓名,stuAge 年龄,writtenExan 笔试成绩,labExam 机试成绩 from Stuinfo inner join Scoreinfo ON Stuinfo.stuNO =Scoreinfo.stuNO + + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +go +select Scoreinfo.stuNO 学号,stuName 名字,writtenExan 笔试成绩,labExam 机试成绩 from Stuinfo inner join Scoreinfo on Scoreinfo.stuNO =Stuinfo.stuNO where writtenExan>60 and labExam>60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +go +select Stuinfo.stuNO 学号,stuName 名字,writtenExan 笔试成绩,labExam 机试成绩 from Stuinfo left join Scoreinfo on Scoreinfo.stuNO = Stuinfo.stuNO + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +go +select stuName 姓名,stuAge 年龄,writtenExan 笔试成绩,labExam 机试成绩 from Stuinfo inner join Scoreinfo on Scoreinfo.stuNO = Stuinfo.stuNO where stuAge>20 order by writtenExan desc + +--5.查询男女生的机试平均分 +go +select stuSex 性别,AVG(labExam)机试平均分 from Stuinfo inner join Scoreinfo on Scoreinfo.stuNO = Stuinfo.stuNO group by stuSex + +--6.查询男女生的笔试总分 +go +select stuSex 性别,SUM(writtenExan)笔试平均分 from Stuinfo inner join Scoreinfo on Scoreinfo.stuNO = Stuinfo.stuNO group by stuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..205bee802840e186a5af8a56077745483f78d203 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery2.sql" @@ -0,0 +1,111 @@ +use master +create database bbs +on +( + name='bbs', + filename='D:\test\bbs.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='bbs_log', + filename='D:\test\bbs_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go +use bbs +go +create table bbsUser +( + uID int identity(1,1) not null, + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) + + + +--添加约束 +-- alter table 表名 add constraint 约束名 约束类型 +go +alter table bbsUser add constraint PK_bbsUser_uID primary key(uID) +alter table bbsUser add constraint UK_bbsUser_uName unique(uName) +alter table bbsUser add constraint CK_bbsUser_uSex check(uSex='男' or uSex='女') +alter table bbsUser add constraint CK_bbsUser_uAge check(uAge>=15 and uAge<=60) +alter table bbsUser add constraint CK_bbsUser_uPoint check(uPoint>=0) + +go +alter table bbsSection add constraint PK_bbsSection_sID primary key(sID) +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUser(uID) + +go +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int references bbsUser(uID), + tSID int references bbsSection(sID), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) +go +create table bbsReply +( + rID int identity(1,1) primary key, + rUID int references bbsUser(uID), + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +go +insert into bbsUser (uName,uSex,uAge,uPoint) values ('小雨点','女','20','0') +,('逍遥','男','18','4'),('七年级生','男','19','2') + +go +select uName,uPoint into bbsPoint from bbsUser + +go +insert into bbsSection (sName,sUid) values ('技术交流',1), +('读书世界',3),('生活百科',1),('八卦区',3) + +go +insert into bbsTopic (tUID,tSID,tTitle,tMsg,tTime,tCount) values ('2','4','范跑跑','谁是范跑跑','2008-7-8','1'), +('3','1','.NET','谁是范跑跑','2008-9-1','2'),('1','3','.今年夏天最流行什么呀?','有谁知道今年夏天最流行','2008-9-10','0') + +go +insert into bbsReply (rUID,rMsg,rTime) values ('1','范跑跑是谁啊','2021-1-1'), +('2','名字不一样','2021-1-2'),('3','EMO','2021-1-3') + +go +select * from bbsUser +select * from bbsSection +select * from bbsTopic +select * from bbsReply + +--在论坛数据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 +go +select sUid 版主编号,uName 版主姓名,sName 版块名称 from bbsUser inner join bbsSection on bbsUser.uID= bbsSection.sUid + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +go +select uID 发帖人编号,uName 发贴人编号,tTitle 帖子标题,tMsg 帖子内容,tTime 发帖时间 from bbsTopic inner join bbsUser on bbsTopic.tSID=bbsUser.uID where tTime>'2008-9-1' + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +go +select sID 版主编号,uName 版主名称,sName 版块吧名称,uAge 年龄 from bbsUser inner join bbsSection on bbsUser.uID=bbsSection.sUid where uAge<20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +go +select uID发帖人编号,uName 发帖人名字,tTitle 主贴标题,tMsg 主题内容,tCount 回复数量 from bbsTopic inner join bbsUser on bbsTopic.tSID=bbsUser.uID where tCount= (select max(tCount) from bbsTopic) + +--5.在主贴表中查询每个版块中每个用户的发帖总数 +go +select tsid 版块编号,count(tSID) 发帖总数 from bbsTopic group by tUID,tSID \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery8.sql" "b/20210326\344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery8.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a6b3f2fe312b9a23d61e04b2dac9c47bf71bffeb --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\235\250\344\270\260\350\261\252/SQLQuery8.sql" @@ -0,0 +1,465 @@ +USE [master] +GO +/****** Object: Database [TestDB] Script Date: 2021/3/15 16:11:24 ******/ +CREATE DATABASE [TestDB] + CONTAINMENT = NONE + ON PRIMARY +( NAME = N'TestDB', FILENAME = N'D:\TestDB.mdf' , SIZE = 4288KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) + LOG ON +( NAME = N'TestDB_log', FILENAME = N'D:\TestDB_log.ldf' , SIZE = 1072KB , MAXSIZE = 2048GB , FILEGROWTH = 10%) +GO +ALTER DATABASE [TestDB] SET COMPATIBILITY_LEVEL = 120 +GO +IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled')) +begin +EXEC [TestDB].[dbo].[sp_fulltext_database] @action = 'enable' +end +GO +ALTER DATABASE [TestDB] SET ANSI_NULL_DEFAULT OFF +GO +ALTER DATABASE [TestDB] SET ANSI_NULLS OFF +GO +ALTER DATABASE [TestDB] SET ANSI_PADDING OFF +GO +ALTER DATABASE [TestDB] SET ANSI_WARNINGS OFF +GO +ALTER DATABASE [TestDB] SET ARITHABORT OFF +GO +ALTER DATABASE [TestDB] SET AUTO_CLOSE OFF +GO +ALTER DATABASE [TestDB] SET AUTO_SHRINK OFF +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS ON +GO +ALTER DATABASE [TestDB] SET CURSOR_CLOSE_ON_COMMIT OFF +GO +ALTER DATABASE [TestDB] SET CURSOR_DEFAULT GLOBAL +GO +ALTER DATABASE [TestDB] SET CONCAT_NULL_YIELDS_NULL OFF +GO +ALTER DATABASE [TestDB] SET NUMERIC_ROUNDABORT OFF +GO +ALTER DATABASE [TestDB] SET QUOTED_IDENTIFIER OFF +GO +ALTER DATABASE [TestDB] SET RECURSIVE_TRIGGERS OFF +GO +ALTER DATABASE [TestDB] SET ENABLE_BROKER +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF +GO +ALTER DATABASE [TestDB] SET DATE_CORRELATION_OPTIMIZATION OFF +GO +ALTER DATABASE [TestDB] SET TRUSTWORTHY OFF +GO +ALTER DATABASE [TestDB] SET ALLOW_SNAPSHOT_ISOLATION OFF +GO +ALTER DATABASE [TestDB] SET PARAMETERIZATION SIMPLE +GO +ALTER DATABASE [TestDB] SET READ_COMMITTED_SNAPSHOT OFF +GO +ALTER DATABASE [TestDB] SET HONOR_BROKER_PRIORITY OFF +GO +ALTER DATABASE [TestDB] SET RECOVERY FULL +GO +ALTER DATABASE [TestDB] SET MULTI_USER +GO +ALTER DATABASE [TestDB] SET PAGE_VERIFY CHECKSUM +GO +ALTER DATABASE [TestDB] SET DB_CHAINING OFF +GO +ALTER DATABASE [TestDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) +GO +ALTER DATABASE [TestDB] SET TARGET_RECOVERY_TIME = 0 SECONDS +GO +ALTER DATABASE [TestDB] SET DELAYED_DURABILITY = DISABLED +GO +EXEC sys.sp_db_vardecimal_storage_format N'TestDB', N'ON' +GO +USE [TestDB] +GO +/****** Object: Table [dbo].[ClassInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[ClassInfo]( + [ClassId] [int] IDENTITY(1,1) NOT NULL, + [ClassName] [nvarchar](20) NOT NULL, +PRIMARY KEY CLUSTERED +( + [ClassId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[CourseInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[CourseInfo]( + [CourseId] [int] IDENTITY(1,1) NOT NULL, + [CourseName] [nvarchar](50) NOT NULL, + [CourseCredit] [int] NULL DEFAULT ((1)), +PRIMARY KEY CLUSTERED +( + [CourseId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[Scores] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[Scores]( + [ScoreId] [int] IDENTITY(1,1) NOT NULL, + [StuId] [int] NULL, + [CourseId] [int] NULL, + [Score] [int] NULL DEFAULT ((0)), +PRIMARY KEY CLUSTERED +( + [ScoreId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[StuInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[StuInfo]( + [StuId] [int] IDENTITY(1,1) NOT NULL, + [ClassId] [int] NULL, + [StuName] [nvarchar](10) NOT NULL, + [StuSex] [nvarchar](1) NULL DEFAULT ('男'), + [StuBrithday] [date] NULL, + [StuPhone] [nvarchar](11) NULL, + [StuProvince] [nvarchar](200) NULL, + [CreateDate] [datetime] NULL DEFAULT (getdate()), + [StuAge] [int] NULL, +PRIMARY KEY CLUSTERED +( + [StuId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] ON + +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (1, N'软件1班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (2, N'软件2班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (3, N'软件3班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (4, N'软件4班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (5, N'软件5班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (6, N'软件6班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (7, N'软件7班') +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] ON + +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (1, N'计算机基础', 3) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (2, N'HTML+CSS网页制作', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (3, N'JAVA编程基础', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (4, N'SQL Server数据库基础', 4) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (5, N'C#面向对象编程', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (6, N'Winform桌面应用程序设计', 5) +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[Scores] ON + +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (1, 1, 1, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (2, 1, 2, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (3, 1, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (4, 1, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (5, 2, 1, 60) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (6, 2, 2, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (7, 2, 3, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (8, 2, 4, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (9, 3, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (10, 3, 2, 45) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (11, 3, 3, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (12, 3, 4, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (13, 4, 1, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (14, 4, 2, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (15, 4, 3, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (16, 4, 4, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (17, 5, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (18, 5, 2, 79) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (19, 5, 3, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (20, 5, 4, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (21, 6, 1, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (22, 6, 2, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (23, 6, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (24, 6, 5, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (25, 7, 1, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (26, 7, 2, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (27, 7, 3, 92) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (28, 7, 5, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (29, 8, 1, 58) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (30, 8, 2, 59) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (31, 8, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (32, 8, 5, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (33, 9, 1, 48) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (34, 9, 2, 67) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (35, 9, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (36, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (37, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (38, 1, 1, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (39, 1, 2, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (40, 1, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (41, 1, 4, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (42, 2, 1, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (43, 2, 2, 82) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (44, 2, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (45, 2, 4, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (46, 3, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (47, 3, 2, 50) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (48, 3, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (49, 3, 4, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (50, 4, 1, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (51, 4, 2, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (52, 4, 3, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (53, 4, 4, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (54, 5, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (55, 5, 2, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (56, 5, 3, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (57, 5, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (58, 6, 1, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (59, 6, 2, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (60, 6, 3, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (61, 6, 5, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (62, 7, 1, 89) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (63, 7, 2, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (64, 7, 3, 97) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (65, 7, 5, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (66, 8, 1, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (67, 8, 2, 64) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (68, 8, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (69, 8, 5, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (70, 9, 1, 53) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (71, 9, 2, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (72, 9, 3, 76) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (73, 9, 5, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (74, 9, 5, 61) +GO +SET IDENTITY_INSERT [dbo].[Scores] OFF +GO +SET IDENTITY_INSERT [dbo].[StuInfo] ON + +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (1, 1, N'刘正', N'男', CAST(N'2002-08-02' AS Date), N'13245678121', N'广西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (2, 1, N'黄贵', N'男', CAST(N'2003-07-02' AS Date), N'13345678121', N'江西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (3, 1, N'陈美', N'女', CAST(N'2002-07-22' AS Date), N'13355678125', N'福建省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (4, 2, N'江文', N'男', CAST(N'2001-07-02' AS Date), N'13347678181', N'湖南省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (5, 2, N'钟琪', N'女', CAST(N'2004-01-13' AS Date), N'13345778129', N'安徽省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (6, 3, N'曾小林', N'男', CAST(N'2005-05-15' AS Date), N'13345378563', N'安徽省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (7, 3, N'欧阳天天', N'女', CAST(N'2000-08-19' AS Date), N'13347878121', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (8, 3, N'李逍遥', N'男', CAST(N'1999-09-02' AS Date), N'13345678557', N'广东省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 22) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (9, 4, N'刘德华', N'男', CAST(N'1995-06-11' AS Date), N'15345679557', N'福建省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 26) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (10, 4, N'刘翔', N'男', CAST(N'1996-07-09' AS Date), N'18346679589', N'江西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (11, 4, N'曾小贤', N'男', CAST(N'2003-07-02' AS Date), N'18348979589', N'湖南省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (12, 5, N'刘德华', N'男', CAST(N'2002-07-02' AS Date), N'18348979509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (13, 5, N'陈天翔', N'男', CAST(N'2003-07-02' AS Date), N'18348079509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (14, 5, N'刘能', N'男', CAST(N'2005-08-02' AS Date), N'13245678122', N'广西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (15, 5, N'钟馗', N'男', CAST(N'2004-08-02' AS Date), N'13245678123', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (16, 5, N'钟吴艳', N'女', CAST(N'2002-08-02' AS Date), N'13245678124', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (17, 5, N'刘欢', N'男', CAST(N'2001-07-02' AS Date), N'13245678125', N'湖南省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (18, 5, N'张庭', N'女', CAST(N'2000-07-02' AS Date), N'13245678126', N'江西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (19, 5, N'曹植', N'男', CAST(N'2000-08-02' AS Date), N'13245678127', N'福建省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (20, 5, N'曹操', N'男', CAST(N'2002-08-02' AS Date), N'13245678128', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (21, 5, N'孙尚香', N'女', CAST(N'2003-08-02' AS Date), N'13245678129', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (22, 3, N'老1', N'女', CAST(N'2002-08-02' AS Date), N'13245678130', N'广东省', CAST(N'2021-03-14 17:02:36.347' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (24, 2, N'老2', N'男', CAST(N'2002-08-03' AS Date), N'13345678945', NULL, CAST(N'2021-03-14 17:03:37.733' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (25, 4, N'老3', N'男', NULL, N'13645987545', N'广东省', CAST(N'2021-03-14 17:03:43.307' AS DateTime), NULL) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (28, 5, N'老4', N'男', CAST(N'2006-03-05' AS Date), N'13456987456', NULL, CAST(N'2021-03-14 17:04:03.957' AS DateTime), 15) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (29, 5, N'老5', N'女', CAST(N'1998-04-12' AS Date), N'15978456123', NULL, CAST(N'2021-03-14 17:04:47.103' AS DateTime), 23) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (30, 4, N'老6', N'男', CAST(N'1996-08-06' AS Date), N'18945674561', NULL, CAST(N'2021-03-14 17:05:04.990' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (31, 3, N'老7', N'女', CAST(N'1997-04-06' AS Date), N'18845678912', NULL, CAST(N'2021-03-14 17:05:20.570' AS DateTime), 24) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (32, 2, N'老10', N'女', CAST(N'1998-08-09' AS Date), N'19945645612', NULL, CAST(N'2021-03-14 17:06:08.107' AS DateTime), 23) +GO +SET IDENTITY_INSERT [dbo].[StuInfo] OFF +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__CourseIn__9526E2773AB7BECE] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[CourseInfo] ADD UNIQUE NONCLUSTERED +( + [CourseName] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__StuInfo__2D85FC63AF6FC6FA] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[StuInfo] ADD UNIQUE NONCLUSTERED +( + [StuPhone] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([CourseId]) +REFERENCES [dbo].[CourseInfo] ([CourseId]) +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([StuId]) +REFERENCES [dbo].[StuInfo] ([StuId]) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD FOREIGN KEY([ClassId]) +REFERENCES [dbo].[ClassInfo] ([ClassId]) +ON DELETE SET NULL +GO +ALTER TABLE [dbo].[CourseInfo] WITH CHECK ADD CHECK (([CourseCredit]>=(1) AND [CourseCredit]<=(5))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK ((len([StuPhone])=(11))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK (([StuSex]='女' OR [StuSex]='男')) +GO +USE [master] +GO +ALTER DATABASE [TestDB] SET READ_WRITE +GO + +select * from ClassInfo +select * from CourseInfo +select * from Scores +select * from StuInfo + +--统计每个班的男生数 +select ClassId 班级,COUNT(StuSex)男生人数 from StuInfo where StuSex='男' group by ClassId +--统计每个班的男、女生数 +select ClassId 班级, StuSex 性别,COUNT(StuSex)人数 from StuInfo group by ClassId , StuSex order by ClassId +--统计每个班的福建人数 +select ClassId 班级, COUNT(StuProvince)福建省人数 from StuInfo group by ClassId +--统计每个班的各个省的总人数 +select ClassId 班级,StuProvince 省份,COUNT(*)总人数 from StuInfo group by ClassId , StuProvince order by ClassId +--统计每个省的女生数 +select StuProvince 省份,COUNT(StuSex)女生人数 from StuInfo where StuSex='女' group by StuProvince +--统计每个省的男、女生数 +select StuProvince 省份, StuSex 性别,COUNT(*)人数 from StuInfo group by StuProvince,StuSex order by StuProvince +--统计每个学生的考试总分、平均分 +select StuId 学生ID , SUM(score)总分,AVG(score)平均分 from Scores group by StuId +--统计出考试总分大于620的学生的考试总分 +select StuId 学生ID,SUM(score)总分 from Scores group by StuId HAVING SUM(score)>620 +--统计出每门考试成绩最高分和最低分 +select CourseID 科目,max(score)最高分,min(score)最低分 from Scores group by CourseID +--统计出每个学生的各门成绩的平均分 +select StuId 学生ID,CourseId 科目 , Avg(score)平均分 from Scores group by StuId,CourseId order by StuId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\235\250\345\270\206/ABC.sql" "b/20210326\344\275\234\344\270\232/\346\235\250\345\270\206/ABC.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5406a2a3b26b08b70f5d0048ed4892152fb5d0a9 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\235\250\345\270\206/ABC.sql" @@ -0,0 +1,112 @@ +use master +go + +create database ABC +on +( + name='ABC', + filename='D:\test\ABC.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='ABC_log', + filename='D:\test\ABC_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +go +use ABC +go + +create table orders +( + orderId int primary key identity , + orderDate datetime +) +go + +create table orderItem +( + ItemiD int primary key identity , + orderId int references orders(orderId), + itemType varchar(10), + itemName varchar(10), + theNumber int , + theMoney int +) +go + +insert orders values +('2008-01-12'),('2008-02-10'), +('2008-02-15'),('2008-03-10') +go + +insert orderItem values +('1','文具','笔',72,2), +('1','文具','尺',10,1), +('1','体育用品','篮球',1,56), +('2','文具','笔',36,2), +('2','文具','固体胶',20,3), +('2','日常用品','透明胶',2,1), +('2','体育用品','羽毛球',20,3), +('3','文具','订书机',20,3), +('3','文具','订书针',10,3), +('3','文具','裁纸刀',5,5), +('4','文具','笔',20,2), +('4','文具','信纸',50,1), +('4','日常用品','毛巾',4,5), +('4','日常用品','透明胶',30,1), +('4','体育用品','羽毛球',20,3) +go + +select SUM(theNumber)as 总数 from dbo.orderItem +go + +select SUM(theNumber) as 数量 ,AVG(theMoney) as 平均单价 from dbo.orderItem where orderId<3 group by orderId having AVG(theMoney)<10 +go + +select SUM(theNumber) as 数量,AVG(theMoney) as 平均单价 from dbo.orderItem where theNumber<50 group by orderId having AVG(theMoney)<10 +go + +select '文具:' as 类型,COUNT(itemType) 数量 from orderItem where itemType='文具' union +select '体育用品:' as 类型,COUNT(itemType) 数量 from orderItem where itemType='体育用品' union +select '日常用品:' as 类型, COUNT(itemType) 数量 from orderItem where itemType='日常用品' +go + +select itemType,SUM(theNumber) as 数量,AVG(theMoney) as 平均单价 from dbo.orderItem where theNumber>100 group by itemType +go + +select itemName as 产品名称,COUNT(itemName) as 订购次数,SUM(theNumber) as 总数量,AVG(theMoney) as 平均单价 from dbo.orderItem group by itemName +go + +select orders.orderId,orderDate,itemType,itemName,theNumber,theMoney from orderItem +inner join orders on orderItem.orderId = orders.orderId +go + +select orderDate,itemType,itemName from orderItem +inner join orders on orderItem.orderId = orders.orderId +where theNumber>50 +go + +select orders.orderId,orderDate,itemType,itemName,theNumber,theMoney * theNumber as 订购总价 from orderItem +inner join orders on orderItem.orderId = orders.orderId +go + +select orders.orderId,orderDate,itemType,itemName,theNumber,theMoney * theNumber as 订购总价 from orderItem +inner join orders on orderItem.orderId = orders.orderId +where theMoney > 5 and theNumber > 50 +go + +select orders.orderId as 编号,count(*) as 订购产品数 from orderItem +inner join orders on orderItem.orderId = orders.orderId +group by orders.orderId + +select orders.orderId as 订单编号,itemType as 产品类别,count(*)as 订购次数,sum(theNumber) as 总数量 from orderItem +inner join orders on orderItem.orderId = orders.orderId +group by orders.orderId,itemType +order by orders.orderId + diff --git "a/20210326\344\275\234\344\270\232/\346\235\250\345\270\206/BBS.sql" "b/20210326\344\275\234\344\270\232/\346\235\250\345\270\206/BBS.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7f2357982a9485b531b5131c4a4536ed6481e852 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\235\250\345\270\206/BBS.sql" @@ -0,0 +1,176 @@ +use master +go + +create database bbs +on +( + name='bbs', + filename='D:\test\bbs.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='bbs_log', + filename='D:\test\bbs_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) + +go +use bbs +go + +create table bbsUsers +( + UuId int identity(1,1), + UName varchar(10) not null , + USex varchar(2) , + UAge int , + UPoint int not null +) +go + +alter table bbsUsers add constraint PK_UuId primary key (UuId) +go +alter table bbsUsers add constraint UQ_UName unique (UName) +go +alter table bbsUsers add constraint CK_USex check(USex='男'or USex='女') +go +alter table bbsUsers add constraint CK_UAge check(UAge>=15 and UAge<=60) +go +alter table bbsUsers add constraint CK_UPoint check(Upoint>=0) +go + +create table bbsSection +( + Ssid int identity(1,1) not null, + SName varchar(10) not null, + SuId int, +) +go + +alter table bbsSection add constraint PK_Ssid primary key (Ssid) +go +alter table bbsSection add constraint FK_SuId foreign key(SuId) references bbsUsers(UuId) +go + +create table bbsTopic +( + TtId int primary key identity(1,1), + TuId int references bbsUsers(Uuid), + TsId int references bbsSection(SsId), + TTitlr varchar(100) not null, + TMsg text, + TTime datetime, + TCount int +) +go + +create table bbsReply +( + RrId int primary key identity(1,1), + RuId int references bbsUsers(UuId), + RtId int references bbsTopic(TtId), + RMsg text not null, + RTime datetime, +) +go + +insert bbsUsers (UName,USex,UAge,UPoint) +select '小雨点','女',20,0 union +select '逍遥','男',18,4 union +select '七年级生','男',19,2 +go + +select UName ,UPoint into bbsPoint from bbsUsers +go + +insert into bbsSection(SName,SuId)values +('技术交流',2), +('读书世界',3), +('生活百科',2), +('八卦区',3) +go + +insert into bbsTopic(TuId,TsId,TTitlr,TMsg,TTime,TCount)values +(3,4,'范跑跑','谁是范跑跑','2008-7-8','1'), +(2,1,'.NET ','与JAVA的区别是什么呀?','2008-9-1','2'), +(4,3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?','2008-9-10','0'), +(3,3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?','2008-9-10','0'), +(4,1,'.NET ','与JAVA的区别是什么呀?','2008-9-1','0'), +(2,4,'范跑跑','范跑跑谁是???','2008-7-8','0') +go + +insert into bbsReply(RuId,RMsg,RTime,RtId)values +('2','范跑跑 ','2008-7-8','1'), +('3','NET ','2008-9-1','2'), +('4','.NET ','2008-9-10','2') +go + +update bbsUsers set UPoint=UPoint+10 where UuId=3 + +truncate table bbsReply +go + +select TsId,COUNT(TtId) as 发帖总数 from bbsTopic group by TsId +go + +select RtId, COUNT(RrId) as 回帖总数量 from bbsReply group by RtId +go + +select TuId,COUNT(TtId) as 发帖总数 from bbsTopic group by TuId +go + +select TuId,SUM(TCount) as 回复数量总和 from bbsTopic group by TuId +go + +select TsId,AVG(TCount) as 平均回复数量 from bbsTopic group by TsId having AVG(TCount)>3 +go + +select * from bbsUsers where UPoint=(select MAX(UPoint) from bbsUsers) +go + +select * from bbsTopic where TTitlr like('%快乐%') or TMsg like('%快乐%') +go + +select * from bbsUsers where UAge>=15 and UAge<=20 and UPoint>=10 +go + +select * from bbsUsers where UName like('小%') and UName like('__大%') +go + +select TTitlr as 标题, TMsg as 内容 from bbsTopic where TTime>='2008-9-10 12:00:00' and TCount>=10 + go + + select TuId ,TCount from bbsTopic where TTitlr like ('%!') + go + + select UuId,UName,SName from bbsTopic + inner join bbsUsers on bbsTopic.TuId = bbsUsers.UuId + inner join bbsSection on bbsTopic.TsId = bbsSection.Ssid + go + + select UuId,UName,TTitlr,TMsg,TTime from bbsTopic + inner join bbsUsers on bbsTopic.TuId = bbsUsers.UuId +where TTime >= '2008-09-15' +go + + select UuId,UName,SName from bbsTopic + inner join bbsUsers on bbsTopic.TuId = bbsUsers.UuId + inner join bbsSection on bbsTopic.TsId = bbsSection.Ssid + where UAge<20 + go + + select top 1 UuId,UName,TTitlr,TMsg,TCount from bbsTopic + inner join bbsUsers on bbsTopic.TuId = bbsUsers.UuId + order by TCount desc + go + + + select SName,UName,count(TCount) from bbsTopic + inner join bbsSection on bbsTopic.TsId = bbsSection.Ssid + inner join bbsUsers on bbsUsers.UuId = bbsTopic.TuId + group by UuId,TsId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\235\250\345\270\206/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\346\235\250\345\270\206/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7940a80b63176ad4abd72a616b274b1a66f35e04 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\235\250\345\270\206/SQLQuery1.sql" @@ -0,0 +1,32 @@ +select '男生的数量',count(StuSex) from StuInfo group by StuSex having StuSex='男' +go + +select ClassId,'男生的数量',count(StuSex) from StuInfo where StuSex='男' group by ClassId union +select ClassId,'女生的数量',count(StuSex) from StuInfo where StuSex='女' group by ClassId +go + +select ClassId,Stusex,count(StuSex) from StuInfo group by ClassId,StuSex +go + +select ClassId,count(StuProvince)福建省的人数 from StuInfo group by ClassId,StuProvince having StuProvince='福建省' +go + +select ClassId,StuProvince,count(StuProvince) from StuInfo group by ClassId,StuProvince order by ClassId +go + +select StuProvince,count(StuSex)女生人数 from StuInfo where StuSex='女' group by StuProvince +go + +select StuProvince,StuSex,count(StuSex)人数 from StuInfo group by StuProvince,StuSex +go + +select StuId 学号,sum(Score)总分,avg(Score)平均分 from Scores group by StuId +go + +select StuId 学号,sum(Score)总分 from Scores group by StuId having sum(Score)>620 +go + +select CourseId,max(Score)最高分,min(Score)最低分 from Scores group by CourseId +go + +select StuId,CourseId,avg(score) from Scores group by StuId,CourseId order by StuId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\235\250\345\270\206/Student.sql" "b/20210326\344\275\234\344\270\232/\346\235\250\345\270\206/Student.sql" new file mode 100644 index 0000000000000000000000000000000000000000..541f42ced9221c615d2353d8f0b25ab96f5b9d45 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\235\250\345\270\206/Student.sql" @@ -0,0 +1,93 @@ +use master +go + +create database Dwu +on +( +name='Dwu', +filename='D:\test\Dwu.mdf', +size=5mb, +maxsize=100mb, +filegrowth=10mb +) +log on +( +name='Dwu_log', +filename='D:\test\Dwu_log.ldf', +size=5mb, +maxsize=100mb, +filegrowth=10mb +) + +go +use Dwu +go + +create table stuinfo +( +stuNo char(5) primary key not null, +stuName nvarchar(20) not null, +stuAge char(3) not null, +stuAddress nvarchar(200), +stuSeat int identity(1,1)not null, +stuSex char(1) check(stuSex in (1,0)) default(1) not null +) +go + +insert into stuinfo(stuNo,stuName,stuAge,stuAddress,stuSex) values +('s2501','张秋利',20,'美国硅谷',1), +('s2502','李斯文',18,'湖北武汉',0), +('s2503','马文才',22,'湖南长沙',1), +('s2504','欧阳俊雄',21,'湖北武汉',0), +('s2505','梅超风',20,'湖北武汉',1), +('s2506','陈旋风',19,'美国硅谷',1), +('s2507','陈凤',20,'美国硅谷',0) +go + +create table stuexam +( +examNo int primary key identity(1,1), +stuNo char(5) references stuinfo(stuNo) not null, +writtenExam int not null, +labExam int not null +) +go + +insert stuexam(stuNo,writtenExam,labExam)values +('s2501',50,70), +('s2502',60,65), +('s2503',86,85), +('s2504',40,80), +('s2505',70,90), +('s2506',85,90) +go + +select stuName,stuAge,writtenExam,labExam from stuexam +inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +go + + +select stuName,stuAge,writtenExam,labExam from stuexam +inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +where writtenExam > 60 and labExam > 60 +go + +select stuName,stuAge,writtenExam,labExam from stuinfo +left join stuexam on stuexam.stuNo = stuinfo.stuNo +go + +select stuName,stuAge,writtenExam,labExam from stuexam +inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +where stuAge >= 20 +order by writtenExam desc +go + +select stuSex,avg(labExam) from stuexam +inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +group by stuSex +go + +select stuSex,sum(writtenExam) from stuexam +inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +group by stuSex +go \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a7a6a21be056f3aadb542becc55f190be5ba792d --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/SQLQuery1.sql" @@ -0,0 +1,466 @@ +USE [master] +GO +/****** Object: Database [TestDB] Script Date: 2021/3/15 16:11:24 ******/ +CREATE DATABASE [TestDB] + CONTAINMENT = NONE + ON PRIMARY +( NAME = N'TestDB', FILENAME = N'D:\TestDB.mdf' , SIZE = 4288KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) + LOG ON +( NAME = N'TestDB_log', FILENAME = N'D:\TestDB_log.ldf' , SIZE = 1072KB , MAXSIZE = 2048GB , FILEGROWTH = 10%) +GO +ALTER DATABASE [TestDB] SET COMPATIBILITY_LEVEL = 120 +GO +IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled')) +begin +EXEC [TestDB].[dbo].[sp_fulltext_database] @action = 'enable' +end +GO +ALTER DATABASE [TestDB] SET ANSI_NULL_DEFAULT OFF +GO +ALTER DATABASE [TestDB] SET ANSI_NULLS OFF +GO +ALTER DATABASE [TestDB] SET ANSI_PADDING OFF +GO +ALTER DATABASE [TestDB] SET ANSI_WARNINGS OFF +GO +ALTER DATABASE [TestDB] SET ARITHABORT OFF +GO +ALTER DATABASE [TestDB] SET AUTO_CLOSE OFF +GO +ALTER DATABASE [TestDB] SET AUTO_SHRINK OFF +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS ON +GO +ALTER DATABASE [TestDB] SET CURSOR_CLOSE_ON_COMMIT OFF +GO +ALTER DATABASE [TestDB] SET CURSOR_DEFAULT GLOBAL +GO +ALTER DATABASE [TestDB] SET CONCAT_NULL_YIELDS_NULL OFF +GO +ALTER DATABASE [TestDB] SET NUMERIC_ROUNDABORT OFF +GO +ALTER DATABASE [TestDB] SET QUOTED_IDENTIFIER OFF +GO +ALTER DATABASE [TestDB] SET RECURSIVE_TRIGGERS OFF +GO +ALTER DATABASE [TestDB] SET ENABLE_BROKER +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF +GO +ALTER DATABASE [TestDB] SET DATE_CORRELATION_OPTIMIZATION OFF +GO +ALTER DATABASE [TestDB] SET TRUSTWORTHY OFF +GO +ALTER DATABASE [TestDB] SET ALLOW_SNAPSHOT_ISOLATION OFF +GO +ALTER DATABASE [TestDB] SET PARAMETERIZATION SIMPLE +GO +ALTER DATABASE [TestDB] SET READ_COMMITTED_SNAPSHOT OFF +GO +ALTER DATABASE [TestDB] SET HONOR_BROKER_PRIORITY OFF +GO +ALTER DATABASE [TestDB] SET RECOVERY FULL +GO +ALTER DATABASE [TestDB] SET MULTI_USER +GO +ALTER DATABASE [TestDB] SET PAGE_VERIFY CHECKSUM +GO +ALTER DATABASE [TestDB] SET DB_CHAINING OFF +GO +ALTER DATABASE [TestDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) +GO +ALTER DATABASE [TestDB] SET TARGET_RECOVERY_TIME = 0 SECONDS +GO +ALTER DATABASE [TestDB] SET DELAYED_DURABILITY = DISABLED +GO +EXEC sys.sp_db_vardecimal_storage_format N'TestDB', N'ON' +GO +USE [TestDB] +GO +/****** Object: Table [dbo].[ClassInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[ClassInfo]( + [ClassId] [int] IDENTITY(1,1) NOT NULL, + [ClassName] [nvarchar](20) NOT NULL, +PRIMARY KEY CLUSTERED +( + [ClassId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[CourseInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[CourseInfo]( + [CourseId] [int] IDENTITY(1,1) NOT NULL, + [CourseName] [nvarchar](50) NOT NULL, + [CourseCredit] [int] NULL DEFAULT ((1)), +PRIMARY KEY CLUSTERED +( + [CourseId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[Scores] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[Scores]( + [ScoreId] [int] IDENTITY(1,1) NOT NULL, + [StuId] [int] NULL, + [CourseId] [int] NULL, + [Score] [int] NULL DEFAULT ((0)), +PRIMARY KEY CLUSTERED +( + [ScoreId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[StuInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[StuInfo]( + [StuId] [int] IDENTITY(1,1) NOT NULL, + [ClassId] [int] NULL, + [StuName] [nvarchar](10) NOT NULL, + [StuSex] [nvarchar](1) NULL DEFAULT ('男'), + [StuBrithday] [date] NULL, + [StuPhone] [nvarchar](11) NULL, + [StuProvince] [nvarchar](200) NULL, + [CreateDate] [datetime] NULL DEFAULT (getdate()), + [StuAge] [int] NULL, +PRIMARY KEY CLUSTERED +( + [StuId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] ON + +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (1, N'软件1班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (2, N'软件2班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (3, N'软件3班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (4, N'软件4班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (5, N'软件5班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (6, N'软件6班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (7, N'软件7班') +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] ON + +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (1, N'计算机基础', 3) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (2, N'HTML+CSS网页制作', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (3, N'JAVA编程基础', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (4, N'SQL Server数据库基础', 4) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (5, N'C#面向对象编程', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (6, N'Winform桌面应用程序设计', 5) +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[Scores] ON + +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (1, 1, 1, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (2, 1, 2, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (3, 1, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (4, 1, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (5, 2, 1, 60) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (6, 2, 2, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (7, 2, 3, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (8, 2, 4, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (9, 3, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (10, 3, 2, 45) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (11, 3, 3, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (12, 3, 4, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (13, 4, 1, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (14, 4, 2, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (15, 4, 3, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (16, 4, 4, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (17, 5, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (18, 5, 2, 79) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (19, 5, 3, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (20, 5, 4, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (21, 6, 1, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (22, 6, 2, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (23, 6, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (24, 6, 5, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (25, 7, 1, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (26, 7, 2, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (27, 7, 3, 92) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (28, 7, 5, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (29, 8, 1, 58) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (30, 8, 2, 59) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (31, 8, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (32, 8, 5, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (33, 9, 1, 48) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (34, 9, 2, 67) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (35, 9, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (36, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (37, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (38, 1, 1, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (39, 1, 2, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (40, 1, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (41, 1, 4, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (42, 2, 1, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (43, 2, 2, 82) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (44, 2, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (45, 2, 4, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (46, 3, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (47, 3, 2, 50) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (48, 3, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (49, 3, 4, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (50, 4, 1, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (51, 4, 2, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (52, 4, 3, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (53, 4, 4, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (54, 5, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (55, 5, 2, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (56, 5, 3, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (57, 5, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (58, 6, 1, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (59, 6, 2, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (60, 6, 3, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (61, 6, 5, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (62, 7, 1, 89) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (63, 7, 2, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (64, 7, 3, 97) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (65, 7, 5, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (66, 8, 1, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (67, 8, 2, 64) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (68, 8, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (69, 8, 5, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (70, 9, 1, 53) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (71, 9, 2, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (72, 9, 3, 76) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (73, 9, 5, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (74, 9, 5, 61) +GO +SET IDENTITY_INSERT [dbo].[Scores] OFF +GO +SET IDENTITY_INSERT [dbo].[StuInfo] ON + +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (1, 1, N'刘正', N'男', CAST(N'2002-08-02' AS Date), N'13245678121', N'广西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (2, 1, N'黄贵', N'男', CAST(N'2003-07-02' AS Date), N'13345678121', N'江西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (3, 1, N'陈美', N'女', CAST(N'2002-07-22' AS Date), N'13355678125', N'福建省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (4, 2, N'江文', N'男', CAST(N'2001-07-02' AS Date), N'13347678181', N'湖南省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (5, 2, N'钟琪', N'女', CAST(N'2004-01-13' AS Date), N'13345778129', N'安徽省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (6, 3, N'曾小林', N'男', CAST(N'2005-05-15' AS Date), N'13345378563', N'安徽省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (7, 3, N'欧阳天天', N'女', CAST(N'2000-08-19' AS Date), N'13347878121', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (8, 3, N'李逍遥', N'男', CAST(N'1999-09-02' AS Date), N'13345678557', N'广东省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 22) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (9, 4, N'刘德华', N'男', CAST(N'1995-06-11' AS Date), N'15345679557', N'福建省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 26) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (10, 4, N'刘翔', N'男', CAST(N'1996-07-09' AS Date), N'18346679589', N'江西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (11, 4, N'曾小贤', N'男', CAST(N'2003-07-02' AS Date), N'18348979589', N'湖南省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (12, 5, N'刘德华', N'男', CAST(N'2002-07-02' AS Date), N'18348979509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (13, 5, N'陈天翔', N'男', CAST(N'2003-07-02' AS Date), N'18348079509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (14, 5, N'刘能', N'男', CAST(N'2005-08-02' AS Date), N'13245678122', N'广西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (15, 5, N'钟馗', N'男', CAST(N'2004-08-02' AS Date), N'13245678123', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (16, 5, N'钟吴艳', N'女', CAST(N'2002-08-02' AS Date), N'13245678124', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (17, 5, N'刘欢', N'男', CAST(N'2001-07-02' AS Date), N'13245678125', N'湖南省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (18, 5, N'张庭', N'女', CAST(N'2000-07-02' AS Date), N'13245678126', N'江西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (19, 5, N'曹植', N'男', CAST(N'2000-08-02' AS Date), N'13245678127', N'福建省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (20, 5, N'曹操', N'男', CAST(N'2002-08-02' AS Date), N'13245678128', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (21, 5, N'孙尚香', N'女', CAST(N'2003-08-02' AS Date), N'13245678129', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (22, 3, N'老1', N'女', CAST(N'2002-08-02' AS Date), N'13245678130', N'广东省', CAST(N'2021-03-14 17:02:36.347' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (24, 2, N'老2', N'男', CAST(N'2002-08-03' AS Date), N'13345678945', NULL, CAST(N'2021-03-14 17:03:37.733' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (25, 4, N'老3', N'男', NULL, N'13645987545', N'广东省', CAST(N'2021-03-14 17:03:43.307' AS DateTime), NULL) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (28, 5, N'老4', N'男', CAST(N'2006-03-05' AS Date), N'13456987456', NULL, CAST(N'2021-03-14 17:04:03.957' AS DateTime), 15) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (29, 5, N'老5', N'女', CAST(N'1998-04-12' AS Date), N'15978456123', NULL, CAST(N'2021-03-14 17:04:47.103' AS DateTime), 23) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (30, 4, N'老6', N'男', CAST(N'1996-08-06' AS Date), N'18945674561', NULL, CAST(N'2021-03-14 17:05:04.990' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (31, 3, N'老7', N'女', CAST(N'1997-04-06' AS Date), N'18845678912', NULL, CAST(N'2021-03-14 17:05:20.570' AS DateTime), 24) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (32, 2, N'老10', N'女', CAST(N'1998-08-09' AS Date), N'19945645612', NULL, CAST(N'2021-03-14 17:06:08.107' AS DateTime), 23) +GO +SET IDENTITY_INSERT [dbo].[StuInfo] OFF +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__CourseIn__9526E2773AB7BECE] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[CourseInfo] ADD UNIQUE NONCLUSTERED +( + [CourseName] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__StuInfo__2D85FC63AF6FC6FA] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[StuInfo] ADD UNIQUE NONCLUSTERED +( + [StuPhone] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([CourseId]) +REFERENCES [dbo].[CourseInfo] ([CourseId]) +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([StuId]) +REFERENCES [dbo].[StuInfo] ([StuId]) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD FOREIGN KEY([ClassId]) +REFERENCES [dbo].[ClassInfo] ([ClassId]) +ON DELETE SET NULL +GO +ALTER TABLE [dbo].[CourseInfo] WITH CHECK ADD CHECK (([CourseCredit]>=(1) AND [CourseCredit]<=(5))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK ((len([StuPhone])=(11))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK (([StuSex]='女' OR [StuSex]='男')) +GO +USE [master] +GO +ALTER DATABASE [TestDB] SET READ_WRITE +GO + + +select * from Stuinfo +select * from Scores +select * from Courseinfo +select * from Classinfo + +--统计 每个班 的 男生数 +select ClassId,count(Stusex)男生数 from Stuinfo where Stusex='男' group by ClassId +--统计 每个班 的 男、女生数 +select ClassId,Stusex,count(*)人数 from Stuinfo group by ClassId,Stusex +--统计 每个班 的 福建人数 +select ClassId,count(*)福建人数 from Stuinfo where StuProvince='福建省' group by ClassId +--统计 每个班 的 各个省的总人数 +select ClassId,StuProvince 省份,count(*)人数 from Stuinfo group by ClassId,StuProvince order by ClassID +--统计 每个省 的 女生数 +select StuProvince 省份,count(StuSex)女生人数 from Stuinfo where StuSex='女' group by StuProvince +--统计 每个省 的 男、女生数 +select StuProvince,StuSex,count(*)人数 from Stuinfo group by StuProvince,StuSex +--统计 每个学生 的 考试总分、平均分 +select StuId,SUM(Score)考试总分,AVG(Score)平均分 from Scores group by StuId +--统计出 考试总分大于620 的学生 的考试总分 +select StuId,SUM(Score)总分 from Scores group by StuId having SUM(Score)>620 +--统计出 每门考试 成绩最高分和最低分 +select CourseId,MAX(Score)最高分,MIN(Score)最低分 from Scores group by CourseId +--统计出 每个学生 的 各门成绩的平均分 +select StuId,CourseId,AVG(Score)各门成绩的平均分 from Scores group by StuId,CourseId order by StuId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/\347\273\203\344\271\2401.sql" "b/20210326\344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/\347\273\203\344\271\2401.sql" new file mode 100644 index 0000000000000000000000000000000000000000..26098a0376f776e7c29bb5932f622c7172c1cb64 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/\347\273\203\344\271\2401.sql" @@ -0,0 +1,97 @@ +create database Student +on +( + name=Student, + filename='E:\SQL课堂\Student.mdf', + size=10MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name=Student_log, + filename='E:\SQL课堂\Student_log.ldf', + size=10MB, + maxsize=50MB, + filegrowth=10% +) + +use Student +go + +create table Stuinfo +( + stuNO varchar(5) unique not null, + stuName nvarchar(20), + stuAge int, + stuAddress nvarchar(20), + stuSeat int identity(1,1) unique not null, + stuSex nchar(1) check(stuSex='男' or stuSex='女') +) +go +insert into Stuinfo values ('s2501','张秋利',20,'美国硅谷','女'),('s2502','李斯文',18,'湖北武汉','男'),('s2503','马文才',22,'湖南长沙','女'), +('s2504','欧阳俊雄',21,'湖北武汉','男'),('s2505','梅超风',20,'湖北武汉','女'),('s2506','陈旋风',19,'美国硅谷','女'),('s2507','陈风',20,'美国硅谷','男') + +select 学号=stuNO,stuName as 姓名,stuAge as 年龄,stuAddress as 地址,stuSeat as 座位号,stuSex as 性别 from Stuinfo +select stuName,stuAge,stuAddress from Stuinfo +select stuNO,邮箱=stuName+'@'+stuAddress from Stuinfo +select stuAddress from Stuinfo +select distinct 所有年龄=stuAge from Stuinfo +select * from Stuinfo where stuSeat<=3 +select stuName,stuSeat from Stuinfo where stuSeat<=4 +select top 50 percent * from Stuinfo +select * from Stuinfo where stuAddress='湖北武汉' and stuAge=20 +select * from Stuinfo where stuAddress='湖北武汉' or stuAddress='湖南长沙' +select * from Stuinfo where stuAddress in('湖北武汉' , '湖南长沙') +select * from Stuinfo where stuAge is null +select * from Stuinfo where stuAge is not null +select * from Stuinfo where stuName like '张%' +select * from Stuinfo where stuAddress like '湖%' +select * from Stuinfo where stuName like '张_' +select * from Stuinfo where stuName like '__俊_' +select * from Stuinfo order by stuAge desc +select * from Stuinfo order by stuAge desc,stuSeat asc + + + +create table Scoreinfo +( + examNO int primary key identity(1,1), + stuNO varchar(5) references Stuinfo(stuNO), + writtenExan int, + labExam int, +) +insert into Scoreinfo values('s2501',50,70),('s2502',60,65),('s2503',86,85),('s2504',40,80),('s2505',70,90),('s2506',85,90) + +select 学号=stuNO,writtenExan as 笔试 ,labExam as 机试 from Scoreinfo +select examNO,stuNO,总分=writtenExan+labExam from Scoreinfo +select labExam from Scoreinfo where labExam>=60 and labExam<=80 order by labExam desc +select writtenExan from Scoreinfo where not labExam>=70 and labExam<=90 +select top 1* from Scoreinfo order by writtenExan desc +select top 1* from Scoreinfo order by labExam + +select stuAddress,AVG(stuAge)平均年龄 from dbo.Stuinfo group by stuAddress +select stuSex,SUM(stuAge)年龄总和 from dbo.Stuinfo group by stuSex +select stuAddress,AVG(stuAge)平均年龄,SUM(stuAge)年龄总和 from dbo.Stuinfo group by stuAddress + + +--数据如图片1,使用上次作业的数据 +select * from Stuinfo +select * from Scoreinfo + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 stuName stuAge writtenExan labExam +select stuName 姓名,stuAge 年龄,writtenExan 笔试成绩,labExam 机试成绩 from Scoreinfo inner join Stuinfo on Scoreinfo.stuNO = Stuinfo.stuNO +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select Stuinfo.stuNO 学号,Stuinfo.stuName 姓名,Scoreinfo.writtenExan 笔试成绩,Scoreinfo.labExam 机试成绩 from Scoreinfo inner join Stuinfo on Scoreinfo.stuNO = Stuinfo.stuNO +where Scoreinfo.writtenExan>=60 and Scoreinfo.labExam>=60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select A.stuNO 学号,A.stuName 姓名,B.writtenExan 笔试成绩,B.labExam 机试成绩 from Stuinfo A +left join Scoreinfo B on A.stuNO = B.stuNO +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName 姓名,stuAge 年龄,writtenExan 笔试成绩,labExam 机试成绩 from Stuinfo inner join Scoreinfo on Stuinfo.stuNO = Scoreinfo.stuNO +where stuAge>=20 order by writtenExan DESC +--5.查询男女生的机试平均分 +select stuSex 性别,AVG(labExam)机试平均分 from Stuinfo inner join Scoreinfo on Stuinfo.stuNO = Scoreinfo.stuNO group by stuSex + --结果四舍五入 +--6.查询男女生的笔试总分 +select stuSex 性别,SUM(writtenExan)笔试总分 from Stuinfo inner join Scoreinfo on Stuinfo.stuNO = Scoreinfo.stuNO group by stuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/\347\273\203\344\271\2402.sql" "b/20210326\344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/\347\273\203\344\271\2402.sql" new file mode 100644 index 0000000000000000000000000000000000000000..459f349147fc8e68fe9d2094de4e006a48f36eb7 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/\347\273\203\344\271\2402.sql" @@ -0,0 +1,111 @@ +create database BBS +on +( + name='BBS', + filename='E:\SQL课堂\BBS.mdf', + size=20, + maxsize=300, + filegrowth=50 +) +log on +( + name='BBS_log', + filename='E:\SQL课堂\BBS_log.ldf', + size=20, + maxsize=300, + filegrowth=50 +) +go +use BBS +go +create table orderItem +( +ItemiD int , +orderId int, +itemType varchar(10), +itemName nvarchar(10), +theNumber int, +theMoney money +) +create table orders +( +orderId int not null primary key , +orderDate datetime +) +insert into orders(orderId,orderDate) values (1,'2008-1-12'),(2,'2008-2-10'),(3,'2008-2-15'),(4,'2008-3-10') +select * from orders +insert into orderItem(ItemiD,orderId,itemType,itemName,theNumber,theMoney) values +(1,1,'文具','笔',72,2), +(2,1,'文具','尺',10,1), +(3,1,'体育用品','篮球',1,56), +(4,2,'文具','笔',36,2), +(5,2,'文具','固体胶',20,3), +(6,2,'日常用品','透明胶',2,1), +(7,2,'体育用品','羽毛球',20,3), +(8,3,'文具','订书机',20,3), +(9,3,'文具','订书钉',10,3), +(10,3,'文具','裁纸刀',5,5), +(11,4,'文具','笔',20,2), +(12,4,'文具','信纸',50,1), +(13,4,'日常用品','毛巾',4,5), +(14,4,'日常用品','透明胶',30,1), +(15,4,'体育用品','羽毛球',20,3) + +select * from orderItem +--1.查询所有订单订购的所有物品数量总和 +select SUM(theNumber) 数量总和 from orderItem +--2.查询 每个订单订购的所有物品的 数量和 以及 平均单价 +select orderId,SUM(theNumber), AVG(theMoney) from orderItem group by orderId having AVG(theMoney)<10 and orderId<3 + +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select orderId ,sum(theNumber),AVG(theMoney) from orderItem where theNumber>50 group by orderId having AVG(theMoney)<10 +--4.查询每种类别的产品分别订购了几次,例如: +-- 文具 9 +-- 体育用品 3 +-- 日常用品 3 +select itemType 类型,COUNT(itemType) 数量 from orderItem group by itemType + +--5.查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 + +select itemType 类型,sum(theNumber)总数,avg(theMoney) 平均单价 from orderItem group by itemType having sum(theNumber)>100 + +--6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: + +-- 产品名称 订购次数 总数量 平均单价 +-- 笔 3 120 2 +select itemName 产品名称 ,COUNT(itemName) 订购次数,sum(theNumber) 总数量 ,avg(theMoney)平均单价 from orderItem group by itemName + + +-------------------------------------------------------- + +--使用上次作业的订单数据库,完成下列题目: +select * from orders +select * from orderItem + +--1.查询所有的订单的 订单的编号ItemiD,订单日期orderDate,订购产品的类别itemType和订购的产品名称itemName,订购数量theNumber和订购单价theMoney +select orders.orderId 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价 from orders inner join orderItem on orders.orderId = orderItem.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orders.orderId 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称 from orders inner join orderItem on orders.orderId = orderItem.orderId +where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderId 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价,SUM(theNumber * theMoney) 订购总价 from orders inner join orderItem on orders.orderId = orderItem.orderId +group by orders.orderId,orderDate,itemType,itemName,theNumber,theMoney +--4.查询单价大于等于3并且数量大于等于20的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderId 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价,SUM(theNumber * theMoney) 订购总价 from orders inner join orderItem on orders.orderId = orderItem.orderId +where theMoney>=3 and theNumber>=20 +group by orders.orderId,orderDate,itemType,itemName,theNumber,theMoney +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderItem.orderId 编号,COUNT(itemName)订购产品数 from orderItem group by orderItem.orderId +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 +select orderItem.orderId 订单编号,itemType 产品类别,COUNT(itemType)订购次数,SUM(theNumber)总数量 from orderItem group by orderItem.orderId,itemType order by orderItem.orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/\347\273\203\344\271\2403.sql" "b/20210326\344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/\347\273\203\344\271\2403.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c265c8ee6f9e12ce6a5500f5c3d97cd806a5bdd1 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\236\227\344\275\263\345\205\203/\347\273\203\344\271\2403.sql" @@ -0,0 +1,136 @@ +create database 论坛 +on +(name='论坛', + filename='E:\SQL论坛\论坛.mdf', + size=20, + maxsize=300, + filegrowth=50 +) +log on +(name='论坛_log', + filename='E:\SQL论坛\论坛_log.ldf', + size=20, + maxsize=300, + filegrowth=50 +) +go +use 论坛 +create table bbsUsers +( +uIDD int primary key identity(1,1), +uName varchar(10) not null unique , + uSex varchar(2) not null check(uSex='男' or uSex='女'), + uAge int not null check(uAge>15 and uAge<60), + uPoint int not null check(uPoint>=0) +) +create table bbsSection( +sIDD int primary key identity(1,1), +sName varchar(10) not null , + sUid int references bbsUsers(uIDD) +) +create table bbsTopic( + tID int primary key identity(1,1), + tUID int references bbsUsers(uIDD), + tSID int references bbsSection(sIDD), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime , + tCount int +) +create table bbsReply( +rID int primary key identity(1,1), + rUID int references bbsUsers(uIDD ), + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime + +) + +--1.现在有3个会员注册成功,请用一次插入多行数据的方法向bbsUsers表种插入3行记录,记录值如下: +-- 小雨点 女 20 0 +-- 逍遥 男 18 4 +-- 七年级生 男 19 2 +insert into bbsUsers(uName,uSex,uAge,uPoint) +select'小雨点','女','20','0'union +select'逍遥','男','18','4'union +select'七年级生','男','19','2' +go + + --2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中,提示查询部分列:select 列名1,列名2 from 表名 + select uName,uPoint into bbsPoint from bbsUsers + go + --3.给论坛开设4个板块 + -- 名称 版主名 + -- 技术交流 小雨点 + -- 读书世界 七年级生 + -- 生活百科 小雨点 + -- 八卦区 七年级生 + insert into bbsSection(sName,sUid) + select'技术交流',3 union + select'读书世界',1 union + select'生活百科',3 union + select'八卦区',1 + go + --4.向主贴和回帖表中添加几条记录 + insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) +select '2','1','范跑跑','谁是范跑跑','2008-7-8','1'union +select '1','3','.NET','与Java的区别是什么呀?','2008-9-1','2'union +select '3','4','今年夏天最流行什么呀?','有谁知道今年夏天最流行什么呀?','2008-9-10','0' + + +-- 回帖: +-- 分别给上面三个主贴添加对应的回帖,回帖的内容,时间,回帖人自定 + +insert into bbsReply(rTID,rMsg,rTime,rUId) +select'1','不知道','2008-9-5','2'union +select'2','不认识','2008-9-10','1'union +select'3','不知道','2008-10-1','3' + +-- 5.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户,请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) + + + +alter table bbsReply drop constraint FK_bbsReply_rUID ----出错 +alter table bbsTopic drop constraint FK_bbsTopic_tUID +alter table bbsSection drop constraint FK_bbsSection_sUid +alter table bbsUsers drop constraint PK_bbsUsers_UID + +delete from bbsUsers where UIDD=2 + +-- 6.因为小雨点发帖较多,将其积分增加10分 + + +update bbsPoint set uPoint=(uPoint + 10) where uName='小雨点' + + +-- 7.因为板块“生活百科”灌水的人太少,现决定取消该板块,即删除(注意主外键) + +delete from bbsReply where rTID=3 +delete from bbsTopic where tSID=4 +delete from bbsSection where sIDD=4 + +-- 8.因回帖积累太多,现需要将所有的回帖删除 + +delete from bbsReply +truncate table bbsReply +drop table bbsReply + + +--在论坛数据库中完成以下题目 +select * from bbsUsers +select * from bbsTopic +select * from bbsSection +select * from bbsPoint +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select sUid 版主编号,uName 版主姓名,sName 版块名称 from bbsSection inner join bbsUsers on bbsSection.sUid = bbsUsers.uIDD +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID 发帖人编号,uName 发帖人姓名,tTitle 帖子的标题,tMsg 帖子的内容,tTime 发帖时间 from bbsTopic inner join bbsUsers on bbsTopic.tUID = bbsUsers.uIDD +where tTime>'2008-8-15' +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sUid 版主编号,uName 版主的名称,sName 版块的名称 from bbsSection inner join bbsUsers on bbsSection.sUid = bbsUsers.uIDD +where bbsUsers.uAge<20 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select tUID 发帖人编号,uName 发帖人姓名,tTitle 主贴标题,tMsg 主贴内容,tCount 回复数量 from bbsTopic inner join bbsUsers on bbsTopic.tUID = bbsUsers.uIDD +where tcount=(select MAX(tcount)from bbsTopic) +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select tSID 版块编号,tUID 用户,count(tID)发帖总数 from bbsTopic inner join bbsUsers on bbsTopic.tUID = bbsUsers.uIDD inner join bbssection on bbsTopic.tUID = bbsUsers.uIDD group by tSID,tUID diff --git "a/20210326\344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/SQLQuery3.14.sql" "b/20210326\344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/SQLQuery3.14.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6cb9a0e18521c236f812896cb726f481af31b1f1 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/SQLQuery3.14.sql" @@ -0,0 +1,100 @@ +use Students +go + +create table Stuinfo002 +( + stuNo char(100) not null, + stuName nvarchar(20) not null, + stuAge char(3) not null, + stuAddress nvarchar(20) not null, + stuSeae int primary key identity(1,1), + stuSex char(1) check(stuSex in(1,0)) +) +go + + + +create table fraction +( + exanNo int primary key identity(1,1), + stuNo char(10) not null, + wittenexam int , + labexam int +) + +go +insert into Stuinfo002(stuNo,stuName,stuAge,stuAddress,stuSex) values('s2501','张秋利',20,'美国硅谷',1),('s2502','李斯文',18,'湖北武汉',0), +('s2503','马文才',22,'湖南长沙',1),('s2504','欧阳俊雄',21,'湖北武汉',0), +('s2505','梅超风',20,'湖北武汉',1),('s2506','陈旋风',19,'美国硅谷',1), +('s2507','陈风',20,'美国硅谷',0) +insert into fraction(stuNo,wittenexam,labexam) values('s2501',50,70),('s2502',60,86),('s2503',86,85),('s2504',40,80),('s2505',70,90),('s2506',85,90) + +select stuNo as 学号 from Stuinfo002 +select stuName as 姓名 from Stuinfo002 +select stuAge as 年龄 from Stuinfo002 +select stuAddress as 地址 from Stuinfo002 +select stuSeae as 座位号 from Stuinfo002 +select stuSex as 性别 from Stuinfo002 + +select stuNo,stuAge,stuName from Stuinfo002 + +select stuNo 学号,wittenexam 笔试,labexam 机试 from fraction +select stuNo as 学号,wittenexam as 笔试,labexam as 机试 from fraction +select 学号=stuNo, 笔试=wittenexam, 机试=labexam from fraction + +select stuNo 学号,stuName 姓名, stuAddress 地址 ,stuName+'@'+stuAddress 邮箱 from Stuinfo002 + +select stuNo 学号,wittenexam 笔试,labexam 机试,labexam+wittenexam 总分 from fraction + +select distinct stuAddress from Stuinfo002 + +select distinct stuAge as 所有年龄 from Stuinfo002 + +select top 3 * from Stuinfo002 + +select top 4 stuName,stuSeae from Stuinfo002 + +select top 50 percent * from Stuinfo002 + +select * from Stuinfo002 where stuAddress='湖北武汉' and stuAge=20 + +select * from fraction where labexam>60 and labexam<80 order by labexam desc + +select * from Stuinfo002 where stuAddress='湖北武汉' OR stuAddress='湖南长沙' +select * from Stuinfo002 where (stuAddress in('湖北武汉','湖南长沙')) + +select * from fraction where wittenexam like '[^7,8,9]%' order by wittenexam asc + +select * from Stuinfo002 where stuAge is null or stuAge='' + +select * from Stuinfo002 where stuAge like '%' + +select * from Stuinfo002 where stuName like '张%' + +select * from Stuinfo002 where stuAddress like '湖%' + +select * from Stuinfo002 where stuName like '张_' + +select * from Stuinfo002 where stuName like '__俊%' + +select * from Stuinfo002 order by stuAge desc + +select * from Stuinfo002 order by stuAge desc,stuSeae asc + +select top 1 * from fraction order by wittenexam desc + +select top 1 * from fraction order by wittenexam + +select * from Stuinfo002 +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName,stuAge,wittenexam,labexam from Stuinfo002 inner join fraction on Stuinfo002.stuNo = fraction.stuNo +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select stuName,stuAge,wittenexam,labexam from Stuinfo002 inner join fraction on Stuinfo002.stuNo = fraction.stuNo where wittenexam>60 and labexam>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select stuName,stuAge,wittenexam,labexam from Stuinfo002 left join fraction on Stuinfo002.stuNo = fraction.stuNo +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName,stuAge,wittenexam,labexam from Stuinfo002 inner join fraction on Stuinfo002.stuNo = fraction.stuNo where stuAge>=20 order by wittenexam desc +--5.查询男女生的机试平均分 +select stuName,stuSex,avg(labexam) from Stuinfo002 inner join fraction on Stuinfo002.stuNo = fraction.stuNo group by stuName,stuSex +--6.查询男女生的笔试总分 +select stuSex,sum(wittenexam) from Stuinfo002 inner join fraction on Stuinfo002.stuNo = fraction.stuNo group by stuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/SQLQuery3.23(2).sql" "b/20210326\344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/SQLQuery3.23(2).sql" new file mode 100644 index 0000000000000000000000000000000000000000..140ed8d2baf27f27b10b68eb532bb6d00e727437 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/SQLQuery3.23(2).sql" @@ -0,0 +1,76 @@ +create database orders +go +use orders +go +create table orders --订单表 +( + orderId int primary key, --订单编号 + orderDate smalldatetime --订购日期 +) +go +create table orderItem --订购项目表 +( + itemName nvarchar(10) not null, --产品名称 + theNumber int not null, --订购数量 + theMoney money not null, --订购单价 + itemType nvarchar(4) check(itemType in('文具','体育用品','日常用品')) not null,--产品类别 + orderId int references orders(orderId) not null, --订单编号 + ItemiD int identity(1,1) primary key, --项目编号 +) +insert into orders(orderId,orderDate) values(1,'2008-01-12'),(2,'2008-02-10'),(3,'2008-02-15'),(4,'2008-03-10') +insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) +select 1,'文具','笔',72,2 union +select 1,'文具','尺',10,1 union +select 1, '体育用品','篮球',1,56 union +select 2,'文具','笔',36,2 union +select 2,'文具','固体胶',20,3 union +select 2,'日常用品','透明胶',2,1 union +select 2,'体育用品','羽毛球',20,3 union +select 3,'文具','订书机',20,3 union +select 3,'文具','订书机',10,3 union +select 3,'文具','裁缝刀',5,5 union +select 4,'文具','笔',20,2 union +select 4,'文具','信纸',50,1 union +select 4,'日常用品','毛巾',4,5 union +select 4,'日常用品','透明胶',30,1 union +select 4,'体育用品','羽毛球',20,3 +select sum(theNumber) as 所有物品数量 from orderItem + +select orderId,AVG(theMoney) from orderItem where orderId<3 group by orderId having AVG(theMoney)<10 + +select orderId,itemName,theNumber,AVG(theMoney) from orderItem where orderId<3 group by orderId,itemName,theNumber having AVG(theMoney)<10 and sum(theNumber)>50 + +select itemType,count(itemType)订购次数 from orderItem group by itemType + +select itemName,avg(theMoney)平均价格,sum(theNumber)订购总数 from orderItem group by itemName having sum(theNumber)>100 + +select itemName as 文具类型,count(itemType)订购次数,sum(theNumber)订购总数,avg(theMoney)平均价格 from orderItem group by itemName + +select * from orderItem + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select orderItem.orderId,orderDate,itemType,itemName,theNumber,theMoney from orderItem inner join orders on orderItem.orderId = orders.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 + +select orderItem.orderId,orderDate,itemType,itemName,theNumber,theMoney from orderItem inner join orders on orderItem.orderId = orders.orderId where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +select orderItem.orderId,orderDate,itemType,itemName,theNumber,theMoney,theMoney*theNumber 总价 from orderItem inner join orders on orderItem.orderId = orders.orderId +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +select orderItem.orderId,orderDate,itemType,itemName,theNumber,theMoney from orderItem inner join orders on orderItem.orderId = orders.orderId where theMoney>=5 and theNumber>=50 +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select ItemiD 编号,theNumber 订购产品数 from orderItem +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +select orderId 订单编号,itemType 产品类别,count(theNumber) 订购产品数,sum(theNumber) 总数量 from orderItem group by orderId,itemType +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 diff --git "a/20210326\344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/bbs.sql" "b/20210326\344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/bbs.sql" new file mode 100644 index 0000000000000000000000000000000000000000..37efc8b0b573cd1279cd91426b083d0bc867bde9 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/bbs.sql" @@ -0,0 +1,27 @@ + +use bbs9 +go + +--1.查询出每个版块的版主编号,版主姓名和版块名称 + +select sUid 版主编号,uName 版主姓名,sName 板块名称 from bbsSection inner join bbsUsers on bbsUsers.bbbsUID = bbsSection.sUid + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 + +select tUID,uName,tTitle,tMsg,tTime from bbsTopic inner join bbsUsers on bbsUsers.bbbsUID = bbsTopic.tUID where tTime>'2008-9-15' +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 + +select sUid 版主的编号,uName 版主的名称,sName 版块的名称 from bbsUsers left join bbsSection on bbsSection.sUid = bbsUsers.bbbsUID where uAge<20 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 + +select tuid,uName,tTitle,tMsg,tCount from bbsTopic T inner join bbsUsers U on T.tUID = U.bbbsUID where tCount=(select max(tCount) from bbsTopic) +--5.在主贴表中查询每个版块中每个用户的发帖总数 + +select sName,uName,count(tUID) from bbsTopic +inner join bbsSection on bbsTopic.tSID = bbsSection.bbssID +inner join bbsUsers on bbsTopic.tUID = bbsUsers.bbbsUID +group by sName,uName + +select * from bbsSection +select * from bbsTopic +select * from bbsUsers \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\270\251\345\271\277\347\224\237/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\346\270\251\345\271\277\347\224\237/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..76827d79167419ea74fa2799c16f59218639e687 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\270\251\345\271\277\347\224\237/SQLQuery1.sql" @@ -0,0 +1,80 @@ +create database sss +on +( + name='sss', + FILENAME='D:\temp\sss.mdf', + SIZE=5, + MAXSIZE=50, + FILEGROWTH=10% +) +log on +( + NAME='sss_log', + FILENAME='D:\temp\sss_log.ldf', + SIZE=5, + MAXSIZE=50, + FILEGROWTH=10% + +) +go +use sss +go + +create table stuinfo +( + StuNo varchar(20), + stuName varchar(10) not null, + stuAge int not null, + stuAddress varchar(100), + stuSeat int primary key identity, + stuSex char(2) default(1) check(stuSex=0 or stuSex=1) +) +create table stuexam2 +( + examNo2 int primary key identity(1,1), + stuNo2 varchar(20), + writtenExam2 int, + labExam2 int +) + +insert into stuinfo(StuNo,stuName,stuAge,stuAddress,stuSex) +select 's2501','张秋利',20,'美国硅谷',1 union +select 's2502','李斯文',18,'湖北武汉',0 union +select 's2503','马文才',22,'湖南长沙',1 union +select 's2504','欧阳俊雄',21,'湖北武汉',0 union +select 's2505','梅超风',20,'湖北武汉',1 union +select 's2506','陈旋风',19,'美国硅谷',1 union +select 's2507','陈风',20,'美国硅谷',0 + +insert into stuexam2(stuNo2,writtenExam2,labExam2) +select 's2501',50,70 union +select 's2502',60,65 union +select 's2503',86,85 union +select 's2504',40,80 union +select 's2505',70,90 union +select 's2506',85,90 + +select * from stuinfo +select * from stuexam2 + +select * from stuinfo SI inner join stuexam2 SE on SI.StuNo=SE.stuNo2 + + + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select StuNO,stuname,writtenExam2,labExam2 from stuinfo inner join stuexam2 +on stuinfo.StuNo=stuexam2.stuNo2 +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select StuNO,stuname,writtenExam2,labExam2 from stuinfo inner join stuexam2 +on stuinfo.StuNo=stuexam2.stuNo2 where writtenExam2>60 and labExam2>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +---select StuNO,stuname,writtenExam2,labExam2 from stuinfo inner join stuexam2 +on stuinfo.StuNo=stuexam2.stuNo2 +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuname,stuAge,writtenExam2,labExam2 from stuinfo inner join stuexam2 +on stuinfo.StuNo=stuexam2.stuNo2 where stuAge>=20 order by writtenExam2 desc +--5.查询男女生的机试平均分 +--select xb 性别,SUM(yw) 语文总分,SUM(sx) 数学总分,SUM(yy) 英语总分,AVG(yw) 语文平均分,AVG(sx) 数学平均分,AVG(yy) 英语平均分 from xs left join cj on xs.xm=cj.xm group by xb +select stusex 性别,avg(labExam2) from stuinfo inner join stuexam2 on stuinfo.StuNo=stuexam2.stuNo2 group by stuSex +--6.查询男女生的笔试总分 +select stusex 性别,sum(writtenExam2) from stuinfo inner join stuexam2 on stuinfo.StuNo=stuexam2.stuNo2 group by stuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\270\251\345\271\277\347\224\237/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\346\270\251\345\271\277\347\224\237/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..06d9f96b288c8223196fa5ca08be67b26f4c89f8 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\270\251\345\271\277\347\224\237/SQLQuery2.sql" @@ -0,0 +1,104 @@ +create database a +on +( + name='a', + filename='D:\temp\ATM.mdf', + size=5, + maxsize=10, + filegrowth=15% +) +log on +( + name='a_log', + filename='D:\temp\ATM.ldf', + size=5, + maxsize=10, + filegrowth=15% +) +go +use a +go + +create table orders +( +--订单编号(orderId 主键) 订购日期(orderDate) + orderId int primary key, + orderDate datetime default(getdate()) +) +--订购项目表(orderItem),列为: +--项目编号(ItemiD)订单编号(orderId)产品类别(itemType) +--产品名称(itemName) 订购数量(theNumber) 订购单价(theMoney) +create table orderItem +( +--订单编号(orderId 主键) 订购日期(orderDate) + ItemiD int identity(1,1), + orderId int, + itemType varchar(20), + itemName varchar(20), + theNumber int, + theMoney int +) + +select * from orders +select * from orderItem +insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) +select 1,'文具','笔',72,2 union +select 1,'文具','尺',10,1 union +select 1,'体育用品','篮球',1,56 union +select 2,'文具','笔',36,2 union +select 2,'文具','固体胶',20,3 union +select 2,'日常用品','透明胶',2,1 union +select 2,'体育用品','羽毛球',20,3 union +select 3,'文具','订书机',20,3 union +select 3,'文具','订书针',10,3 union +select 3,'文具','栽纸刀',5,5 union +select 4,'文具','笔',20,2 union +select 4,'文具','信纸',50,1 union +select 4,'日常用品','毛巾',4,5 union +select 4,'日常用品','透明胶',30,1 union +select 4,'体育用品','羽毛球',20,3 + +insert into orders(orderId,orderDate) +select 1,'2008-01-12' union +select 2,'2008-02-10' union +select 3,'2008-02-15' union +select 4,'2008-03-10' +select * from orders +select * from orderItem +--orderId,itemType,itemName,theNumber,theMoney + +--使用上次作业的订单数据库,完成下列题目: + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select I.orderId 订单编号,orderDate 订单日期,itemType 产品的类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价 +from orders I inner join orderItem D on I.orderId=D.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select I.orderId 订单编号,orderDate 订单日期,itemType 产品的类别,itemName 产品名称 +from orders I inner join orderItem D on I.orderId=D.orderId where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select I.orderId 订单编号,orderDate 订单日期,itemType 产品的类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价,theNumber*theMoney 总价 +from orders I inner join orderItem D on I.orderId=D.orderId where theMoney = (select sum(theMoney) from orders) +------------------------------------------------------------where uPoint = (select MAX(uPoint) from bbsUsers +select orderId,sum(theMoney)from orderItem group by orderId +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select I.orderId 订单编号,orderDate 订单日期,itemType 产品的类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价,theNumber*theMoney 总价 +from orders I inner join orderItem D on I.orderId=D.orderId where theNumber>=50 and theMoney>=5 + +select orderId,sum(theMoney) from orderItem where theMoney >5 group by orderId having(sum(theMoney)>50) +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select * from orderItem +select orderId 编号,count(*) from orderItem group by orderId +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 +select * from orderItem +select orderId 编号,itemType 产品类别,count(*),sum(theNumber) from orderItem group by orderId,itemType order by orderId,itemType desc \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\270\251\345\271\277\347\224\237/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\346\270\251\345\271\277\347\224\237/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3adadff17cd7802704a0d10ab9eab1c155a10a97 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\270\251\345\271\277\347\224\237/SQLQuery3.sql" @@ -0,0 +1,122 @@ +create database bbs +on +( + NAME='bbs', + FILENAME='D:\temp\bbss.mdf', + SIZE=5, + MAXSIZE=50, + FILEGROWTH=10% +) +log on +( + NAME='TestDB_log', + FILENAME='D:\temp\bbss_log.ldf', + SIZE=5, + MAXSIZE=50, + FILEGROWTH=10% + +) +go +use bbs +go + +create table bbsUsers +( + UIDa int primary key identity(1,1), + uName varchar(10) not null, + uSex varchar(2) not null check(uSex='男' or uSex='女'), + uAge int not null check(uAge>=15 and uAge<=60), + uPoint int not null check(uPoint>=0) +) +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int foreign key references bbsUSers(UIDa), + tSID int , + tTitle varchar(100) not null, + tMsg varchar(100) not null, + tTime datetime, + tCount int, +) +create table bbsReply +( + rID int primary key identity(1,1), + rUID int foreign key references bbsUsers(UIDa), + rTID int foreign key references bbsTopic(tID), + rMsg varchar(100) not null, + rTime datetime, + +) +create table bbsSection +( + SIDaa int primary key identity(1,1), + sName varchar(10) not null, + sUid int +) + +select *from bbsReply + +--alter table bbsSection add SIDaa int primary key identity(1,1) +alter table bbsSection add constraint FK_UIDa foreign key(SUid) references bbsUsers(UIDa) + +alter table bbsTopic add constraint FK_SIDa foreign key(tSID) references bbsSection(SIDaa) + +select * from bbsUsers +insert into bbsUsers(uName,uSex,uAge,uPoint) +select '小雨点','女',20,0 union +select '逍遥','男',18,4 union +select '七年级生','男',19,2 + +select uName,uPoint from bbsUsers + +select * from bbsSection +insert into bbsSection(sName,sUid) +select '技术交流',3 union +select '读书世界',1 union +select '生活百科',3 union +select '八卦区',1 + +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) +select 2,2,'范跑跑','谁是范跑跑','2008-7-8',1 union +select 1,2,'.NET ','与JAVA的区别是什么呀?','2008-9-1',2 union +select 3,2,'今年夏天最流行什么','有谁知道今年夏天最流行','2008-9-10',0 + +select *from bbsTopic +insert into bbsReply(rUID,rTID,rMsg,rTime) +select 3,1,'与JAVA的区别是程序语言','2008-9-10' union +select 1,2,'有好有坏,不要站在道德的制高点哔赖','2008-9-10' union +select 2,3,'当然是短袖呀','2008-9-10' + +--select *from bbsReply + +--delete from bbsUsers where uName='逍遥' + +--alter table bbsSection drop constraint FK_UIDa + +--alter table bbsTopic drop constraint FK__bbsTopic__tUID__15502E78 +--alter table bbsReply drop constraint FK__bbsReply__rUID__182C9B23 + +--update bbsUsers set uPoin t=10 where UIDa=3 + +--select *from bbsUsers +--delete from bbsSection where sName='生活百科' +--select *from bbsSection + + + +select *from bbsTopic +select *from bbsReply +select *from bbsUsers +select *from bbsSection +--在论坛数据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select UIDa 编号,uName 姓名,sName 板块名称 from bbsUsers U inner join bbsSection T on U.UIDa=T.SIDaa +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID 编号,tSID 发帖人姓名,tTitle 帖子的标题,tMsg 帖子的内容,tTime 发帖时间 from bbsTopic where tTime>'2008-9-15' +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select UIDa 编号,uName 姓名,sName 板块名称 from bbsUsers U inner join bbsSection T on U.UIDa=T.SIDaa where uAge<20 +*--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select UIDa,uName,tTitle,tmsg,tCount from bbsTopic T inner join bbsUsers U on T.tUID=U.UIDa where tCount>1 +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select tSID 板块编号,tUID 用户名,count(*) 总数 from bbsTopic group by tSID,tUID + diff --git "a/20210326\344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a7a6a21be056f3aadb542becc55f190be5ba792d --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/SQLQuery1.sql" @@ -0,0 +1,466 @@ +USE [master] +GO +/****** Object: Database [TestDB] Script Date: 2021/3/15 16:11:24 ******/ +CREATE DATABASE [TestDB] + CONTAINMENT = NONE + ON PRIMARY +( NAME = N'TestDB', FILENAME = N'D:\TestDB.mdf' , SIZE = 4288KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) + LOG ON +( NAME = N'TestDB_log', FILENAME = N'D:\TestDB_log.ldf' , SIZE = 1072KB , MAXSIZE = 2048GB , FILEGROWTH = 10%) +GO +ALTER DATABASE [TestDB] SET COMPATIBILITY_LEVEL = 120 +GO +IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled')) +begin +EXEC [TestDB].[dbo].[sp_fulltext_database] @action = 'enable' +end +GO +ALTER DATABASE [TestDB] SET ANSI_NULL_DEFAULT OFF +GO +ALTER DATABASE [TestDB] SET ANSI_NULLS OFF +GO +ALTER DATABASE [TestDB] SET ANSI_PADDING OFF +GO +ALTER DATABASE [TestDB] SET ANSI_WARNINGS OFF +GO +ALTER DATABASE [TestDB] SET ARITHABORT OFF +GO +ALTER DATABASE [TestDB] SET AUTO_CLOSE OFF +GO +ALTER DATABASE [TestDB] SET AUTO_SHRINK OFF +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS ON +GO +ALTER DATABASE [TestDB] SET CURSOR_CLOSE_ON_COMMIT OFF +GO +ALTER DATABASE [TestDB] SET CURSOR_DEFAULT GLOBAL +GO +ALTER DATABASE [TestDB] SET CONCAT_NULL_YIELDS_NULL OFF +GO +ALTER DATABASE [TestDB] SET NUMERIC_ROUNDABORT OFF +GO +ALTER DATABASE [TestDB] SET QUOTED_IDENTIFIER OFF +GO +ALTER DATABASE [TestDB] SET RECURSIVE_TRIGGERS OFF +GO +ALTER DATABASE [TestDB] SET ENABLE_BROKER +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF +GO +ALTER DATABASE [TestDB] SET DATE_CORRELATION_OPTIMIZATION OFF +GO +ALTER DATABASE [TestDB] SET TRUSTWORTHY OFF +GO +ALTER DATABASE [TestDB] SET ALLOW_SNAPSHOT_ISOLATION OFF +GO +ALTER DATABASE [TestDB] SET PARAMETERIZATION SIMPLE +GO +ALTER DATABASE [TestDB] SET READ_COMMITTED_SNAPSHOT OFF +GO +ALTER DATABASE [TestDB] SET HONOR_BROKER_PRIORITY OFF +GO +ALTER DATABASE [TestDB] SET RECOVERY FULL +GO +ALTER DATABASE [TestDB] SET MULTI_USER +GO +ALTER DATABASE [TestDB] SET PAGE_VERIFY CHECKSUM +GO +ALTER DATABASE [TestDB] SET DB_CHAINING OFF +GO +ALTER DATABASE [TestDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) +GO +ALTER DATABASE [TestDB] SET TARGET_RECOVERY_TIME = 0 SECONDS +GO +ALTER DATABASE [TestDB] SET DELAYED_DURABILITY = DISABLED +GO +EXEC sys.sp_db_vardecimal_storage_format N'TestDB', N'ON' +GO +USE [TestDB] +GO +/****** Object: Table [dbo].[ClassInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[ClassInfo]( + [ClassId] [int] IDENTITY(1,1) NOT NULL, + [ClassName] [nvarchar](20) NOT NULL, +PRIMARY KEY CLUSTERED +( + [ClassId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[CourseInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[CourseInfo]( + [CourseId] [int] IDENTITY(1,1) NOT NULL, + [CourseName] [nvarchar](50) NOT NULL, + [CourseCredit] [int] NULL DEFAULT ((1)), +PRIMARY KEY CLUSTERED +( + [CourseId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[Scores] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[Scores]( + [ScoreId] [int] IDENTITY(1,1) NOT NULL, + [StuId] [int] NULL, + [CourseId] [int] NULL, + [Score] [int] NULL DEFAULT ((0)), +PRIMARY KEY CLUSTERED +( + [ScoreId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[StuInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[StuInfo]( + [StuId] [int] IDENTITY(1,1) NOT NULL, + [ClassId] [int] NULL, + [StuName] [nvarchar](10) NOT NULL, + [StuSex] [nvarchar](1) NULL DEFAULT ('男'), + [StuBrithday] [date] NULL, + [StuPhone] [nvarchar](11) NULL, + [StuProvince] [nvarchar](200) NULL, + [CreateDate] [datetime] NULL DEFAULT (getdate()), + [StuAge] [int] NULL, +PRIMARY KEY CLUSTERED +( + [StuId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] ON + +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (1, N'软件1班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (2, N'软件2班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (3, N'软件3班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (4, N'软件4班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (5, N'软件5班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (6, N'软件6班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (7, N'软件7班') +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] ON + +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (1, N'计算机基础', 3) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (2, N'HTML+CSS网页制作', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (3, N'JAVA编程基础', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (4, N'SQL Server数据库基础', 4) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (5, N'C#面向对象编程', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (6, N'Winform桌面应用程序设计', 5) +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[Scores] ON + +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (1, 1, 1, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (2, 1, 2, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (3, 1, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (4, 1, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (5, 2, 1, 60) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (6, 2, 2, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (7, 2, 3, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (8, 2, 4, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (9, 3, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (10, 3, 2, 45) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (11, 3, 3, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (12, 3, 4, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (13, 4, 1, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (14, 4, 2, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (15, 4, 3, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (16, 4, 4, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (17, 5, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (18, 5, 2, 79) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (19, 5, 3, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (20, 5, 4, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (21, 6, 1, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (22, 6, 2, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (23, 6, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (24, 6, 5, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (25, 7, 1, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (26, 7, 2, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (27, 7, 3, 92) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (28, 7, 5, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (29, 8, 1, 58) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (30, 8, 2, 59) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (31, 8, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (32, 8, 5, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (33, 9, 1, 48) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (34, 9, 2, 67) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (35, 9, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (36, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (37, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (38, 1, 1, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (39, 1, 2, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (40, 1, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (41, 1, 4, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (42, 2, 1, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (43, 2, 2, 82) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (44, 2, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (45, 2, 4, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (46, 3, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (47, 3, 2, 50) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (48, 3, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (49, 3, 4, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (50, 4, 1, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (51, 4, 2, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (52, 4, 3, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (53, 4, 4, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (54, 5, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (55, 5, 2, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (56, 5, 3, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (57, 5, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (58, 6, 1, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (59, 6, 2, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (60, 6, 3, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (61, 6, 5, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (62, 7, 1, 89) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (63, 7, 2, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (64, 7, 3, 97) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (65, 7, 5, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (66, 8, 1, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (67, 8, 2, 64) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (68, 8, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (69, 8, 5, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (70, 9, 1, 53) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (71, 9, 2, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (72, 9, 3, 76) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (73, 9, 5, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (74, 9, 5, 61) +GO +SET IDENTITY_INSERT [dbo].[Scores] OFF +GO +SET IDENTITY_INSERT [dbo].[StuInfo] ON + +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (1, 1, N'刘正', N'男', CAST(N'2002-08-02' AS Date), N'13245678121', N'广西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (2, 1, N'黄贵', N'男', CAST(N'2003-07-02' AS Date), N'13345678121', N'江西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (3, 1, N'陈美', N'女', CAST(N'2002-07-22' AS Date), N'13355678125', N'福建省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (4, 2, N'江文', N'男', CAST(N'2001-07-02' AS Date), N'13347678181', N'湖南省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (5, 2, N'钟琪', N'女', CAST(N'2004-01-13' AS Date), N'13345778129', N'安徽省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (6, 3, N'曾小林', N'男', CAST(N'2005-05-15' AS Date), N'13345378563', N'安徽省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (7, 3, N'欧阳天天', N'女', CAST(N'2000-08-19' AS Date), N'13347878121', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (8, 3, N'李逍遥', N'男', CAST(N'1999-09-02' AS Date), N'13345678557', N'广东省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 22) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (9, 4, N'刘德华', N'男', CAST(N'1995-06-11' AS Date), N'15345679557', N'福建省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 26) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (10, 4, N'刘翔', N'男', CAST(N'1996-07-09' AS Date), N'18346679589', N'江西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (11, 4, N'曾小贤', N'男', CAST(N'2003-07-02' AS Date), N'18348979589', N'湖南省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (12, 5, N'刘德华', N'男', CAST(N'2002-07-02' AS Date), N'18348979509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (13, 5, N'陈天翔', N'男', CAST(N'2003-07-02' AS Date), N'18348079509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (14, 5, N'刘能', N'男', CAST(N'2005-08-02' AS Date), N'13245678122', N'广西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (15, 5, N'钟馗', N'男', CAST(N'2004-08-02' AS Date), N'13245678123', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (16, 5, N'钟吴艳', N'女', CAST(N'2002-08-02' AS Date), N'13245678124', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (17, 5, N'刘欢', N'男', CAST(N'2001-07-02' AS Date), N'13245678125', N'湖南省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (18, 5, N'张庭', N'女', CAST(N'2000-07-02' AS Date), N'13245678126', N'江西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (19, 5, N'曹植', N'男', CAST(N'2000-08-02' AS Date), N'13245678127', N'福建省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (20, 5, N'曹操', N'男', CAST(N'2002-08-02' AS Date), N'13245678128', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (21, 5, N'孙尚香', N'女', CAST(N'2003-08-02' AS Date), N'13245678129', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (22, 3, N'老1', N'女', CAST(N'2002-08-02' AS Date), N'13245678130', N'广东省', CAST(N'2021-03-14 17:02:36.347' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (24, 2, N'老2', N'男', CAST(N'2002-08-03' AS Date), N'13345678945', NULL, CAST(N'2021-03-14 17:03:37.733' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (25, 4, N'老3', N'男', NULL, N'13645987545', N'广东省', CAST(N'2021-03-14 17:03:43.307' AS DateTime), NULL) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (28, 5, N'老4', N'男', CAST(N'2006-03-05' AS Date), N'13456987456', NULL, CAST(N'2021-03-14 17:04:03.957' AS DateTime), 15) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (29, 5, N'老5', N'女', CAST(N'1998-04-12' AS Date), N'15978456123', NULL, CAST(N'2021-03-14 17:04:47.103' AS DateTime), 23) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (30, 4, N'老6', N'男', CAST(N'1996-08-06' AS Date), N'18945674561', NULL, CAST(N'2021-03-14 17:05:04.990' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (31, 3, N'老7', N'女', CAST(N'1997-04-06' AS Date), N'18845678912', NULL, CAST(N'2021-03-14 17:05:20.570' AS DateTime), 24) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (32, 2, N'老10', N'女', CAST(N'1998-08-09' AS Date), N'19945645612', NULL, CAST(N'2021-03-14 17:06:08.107' AS DateTime), 23) +GO +SET IDENTITY_INSERT [dbo].[StuInfo] OFF +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__CourseIn__9526E2773AB7BECE] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[CourseInfo] ADD UNIQUE NONCLUSTERED +( + [CourseName] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__StuInfo__2D85FC63AF6FC6FA] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[StuInfo] ADD UNIQUE NONCLUSTERED +( + [StuPhone] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([CourseId]) +REFERENCES [dbo].[CourseInfo] ([CourseId]) +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([StuId]) +REFERENCES [dbo].[StuInfo] ([StuId]) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD FOREIGN KEY([ClassId]) +REFERENCES [dbo].[ClassInfo] ([ClassId]) +ON DELETE SET NULL +GO +ALTER TABLE [dbo].[CourseInfo] WITH CHECK ADD CHECK (([CourseCredit]>=(1) AND [CourseCredit]<=(5))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK ((len([StuPhone])=(11))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK (([StuSex]='女' OR [StuSex]='男')) +GO +USE [master] +GO +ALTER DATABASE [TestDB] SET READ_WRITE +GO + + +select * from Stuinfo +select * from Scores +select * from Courseinfo +select * from Classinfo + +--统计 每个班 的 男生数 +select ClassId,count(Stusex)男生数 from Stuinfo where Stusex='男' group by ClassId +--统计 每个班 的 男、女生数 +select ClassId,Stusex,count(*)人数 from Stuinfo group by ClassId,Stusex +--统计 每个班 的 福建人数 +select ClassId,count(*)福建人数 from Stuinfo where StuProvince='福建省' group by ClassId +--统计 每个班 的 各个省的总人数 +select ClassId,StuProvince 省份,count(*)人数 from Stuinfo group by ClassId,StuProvince order by ClassID +--统计 每个省 的 女生数 +select StuProvince 省份,count(StuSex)女生人数 from Stuinfo where StuSex='女' group by StuProvince +--统计 每个省 的 男、女生数 +select StuProvince,StuSex,count(*)人数 from Stuinfo group by StuProvince,StuSex +--统计 每个学生 的 考试总分、平均分 +select StuId,SUM(Score)考试总分,AVG(Score)平均分 from Scores group by StuId +--统计出 考试总分大于620 的学生 的考试总分 +select StuId,SUM(Score)总分 from Scores group by StuId having SUM(Score)>620 +--统计出 每门考试 成绩最高分和最低分 +select CourseId,MAX(Score)最高分,MIN(Score)最低分 from Scores group by CourseId +--统计出 每个学生 的 各门成绩的平均分 +select StuId,CourseId,AVG(Score)各门成绩的平均分 from Scores group by StuId,CourseId order by StuId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/\347\273\203\344\271\2401.sql" "b/20210326\344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/\347\273\203\344\271\2401.sql" new file mode 100644 index 0000000000000000000000000000000000000000..26098a0376f776e7c29bb5932f622c7172c1cb64 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/\347\273\203\344\271\2401.sql" @@ -0,0 +1,97 @@ +create database Student +on +( + name=Student, + filename='E:\SQL课堂\Student.mdf', + size=10MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name=Student_log, + filename='E:\SQL课堂\Student_log.ldf', + size=10MB, + maxsize=50MB, + filegrowth=10% +) + +use Student +go + +create table Stuinfo +( + stuNO varchar(5) unique not null, + stuName nvarchar(20), + stuAge int, + stuAddress nvarchar(20), + stuSeat int identity(1,1) unique not null, + stuSex nchar(1) check(stuSex='男' or stuSex='女') +) +go +insert into Stuinfo values ('s2501','张秋利',20,'美国硅谷','女'),('s2502','李斯文',18,'湖北武汉','男'),('s2503','马文才',22,'湖南长沙','女'), +('s2504','欧阳俊雄',21,'湖北武汉','男'),('s2505','梅超风',20,'湖北武汉','女'),('s2506','陈旋风',19,'美国硅谷','女'),('s2507','陈风',20,'美国硅谷','男') + +select 学号=stuNO,stuName as 姓名,stuAge as 年龄,stuAddress as 地址,stuSeat as 座位号,stuSex as 性别 from Stuinfo +select stuName,stuAge,stuAddress from Stuinfo +select stuNO,邮箱=stuName+'@'+stuAddress from Stuinfo +select stuAddress from Stuinfo +select distinct 所有年龄=stuAge from Stuinfo +select * from Stuinfo where stuSeat<=3 +select stuName,stuSeat from Stuinfo where stuSeat<=4 +select top 50 percent * from Stuinfo +select * from Stuinfo where stuAddress='湖北武汉' and stuAge=20 +select * from Stuinfo where stuAddress='湖北武汉' or stuAddress='湖南长沙' +select * from Stuinfo where stuAddress in('湖北武汉' , '湖南长沙') +select * from Stuinfo where stuAge is null +select * from Stuinfo where stuAge is not null +select * from Stuinfo where stuName like '张%' +select * from Stuinfo where stuAddress like '湖%' +select * from Stuinfo where stuName like '张_' +select * from Stuinfo where stuName like '__俊_' +select * from Stuinfo order by stuAge desc +select * from Stuinfo order by stuAge desc,stuSeat asc + + + +create table Scoreinfo +( + examNO int primary key identity(1,1), + stuNO varchar(5) references Stuinfo(stuNO), + writtenExan int, + labExam int, +) +insert into Scoreinfo values('s2501',50,70),('s2502',60,65),('s2503',86,85),('s2504',40,80),('s2505',70,90),('s2506',85,90) + +select 学号=stuNO,writtenExan as 笔试 ,labExam as 机试 from Scoreinfo +select examNO,stuNO,总分=writtenExan+labExam from Scoreinfo +select labExam from Scoreinfo where labExam>=60 and labExam<=80 order by labExam desc +select writtenExan from Scoreinfo where not labExam>=70 and labExam<=90 +select top 1* from Scoreinfo order by writtenExan desc +select top 1* from Scoreinfo order by labExam + +select stuAddress,AVG(stuAge)平均年龄 from dbo.Stuinfo group by stuAddress +select stuSex,SUM(stuAge)年龄总和 from dbo.Stuinfo group by stuSex +select stuAddress,AVG(stuAge)平均年龄,SUM(stuAge)年龄总和 from dbo.Stuinfo group by stuAddress + + +--数据如图片1,使用上次作业的数据 +select * from Stuinfo +select * from Scoreinfo + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 stuName stuAge writtenExan labExam +select stuName 姓名,stuAge 年龄,writtenExan 笔试成绩,labExam 机试成绩 from Scoreinfo inner join Stuinfo on Scoreinfo.stuNO = Stuinfo.stuNO +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select Stuinfo.stuNO 学号,Stuinfo.stuName 姓名,Scoreinfo.writtenExan 笔试成绩,Scoreinfo.labExam 机试成绩 from Scoreinfo inner join Stuinfo on Scoreinfo.stuNO = Stuinfo.stuNO +where Scoreinfo.writtenExan>=60 and Scoreinfo.labExam>=60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select A.stuNO 学号,A.stuName 姓名,B.writtenExan 笔试成绩,B.labExam 机试成绩 from Stuinfo A +left join Scoreinfo B on A.stuNO = B.stuNO +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName 姓名,stuAge 年龄,writtenExan 笔试成绩,labExam 机试成绩 from Stuinfo inner join Scoreinfo on Stuinfo.stuNO = Scoreinfo.stuNO +where stuAge>=20 order by writtenExan DESC +--5.查询男女生的机试平均分 +select stuSex 性别,AVG(labExam)机试平均分 from Stuinfo inner join Scoreinfo on Stuinfo.stuNO = Scoreinfo.stuNO group by stuSex + --结果四舍五入 +--6.查询男女生的笔试总分 +select stuSex 性别,SUM(writtenExan)笔试总分 from Stuinfo inner join Scoreinfo on Stuinfo.stuNO = Scoreinfo.stuNO group by stuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/\347\273\203\344\271\2402.sql" "b/20210326\344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/\347\273\203\344\271\2402.sql" new file mode 100644 index 0000000000000000000000000000000000000000..459f349147fc8e68fe9d2094de4e006a48f36eb7 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/\347\273\203\344\271\2402.sql" @@ -0,0 +1,111 @@ +create database BBS +on +( + name='BBS', + filename='E:\SQL课堂\BBS.mdf', + size=20, + maxsize=300, + filegrowth=50 +) +log on +( + name='BBS_log', + filename='E:\SQL课堂\BBS_log.ldf', + size=20, + maxsize=300, + filegrowth=50 +) +go +use BBS +go +create table orderItem +( +ItemiD int , +orderId int, +itemType varchar(10), +itemName nvarchar(10), +theNumber int, +theMoney money +) +create table orders +( +orderId int not null primary key , +orderDate datetime +) +insert into orders(orderId,orderDate) values (1,'2008-1-12'),(2,'2008-2-10'),(3,'2008-2-15'),(4,'2008-3-10') +select * from orders +insert into orderItem(ItemiD,orderId,itemType,itemName,theNumber,theMoney) values +(1,1,'文具','笔',72,2), +(2,1,'文具','尺',10,1), +(3,1,'体育用品','篮球',1,56), +(4,2,'文具','笔',36,2), +(5,2,'文具','固体胶',20,3), +(6,2,'日常用品','透明胶',2,1), +(7,2,'体育用品','羽毛球',20,3), +(8,3,'文具','订书机',20,3), +(9,3,'文具','订书钉',10,3), +(10,3,'文具','裁纸刀',5,5), +(11,4,'文具','笔',20,2), +(12,4,'文具','信纸',50,1), +(13,4,'日常用品','毛巾',4,5), +(14,4,'日常用品','透明胶',30,1), +(15,4,'体育用品','羽毛球',20,3) + +select * from orderItem +--1.查询所有订单订购的所有物品数量总和 +select SUM(theNumber) 数量总和 from orderItem +--2.查询 每个订单订购的所有物品的 数量和 以及 平均单价 +select orderId,SUM(theNumber), AVG(theMoney) from orderItem group by orderId having AVG(theMoney)<10 and orderId<3 + +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select orderId ,sum(theNumber),AVG(theMoney) from orderItem where theNumber>50 group by orderId having AVG(theMoney)<10 +--4.查询每种类别的产品分别订购了几次,例如: +-- 文具 9 +-- 体育用品 3 +-- 日常用品 3 +select itemType 类型,COUNT(itemType) 数量 from orderItem group by itemType + +--5.查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 + +select itemType 类型,sum(theNumber)总数,avg(theMoney) 平均单价 from orderItem group by itemType having sum(theNumber)>100 + +--6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: + +-- 产品名称 订购次数 总数量 平均单价 +-- 笔 3 120 2 +select itemName 产品名称 ,COUNT(itemName) 订购次数,sum(theNumber) 总数量 ,avg(theMoney)平均单价 from orderItem group by itemName + + +-------------------------------------------------------- + +--使用上次作业的订单数据库,完成下列题目: +select * from orders +select * from orderItem + +--1.查询所有的订单的 订单的编号ItemiD,订单日期orderDate,订购产品的类别itemType和订购的产品名称itemName,订购数量theNumber和订购单价theMoney +select orders.orderId 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价 from orders inner join orderItem on orders.orderId = orderItem.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orders.orderId 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称 from orders inner join orderItem on orders.orderId = orderItem.orderId +where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderId 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价,SUM(theNumber * theMoney) 订购总价 from orders inner join orderItem on orders.orderId = orderItem.orderId +group by orders.orderId,orderDate,itemType,itemName,theNumber,theMoney +--4.查询单价大于等于3并且数量大于等于20的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderId 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价,SUM(theNumber * theMoney) 订购总价 from orders inner join orderItem on orders.orderId = orderItem.orderId +where theMoney>=3 and theNumber>=20 +group by orders.orderId,orderDate,itemType,itemName,theNumber,theMoney +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderItem.orderId 编号,COUNT(itemName)订购产品数 from orderItem group by orderItem.orderId +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 +select orderItem.orderId 订单编号,itemType 产品类别,COUNT(itemType)订购次数,SUM(theNumber)总数量 from orderItem group by orderItem.orderId,itemType order by orderItem.orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/\347\273\203\344\271\2403.sql" "b/20210326\344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/\347\273\203\344\271\2403.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c265c8ee6f9e12ce6a5500f5c3d97cd806a5bdd1 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\347\206\212\346\226\207\351\221\253/\347\273\203\344\271\2403.sql" @@ -0,0 +1,136 @@ +create database 论坛 +on +(name='论坛', + filename='E:\SQL论坛\论坛.mdf', + size=20, + maxsize=300, + filegrowth=50 +) +log on +(name='论坛_log', + filename='E:\SQL论坛\论坛_log.ldf', + size=20, + maxsize=300, + filegrowth=50 +) +go +use 论坛 +create table bbsUsers +( +uIDD int primary key identity(1,1), +uName varchar(10) not null unique , + uSex varchar(2) not null check(uSex='男' or uSex='女'), + uAge int not null check(uAge>15 and uAge<60), + uPoint int not null check(uPoint>=0) +) +create table bbsSection( +sIDD int primary key identity(1,1), +sName varchar(10) not null , + sUid int references bbsUsers(uIDD) +) +create table bbsTopic( + tID int primary key identity(1,1), + tUID int references bbsUsers(uIDD), + tSID int references bbsSection(sIDD), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime , + tCount int +) +create table bbsReply( +rID int primary key identity(1,1), + rUID int references bbsUsers(uIDD ), + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime + +) + +--1.现在有3个会员注册成功,请用一次插入多行数据的方法向bbsUsers表种插入3行记录,记录值如下: +-- 小雨点 女 20 0 +-- 逍遥 男 18 4 +-- 七年级生 男 19 2 +insert into bbsUsers(uName,uSex,uAge,uPoint) +select'小雨点','女','20','0'union +select'逍遥','男','18','4'union +select'七年级生','男','19','2' +go + + --2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中,提示查询部分列:select 列名1,列名2 from 表名 + select uName,uPoint into bbsPoint from bbsUsers + go + --3.给论坛开设4个板块 + -- 名称 版主名 + -- 技术交流 小雨点 + -- 读书世界 七年级生 + -- 生活百科 小雨点 + -- 八卦区 七年级生 + insert into bbsSection(sName,sUid) + select'技术交流',3 union + select'读书世界',1 union + select'生活百科',3 union + select'八卦区',1 + go + --4.向主贴和回帖表中添加几条记录 + insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) +select '2','1','范跑跑','谁是范跑跑','2008-7-8','1'union +select '1','3','.NET','与Java的区别是什么呀?','2008-9-1','2'union +select '3','4','今年夏天最流行什么呀?','有谁知道今年夏天最流行什么呀?','2008-9-10','0' + + +-- 回帖: +-- 分别给上面三个主贴添加对应的回帖,回帖的内容,时间,回帖人自定 + +insert into bbsReply(rTID,rMsg,rTime,rUId) +select'1','不知道','2008-9-5','2'union +select'2','不认识','2008-9-10','1'union +select'3','不知道','2008-10-1','3' + +-- 5.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户,请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) + + + +alter table bbsReply drop constraint FK_bbsReply_rUID ----出错 +alter table bbsTopic drop constraint FK_bbsTopic_tUID +alter table bbsSection drop constraint FK_bbsSection_sUid +alter table bbsUsers drop constraint PK_bbsUsers_UID + +delete from bbsUsers where UIDD=2 + +-- 6.因为小雨点发帖较多,将其积分增加10分 + + +update bbsPoint set uPoint=(uPoint + 10) where uName='小雨点' + + +-- 7.因为板块“生活百科”灌水的人太少,现决定取消该板块,即删除(注意主外键) + +delete from bbsReply where rTID=3 +delete from bbsTopic where tSID=4 +delete from bbsSection where sIDD=4 + +-- 8.因回帖积累太多,现需要将所有的回帖删除 + +delete from bbsReply +truncate table bbsReply +drop table bbsReply + + +--在论坛数据库中完成以下题目 +select * from bbsUsers +select * from bbsTopic +select * from bbsSection +select * from bbsPoint +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select sUid 版主编号,uName 版主姓名,sName 版块名称 from bbsSection inner join bbsUsers on bbsSection.sUid = bbsUsers.uIDD +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID 发帖人编号,uName 发帖人姓名,tTitle 帖子的标题,tMsg 帖子的内容,tTime 发帖时间 from bbsTopic inner join bbsUsers on bbsTopic.tUID = bbsUsers.uIDD +where tTime>'2008-8-15' +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sUid 版主编号,uName 版主的名称,sName 版块的名称 from bbsSection inner join bbsUsers on bbsSection.sUid = bbsUsers.uIDD +where bbsUsers.uAge<20 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select tUID 发帖人编号,uName 发帖人姓名,tTitle 主贴标题,tMsg 主贴内容,tCount 回复数量 from bbsTopic inner join bbsUsers on bbsTopic.tUID = bbsUsers.uIDD +where tcount=(select MAX(tcount)from bbsTopic) +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select tSID 版块编号,tUID 用户,count(tID)发帖总数 from bbsTopic inner join bbsUsers on bbsTopic.tUID = bbsUsers.uIDD inner join bbssection on bbsTopic.tUID = bbsUsers.uIDD group by tSID,tUID diff --git "a/20210326\344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c85238f0191932801892e6b5f9bfaf593a3e81be --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery3.sql" @@ -0,0 +1,104 @@ +create database dididada +on( + name='dididada', + filename='D:\dididada.mdf', + size=10, + maxsize=100, + filegrowth=10 + + +) +log on +( + name='dididada_log', + filename='D:\dididada_log.ldf', + size=10, + maxsize=100, + filegrowth=10 + + + +) +go +create table stuInfo( +stuID int identity(1,1) primary key, +stuName nvarchar(10)not null , +stuAge varchar(10) not null, +stuSex varchar(2) check(stuSex=1 or stuSex=0), +times datetime +) +go +create table courseInfo( +courseID int identity(1,1) primary key, +courseName nvarchar(10) not null, +courseMarks varchar(5) +) +go +create table scoreInfo( +scoreID int identity(1,1) primary key, +stuID int references stuInfo(stuID), +courseID int references courseInfo(courseID), +score int not null + +) +insert into stuInfo +select 'Tom',19,1,null union +select 'Jack',20,0 ,null union +select 'Rose',21,1,null union +select 'Lulu',19,1 ,null union +select 'Lili',21,0,null union +select 'abc',20,1 ,'2007-01-07 ' +insert into courseInfo +select 'JavaBase',4 union +select 'HTML',2 union +select 'JavaScript',2 union +select 'SqlBase',2 + +insert into scoreInfo +select 1,1 , 80union +select 1,2,85 union +select 1,4,50 union +select 2,1,75 union +select 2,3 ,45union +select 2,4 ,75union +select 3,1,45 union +select 4,1, 95union +select 4,2,75 union +select 4,3,90 union +select 4,4 ,45 + +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuID, courseName, courseMarks,avg(score) from courseInfo c inner join scoreInfo s on c.courseID=s.courseID group by courseName, courseMarks,score,stuID ORDER BY courseName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 + select scoreInfo.courseID,coursename,COUNT(stuid),SUM(score) from scoreInfo inner join courseInfo on scoreInfo.courseID=courseInfo.courseid group by scoreInfo.courseld,coursename + --3.查询出性别一样并且年龄一样的学生的信息 + select a.* from stuInfo a , stuInfo b where a.stusex=b.stusex and a.stuage=b.stuage and a.stuID!=b.stuID --4.查询出学分一样的课程信息 + select distinct a.courseid,a.coursename,a.coursemarks from courseInfo a,courseInfo b where a.coursemarks=b.coursemarks and a.courseid!=b.courseid + --5.查询出参加了考试的学生的学号,姓名,课程号和分数 + select stuInfo.stuID,stuname,courseID,score from scoreInfo inner join stuInfo on stuInfo.stuID=scoreInfo.stuid group by stuInfo.stuID,stuname,courseID,score + --6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 + select stuid,courseInfo.courseid,coursename,coursemarks,score from scoreInfo inner join courseInfo on scoreInfo.courseID=courseInfo.courseid group by stuid,courseInfo.courseid,coursename,coursemarks,score + --7.查询出没有参加考试的学生的学号和姓名 + select * from stuInfo left join scoreInfo on scoreInfo.stuid=stuInfo.stuID WHERE score IS NULL + --8.查询出是周六周天来报到的学生 +select * from stuInfo where datepart(weekday,times)=1 or datepart(weekday,times)=7 +--9.查询出姓名中有字母a的学生的信息 + select * from stuInfo where stuname like '%a%' + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 + select stuid,count(courseID),AVG(score) from scoreInfo group by stuid having COUNT(courseID)>2 and AVG(score)>70 + + + + + + + + + + + + + + + diff --git "a/20210326\344\275\234\344\270\232/\347\275\227\344\273\225\345\244\251/SQLQuery55.sql" "b/20210326\344\275\234\344\270\232/\347\275\227\344\273\225\345\244\251/SQLQuery55.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f0a9885b9133bd2a11aa76f70f0c09121f35472e --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\347\275\227\344\273\225\345\244\251/SQLQuery55.sql" @@ -0,0 +1,105 @@ +create database orderskk +on( + name='orderskk', + filename='D:\orderskk.mdf', + size=10, + maxsize=100, + filegrowth=10 + + +) +log on +( + name='orderskk_log', + filename='D:\orderskk_log.ldf', + size=10, + maxsize=100, + filegrowth=10 + + + +) +go +use orderskk +go +create table orders +( orderId int primary key identity , + orderDate datetime +) +go +create table orderItem +( + ItemiD int primary key identity , + orderId int , + itemType nvarchar(10), + itemName nvarchar(10), + theNumber money , + theMoney money , +) +insert orders values('2008-01-12'),('2008-02-10'),('2008-02-15'),('2008-03-10') +insert orderItem values +(1,'文具','笔',72,2), +(1,'文具','尺',10,1), +(1,'体育用品','篮球',1,56), +(2,'文具','笔',36,2), +(2,'文具','固体胶',20,3), +(2,'日常用品','透明胶',2,4), +(2,'体育用品','羽毛球',20,3), +(3,'文具','订书机',20,3), +(3,'文具','订书针',10,3), +(3,'文具','裁纸刀',5,5), +(4,'文具','笔',20,2), +(4,'文具','信纸',50,1), +(4,'日常用品','毛巾',4,5), +(4,'日常用品','透明胶',30,1), +(4,'体育用品','羽毛球',20,3) + +select * from orders +select * from orderItem +select SUM (theNumber) as 总数 from dbo.orderItem +select orderId ,sum(theNumber), AVG(theMoney) from orderItem where orderId<3 group by orderId HAVING avg(theMoney)<10 +select orderId ,sum(theNumber), AVG(theMoney) from orderItem where theNumber>50 group by orderId HAVING avg(theMoney)<10 +select itemType,COUNT(itemType)人数 from dbo.orderItem group by itemType +select itemType ,sum(theNumber), AVG(theMoney) from orderItem group by itemType HAVING sum(theNumber)>100 +select itemName ,COUNT(itemName),SUM(theNumber),AVG(theMoney) from orderItem group by itemName + + + +--使用上次作业的订单数据库,完成下列题目: + +select * from orders +select * from orderItem + + + +--1.查询所有的订单的 订单的编号,订单日期,订购产品的类别 和 订购的产品名称,订购数量 和 订购单价 +select OI.orderid,O.orderDate,itemType,itemName,theNumber,theMoney from orders O inner join orderItem OI on O.orderid=OI.orderid + + + +--2.查询订购数量大于50的 订单的编号,订单日期,订购产品的类别 和 订购的产品名称 + +select 1.theNumber ,2.orderDate,orderId from orders 1 inner join orderItem 2 on 1.orderId=2.orderId + + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select * from orders + + + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\347\275\227\344\273\225\345\244\251/SQLQuery66.sql" "b/20210326\344\275\234\344\270\232/\347\275\227\344\273\225\345\244\251/SQLQuery66.sql" new file mode 100644 index 0000000000000000000000000000000000000000..45d4ba295f1eca86821b56e396259092b10953aa --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\347\275\227\344\273\225\345\244\251/SQLQuery66.sql" @@ -0,0 +1,96 @@ +create database bbs + +go +use bbs +go + +create table bbsUsers +( + bbbsUID int identity(1,1), + uName varchar(10) not null, + uSex varchar(2) not null , + uAge int not null , + uPoint int not null +) +go + +alter table bbsUsers add constraint PK_bbbsUID primary key (bbbsUID) +alter table bbsUsers add constraint UK_uName unique(uName) +alter table bbsUsers add constraint CK_uSex check(uSex in('男','女')) +alter table bbsUsers add constraint CK_uAge check(uAge>14 and uAge<61 ) +alter table bbsUsers add constraint CK_uPoint check(len(uPoint)>=0) +go + +create table bbsSection +( + bbssID int identity(1,1), + sName varchar(10) not null, + sUid int +) +go + +alter table bbsSection add constraint PK_bbssID primary key (bbssID) +go + +alter table bbsSection add constraint FK_sUid foreign key (sUid) references bbsUsers(bbbsUID) +go + +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int foreign key references bbsUsers(bbbsUID), + tSID int foreign key references bbsSection(bbssID), + tTitle varchar(100) not null, + tMsg text, + tTime datetime default(getdate()), + tCount int +) +go + + +create table bbsReply +( + rID int primary key identity(1,1), + rUID int foreign key references bbsUsers(bbbsUID), + rTID int foreign key references bbsTopic(tID), + rMsg text NOT NULL, + rTime datetime default(getdate()) +) +go + +insert into bbsUsers(uName,uSex,uAge,uPoint) values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) +select uName,uPoint into bbsPoint from bbsUsers +insert into bbsSection (sName,sUid) values('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) + +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values(2 ,4,'范跑跑','谁是范跑跑 ',2008-7-8,1) +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values(3 ,1,'.NET','与JAVA的区别是什么呀? ',2008-9-1,2) +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values(1 ,3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀 ',2008-9-10,0) + +insert into bbsReply(rUID,rTID,rMsg,rTime) values(1 ,1,'不认识',2008-7-11) +insert into bbsReply(rUID,rTID,rMsg,rTime) values(1 ,2,'没有区别',2008-9-11) +insert into bbsReply(rUID,rTID,rMsg,rTime) values(2 ,2,'请百度',2008-9-12) + + +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select sUid,uName,sName +from bbsSection BS inner join bbsUsers BU on BS.sUid=BU.bbbsUID + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tID,tUID,tSID,tTitle,tMsg,tTime +from bbsTopic BT inner join bbsUsers BU on BT.tUID=BU.bbbsUID +where tTime >'2008-9-15' + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sUid,uName,uAge,sName +from bbsSection BS inner join bbsUsers BU on BS.sUid=BU.bbbsUID +where uAge<20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select sUid,uName,sName,tTitle,tMsg,tCount from bbsTopic BT inner join bbsUsers BU on BT.tUID=BU.bbbsUID +inner join bbsSection BS on BT.tSID=BS.bbssID where tCount=(select max(tCount) from bbsTopic) + +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select uName,(count(tUID)+count(tCount))发帖总数 +from bbsTopic BT inner join bbsUsers BU on BT.tUID=BU.bbbsUID +inner join bbsSection BS on BT.tSID=BS.bbssID +group by uName \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\347\275\227\344\273\225\345\244\251/SQLQuery77.sql" "b/20210326\344\275\234\344\270\232/\347\275\227\344\273\225\345\244\251/SQLQuery77.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7c28f6739e1cfb7f4b5fb01e01743272a7d108ad --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\347\275\227\344\273\225\345\244\251/SQLQuery77.sql" @@ -0,0 +1,53 @@ +use master +go + +create database Studen +go +use Studen +go + +create table Stuinfo +( + stuNO varchar(5) unique not null, + stuName nvarchar(20), + stuAge int, + stuAddress nvarchar(20), + stuSeat int identity(1,1) unique not null, + stuSex nchar(1) check(stuSex='男' or stuSex='女') +) +go + +insert into Stuinfo values ('s2501','张秋利',20,'美国硅谷','女'),('s2502','李斯文',18,'湖北武汉','男'),('s2503','马文才',22,'湖南长沙','女'), +('s2504','欧阳俊雄',21,'湖北武汉','男'),('s2505','梅超风',20,'湖北武汉','女'),('s2506','陈旋风',19,'美国硅谷','女'),('s2507','陈风',20,'美国硅谷','男') +go + +create table Scoreinfo +( + examNO int primary key identity(1,1), + stuNO varchar(5) references Stuinfo(stuNO), + writtenExan int, + labExam int, +) +go + +insert into Scoreinfo values('s2501',50,70),('s2502',60,65),('s2503',86,85),('s2504',40,80),('s2505',70,90),('s2506',85,90) +go + + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName,stuAge,writtenExan,labExam from Stuinfo S inner join Scoreinfo SI on S.stuNO=SI.stuNO + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select S.stuNO,stuName,writtenExan,labExam from Stuinfo S inner join Scoreinfo SI on S.stuNO=SI.stuNO where writtenExan>60 and labExam>60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select S.stuNO,stuName,writtenExan,labExam from Stuinfo S left join Scoreinfo SI on S.stuNO=SI.stuNO + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName,stuAge,writtenExan,labExam from Stuinfo S inner join Scoreinfo SI on S.stuNO=SI.stuNO order by writtenExan desc + +--5.查询男女生的机试平均分 +select stuSex,avg(labExam)机试平均分 from Stuinfo S inner join Scoreinfo SI on S.stuNO=SI.stuNO group by stuSex + +--6.查询男女生的笔试总分 +select stuSex,sum(writtenExan)笔试总分 from Stuinfo S inner join Scoreinfo SI on S.stuNO=SI.stuNO group by stuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\347\275\227\346\265\252\345\255\220/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\347\275\227\346\265\252\345\255\220/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0ea0b85a4bf4661108bdd9b6b6fab09df0b623ec --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\347\275\227\346\265\252\345\255\220/SQLQuery2.sql" @@ -0,0 +1,191 @@ +create database bbs + + + +go + +use bbs + +go + + + +create table bbsUsers + +( + + bbbsUID int identity(1,1), + + uName varchar(10) not null, + + uSex varchar(2) not null , + + uAge int not null , + + uPoint int not null + +) + +go + + + +alter table bbsUsers add constraint PK_bbbsUID primary key (bbbsUID) + +alter table bbsUsers add constraint UK_uName unique(uName) + +alter table bbsUsers add constraint CK_uSex check(uSex in('男','女')) + +alter table bbsUsers add constraint CK_uAge check(uAge>14 and uAge<61 ) + +alter table bbsUsers add constraint CK_uPoint check(len(uPoint)>=0) + +go + + + +create table bbsSection + +( + + bbssID int identity(1,1), + + sName varchar(10) not null, + + sUid int + +) + +go + + + +alter table bbsSection add constraint PK_bbssID primary key (bbssID) + +go + + + +alter table bbsSection add constraint FK_sUid foreign key (sUid) references bbsUsers(bbbsUID) + +go + + + +create table bbsTopic + +( + + tID int primary key identity(1,1), + + tUID int foreign key references bbsUsers(bbbsUID), + + tSID int foreign key references bbsSection(bbssID), + + tTitle varchar(100) not null, + + tMsg text, + + tTime datetime default(getdate()), + + tCount int + +) + +go + + + + + +create table bbsReply + +( + + rID int primary key identity(1,1), + + rUID int foreign key references bbsUsers(bbbsUID), + + rTID int foreign key references bbsTopic(tID), + + rMsg text NOT NULL, + + rTime datetime default(getdate()) + +) + +go + + + +insert into bbsUsers(uName,uSex,uAge,uPoint) values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) + +select uName,uPoint into bbsPoint from bbsUsers + +insert into bbsSection (sName,sUid) values('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) + + + +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values(2 ,4,'范跑跑','谁是范跑跑 ',2008-7-8,1) + +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values(3 ,1,'.NET','与JAVA的区别是什么呀? ',2008-9-1,2) + +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values(1 ,3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀 ',2008-9-10,0) + + + +insert into bbsReply(rUID,rTID,rMsg,rTime) values(1 ,1,'不认识',2008-7-11) + +insert into bbsReply(rUID,rTID,rMsg,rTime) values(1 ,2,'没有区别',2008-9-11) + +insert into bbsReply(rUID,rTID,rMsg,rTime) values(2 ,2,'请百度',2008-9-12) + + + + + +--1.查询出每个版块的版主编号,版主姓名和版块名称 + +select sUid,uName,sName + +from bbsSection BS inner join bbsUsers BU on BS.sUid=BU.bbbsUID + + + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 + +select tID,tUID,tSID,tTitle,tMsg,tTime + +from bbsTopic BT inner join bbsUsers BU on BT.tUID=BU.bbbsUID + +where tTime >'2008-9-15' + + + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 + +select sUid,uName,uAge,sName + +from bbsSection BS inner join bbsUsers BU on BS.sUid=BU.bbbsUID + +where uAge<20 + + + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 + +select sUid,uName,sName,tTitle,tMsg,tCount from bbsTopic BT inner join bbsUsers BU on BT.tUID=BU.bbbsUID + +inner join bbsSection BS on BT.tSID=BS.bbssID where tCount=(select max(tCount) from bbsTopic) + + + +--5.在主贴表中查询每个版块中每个用户的发帖总数 + +select uName,(count(tUID)+count(tCount))发帖总数 + +from bbsTopic BT inner join bbsUsers BU on BT.tUID=BU.bbbsUID + +inner join bbsSection BS on BT.tSID=BS.bbssID + +group by uName diff --git "a/20210326\344\275\234\344\270\232/\347\275\227\346\265\252\345\255\220/\344\275\234\344\270\232\344\270\2002.sql" "b/20210326\344\275\234\344\270\232/\347\275\227\346\265\252\345\255\220/\344\275\234\344\270\232\344\270\2002.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2c90fda1507a735a710f0ce16c518d02f674fffb --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\347\275\227\346\265\252\345\255\220/\344\275\234\344\270\232\344\270\2002.sql" @@ -0,0 +1,90 @@ +use master +go + +create database Dwu +on +( +name='Dwu', +filename='D:\test\Dwu.mdf', +size=5mb, +maxsize=100mb, +filegrowth=10mb +) +log on +( +name='Dwu_log', +filename='D:\test\Dwu_log.ldf', +size=5mb, +maxsize=100mb, +filegrowth=10mb +) + +go +use Dwu +go + +create table stuinfo +( +stuNo char(5) primary key not null, +stuName nvarchar(20) not null, +stuAge char(3) not null, +stuAddress nvarchar(200), +stuSeat int identity(1,1)not null, +stuSex char(1) check(stuSex in (1,0)) default(1) not null +) +go + +insert into stuinfo(stuNo,stuName,stuAge,stuAddress,stuSex) values +('s2501','张秋利',20,'美国硅谷',1), +('s2502','李斯文',18,'湖北武汉',0), +('s2503','马文才',22,'湖南长沙',1), +('s2504','欧阳俊雄',21,'湖北武汉',0), +('s2505','梅超风',20,'湖北武汉',1), +('s2506','陈旋风',19,'美国硅谷',1), +('s2507','陈凤',20,'美国硅谷',0) +go + +create table stuexam +( +examNo int primary key identity(1,1), +stuNo char(5) references stuinfo(stuNo) not null, +writtenExam int not null, +labExam int not null +) +go + +insert stuexam(stuNo,writtenExam,labExam)values +('s2501',50,70), +('s2502',60,65), +('s2503',86,85), +('s2504',40,80), +('s2505',70,90), +('s2506',85,90) +go +--数据如图片1,使用上次作业的数据 + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName,stuAge,writtenExam,labExam from stuinfo inner join stuexam on stuexam.stuNo = stuinfo.stuNo + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select stuName,stuAge, writtenExam,labExam from stuexam inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +where writtenExam>60 and labExam>60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select stuname,stuAge ,writtenExam,labExam from stuexam left join stuinfo on stuexam.stuNo = stuinfo.stuNo + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName,stuAge,writtenExam,labExam from stuexam inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +where stuAge>=20 +order by writtenExam desc + +--5.查询男女生的机试平均分 +select stuSex,AVG(labExam)机试 from stuexam inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +group by stuSex + +--6.查询男女生的笔试总分 +--1.查询所有订单订购的所有物品数量总和 +--select '所有物品综合',sum(theNumber) as 订购数量 from orderItem +select stuSex ,SUM(writtenExam)总分 from stuexam inner join stuinfo on stuexam.stuNo=stuinfo.stuNo +group by stuSex + diff --git "a/20210326\344\275\234\344\270\232/\347\275\227\346\265\252\345\255\220/\344\275\234\344\270\232\344\272\214.sql" "b/20210326\344\275\234\344\270\232/\347\275\227\346\265\252\345\255\220/\344\275\234\344\270\232\344\272\214.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7fea28e04ed24a306cefdb65a865c3974eafe45d --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\347\275\227\346\265\252\345\255\220/\344\275\234\344\270\232\344\272\214.sql" @@ -0,0 +1,112 @@ +use master +go + +create database shopping +on +( + name='shopping', + filename='D:\Demo\shopping.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='shopping_log', + filename='D:\Demo\shopping_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use shopping +go + +create table orders +( + orderId int primary key identity(1,1) , + orderDate datetime default(getdate()) +) + +create table orderItem +( + ItemiD int primary key identity(1,1), + orderId int references orders(orderId) not null, + itemType nvarchar(4) check(itemType='文具' or itemType='日常用品' or itemType='体育用品' ) not null, + itemName nvarchar(4) not null, + theNumber int not null, + theMoney money not null +) + +insert into orders values (default),(default),(default),(default) + + +insert into orderItem (orderId,itemType,itemName,theNumber,theMoney) values (1 ,'文具',' 笔' ,72, 2) +,(1,'文具','尺',10,1) +,(1,'体育用品','篮球',1,56) +,(2,'文具','笔',36,2) +,(2,'文具','固体胶',20,3) +,(2,'日常用品','透明胶',2,1) +,(2,'体育用品','羽毛球',20,3) +,(3,'文具','订书机',20,3) +,(3,'文具','订书针',10,3) +,(3,'文具','裁纸刀',5,5) +,(4,'文具','笔',20,2) +,(4,'文具','信纸',50,1) +,(4,'日常用品','毛巾',4,5) +,(4,'日常用品','透明胶',30,1) +,(4,'体育用品','羽毛球',20,3) + + +select orderDate,itemType,itemName from orderItem +inner join orders on orderItem.orderId = orders.orderId +where theNumber>50 +--使用上次作业的订单数据库,完成下列题目: +select * from orderItem +select * from orders +--1.查询 所有的订单 的 订单的编号, 订单日期, 订购产品的类别 和 订购的产品名称,订购数量 和 订购单价 +select orders.orderId,orderDate,itemType,itemName,theNumber,theMoney from + orderItem inner join orders on orderItem.orderId = orders.orderId + go +--2.查询 订购数量大于50 的 订单的编号,订单日期,订购产品的类别 和 订购的产品名称 +select orders.orderId,theNumber,orderDate,itemType,itemName from +orderItem inner join orders on orderItem.orderId = orders.orderId +where theNumber >50 +go + + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +select orders.orderId,orderDate,itemType,itemName,theNumber,theMoney * theNumber as 订购总价 from orderItem +inner join orders on orderItem.orderId = orders.orderId +go + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderId,orderDate,itemType,itemName,theNumber,theMoney * theNumber as 订购总价 from orderItem +inner join orders on orderItem.orderId = orders.orderId +where theMoney > 5 and theNumber > 50 +go + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orders.orderId as 编号,count(*) as 订购产品数 from orderItem +inner join orders on orderItem.orderId = orders.orderId +group by orders.orderId + + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 +select orders.orderId as 订单编号,itemType as 产品类别,count(*)as 订购次数,sum(theNumber) as 总数量 from orderItem +inner join orders on orderItem.orderId = orders.orderId +group by orders.orderId,itemType +order by orders.orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b113faaa18fc534b819c3464c4388dfb5a0ec6d6 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery2.sql" @@ -0,0 +1,15 @@ + + + +select oi.orderId,orderDate,itemType,itemName,theNumber,theMoney from dbo.orderItem oi inner join dbo.orders O on oi.orderId = o.orderId + + +select oi.orderId,orderDate,itemType,itemName from dbo.orderItem oi inner join dbo.orders o on oi.orderId = o.orderId where theNumber>50 + +select oi.orderId,orderDate,itemType,itemName,theNumber,theMoney,theMoney * theNumber from dbo.orderItem oi inner join dbo.orders o on oi.orderId = o.orderId + +select oi.orderId,orderDate,itemType,itemName,theNumber,theMoney,theMoney * theNumber from dbo.orderItem oi inner join dbo.orders o on oi.orderId =o.orderId where theNumber>5 and theNumber<50 + +select oi.orderId,COUNT(*) from dbo.orderItem oi inner join dbo.orders o on oi.orderId = o.orderId group by oi.orderId + +select oi.orderId,itemType,COUNT(*),sum(theNumber) from dbo.orderItem oi inner join dbo.orders o on oi.orderId = o.orderId group by oi.orderId,itemType order by oi.orderId diff --git "a/20210326\344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery6.sql" "b/20210326\344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery6.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2502aa462e155153480d94a63c3082d88f6f2a87 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery6.sql" @@ -0,0 +1,24 @@ + + + +select ClassId, COUNT(StuSex) from dbo.StuInfo where StuSex = '男' group by ClassId + +select ClassId,StuSex, COUNT(StuSex) from dbo.StuInfo group by ClassId ,StuSex + +select ClassId,COUNT(*) from dbo.StuInfo where StuProvince='福建省' group by ClassId + +select StuProvince,COUNT(*) from dbo.StuInfo where StuProvince is not null and StuProvince!='' group by StuProvince + +select StuProvince, COUNT(StuSex) from dbo.StuInfo where StuSex = '女' and StuProvince is not null and StuProvince!='' group by StuProvince + +select StuProvince,StuSex, COUNT(StuSex) from dbo.StuInfo where StuProvince is not null and StuProvince!='' group by StuProvince,StuSex + +select StuId,SUM(Score),AVG(Score) from dbo.Scores group by StuId + +select StuId,sum(Score) from dbo.Scores group by StuId having sum(Score)>620 + +select CourseId,max(Score), min(score) from dbo.Scores group by CourseId + +select StuId,CourseId,AVG(Score) from dbo.Scores group by StuId,CourseId + + diff --git "a/20210326\344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery7.sql" "b/20210326\344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery7.sql" new file mode 100644 index 0000000000000000000000000000000000000000..79704c922814cd9be65195fa873badc77f4febbc --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery7.sql" @@ -0,0 +1,19 @@ + + + + select UID,uName,sName from dbo.bbsUsers bu inner join dbo.bbsSection bs on bu.UID=bs.sUid + + select UID,uName,tTitle,tMsg,tTime from dbo.bbsUsers bs inner join dbo.bbsTopic bt on bs.UID=bt.tUID where tTime > '2008-09-15' + + select sUid,uName,sName from dbo.bbsSection bs inner join dbo.bbsUsers bu on bs.sUid=bu.UID where uAge<20 + + select UID,uName,tTitle,tMsg,tCount from dbo.bbsUsers bu inner join dbo.bbsTopic bt on bu.UID=bt.tUID where tCount=(select MAX(tCount) from bbsTopic) + + select tSID,tUID,uName,sName,count(tSID)as 发帖总数 from dbo.bbsTopic inner join dbo.bbsSection on bbsTopic.tSID=bbsSection.sID inner join dbo.bbsUsers on bbsTopic.tUID=bbsUsers.UID group by tSID,tUID,uName,sName + + + + + + + diff --git "a/20210326\344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery9.sql" "b/20210326\344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery9.sql" new file mode 100644 index 0000000000000000000000000000000000000000..30eb8059b05e0a3caa92d497a04180f0b8685fb1 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/SQLQuery9.sql" @@ -0,0 +1,17 @@ +select stuName,stuAge,writtenExam,labExam from dbo.StudenIfo inner join dbo.Class01 on StudenIfo.stuNo=Class01.stuNo + +select StudenIfo.stuNo,stuName,writtenExam,labExam from dbo.StudenIfo inner join dbo.Class01 on StudenIfo.stuNo=Class01.stuNo where writtenExam>60 and labExam>60 + +select StudenIfo.stuNo,stuName,writtenExam,labExam from dbo.StudenIfo left join dbo.Class01 on StudenIfo.stuNo=Class01.stuNo + +select stuName,stuAge,writtenExam,labExam from dbo.StudenIfo inner join dbo.Class01 on StudenIfo.stuNo=Class01.stuNo where stuAge>=20 order by writtenExam desc + +select stuSext,AVG(labExam) from dbo.StudenIfo inner join dbo.Class01 on StudenIfo.stuNo=Class01.stuNo group by stuSext + +select stuSext,SUM(writtenExam) from dbo.StudenIfo inner join dbo.Class01 on StudenIfo.stuNo=Class01.stuNo group by stuSext + + + + + + diff --git "a/20210326\344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a5ee43624011566c4eabb2fc5a452ad206ce5790 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery1.sql" @@ -0,0 +1,472 @@ +USE [master] +GO +/****** Object: Database [TestDB] Script Date: 2021/3/15 16:11:24 ******/ +CREATE DATABASE [TestDB] + CONTAINMENT = NONE + ON PRIMARY +( NAME = N'TestDB', FILENAME = N'D:\TestDB.mdf' , SIZE = 4288KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) + LOG ON +( NAME = N'TestDB_log', FILENAME = N'D:\TestDB_log.ldf' , SIZE = 1072KB , MAXSIZE = 2048GB , FILEGROWTH = 10%) +GO +ALTER DATABASE [TestDB] SET COMPATIBILITY_LEVEL = 120 +GO +IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled')) +begin +EXEC [TestDB].[dbo].[sp_fulltext_database] @action = 'enable' +end +GO +ALTER DATABASE [TestDB] SET ANSI_NULL_DEFAULT OFF +GO +ALTER DATABASE [TestDB] SET ANSI_NULLS OFF +GO +ALTER DATABASE [TestDB] SET ANSI_PADDING OFF +GO +ALTER DATABASE [TestDB] SET ANSI_WARNINGS OFF +GO +ALTER DATABASE [TestDB] SET ARITHABORT OFF +GO +ALTER DATABASE [TestDB] SET AUTO_CLOSE OFF +GO +ALTER DATABASE [TestDB] SET AUTO_SHRINK OFF +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS ON +GO +ALTER DATABASE [TestDB] SET CURSOR_CLOSE_ON_COMMIT OFF +GO +ALTER DATABASE [TestDB] SET CURSOR_DEFAULT GLOBAL +GO +ALTER DATABASE [TestDB] SET CONCAT_NULL_YIELDS_NULL OFF +GO +ALTER DATABASE [TestDB] SET NUMERIC_ROUNDABORT OFF +GO +ALTER DATABASE [TestDB] SET QUOTED_IDENTIFIER OFF +GO +ALTER DATABASE [TestDB] SET RECURSIVE_TRIGGERS OFF +GO +ALTER DATABASE [TestDB] SET ENABLE_BROKER +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF +GO +ALTER DATABASE [TestDB] SET DATE_CORRELATION_OPTIMIZATION OFF +GO +ALTER DATABASE [TestDB] SET TRUSTWORTHY OFF +GO +ALTER DATABASE [TestDB] SET ALLOW_SNAPSHOT_ISOLATION OFF +GO +ALTER DATABASE [TestDB] SET PARAMETERIZATION SIMPLE +GO +ALTER DATABASE [TestDB] SET READ_COMMITTED_SNAPSHOT OFF +GO +ALTER DATABASE [TestDB] SET HONOR_BROKER_PRIORITY OFF +GO +ALTER DATABASE [TestDB] SET RECOVERY FULL +GO +ALTER DATABASE [TestDB] SET MULTI_USER +GO +ALTER DATABASE [TestDB] SET PAGE_VERIFY CHECKSUM +GO +ALTER DATABASE [TestDB] SET DB_CHAINING OFF +GO +ALTER DATABASE [TestDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) +GO +ALTER DATABASE [TestDB] SET TARGET_RECOVERY_TIME = 0 SECONDS +GO +ALTER DATABASE [TestDB] SET DELAYED_DURABILITY = DISABLED +GO +EXEC sys.sp_db_vardecimal_storage_format N'TestDB', N'ON' +GO +USE [TestDB] +GO +/****** Object: Table [dbo].[ClassInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[ClassInfo]( + [ClassId] [int] IDENTITY(1,1) NOT NULL, + [ClassName] [nvarchar](20) NOT NULL, +PRIMARY KEY CLUSTERED +( + [ClassId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[CourseInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[CourseInfo]( + [CourseId] [int] IDENTITY(1,1) NOT NULL, + [CourseName] [nvarchar](50) NOT NULL, + [CourseCredit] [int] NULL DEFAULT ((1)), +PRIMARY KEY CLUSTERED +( + [CourseId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[Scores] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[Scores]( + [ScoreId] [int] IDENTITY(1,1) NOT NULL, + [StuId] [int] NULL, + [CourseId] [int] NULL, + [Score] [int] NULL DEFAULT ((0)), +PRIMARY KEY CLUSTERED +( + [ScoreId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[StuInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[StuInfo]( + [StuId] [int] IDENTITY(1,1) NOT NULL, + [ClassId] [int] NULL, + [StuName] [nvarchar](10) NOT NULL, + [StuSex] [nvarchar](1) NULL DEFAULT ('男'), + [StuBrithday] [date] NULL, + [StuPhone] [nvarchar](11) NULL, + [StuProvince] [nvarchar](200) NULL, + [CreateDate] [datetime] NULL DEFAULT (getdate()), + [StuAge] [int] NULL, +PRIMARY KEY CLUSTERED +( + [StuId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] ON + +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (1, N'软件1班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (2, N'软件2班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (3, N'软件3班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (4, N'软件4班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (5, N'软件5班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (6, N'软件6班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (7, N'软件7班') +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] ON + +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (1, N'计算机基础', 3) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (2, N'HTML+CSS网页制作', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (3, N'JAVA编程基础', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (4, N'SQL Server数据库基础', 4) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (5, N'C#面向对象编程', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (6, N'Winform桌面应用程序设计', 5) +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[Scores] ON + +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (1, 1, 1, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (2, 1, 2, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (3, 1, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (4, 1, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (5, 2, 1, 60) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (6, 2, 2, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (7, 2, 3, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (8, 2, 4, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (9, 3, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (10, 3, 2, 45) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (11, 3, 3, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (12, 3, 4, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (13, 4, 1, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (14, 4, 2, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (15, 4, 3, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (16, 4, 4, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (17, 5, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (18, 5, 2, 79) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (19, 5, 3, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (20, 5, 4, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (21, 6, 1, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (22, 6, 2, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (23, 6, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (24, 6, 5, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (25, 7, 1, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (26, 7, 2, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (27, 7, 3, 92) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (28, 7, 5, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (29, 8, 1, 58) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (30, 8, 2, 59) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (31, 8, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (32, 8, 5, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (33, 9, 1, 48) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (34, 9, 2, 67) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (35, 9, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (36, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (37, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (38, 1, 1, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (39, 1, 2, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (40, 1, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (41, 1, 4, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (42, 2, 1, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (43, 2, 2, 82) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (44, 2, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (45, 2, 4, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (46, 3, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (47, 3, 2, 50) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (48, 3, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (49, 3, 4, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (50, 4, 1, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (51, 4, 2, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (52, 4, 3, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (53, 4, 4, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (54, 5, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (55, 5, 2, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (56, 5, 3, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (57, 5, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (58, 6, 1, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (59, 6, 2, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (60, 6, 3, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (61, 6, 5, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (62, 7, 1, 89) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (63, 7, 2, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (64, 7, 3, 97) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (65, 7, 5, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (66, 8, 1, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (67, 8, 2, 64) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (68, 8, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (69, 8, 5, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (70, 9, 1, 53) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (71, 9, 2, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (72, 9, 3, 76) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (73, 9, 5, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (74, 9, 5, 61) +GO +SET IDENTITY_INSERT [dbo].[Scores] OFF +GO +SET IDENTITY_INSERT [dbo].[StuInfo] ON + +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (1, 1, N'刘正', N'男', CAST(N'2002-08-02' AS Date), N'13245678121', N'广西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (2, 1, N'黄贵', N'男', CAST(N'2003-07-02' AS Date), N'13345678121', N'江西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (3, 1, N'陈美', N'女', CAST(N'2002-07-22' AS Date), N'13355678125', N'福建省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (4, 2, N'江文', N'男', CAST(N'2001-07-02' AS Date), N'13347678181', N'湖南省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (5, 2, N'钟琪', N'女', CAST(N'2004-01-13' AS Date), N'13345778129', N'安徽省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (6, 3, N'曾小林', N'男', CAST(N'2005-05-15' AS Date), N'13345378563', N'安徽省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (7, 3, N'欧阳天天', N'女', CAST(N'2000-08-19' AS Date), N'13347878121', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (8, 3, N'李逍遥', N'男', CAST(N'1999-09-02' AS Date), N'13345678557', N'广东省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 22) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (9, 4, N'刘德华', N'男', CAST(N'1995-06-11' AS Date), N'15345679557', N'福建省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 26) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (10, 4, N'刘翔', N'男', CAST(N'1996-07-09' AS Date), N'18346679589', N'江西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (11, 4, N'曾小贤', N'男', CAST(N'2003-07-02' AS Date), N'18348979589', N'湖南省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (12, 5, N'刘德华', N'男', CAST(N'2002-07-02' AS Date), N'18348979509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (13, 5, N'陈天翔', N'男', CAST(N'2003-07-02' AS Date), N'18348079509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (14, 5, N'刘能', N'男', CAST(N'2005-08-02' AS Date), N'13245678122', N'广西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (15, 5, N'钟馗', N'男', CAST(N'2004-08-02' AS Date), N'13245678123', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (16, 5, N'钟吴艳', N'女', CAST(N'2002-08-02' AS Date), N'13245678124', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (17, 5, N'刘欢', N'男', CAST(N'2001-07-02' AS Date), N'13245678125', N'湖南省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (18, 5, N'张庭', N'女', CAST(N'2000-07-02' AS Date), N'13245678126', N'江西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (19, 5, N'曹植', N'男', CAST(N'2000-08-02' AS Date), N'13245678127', N'福建省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (20, 5, N'曹操', N'男', CAST(N'2002-08-02' AS Date), N'13245678128', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (21, 5, N'孙尚香', N'女', CAST(N'2003-08-02' AS Date), N'13245678129', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (22, 3, N'老1', N'女', CAST(N'2002-08-02' AS Date), N'13245678130', N'广东省', CAST(N'2021-03-14 17:02:36.347' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (24, 2, N'老2', N'男', CAST(N'2002-08-03' AS Date), N'13345678945', NULL, CAST(N'2021-03-14 17:03:37.733' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (25, 4, N'老3', N'男', NULL, N'13645987545', N'广东省', CAST(N'2021-03-14 17:03:43.307' AS DateTime), NULL) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (28, 5, N'老4', N'男', CAST(N'2006-03-05' AS Date), N'13456987456', NULL, CAST(N'2021-03-14 17:04:03.957' AS DateTime), 15) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (29, 5, N'老5', N'女', CAST(N'1998-04-12' AS Date), N'15978456123', NULL, CAST(N'2021-03-14 17:04:47.103' AS DateTime), 23) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (30, 4, N'老6', N'男', CAST(N'1996-08-06' AS Date), N'18945674561', NULL, CAST(N'2021-03-14 17:05:04.990' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (31, 3, N'老7', N'女', CAST(N'1997-04-06' AS Date), N'18845678912', NULL, CAST(N'2021-03-14 17:05:20.570' AS DateTime), 24) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (32, 2, N'老10', N'女', CAST(N'1998-08-09' AS Date), N'19945645612', NULL, CAST(N'2021-03-14 17:06:08.107' AS DateTime), 23) +GO +SET IDENTITY_INSERT [dbo].[StuInfo] OFF +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__CourseIn__9526E2773AB7BECE] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[CourseInfo] ADD UNIQUE NONCLUSTERED +( + [CourseName] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__StuInfo__2D85FC63AF6FC6FA] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[StuInfo] ADD UNIQUE NONCLUSTERED +( + [StuPhone] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([CourseId]) +REFERENCES [dbo].[CourseInfo] ([CourseId]) +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([StuId]) +REFERENCES [dbo].[StuInfo] ([StuId]) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD FOREIGN KEY([ClassId]) +REFERENCES [dbo].[ClassInfo] ([ClassId]) +ON DELETE SET NULL +GO +ALTER TABLE [dbo].[CourseInfo] WITH CHECK ADD CHECK (([CourseCredit]>=(1) AND [CourseCredit]<=(5))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK ((len([StuPhone])=(11))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK (([StuSex]='女' OR [StuSex]='男')) +GO +USE [master] +GO +ALTER DATABASE [TestDB] SET READ_WRITE +GO +select * from CourseInfo +select * from ClassInfo +select * from Scores +select * from StuInfo + +--统计每个班的男生数 +select ClassId 班级, count(StuSex) 男生人数 from StuInfo where StuSex='男' group by ClassId +--统计每个班的男、女生数 +select ClassId 班级,StuSex 性别 ,count(StuSex) from StuInfo group by ClassId ,StuSex +--统计每个班的福建人数 +select ClassId 班级, count(StuProvince) 福建省人数 from StuInfo where StuProvince='福建省' group by ClassId +--统计每个班的各个省的总人数 +select ClassId 班级, StuProvince 省份,count(StuProvince) 人数 from StuInfo where StuProvince is not null and not StuProvince='' group by ClassId ,StuProvince order by ClassId +--统计每个省的女生数 +select StuProvince 省份,count(StuSex) 人数 from StuInfo where StuSex='女' and StuProvince is not null and not StuProvince='' group by StuProvince +--统计每个省的男、女生数 +select StuProvince 省份,StuSex 性别 ,count(StuSex) from StuInfo where StuProvince is not null and not StuProvince='' group by StuProvince ,StuSex order by StuProvince +--统计每个学生的考试总分、平均分 +select StuId 学生编号, sum(Score) 总分 , avg(Score) 平均分数 from Scores group by StuId +--统计出考试总分大于620的学生的考试总分 +select StuId 学生编号, sum(Score) 总分 from Scores group by StuId having sum(Score)>620 +--统计出每门考试成绩最高分和最低分 +select CourseId 学生编号, max(Score) 最高分,min(Score) 最低分 from Scores group by CourseId order by CourseId +--统计出每个学生的各门成绩的平均分 +select StuId 学生编号,CourseId 科目, avg(Score) 平均分数 from Scores group by StuId ,CourseId + + + + + + + + diff --git "a/20210326\344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery1.sql01.sql" "b/20210326\344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery1.sql01.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cb9ab36d5b20cea494f03c5221f08fe371f2d666 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery1.sql01.sql" @@ -0,0 +1,58 @@ +create database Student02 +on +( + name='Student02', + filename='D:\Student02.mdf', + size=20, + maxsize=300, + filegrowth=50 +) +log on +( + name='Student02_log', + filename='D:\Student02_log.ldf', + size=20, + maxsize=300, + filegrowth=50 +) +go +use Student02 +go +create table StudenIfo +( + stuNo int identity(2501,1) primary key, + stuName nvarchar(20) not null unique, + stuAge int null , + studdRess nvarchar(20) not null, + stuSeat int , + stuSext int check(stuSext in('1','0')) default('0') +) +insert into StudenIfo(stuName,stuAge,studdRess,stuSeat,stuSext ) values('张秋利','20','美国硅谷','1','1'),('李斯文','18','湖北武汉','2','0'),('马文才','22','湖南长沙','3','1'), +('欧阳俊雄','21','湖北武汉','4','0'),('梅超凤','20','湖北武汉','5','1'),('陈炫风','20','美国硅谷','6','1'),('陈风','20','美国硅谷','7','0') + +create table Class01 +( + examNo int identity(1,1) primary key, + stuNo char(20) null, + writtenExam int , + labExam int +) +insert into Class01(stuNo,writtenExam,labExam) values ('2501','50','70'),('2502','60','65'),('2503','86','85'),('2503','40','80'),('2504','70','90'),('2505','85','90') + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName 姓名 ,stuAge 年龄,WrittenExam 笔试成绩,labExam 机试成绩 from StudenIfo S inner join Class01 C on S.stuNo=C.stuNo +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select stuName 姓名 ,stuAge 年龄,WrittenExam 笔试成绩,labExam 机试成绩 from StudenIfo S inner join Class01 C on S.stuNo=C.stuNo where WrittenExam >60 and labExam>60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select examNo 学号, stuName 姓名 ,stuAge 年龄,WrittenExam 笔试成绩,labExam 机试成绩 from StudenIfo S left join Class01 C on S.stuNo=C.stuNo + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName 姓名 ,stuAge 年龄,WrittenExam 笔试成绩,labExam 机试成绩 from StudenIfo S inner join Class01 C on S.stuNo=C.stuNo where stuAge >=20 order by WrittenExam DESC +--5.查询男女生的机试平均分 +select stuSext 性别, AVG(labExam) 机试成绩 from StudenIfo S left join Class01 C on S.stuNo=C.stuNo group by stuSext +--6.查询男女生的笔试总分 +select stuSext 性别, sum(WrittenExam) 笔试总分 from StudenIfo S left join Class01 C on S.stuNo=C.stuNo group by stuSext + +select * from StudenIfo +select * from Class01 \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery3.sql02.sql" "b/20210326\344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery3.sql02.sql" new file mode 100644 index 0000000000000000000000000000000000000000..077612902dffc3ca797f594ee390532ab4a5c58b --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery3.sql02.sql" @@ -0,0 +1,66 @@ +use master +go +create database orderr +on +( + name='orderr', + filename= 'D:\orderr.mdf', + size=10, + maxsize=100, + filegrowth=15% +) +log on +( + name='orderr_log', + filename= 'D:\orderr_log.ldf', + size=10, + maxsize=100, + filegrowth=15% +) +go +use orderr +go +create table orders +( + orderId int primary key identity, + orderDate datetime default(getdate()) +) +create table orderItem +( + ItemiD int primary key identity, + orderId int references orders(orderId) , + itemType nvarchar(10), + itemName nvarchar(10), + theNumber int , + theMoney money +) +insert into orders values(default),(default),(default),(default) +select * from orders +insert into orderItem values ('1','文具','笔','72','2'),('1','文具','尺','10','1'),('1','体育用品','蓝球','1','56'),('2','文具','笔','36','2'),('2','文具','固体胶','20','3'), +('2','日常用品','透明胶','2','1'),('2','体育用品','羽毛球','20','3'),('3','文具','订书机','20','3'),('3','文具','订书针','10','3'),('3','文具','裁纸刀','5','5'), +('4','文具','笔','20','2'),('4','文具','信纸','50','1'),('4','日常用品','毛巾','4','5'),('4','日常用品','透明胶','30','1'),('4','体育用品','羽毛球','20','3') +select * from orderItem +select * from orders +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select OI.orderId 订单编号 , orderDate 订单日期 , itemType 产品类型 , itemName 产品名称 ,theNumber 订购数量 , theMoney 订单单价 from orderItem OI inner join orders O on OI.orderId =O.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select OI.orderId 订单编号 , orderDate 订单日期 , itemType 产品类型 , itemName 产品名称 from orderItem OI inner join orders O on OI.orderId =O.orderId where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select OI.orderId 订单编号 , orderDate 订单日期 , itemType 产品类型 , itemName 产品名称 ,theNumber 订购数量 , theMoney 订单单价,theNumber* theMoney 订购总价 from orderItem OI inner join orders O on OI.orderId =O.orderId +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select OI.orderId 订单编号 , orderDate 订单日期 , itemType 产品类型 , itemName 产品名称 ,theNumber 订购数量 , theMoney 订单单价,theNumber* theMoney 订购总价 from orderItem OI inner join orders O on OI.orderId =O.orderId +where theMoney>5 and theNumber>50 +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +select OI.orderId 订单编号 ,sum(theNumber) 订购产品数 from orderItem OI inner join orders O on OI.orderId =O.orderId group by OI.orderId + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: +select OI.orderId 订单编号 ,itemType 产品类型 ,count(itemType) 订购次数 ,sum(theNumber) 总数量 from orderItem OI inner join orders O on OI.orderId =O.orderId group by itemType ,OI.orderId + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery6.sql03.sql" "b/20210326\344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery6.sql03.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ecbe1ed59d3689138b3072ae5d64abc766b18769 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\224\241\345\256\227\351\225\207/SQLQuery6.sql03.sql" @@ -0,0 +1,98 @@ +use master +go +create database bbs +on +( + name='bbs', + filename='D:\bbs.mdf', + size=10, + maxsize=200, + filegrowth=20 +) +log on +( + name='bbs_log', + filename='D:\bbs_log.ldf ', + size=10, + maxsize=200, + filegrowth=20 +) +use bbs + create table bbsUsers + ( + UIDD int identity(1,1) not null , + uName nvarchar(10) not null, + uSex varchar(2) not null , + uAge int not null, + uPoint int not null + ) + alter table bbsUsers add constraint PK_bbsUsers_UIDD primary key(UIDD ) +alter table bbsUsers add constraint UK_bbsUsers_uName unique(uName) +alter table bbsUsers add constraint CK_bbsUsers_uSex check(uSex in('女','男')) +alter table bbsUsers add constraint CK_bbsUsers_uAge check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint CK_bbsUsers_uPoint check(uPoint>=0) + create table bbsTopic + ( + tID int primary key identity(1,1), + tUID int references bbsUsers(UIDD), + tSID int references bbsSection(sIDD), + tTitle nvarchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int + ) + create table bbsReply + ( + rID int primary key identity(1,1), + rUID int references bbsUsers(UIDD) , + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime, + ) + create table bbsSection + ( + sIDD int identity(1,1), + sName nvarchar(10), + sUid int , + ) + alter table bbsSection add constraint PK_bbsSection_sIDD primary key(sIDD) + alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUsers(UIDD) + + insert into bbsUsers(uName,uSex,uAge,uPoint) values ('小雨点','女','20','0'),('逍遥 ','男','18','4'),('七年级生','男','19','2') + + create table bbsPoint + ( + ID nvarchar(10) , + point int + ) + + -- + insert into bbsPoint(ID,point) select uName,uPoint from bbsUsers + + + -- + select uName,uPoint into bbsUsers2 from bbsUsers + + + insert into bbsSection(sName,sUid) values ('技术交流 ','1'),(' 读书世界 ','3'),('生活百科','1'),('八卦区 ','3') + insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values ('2','4','范跑跑 ','谁是范跑跑','2008-7-8 ','1'), + ('3','1','.NET','与JAVA的区别是什么呀?','2008-9-1','2'),('1','3','今年夏天最流行什么','有谁知道今年夏天最流行什么呀?',' 2008-9-10','0') + + insert into bbsReply(rUID,rMsg, rTime) values ('1','你是','2020.1.2'),('3','爱狗','2020.2.15'),('2','还是能','2020.5.25') + select * from bbsSection + select * from bbsUsers + select * from bbsTopic + select * from bbsReply + +--1.查询出每个版块的版块编号,版主姓名和版块名称 +select uName 版主姓名,sIDD 版块编号,sName 版块名称 from bbsSection C inner join bbsUsers B on C.sUid=B.UIDD +--2.查询出主贴的发帖时间在2008-8-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID 发帖人编号, uName 发帖人姓名,tTitle 帖子的标题 ,tMsg 帖子的内容 ,tTime 发帖时间 from bbsTopic BT inner join bbsUsers BU on BT.tUID =BU.UIDD where tTime>'2008-8-15' +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select uName 版主姓名,sUid 版主编号,sName 版块名称 from bbsSection C inner join bbsUsers B on C.sUid=B.UIDD where uAge >18 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select tUID 发帖人编号,uName 发帖人姓名,tTitle 主贴标题 ,tMsg 主贴内容,tCount 回复数量 from bbsTopic C inner join bbsUsers B on C.tUID =B.UIDD where tCount= (select max(tCount)from bbsTopic) +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select sName 板块名称,uName 用户名称 ,sum(tCount) 发帖总数 from bbsTopic inner join bbsSection on sIDD=tSID +inner join bbsUsers on tUID =UIDD group by sName ,uName + diff --git "a/20210326\344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..aeded13695f8cca6209438edf8a8058b08652e16 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/SQLQuery1.sql" @@ -0,0 +1,53 @@ +create database Student02 +on +( + name='Student02', + filename='D:\Student02.mdf', + size=20, + maxsize=300, + filegrowth=50 +) +log on +( + name='Student02_log', + filename='D:\Student02_log.ldf', + size=20, + maxsize=300, + filegrowth=50 +) +go +use Student02 +go +create table StudenIfo +( + stuNo int identity(2501,1) primary key, + stuName nvarchar(20) not null unique, + stuAge int null , + studdRess nvarchar(20) not null, + stuSeat int , + stuSext int check(stuSext in('1','0')) default('0') +) +insert into StudenIfo(stuName,stuAge,studdRess,stuSeat,stuSext ) values('张秋利','20','美国硅谷','1','1'),('李斯文','18','湖北武汉','2','0'),('马文才','22','湖南长沙','3','1'), +('欧阳俊雄','21','湖北武汉','4','0'),('梅超凤','20','湖北武汉','5','1'),('陈炫风','20','美国硅谷','6','1'),('陈风','20','美国硅谷','7','0') + +create table Class01 +( + examNo int identity(1,1) primary key, + stuNo char(20) null, + writtenExam int , + labExam int +) +insert into Class01(stuNo,writtenExam,labExam) values ('2501','50','70'),('2502','60','65'),('2503','86','85'),('2503','40','80'),('2504','70','90'),('2505','85','90') + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName 姓名 ,stuAge 年龄,WrittenExam 笔试成绩,labExam 机试成绩 from StudenIfo S inner join Class01 C on S.stuNo=C.stuNo +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select stuName 姓名 ,stuAge 年龄,WrittenExam 笔试成绩,labExam 机试成绩 from StudenIfo S inner join Class01 C on S.stuNo=C.stuNo where WrittenExam>60 and labExam>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select examNo 学号,stuName 姓名 ,WrittenExam 笔试成绩,labExam 机试成绩 from StudenIfo S left join Class01 C on S.stuNo=C.stuNo +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName 姓名 ,stuAge 年龄,WrittenExam 笔试成绩,labExam 机试成绩 from StudenIfo S inner join Class01 C on S.stuNo=C.stuNo where stuAge>20 order by WrittenExam desc +--5.查询男女生的机试平均分 +select stuSext 性别, avg(labExam) 机试成绩 from StudenIfo S inner join Class01 C on S.stuNo=C.stuNo group by stuSext +--6.查询男女生的笔试总分 +select stuSext 性别,sum(WrittenExam) 笔试总分 from StudenIfo S inner join Class01 C on S.stuNo=C.stuNo group by stuSext diff --git "a/20210326\344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/SQLQuery12.sql" "b/20210326\344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/SQLQuery12.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5fb92838c5dd66e63e6c0995325526f650507d68 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/SQLQuery12.sql" @@ -0,0 +1,105 @@ + +create database bbs +on +( + name='bbs', + filename='D:\bbs.mdf', + size=10, + maxsize=200, + filegrowth=20 +) +log on +( + name='bbs_log', + filename='D:\bbs_log.ldf ', + size=10, + maxsize=200, + filegrowth=20 +) +use bbs + create table bbsUsers + ( + UIDD int identity(1,1) not null , + uName nvarchar(10) not null, + uSex varchar(2) not null , + uAge int not null, + uPoint int not null + ) + alter table bbsUsers add constraint PK_bbsUsers_UIDD primary key(UIDD ) +alter table bbsUsers add constraint UK_bbsUsers_uName unique(uName) +alter table bbsUsers add constraint CK_bbsUsers_uSex check(uSex in('女','男')) +alter table bbsUsers add constraint CK_bbsUsers_uAge check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint CK_bbsUsers_uPoint check(uPoint>=0) + create table bbsTopic + ( + tID int primary key identity(1,1), + tUID int references bbsUsers(UIDD), + tSID int references bbsSection(sIDD), + tTitle nvarchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int + ) + create table bbsReply + ( + rID int primary key identity(1,1), + rUID int references bbsUsers(UIDD) , + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime, + ) + create table bbsSection + ( + sIDD int identity(1,1), + sName nvarchar(10), + sUid int , + ) + alter table bbsSection add constraint PK_bbsSection_sIDD primary key(sIDD) + alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUsers(UIDD) + + insert into bbsUsers(uName,uSex,uAge,uPoint) values ('小雨点','女','20','0'),('逍遥 ','男','18','4'),('七年级生','男','19','2') + + create table bbsPoint + ( + ID nvarchar(10) , + point int + ) + + -- + insert into bbsPoint(ID,point) select uName,uPoint from bbsUsers + + + -- + select uName,uPoint into bbsUsers2 from bbsUsers + + + insert into bbsSection(sName,sUid) values ('技术交流 ','1'),(' 读书世界 ','3'),('生活百科','1'),('八卦区 ','3') + insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values ('2','4','范跑跑 ','谁是范跑跑','2008-7-8 ','1'), + ('3','1','.NET','与JAVA的区别是什么呀?','2008-9-1','2'),('1','3','今年夏天最流行什么','有谁知道今年夏天最流行什么呀?',' 2008-9-10','0') + + insert into bbsReply(rUID,rMsg, rTime) values ('1','你是','2020.1.2'),('3','爱狗','2020.2.15'),('2','还是能','2020.5.25') + select * from bbsSection + select * from bbsUsers + select * from bbsTopic + select * from bbsReply + +--在论坛数据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select sIDD 版主编号, uName 版主姓名,sName 版块名称 +from bbsSection BS inner join bbsUsers BU on BS.sUid=BU.UIDD + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖 +select tUID 发帖人编号, uName 发帖人姓名,tTitle 帖子的标题 ,tMsg 帖子的内容 ,tTime 发帖时间 +from bbsTopic BT inner join bbsUsers BU on BT.tUID =BU.UIDD where tTime>'2008-9-15' + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select uAge 年龄,sIDD 版主编号, uName 版主姓名,sName 版块名称 +from bbsSection bs inner join bbsUsers bu on bs.sUid=bu.UIDD where uAge<20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select top 1 tUID 发帖人编号,uName 发帖人姓名,tTitle 主贴标题 ,tMsg 主贴内容,tCount 回复数量 +from bbsTopic BT inner join bbsUsers BU on BT.tUID =BU.UIDD + +--5.在主贴表中查询每个版块中每个用户的发帖 +select sName 板块名称,uName 用户名称 ,sum(tCount) 发帖总数 +from bbsTopic inner join bbsSection on sIDD=tSID inner join bbsUsers on tUID =UIDD group by sName ,uName diff --git "a/20210326\344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fa8299181c2fb4e844faffec09ab9e333b6e8887 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/SQLQuery2.sql" @@ -0,0 +1,20 @@ +--统计每个班的男生数 +select ClassId ,count(StuSex)男生数 from dbo.StuInfo where StuSex='男' group by ClassId +--统计每个班的男、女生数 +select ClassId StuSex,count(StuSex) from dbo.StuInfo group by ClassId ,StuSex +--统计每个班的福建人数 +select ClassId,count(*)福建人数 from dbo.StuInfo where StuProvince='福建省' group by ClassId +--统计每个班的各个省的总人数 +select StuProvince,count(*) from dbo.StuInfo group by StuProvince +--统计每个省的女生数 +select StuProvince,count(*)女生数 from dbo.StuInfo where StuSex='女' group by StuProvince +--统计每个省的男、女生数 +select StuProvince StuSex,count(StuSex) from dbo.StuInfo group by StuProvince, StuSex order by StuProvince +--统计每个学生的考试总分、平均分 +select StuId,sum(Score)考试总分, avg(Score)平均分 from dbo.Scores group by StuId +--统计出考试总分大于620的学生的考试总分 +select StuId,sum(Score)考试总分 from dbo.Scores group by StuId having sum(Score)>620 +--统计出每门考试成绩最高分和最低分 +select CourseId,max(Score)最高分,min(Score)最低分 from dbo.Scores group by CourseId +--统计出每个学生的各门成绩的平均分 +select StuId,CourseID ,avg(Score)各门成绩的平均分 from dbo.Scores group by StuId, CourseID order by StuId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/SQLQuery6.sql" "b/20210326\344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/SQLQuery6.sql" new file mode 100644 index 0000000000000000000000000000000000000000..bfb0f35d5f41b41b49f7851fce2352346434ded2 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\256\270\345\237\271\346\250\237/SQLQuery6.sql" @@ -0,0 +1,89 @@ +create database orderform +on +( + name='orderform', + filename='D:\orderform.mdf', + size=10, + maxsize=100, + filegrowth=50% +) +log on +( + name='orderform_log', + filename='D:\orderform_log.ldf', + size=10, + maxsize=100, + filegrowth=50% +) +go +use orderform +go + +create table orders +( + orderId int primary key identity, + orderDate datetime not null +) +create table orderItem +( + ItemiD int primary key identity, + orderId int not null, + itemType nvarchar(10), + itemName nvarchar(10) not null, + theNumber int not null, + theMoney money +) +insert orders values +('2008-01-12'),('2008-02-10'), +('2008-02-15'),('2008-03-10') +go +insert into orderItem values +(1,'文具','笔',72,2), +(1,'文具','尺',10,1), +(1,'体育用品','篮球',1,56), +(2,'文具','笔',36,2), +(2,'文具','固体胶',20,3), +(2,'日常用品','透明胶',2,1), +(2,'体育用品','羽毛球',20,3), +(3,'文具','订书机',20,3), +(3,'文具','订书针',10,3), +(3,'文具','裁纸刀',5,5), +(4,'文具','笔',20,2), +(4,'文具','信纸',50,1), +(4,'日常用品','毛巾',4,5), +(4,'日常用品','透明胶',30,1), +(4,'体育用品','羽毛球',20,3) +select * from orderItem +select * from orders + +--使用上次作业的订单数据库,完成下列题目: + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select OI.orderId 编号,orderDate 订单日期,itemType 类别, itemName 产品名称,theNumber 订购数量,theMoney 订购单价 +from orderItem OI inner join orders O on OI.orderId=O.orderId + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select OI.orderId 编号,orderDate 订单日期,itemType 类别, itemName 产品名称,theNumber 订购数量 +from orderItem OI inner join orders O on OI.orderId=O.orderId where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select OI.orderId 编号,orderDate 订单日期,itemType 类别, itemName 产品名称,theNumber 订购数量,theMoney 订购单价,theMoney * theNumber 订购总价 +from orderItem OI inner join orders O on OI.orderId=O.orderId +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select OI.orderId 编号,orderDate 订单日期,itemType 类别, itemName 产品名称,theNumber 订购数量,theMoney 订购单价,theMoney * theNumber 订购总价 +from orderItem OI inner join orders O on OI.orderId=O.orderId where theMoney>5 and theNumber>50 +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select OI.orderId 编号,SUM(theNumber)订购产品数 from orderItem OI inner join orders O on OI.orderId=O.orderId group by OI.orderId +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 +select OI.orderId 编号,itemType 类别,count(itemType)订购次数,sum(theNumber) 总数量 +from orderItem OI inner join orders O on OI.orderId=O.orderId group by itemType, OI.orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\350\256\270\350\207\252\346\246\225/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\350\256\270\350\207\252\346\246\225/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f605494102e3d72114e1ab0fdede260f50d83ded --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\256\270\350\207\252\346\246\225/SQLQuery3.sql" @@ -0,0 +1,64 @@ +create database Student02 +on +( + name='Student02', + filename='D:\Student02.mdf', + size=20, + maxsize=300, + filegrowth=50 +) +log on +( + name='Student02_log', + filename='D:\Student02_log.ldf', + size=20, + maxsize=300, + filegrowth=50 +) +use Student02 +go +create table StudenIfo +( + stuNo varchar(10) primary key, + stuName nvarchar(20) not null unique, + stuAge int null , + studdRess nvarchar(20) not null, + stuSeat int , + stuSext int check(stuSext in('1','0')) default('0') +) +insert into StudenIfo(stuNo,stuName,stuAge,studdRess,stuSeat,stuSext ) values('s2501','张秋利','20','美国硅谷','1','1'),('s2502','李斯文','18','湖北武汉','2','0'),('s2503','马文才','22','湖南长沙','3','1'), +('s2504','欧阳俊雄','21','湖北武汉','4','0'), +('s2505','梅超凤','20','湖北武汉','5','1'), +('s2506','陈炫风','20','美国硅谷','6','1'), +('s2507','陈风','20','美国硅谷','7','0') + +create table Class09 +( + examNo int identity(1,1) primary key, + stuNo char(20) null, + writtenExam int , + labExam int +) +insert into Class09(stuNo,writtenExam,labExam) values ('s2501','50','70'),('s2502','60','65'),('s2503','86','85'),('s2503','40','80'),('s2504','70','90'),('s2505','85','90') + +--数据如图片1,使用上次作业的数据 +select * from StudenIfo +select * from Class09 +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName,stuAge,writtenExam,labExam from StudenIfo inner join Class09 on StudenIfo.stuNo = Class09.stuNo + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select stuName,stuAge,writtenExam,labExam from StudenIfo inner join Class09 on StudenIfo.stuNo = Class09.stuNo where writtenExam>60 and labExam>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select stuName,stuAge,writtenExam,labExam +from StudenIfo S left join Class09 C on S.stuNo = C.stuNo +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName,stuAge,writtenExam,labExam from StudenIfo inner join Class09 on StudenIfo.stuNo = Class09.stuNo where stuAge>=20 order by writtenExam +--5.查询男女生的机试平均分 + select stuSext,avg(labExam) from StudenIfo + inner join Class09 on StudenIfo.stuNo = Class09.stuNo + group by stuSext +--6.查询男女生的笔试总分 + select stuSext,sum(writtenExam) from StudenIfo + inner join Class09 on StudenIfo.stuNo = Class09.stuNo + group by stuSext \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\350\256\270\350\207\252\346\246\225/SQLQuery4.sql" "b/20210326\344\275\234\344\270\232/\350\256\270\350\207\252\346\246\225/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..dd2ddc164e8e3769707abeebbcb62cf317730b99 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\256\270\350\207\252\346\246\225/SQLQuery4.sql" @@ -0,0 +1,80 @@ +create database orderskk +on( + name='orderskk', + filename='D:\orderskk.mdf', + size=10, + maxsize=100, + filegrowth=10 + + +) +log on +( + name='orderskk_log', + filename='D:\orderskk_log.ldf', + size=10, + maxsize=100, + filegrowth=10 + + + +) +go +use orderskk +go +create table orders +( orderId int primary key identity , + orderDate datetime +) +go +create table orderItem +( + ItemiD int primary key identity , + orderId int , + itemType nvarchar(10), + itemName nvarchar(10), + theNumber money , + theMoney money , +) +insert orders values('2008-01-12'),('2008-02-10'),('2008-02-15'),('2008-03-10') +insert orderItem values +(1,'文具','笔',72,2), +(1,'文具','尺',10,1), +(1,'体育用品','篮球',1,56), +(2,'文具','笔',36,2), +(2,'文具','固体胶',20,3), +(2,'日常用品','透明胶',2,4), +(2,'体育用品','羽毛球',20,3), +(3,'文具','订书机',20,3), +(3,'文具','订书针',10,3), +(3,'文具','裁纸刀',5,5), +(4,'文具','笔',20,2), +(4,'文具','信纸',50,1), +(4,'日常用品','毛巾',4,5), +(4,'日常用品','透明胶',30,1), +(4,'体育用品','羽毛球',20,3) +--使用上次作业的订单数据库,完成下列题目: +select * from orders +select * from orderItem +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select * from orders inner join orderItem on orders.orderId = orderItem.ItemiD +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select ItemiD, orderDate,itemType,itemName from orders inner join orderItem on orders.orderId = orderItem.ItemiD where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select ItemiD, orderDate,itemType,itemName,sum(theNumber),sum(theMoney) from orders inner join orderItem on orders.orderId = orderItem.ItemiD group by +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5823880aae58829c504585d8b777f9e452e2d0a0 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/SQLQuery2.sql" @@ -0,0 +1,474 @@ +USE [master] +GO +/****** Object: Database [TestDB] Script Date: 2021/3/15 16:11:24 ******/ +CREATE DATABASE [TestDB] + CONTAINMENT = NONE + ON PRIMARY +( NAME = N'TestDB', FILENAME = N'D:\TestDB.mdf' , SIZE = 4288KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) + LOG ON +( NAME = N'TestDB_log', FILENAME = N'D:\TestDB_log.ldf' , SIZE = 1072KB , MAXSIZE = 2048GB , FILEGROWTH = 10%) +GO +ALTER DATABASE [TestDB] SET COMPATIBILITY_LEVEL = 120 +GO +IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled')) +begin +EXEC [TestDB].[dbo].[sp_fulltext_database] @action = 'enable' +end +GO +ALTER DATABASE [TestDB] SET ANSI_NULL_DEFAULT OFF +GO +ALTER DATABASE [TestDB] SET ANSI_NULLS OFF +GO +ALTER DATABASE [TestDB] SET ANSI_PADDING OFF +GO +ALTER DATABASE [TestDB] SET ANSI_WARNINGS OFF +GO +ALTER DATABASE [TestDB] SET ARITHABORT OFF +GO +ALTER DATABASE [TestDB] SET AUTO_CLOSE OFF +GO +ALTER DATABASE [TestDB] SET AUTO_SHRINK OFF +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS ON +GO +ALTER DATABASE [TestDB] SET CURSOR_CLOSE_ON_COMMIT OFF +GO +ALTER DATABASE [TestDB] SET CURSOR_DEFAULT GLOBAL +GO +ALTER DATABASE [TestDB] SET CONCAT_NULL_YIELDS_NULL OFF +GO +ALTER DATABASE [TestDB] SET NUMERIC_ROUNDABORT OFF +GO +ALTER DATABASE [TestDB] SET QUOTED_IDENTIFIER OFF +GO +ALTER DATABASE [TestDB] SET RECURSIVE_TRIGGERS OFF +GO +ALTER DATABASE [TestDB] SET ENABLE_BROKER +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF +GO +ALTER DATABASE [TestDB] SET DATE_CORRELATION_OPTIMIZATION OFF +GO +ALTER DATABASE [TestDB] SET TRUSTWORTHY OFF +GO +ALTER DATABASE [TestDB] SET ALLOW_SNAPSHOT_ISOLATION OFF +GO +ALTER DATABASE [TestDB] SET PARAMETERIZATION SIMPLE +GO +ALTER DATABASE [TestDB] SET READ_COMMITTED_SNAPSHOT OFF +GO +ALTER DATABASE [TestDB] SET HONOR_BROKER_PRIORITY OFF +GO +ALTER DATABASE [TestDB] SET RECOVERY FULL +GO +ALTER DATABASE [TestDB] SET MULTI_USER +GO +ALTER DATABASE [TestDB] SET PAGE_VERIFY CHECKSUM +GO +ALTER DATABASE [TestDB] SET DB_CHAINING OFF +GO +ALTER DATABASE [TestDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) +GO +ALTER DATABASE [TestDB] SET TARGET_RECOVERY_TIME = 0 SECONDS +GO +ALTER DATABASE [TestDB] SET DELAYED_DURABILITY = DISABLED +GO +EXEC sys.sp_db_vardecimal_storage_format N'TestDB', N'ON' +GO +USE [TestDB] +GO +/****** Object: Table [dbo].[ClassInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[ClassInfo]( + [ClassId] [int] IDENTITY(1,1) NOT NULL, + [ClassName] [nvarchar](20) NOT NULL, +PRIMARY KEY CLUSTERED +( + [ClassId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[CourseInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[CourseInfo]( + [CourseId] [int] IDENTITY(1,1) NOT NULL, + [CourseName] [nvarchar](50) NOT NULL, + [CourseCredit] [int] NULL DEFAULT ((1)), +PRIMARY KEY CLUSTERED +( + [CourseId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[Scores] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[Scores]( + [ScoreId] [int] IDENTITY(1,1) NOT NULL, + [StuId] [int] NULL, + [CourseId] [int] NULL, + [Score] [int] NULL DEFAULT ((0)), +PRIMARY KEY CLUSTERED +( + [ScoreId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[StuInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[StuInfo]( + [StuId] [int] IDENTITY(1,1) NOT NULL, + [ClassId] [int] NULL, + [StuName] [nvarchar](10) NOT NULL, + [StuSex] [nvarchar](1) NULL DEFAULT ('男'), + [StuBrithday] [date] NULL, + [StuPhone] [nvarchar](11) NULL, + [StuProvince] [nvarchar](200) NULL, + [CreateDate] [datetime] NULL DEFAULT (getdate()), + [StuAge] [int] NULL, +PRIMARY KEY CLUSTERED +( + [StuId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] ON + +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (1, N'软件1班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (2, N'软件2班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (3, N'软件3班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (4, N'软件4班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (5, N'软件5班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (6, N'软件6班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (7, N'软件7班') +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] ON + +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (1, N'计算机基础', 3) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (2, N'HTML+CSS网页制作', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (3, N'JAVA编程基础', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (4, N'SQL Server数据库基础', 4) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (5, N'C#面向对象编程', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (6, N'Winform桌面应用程序设计', 5) +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[Scores] ON + +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (1, 1, 1, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (2, 1, 2, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (3, 1, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (4, 1, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (5, 2, 1, 60) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (6, 2, 2, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (7, 2, 3, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (8, 2, 4, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (9, 3, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (10, 3, 2, 45) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (11, 3, 3, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (12, 3, 4, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (13, 4, 1, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (14, 4, 2, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (15, 4, 3, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (16, 4, 4, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (17, 5, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (18, 5, 2, 79) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (19, 5, 3, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (20, 5, 4, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (21, 6, 1, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (22, 6, 2, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (23, 6, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (24, 6, 5, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (25, 7, 1, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (26, 7, 2, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (27, 7, 3, 92) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (28, 7, 5, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (29, 8, 1, 58) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (30, 8, 2, 59) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (31, 8, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (32, 8, 5, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (33, 9, 1, 48) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (34, 9, 2, 67) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (35, 9, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (36, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (37, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (38, 1, 1, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (39, 1, 2, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (40, 1, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (41, 1, 4, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (42, 2, 1, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (43, 2, 2, 82) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (44, 2, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (45, 2, 4, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (46, 3, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (47, 3, 2, 50) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (48, 3, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (49, 3, 4, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (50, 4, 1, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (51, 4, 2, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (52, 4, 3, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (53, 4, 4, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (54, 5, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (55, 5, 2, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (56, 5, 3, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (57, 5, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (58, 6, 1, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (59, 6, 2, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (60, 6, 3, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (61, 6, 5, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (62, 7, 1, 89) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (63, 7, 2, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (64, 7, 3, 97) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (65, 7, 5, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (66, 8, 1, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (67, 8, 2, 64) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (68, 8, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (69, 8, 5, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (70, 9, 1, 53) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (71, 9, 2, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (72, 9, 3, 76) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (73, 9, 5, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (74, 9, 5, 61) +GO +SET IDENTITY_INSERT [dbo].[Scores] OFF +GO +SET IDENTITY_INSERT [dbo].[StuInfo] ON + +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (1, 1, N'刘正', N'男', CAST(N'2002-08-02' AS Date), N'13245678121', N'广西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (2, 1, N'黄贵', N'男', CAST(N'2003-07-02' AS Date), N'13345678121', N'江西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (3, 1, N'陈美', N'女', CAST(N'2002-07-22' AS Date), N'13355678125', N'福建省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (4, 2, N'江文', N'男', CAST(N'2001-07-02' AS Date), N'13347678181', N'湖南省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (5, 2, N'钟琪', N'女', CAST(N'2004-01-13' AS Date), N'13345778129', N'安徽省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (6, 3, N'曾小林', N'男', CAST(N'2005-05-15' AS Date), N'13345378563', N'安徽省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (7, 3, N'欧阳天天', N'女', CAST(N'2000-08-19' AS Date), N'13347878121', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (8, 3, N'李逍遥', N'男', CAST(N'1999-09-02' AS Date), N'13345678557', N'广东省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 22) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (9, 4, N'刘德华', N'男', CAST(N'1995-06-11' AS Date), N'15345679557', N'福建省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 26) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (10, 4, N'刘翔', N'男', CAST(N'1996-07-09' AS Date), N'18346679589', N'江西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (11, 4, N'曾小贤', N'男', CAST(N'2003-07-02' AS Date), N'18348979589', N'湖南省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (12, 5, N'刘德华', N'男', CAST(N'2002-07-02' AS Date), N'18348979509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (13, 5, N'陈天翔', N'男', CAST(N'2003-07-02' AS Date), N'18348079509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (14, 5, N'刘能', N'男', CAST(N'2005-08-02' AS Date), N'13245678122', N'广西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (15, 5, N'钟馗', N'男', CAST(N'2004-08-02' AS Date), N'13245678123', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (16, 5, N'钟吴艳', N'女', CAST(N'2002-08-02' AS Date), N'13245678124', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (17, 5, N'刘欢', N'男', CAST(N'2001-07-02' AS Date), N'13245678125', N'湖南省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (18, 5, N'张庭', N'女', CAST(N'2000-07-02' AS Date), N'13245678126', N'江西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (19, 5, N'曹植', N'男', CAST(N'2000-08-02' AS Date), N'13245678127', N'福建省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (20, 5, N'曹操', N'男', CAST(N'2002-08-02' AS Date), N'13245678128', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (21, 5, N'孙尚香', N'女', CAST(N'2003-08-02' AS Date), N'13245678129', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (22, 3, N'老1', N'女', CAST(N'2002-08-02' AS Date), N'13245678130', N'广东省', CAST(N'2021-03-14 17:02:36.347' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (24, 2, N'老2', N'男', CAST(N'2002-08-03' AS Date), N'13345678945', NULL, CAST(N'2021-03-14 17:03:37.733' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (25, 4, N'老3', N'男', NULL, N'13645987545', N'广东省', CAST(N'2021-03-14 17:03:43.307' AS DateTime), NULL) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (28, 5, N'老4', N'男', CAST(N'2006-03-05' AS Date), N'13456987456', NULL, CAST(N'2021-03-14 17:04:03.957' AS DateTime), 15) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (29, 5, N'老5', N'女', CAST(N'1998-04-12' AS Date), N'15978456123', NULL, CAST(N'2021-03-14 17:04:47.103' AS DateTime), 23) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (30, 4, N'老6', N'男', CAST(N'1996-08-06' AS Date), N'18945674561', NULL, CAST(N'2021-03-14 17:05:04.990' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (31, 3, N'老7', N'女', CAST(N'1997-04-06' AS Date), N'18845678912', NULL, CAST(N'2021-03-14 17:05:20.570' AS DateTime), 24) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (32, 2, N'老10', N'女', CAST(N'1998-08-09' AS Date), N'19945645612', NULL, CAST(N'2021-03-14 17:06:08.107' AS DateTime), 23) +GO +SET IDENTITY_INSERT [dbo].[StuInfo] OFF +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__CourseIn__9526E2773AB7BECE] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[CourseInfo] ADD UNIQUE NONCLUSTERED +( + [CourseName] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__StuInfo__2D85FC63AF6FC6FA] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[StuInfo] ADD UNIQUE NONCLUSTERED +( + [StuPhone] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([CourseId]) +REFERENCES [dbo].[CourseInfo] ([CourseId]) +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([StuId]) +REFERENCES [dbo].[StuInfo] ([StuId]) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD FOREIGN KEY([ClassId]) +REFERENCES [dbo].[ClassInfo] ([ClassId]) +ON DELETE SET NULL +GO +ALTER TABLE [dbo].[CourseInfo] WITH CHECK ADD CHECK (([CourseCredit]>=(1) AND [CourseCredit]<=(5))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK ((len([StuPhone])=(11))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK (([StuSex]='女' OR [StuSex]='男')) +GO +USE [master] +GO +ALTER DATABASE [TestDB] SET READ_WRITE +GO + +select * from ClassInfo +select * from CourseInfo +select * from Scores +select * from StuInfo +--统计每个班的男生数 +select * from StuInfo +select classid, COUNT(Stusex)每个班的男生数 from Stuinfo where Stusex='男' group by classid +--统计每个班的男、女生数 +select * from Stuinfo +select ClassId,StuSex,COUNT(StuSex)人数 from dbo.StuInfo group by ClassId,StuSex order by ClassId + +--统计每个班的福建人数 +select * from Stuinfo +select Classid,Count(Stuprovince)每个班的福建人数 from Stuinfo where Stuprovince='福建省' group by classid +--统计每个班的各个省的总人数 +select * from Stuinfo +select Classid,stuprovince,Count(*) from Stuinfo group by Classid,stuprovince +--统计每个省的女生数 +select * from Stuinfo +select classid,stuprovince,COUNT(stusex) 每个省的女生数 from stuinfo where stusex='女' group by classid,stuprovince +--统计每个省的男、女生数 +select classid,stusex,stuprovince,COUNT(stusex)人数 from stuinfo group by classid,stusex,stuprovince +--统计每个学生的考试总分、平均分 +select * from Scores +select StuId, sum(score)考试总分,AVG(score)平均分 from scores group by StuId +--统计出考试总分大于620的学生的考试总分 +select StuId,SUM(Score) from Scores group by StuId HAVING SUM(Score)>620 +--统计出每门考试成绩最高分和最低分 +select CourseId 课程,MAX(Score)最高分,MIN(Score)最低分 from Scores group by CourseId + +select * from StuInfo + +--统计出 每个学生 的 每门成绩 考试的平均分 +select StuId,CourseId,AVG(Score) from Scores GROUP BY StuId,CourseId ORDER BY StuId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/\347\273\203\344\271\2401/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/\347\273\203\344\271\2401/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..42d93b0649d3b33783bdce2bb0c8082a437413c0 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/\347\273\203\344\271\2401/SQLQuery2.sql" @@ -0,0 +1,92 @@ +create database Student +on +( + name=Student, + filename='D:\Student.mdf', + size=10MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name=Student_log, + filename='D:\Student_log.ldf', + size=10MB, + maxsize=50MB, + filegrowth=10% +) + + +create table Stuinfo +( + stuNO varchar(5) unique not null, + stuName nvarchar(20), + stuAge int, + stuAddress nvarchar(20), + stuSeat int identity(1,1) unique not null, + stuSex nchar(1) check(stuSex='男' or stuSex='女') +) +go +insert into Stuinfo values ('s2501','张秋利',20,'美国硅谷','女'),('s2502','李斯文',18,'湖北武汉','男'),('s2503','马文才',22,'湖南长沙','女'), +('s2504','欧阳俊雄',21,'湖北武汉','男'),('s2505','梅超风',20,'湖北武汉','女'),('s2506','陈旋风',19,'美国硅谷','女'),('s2507','陈风',20,'美国硅谷','男') + +select 学号=stuNO,stuName as 姓名,stuAge as 年龄,stuAddress as 地址,stuSeat as 座位号,stuSex as 性别 from Stuinfo +select stuName,stuAge,stuAddress from Stuinfo +select stuNO,邮箱=stuName+'@'+stuAddress from Stuinfo +select stuAddress from Stuinfo +select distinct 所有年龄=stuAge from Stuinfo +select * from Stuinfo where stuSeat<=3 +select stuName,stuSeat from Stuinfo where stuSeat<=4 +select top 50 percent * from Stuinfo +select * from Stuinfo where stuAddress='湖北武汉' and stuAge=20 +select * from Stuinfo where stuAddress='湖北武汉' or stuAddress='湖南长沙' +select * from Stuinfo where stuAddress in('湖北武汉' , '湖南长沙') +select * from Stuinfo where stuAge is null +select * from Stuinfo where stuAge is not null +select * from Stuinfo where stuName like '张%' +select * from Stuinfo where stuAddress like '湖%' +select * from Stuinfo where stuName like '张_' +select * from Stuinfo where stuName like '__俊_' +select * from Stuinfo order by stuAge desc +select * from Stuinfo order by stuAge desc,stuSeat asc + + + +create table Scoreinfo +( + examNO int primary key identity(1,1), + stuNO varchar(5) references Stuinfo(stuNO), + writtenExan int, + labExam int, +) +insert into Scoreinfo values('s2501',50,70),('s2502',60,65),('s2503',86,85),('s2504',40,80),('s2505',70,90),('s2506',85,90) + +select 学号=stuNO,writtenExan as 笔试 ,labExam as 机试 from Scoreinfo +select examNO,stuNO,总分=writtenExan+labExam from Scoreinfo +select labExam from Scoreinfo where labExam>=60 and labExam<=80 order by labExam desc +select writtenExan from Scoreinfo where not labExam>=70 and labExam<=90 +select top 1* from Scoreinfo order by writtenExan desc +select top 1* from Scoreinfo order by labExam +--25.查询每个地方的学生的平均年龄 +select stuAddress, AVG(StuAge)平均年龄 from dbo.StuInfo group by stuAddress + +--26.查询男女生的分别的年龄总和 +select '男生:',SUM(StuAge)年龄总和 from StuInfo where StuSex='男' +UNION +select '女生:', SUM(StuAge)年龄总和 from StuInfo where StuSex='女' +--27.查询每个地方的男女生的平均年龄和年龄的总和 +select stuAddress,AVG(StuAge)平均年龄,SUM(StuAge)年龄总和 from dbo.StuInfo group by stuAddress +select *from scoreinfo +select *from stuinfo +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuname,stuage,writtenexan,labexam from stuinfo inner join scoreinfo on stuinfo.stuno=scoreinfo.stuno +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select Scoreinfo.stuNO,stuname,writtenexan,labexam from stuinfo inner join scoreinfo on stuinfo.stuno=scoreinfo.stuno where writtenexan>60 and labexam>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select Scoreinfo.stuNO,stuname,writtenexan,labexam from stuinfo left join scoreinfo on stuinfo.stuno=scoreinfo.stuno +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuname,stuage,writtenexan,labexam from stuinfo inner join scoreinfo on stuinfo.stuno=scoreinfo.stuno where stuage>=20 order by writtenexan desc +--5.查询男女生的机试平均分 +select stusex,avg(labexam) from stuinfo inner join scoreinfo on stuinfo.stuno=scoreinfo.stuno group by stusex +--6.查询男女生的笔试总分 +select stusex,sum(writtenexan) from stuinfo inner join scoreinfo on stuinfo.stuno=scoreinfo.stuno group by stusex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/\347\273\203\344\271\2402/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/\347\273\203\344\271\2402/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a734c56eb6f3aaa6261d97653c1d050b53faee2e --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/\347\273\203\344\271\2402/SQLQuery1.sql" @@ -0,0 +1,100 @@ +create database Ordermenber +on +( + name=Ordermenber, + filename='D:\Ordermenber.mdf', + size=10MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name=Ordermenber_log, + filename='D:\Ordermenber_log.ldf', + size=10MB, + maxsize=50MB, + filegrowth=10% +) +go +--订单表(orders)列为:订单编号(orderId 主键) 订购日期(orderDate) +--订购项目表(orderItem),列为: +--项目编号(ItemiD)订单编号(orderId)产品类别(itemType) +--产品名称(itemName) 订购数量(theNumber) 订购单价(theMoney) + +create table orders +( +orderID int primary key identity(1,1), +orderData date default(getdate()) not null, +) +insert into orders(orderData)values('2008-01-12 00:00:00:000') +insert into orders(orderData)values('2008-02-10 00:00:00:000') +insert into orders(orderData)values('2008-02-15 00:00:00:000') +insert into orders(orderData)values('2008-03-10 00:00:00:000') + +create table orderItem +( +itemID int primary key identity(1,1), +orderid int references orders(orderID) not null, +itemType varchar(20) not null, +itemName varchar(20) not null, +theNumber int not null, +theMoney int not null +) +insert into orderItem(orderid,itemType,itemName,theNumber,theMoney)values(1,'文具','笔',72,2) +insert into orderItem(orderid,itemType,itemName,theNumber,theMoney)values(1,'文具','尺',10,1) +insert into orderItem(orderid,itemType,itemName,theNumber,theMoney)values(2,'体育用品','篮球',1,56) +insert into orderItem(orderid,itemType,itemName,theNumber,theMoney)values(2,'文具','笔',36,2) +insert into orderItem(orderid,itemType,itemName,theNumber,theMoney)values(2,'文具','固体胶',20,3) +insert into orderItem(orderid,itemType,itemName,theNumber,theMoney)values(2,'日常用品','透明胶',2,1) +insert into orderItem(orderid,itemType,itemName,theNumber,theMoney)values(3,'体育用品','羽毛球',20,3) +insert into orderItem(orderid,itemType,itemName,theNumber,theMoney)values(3,'文具','订书机',20,3) +insert into orderItem(orderid,itemType,itemName,theNumber,theMoney)values(3,'文具','订书机',10,3) +insert into orderItem(orderid,itemType,itemName,theNumber,theMoney)values(4,'文具','裁纸刀',5,5) +insert into orderItem(orderid,itemType,itemName,theNumber,theMoney)values(4,'文具','笔',20,2) +insert into orderItem(orderid,itemType,itemName,theNumber,theMoney)values(4,'文具','信纸',50,1) +insert into orderItem(orderid,itemType,itemName,theNumber,theMoney)values(4,'日常用品','毛巾',4,5) +insert into orderItem(orderid,itemType,itemName,theNumber,theMoney)values(4,'日常用品','透明胶',30,1) +insert into orderItem(orderid,itemType,itemName,theNumber,theMoney)values(4,'体育用品','羽毛球',20,3) +select orderID, SUM(theNumber)物品数量总和 from dbo.orderItem group by orderID +select orderID, SUM(theNumber)物品数量总和,AVG(theMoney)平均单价 from dbo.orderItem where orderID<3 group by orderID HAVING AVG(theMoney)<10 +select orderID, SUM(theNumber)物品数量总和,AVG(theMoney)平均单价 from dbo.orderItem where theNumber>50 group by orderID HAVING AVG(theMoney)<10 +select '文具',COUNT(itemType)订购次数 from orderItem where itemType='文具' +UNION +select '体育用品',COUNT(itemType)订购次数 from orderItem where itemType='体育用品' +UNION +select'日常用品',COUNT(itemType)订购次数 from orderItem where itemType='日常用品' +select '文具', SUM(theNumber)订购总数量,AVG(theMoney)平均单价 from dbo.orderItem where theNumber>100 group by itemType +UNION +select '体育用品',SUM(theNumber)订购总数量,AVG(theMoney)平均单价 from dbo.orderItem where theNumber>100 group by itemType +UNION +select'日常用品',SUM(theNumber)订购总数量,AVG(theMoney)平均单价 from dbo.orderItem where theNumber>100 group by itemType +select '文具',COUNT(itemType)订购次数,SUM(theNumber)订购总数量,AVG(theMoney)订购的平均单价 from orderItem where itemType='文具' +UNION +select '体育用品',COUNT(itemType)订购次数 ,SUM(theNumber)订购总数量,AVG(theMoney)订购的平均单价 from orderItem where itemType='体育用品' +UNION +select'日常用品',COUNT(itemType)订购次数 ,SUM(theNumber)订购总数量,AVG(theMoney)订购的平均单价 from orderItem where itemType='日常用品' + +select * from orders +select * from orderitem +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select itemid,orderdata,itemtype,itemname,thenumber,themoney from orders inner join orderitem on orders.orderid=orderitem.orderid +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select itemid,orderdata,itemtype,itemname from orders inner join orderitem on orders.orderid=orderitem.orderid where theNumber > 50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select itemid,orderdata,itemtype,itemname,thenumber,themoney,sum(theMoney) from orders inner join orderitem on orders.orderid=orderitem.orderid group by itemid,orderdata,itemtype,itemname,thenumber,themoney +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select itemid,orderdata,itemtype,itemname,thenumber,themoney,sum(theMoney) from orders inner join orderitem on orders.orderid=orderitem.orderid where themoney>=5 and thenumber<=50 group by itemid,orderdata,itemtype,itemname,thenumber,themoney +--5.查询每个订单分别订购了几个产品,例如: + --编号 订购产品数 + --1 3 + -- 2 4 +select orders.orderId,theNumber from orderItem inner join orders on orderItem.orderId=orders.orderId +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + + -- 1 文具 2 82 + -- 1 体育用品 1 1 + -- 2 文具 2 56 + -- 2 体育用品 1 2 + select orders.orderid,thenumber,itemtype,sum(theNumber) from orderitem inner join orders on orderitem.orderid=orders.orderid group by orders.orderid,thenumber,itemtype diff --git "a/20210326\344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/\347\273\203\344\271\2403/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/\347\273\203\344\271\2403/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..91dabc108c32857ac0ed45d7a44c06cb8caffdff --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\265\265\345\230\211\351\252\217/\347\273\203\344\271\2403/SQLQuery2.sql" @@ -0,0 +1,93 @@ +create database bbs +on( + name=bbs, + filename='D:\bbs.mdf', + maxsize=50mb, + size=10mb, + filegrowth=10% +) +log on( + name=bbs_log, + filename='D:\bbs_log.mdf', + maxsize=50mb, + size=10mb, + filegrowth=10% + +) +go +create table bbsUser +( +UID int identity (1,1) not null , +uName varchar(10) not null, +uSex varchar(2) not null, +uAge int not null, +uPoint int not null +) +create table bbsSection +( +sID int identity (1,1) not null, +sName varchar(10) not null, +sUid int +) +--添加约束 +-- alter table 表名 add constraint 约束名 约束类型 +alter table bbsUser add constraint PK_bbsUsers_uID primary key(uID) +alter table bbsUser add constraint UK_bbsUsers_uName unique(uName) +alter table bbsUser add constraint CK_bbsUsers_uSex check(uSex='男' or uSex='女') +alter table bbsUser add constraint CK_bbsUsers_uAge check(uAge>=15 and uAge<=60) +alter table bbsUser add constraint CK_bbsUser_uPoint check(uPoint>=0) + +alter table bbsSection add constraint PK_bbsSection_sID primary key(sID) +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUser(uID) + +create table bbsTopic +( +tID int primary key identity (1,1), +tUID int references bbsUser(UID), +tSID int references bbsSection(sID), +tTitle varchar(100) not null, +tMsg text not null, +tTime datetime, +tCount int +) +create table bbsReply +(rID int primary key identity (1,1), +rUID int references bbsUser(UID), +rTID int references bbsTopic(tID), +rMsg text not null, +rTime datetime +) +insert into bbsUser(uName,uSex,uAge,uPoint)values('小雨点','女',20,0) +insert into bbsUser(uName,uSex,uAge,uPoint)values('逍遥','男',18,4) +insert into bbsUser(uName,uSex,uAge,uPoint)values('七年级生','男',19,2) +--将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中, +select uName,uPoint into bbsPoint from bbsUser + +insert bbsSection(sName,sUid) + select '技术交流', 1 union + select '读书世界', 3 union + select '生活百科', 1 union + select '八卦区', 3 + + insert bbsTopic (tUID,tSID,tTitle,tMsg,tTime,tCount) + select 2 , 4,'范跑跑','谁是范跑跑','2008-7-8',1 union + select 3 , 1,'.NET ','与JAVA的区别是什么呀?',' 2008-9-1 ',2 union + select 1 , 3,'.NET ' , ' 有谁知道今年夏天最流行什么呀?','2008-9-10',0 + + insert into bbsReply (rUID,rMsg,rTime) + select 1,'高奕','2021-3-15' union + select 2,'the name deferent','2021-3-15' union + select 3,'sex','2021-3-15' + +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select sUid,uName,sName from bbsUser inner join bbsSection on bbsUser.uID=bbsSection.sUid + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tID,uName,tTitle,tMsg,tTime from bbsUser inner join bbsTopic on bbsUser.uID=bbsTopic.tID where tTime>='2008-8-15' +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sUID,uname,sname from bbsUser inner join bbsSection on bbsUser.uid=bbsSection.suid where uage<='19' +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select tid,uname,ttitle,tmsg,ttime,tcount from bbsuser inner join bbstopic on bbsuser.uid=bbstopic.tid where tcount=(select max(tCount) from bbsTopic) +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select * from bbssection +select tuid,count(tSID)发帖总数 from bbsTopic group by tUID,tSID diff --git "a/20210326\344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a6b0a17d08ae8cc3285171daf11efe9fc6840061 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/SQLQuery1.sql" @@ -0,0 +1,75 @@ +use master +go + +create database ui +go + +use ui +go +create table orders +( + orderid int primary key identity(1,1), + orderDate datetime +) +go + +create table orderItem +( + itemID int primary key identity(1,1), + orderid int references orders(orderid), + itemType varchar(10), + itemName varchar(20), + theNumber int, + theMoney decimal(7,2) +) +go + +insert into orders values('2008-01-12'),('2008-02-10'),('2008-02-15'),('2008-03-10') +go + +insert into orderItem values(1,'文具','笔','72','2'),(1,'文具','尺','10','1'),(1,'体育用品','篮球','1','56'),(2,'文具','笔','36','2'), +(2,'文具','固体胶','20','3'),(2,'日常用品','透明胶','2','1'),(2,'体育用品','羽毛球','20','3'),(3,'文具','订书机','20','3'),(3,'文具','订书机','10','3'), +(3,'文具','裁纸刀','5','5'),(4,'文具','笔','20','2'),(4,'文具','信纸','50','1'),(4,'日常用品','毛巾','4','5'),(4,'日常用品','透明球','30','1'), +(4,'体育用品','羽毛球','20','3') +go + + + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select OI.orderid,O.orderDate,itemType,itemName,theNumber,theMoney from orders O inner join orderItem OI on O.orderid=OI.orderid + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select OI.orderid,O.orderDate,itemType,itemName from orders O inner join orderItem OI on O.orderid=OI.orderid where theNumber>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select OI.orderid,O.orderDate,itemType,itemName,theNumber,theMoney 订购单价,sum(theMoney)订购总价 +from orders O inner join orderItem OI on O.orderid=OI.orderid +group by OI.orderid,O.orderDate,itemType,itemName,theNumber,theMoney + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select OI.orderid,O.orderDate,itemType,itemName,theNumber 订购数量,theMoney 订购单价,sum(theMoney)订购总价 +from orders O inner join orderItem OI on O.orderid=OI.orderid +where theMoney>=5 and theNumber>=50 +group by OI.orderid,O.orderDate,itemType,itemName,theNumber,theMoney + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select OI.orderid 编号,count(itemType) 订购产品数 +from orders O inner join orderItem OI on O.orderid=OI.orderid +group by OI.orderid + + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 +select OI.orderid,itemType,count(itemType)订购次数,sum(theNumber) +from orderItem OI inner join orders O on OI.orderid=O.orderid +group by OI.orderid,itemType order by OI.orderid diff --git "a/20210326\344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8ef7d7bbe390eabf562abbaf8af6dea16e5655b7 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/SQLQuery2.sql" @@ -0,0 +1,38 @@ +--统计每个班的男生数 +use TestDB +go +select ClassId,count(StuSex) 男生 from StuInfo group by StuSex,ClassId having StuSex='男' +select * from StuInfo + +--统计每个班的男、女生数 +select ClassId,StuSex,count(StuSex)人数 from StuInfo group by StuSex,ClassId + +--统计每个班的福建人数 +select ClassId,count(*)人数 from StuInfo group by StuProvince,ClassId having StuProvince='福建省' +select * from StuInfo + +--统计每个班的各个省的总人数 +select ClassId,StuProvince,count(*)各个省的总人数 from StuInfo group by StuProvince,ClassId +select * from StuInfo + +--统计每个省的女生数 +select StuProvince,count(*)女生数 from StuInfo group by StuProvince,StuSex having StuSex='女' + +--统计每个省的男、女生数 +select StuProvince,StuSex,count(*)人数 from StuInfo group by StuProvince,StuSex + +--统计每个学生的考试总分、平均分 +select SUM(Score) from Scores where StuId=1 +select StuId,SUM(Score)总分,avg(Score)平均分 from Scores group by StuId + +--统计出考试总分大于620的学生的考试总分 +select * from Scores +select StuId,SUM(Score) from Scores group by StuId having SUM(Score)>620 + + +--统计出每门考试成绩最高分和最低分 +select CourseId,max(Score)最高,min(Score)最低 from Scores group by CourseId + +--统计出每个学生的各门成绩的平均分 +select Score from Scores where StuId=1 +select StuId,avg(Score)平均分 from Scores group by StuId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7c28f6739e1cfb7f4b5fb01e01743272a7d108ad --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/SQLQuery3.sql" @@ -0,0 +1,53 @@ +use master +go + +create database Studen +go +use Studen +go + +create table Stuinfo +( + stuNO varchar(5) unique not null, + stuName nvarchar(20), + stuAge int, + stuAddress nvarchar(20), + stuSeat int identity(1,1) unique not null, + stuSex nchar(1) check(stuSex='男' or stuSex='女') +) +go + +insert into Stuinfo values ('s2501','张秋利',20,'美国硅谷','女'),('s2502','李斯文',18,'湖北武汉','男'),('s2503','马文才',22,'湖南长沙','女'), +('s2504','欧阳俊雄',21,'湖北武汉','男'),('s2505','梅超风',20,'湖北武汉','女'),('s2506','陈旋风',19,'美国硅谷','女'),('s2507','陈风',20,'美国硅谷','男') +go + +create table Scoreinfo +( + examNO int primary key identity(1,1), + stuNO varchar(5) references Stuinfo(stuNO), + writtenExan int, + labExam int, +) +go + +insert into Scoreinfo values('s2501',50,70),('s2502',60,65),('s2503',86,85),('s2504',40,80),('s2505',70,90),('s2506',85,90) +go + + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName,stuAge,writtenExan,labExam from Stuinfo S inner join Scoreinfo SI on S.stuNO=SI.stuNO + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select S.stuNO,stuName,writtenExan,labExam from Stuinfo S inner join Scoreinfo SI on S.stuNO=SI.stuNO where writtenExan>60 and labExam>60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select S.stuNO,stuName,writtenExan,labExam from Stuinfo S left join Scoreinfo SI on S.stuNO=SI.stuNO + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName,stuAge,writtenExan,labExam from Stuinfo S inner join Scoreinfo SI on S.stuNO=SI.stuNO order by writtenExan desc + +--5.查询男女生的机试平均分 +select stuSex,avg(labExam)机试平均分 from Stuinfo S inner join Scoreinfo SI on S.stuNO=SI.stuNO group by stuSex + +--6.查询男女生的笔试总分 +select stuSex,sum(writtenExan)笔试总分 from Stuinfo S inner join Scoreinfo SI on S.stuNO=SI.stuNO group by stuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/SQLQuery4.sql" "b/20210326\344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..13864f4445c7bcbbbe686a4a29270a391af24883 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\202\271\346\265\267\345\205\265/SQLQuery4.sql" @@ -0,0 +1,97 @@ +create database bbs + +go +use bbs +go + +create table bbsUsers +( + bbbsUID int identity(1,1), + uName varchar(10) not null, + uSex varchar(2) not null , + uAge int not null , + uPoint int not null +) +go + +alter table bbsUsers add constraint PK_bbbsUID primary key (bbbsUID) +alter table bbsUsers add constraint UK_uName unique(uName) +alter table bbsUsers add constraint CK_uSex check(uSex in('男','女')) +alter table bbsUsers add constraint CK_uAge check(uAge>14 and uAge<61 ) +alter table bbsUsers add constraint CK_uPoint check(len(uPoint)>=0) +go + +create table bbsSection +( + bbssID int identity(1,1), + sName varchar(10) not null, + sUid int +) +go + +alter table bbsSection add constraint PK_bbssID primary key (bbssID) +go + +alter table bbsSection add constraint FK_sUid foreign key (sUid) references bbsUsers(bbbsUID) +go + +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int foreign key references bbsUsers(bbbsUID), + tSID int foreign key references bbsSection(bbssID), + tTitle varchar(100) not null, + tMsg text, + tTime datetime default(getdate()), + tCount int +) +go + + +create table bbsReply +( + rID int primary key identity(1,1), + rUID int foreign key references bbsUsers(bbbsUID), + rTID int foreign key references bbsTopic(tID), + rMsg text NOT NULL, + rTime datetime default(getdate()) +) +go + +insert into bbsUsers(uName,uSex,uAge,uPoint) values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) +select uName,uPoint into bbsPoint from bbsUsers +insert into bbsSection (sName,sUid) values('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) + +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values(2 ,4,'范跑跑','谁是范跑跑 ',2008-7-8,1) +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values(3 ,1,'.NET','与JAVA的区别是什么呀? ',2008-9-1,2) +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values(1 ,3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀 ',2008-9-10,0) + +insert into bbsReply(rUID,rTID,rMsg,rTime) values(1 ,1,'不认识',2008-7-11) +insert into bbsReply(rUID,rTID,rMsg,rTime) values(1 ,2,'没有区别',2008-9-11) +insert into bbsReply(rUID,rTID,rMsg,rTime) values(2 ,2,'请百度',2008-9-12) + + +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select sUid,uName,sName +from bbsSection BS inner join bbsUsers BU on BS.sUid=BU.bbbsUID + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tID,tUID,tSID,tTitle,tMsg,tTime +from bbsTopic BT inner join bbsUsers BU on BT.tUID=BU.bbbsUID +where tTime >'2008-9-15' + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sUid,uName,uAge,sName +from bbsSection BS inner join bbsUsers BU on BS.sUid=BU.bbbsUID +where uAge<20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select sUid,uName,sName,tTitle,tMsg,tCount from bbsTopic BT inner join bbsUsers BU on BT.tUID=BU.bbbsUID +inner join bbsSection BS on BT.tSID=BS.bbssID where tCount=(select max(tCount) from bbsTopic) + +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select uName,(count(tUID)+count(tCount))发帖总数 +from bbsTopic BT inner join bbsUsers BU on BT.tUID=BU.bbbsUID +inner join bbsSection BS on BT.tSID=BS.bbssID +group by uName + diff --git "a/20210326\344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery1.4.sql" "b/20210326\344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery1.4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e0267e810edebdccb2170cae98950b211c3aec28 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery1.4.sql" @@ -0,0 +1,149 @@ +use master +go +if exists(select*from sys.databases where name='bbs') +drop database bbs +create database bbs +on +( +name='bbs', +filename='D:\bbs.dmf', +size=5mb, +maxsize=100mb, +filegrowth=10% +) +log on +( +name='bbs_log', +filename='D:\bbs_log.lmf', +size=5mb, +maxsize=100mb, +filegrowth=10% +) +go +use bbs +go +--用户信息表(bbsUsers) +-- 用户编号 UID int 主键 标识列 +-- 用户名 uName varchar(10) 唯一约束 不能为空 +-- 性别 uSex varchar(2) 不能为空 只能是男或女 +-- 年龄 uAge int 不能为空 范围15-60 +-- 积分 uPoint int 不能为空 范围 >= 0 +create table bbsUsers +( +UID int primary key identity(1,1), +uName varchar(10) unique not null, +uSex varchar(2) not null check(uSex in ('男' , '女')), +uAge int not null check(uAge >=15 and uAge <=60), +uPoint int not null check(uPoint>=0) +) +--主贴表(bbsTopic) +-- 主贴编号 tID int 主键 标识列, +-- 发帖人编号 tUID int 外键 引用用户信息表的用户编号 +-- 版块编号 tSID int 外键 引用版块表的版块编号 (标明该贴子属于哪个版块) +-- 贴子的标题 tTitle varchar(100) 不能为空 +-- 帖子的内容 tMsg text 不能为空 +-- 发帖时间 tTime datetime +-- 回复数量 tCount int +create table bbsSection +( +sID int primary key identity(1,1), +sName varchar(10) not null, +sUid int references bbsUsers(UID) +) +create table bbsTopic +( +tID int primary key identity(1,1), +tUID int references bbsUsers(UID), +tSID int references bbsSection(sID), +tTitle varchar(100) not null, +tMSg nvarchar(200) not null, +tTime datetime , +tCount int +) +--回帖表(bbsReply) +-- 回贴编号 rID int 主键 标识列, +-- 回帖人编号 rUID int 外键 引用用户信息表的用户编号 +-- 对应主贴编号 rTID int 外键 引用主贴表的主贴编号 (标明该贴子属于哪个主贴) +-- 回帖的内容 rMsg text 不能为空 +-- 回帖时间 rTime datetime +create table bbsReply +( +rID int primary key identity(1,1), +rUID int references bbsUsers(UID), +rTID int references bbsTopic(tID), +rMSg nvarchar(200) not null, +rTime datetime +) +--板块表bbsSection) +-- 版块编号 sID int 标识列 主键 +-- 版块名称 sName varchar(10) 不能为空 +-- 版主编号 sUid int 外键 引用用户信息表的用户编号 + +--1.现在有3个会员注册成功,请用一次插入多行数据的方法向bbsUsers表种插入3行记录,记录值如下: +-- 小雨点 女 20 0 +-- 逍遥 男 18 4 +-- 七年级生 男 19 2 +insert into bbsUsers +select '小雨点','女',20,0 union +select '逍遥','男',18,4union +select '七年级生','男',19,2 +-- 2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中,提示查询部分列:select 列名1,列名2 from 表名 +select uName,uPoint into bbsPoint from bbsUsers +-- 3.给论坛开设4个板块 +-- 名称 版主名 +-- 技术交流 小雨点 +-- 读书世界 七年级生 +-- 生活百科 小雨点 +-- 八卦区 七年级生 +insert into bbsSection +select '技术交流','1' union +select '读书世界','3' union +select '生活百科','1' union +select '八卦区','3' +-- 4.向主贴和回帖表中添加几条记录 + +-- 主贴: + +-- 发帖人 板块名 帖子标题 帖子内容 发帖时间 回复数量 +-- 逍遥 八卦区 范跑跑 谁是范跑跑 2008-7-8 1 +-- 七年级生 技术交流 .NET 与JAVA的区别是什么呀? 2008-9-1 2 +-- 小雨点 生活百科 今年夏天最流行什么 有谁知道今年夏天最流行 2008-9-10 0 +-- 什么呀? +insert into bbsTopic +select '2','4','范跑跑','谁是范跑跑','2008-7-8',1 union +select '3','1','.NET','与JAVA的区别是什么呀?','2008-9-1',2 union +select '1','3','今年夏天最流行什么呀?','有谁知道今年夏天最流行什么呀?','2008-9-10',0 union + +select '2','4','范跑跑','谁是范跑跑','2008-7-8',10 +-- 回帖: +-- 分别给上面三个主贴添加对应的回帖,回帖的内容,时间,回帖人自定 +insert into bbsReply +select '2','1','范跑跑是一个胆小鬼!','2009-1-4' union +select '3','2','JAVA头秃首选','2014-12-23' union +select '1','3','流行原宿风!','2020-1-3' + +-- 5.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户,请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) + +delete from bbsTopic where tUID=1 +delete from bbsReply where rUID=1 +delete from bbsSection where sName='逍遥' +-- 6.因为小雨点发帖较多,将其积分增加10分 +update bbsUsers set uPoint=uPoint+10 where UID=1 +-- 7.因为板块“生活百科”灌水的人太少,现决定取消该板块,即删除(注意主外键) +delete from bbsSection where sName='生活板块' +-- 8.因回帖积累太多,现需要将所有的回帖删除 +delete from bbsReply +select*from bbsTopic +select*from bbsSection +select*from bbsReply +select*from bbsUsers + +select bbsSection.sUid 版主编号,uName 版主姓名,sName 板块名称 from bbsSection inner join bbsUsers on bbsSection.sUid=bbsUsers.UID--1.查询出每个版块的版主编号,版主姓名和版块名称 + +select tUID 发帖人编号,uName 发帖人姓名,tTitle 帖子的标题,tMSg 帖子的内容,tTime 发帖时间 from bbsTopic inner join bbsUsers on bbsTopic.tUID=bbsUsers.UID where tTime>'2008-9-15'--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 + +select sUid 版主编号,uName 版主名称,sName 板块名称 from bbsSection inner join bbsUsers on bbsSection.sUid=bbsUsers.UID where uAge<20--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 + +select top 1 tUid 发帖人编号,uName 发帖人姓名,tTitle 主贴标题,tMSg 主贴内容,max(tCount)回复数量 from bbsTopic right join bbsUsers on bbsTopic.tUID=bbsUsers.UID group by uName,tTitle,tMSg,tCount,tUID order by tUid DESC--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 + +select tUID 用户编号,uName 用户姓名,count(*)发帖总数 from bbsTopic inner join bbsUsers on bbsTopic.tUID=bbsUsers.UID group by tUID,uName--5.在主贴表中查询每个版块中每个用户的发帖总数 \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery4.sql" "b/20210326\344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6d7ba530b0912be4dc276a9ad8fd9d9cc5f3c402 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery4.sql" @@ -0,0 +1,454 @@ +USE [master] +GO +/****** Object: Database [TestDB] Script Date: 2021/3/15 16:11:24 ******/ +CREATE DATABASE [TestDB] + CONTAINMENT = NONE + ON PRIMARY +( NAME = N'TestDB', FILENAME = N'D:\TestDB.mdf' , SIZE = 4288KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) + LOG ON +( NAME = N'TestDB_log', FILENAME = N'D:\TestDB_log.ldf' , SIZE = 1072KB , MAXSIZE = 2048GB , FILEGROWTH = 10%) +GO +ALTER DATABASE [TestDB] SET COMPATIBILITY_LEVEL = 120 +GO +IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled')) +begin +EXEC [TestDB].[dbo].[sp_fulltext_database] @action = 'enable' +end +GO +ALTER DATABASE [TestDB] SET ANSI_NULL_DEFAULT OFF +GO +ALTER DATABASE [TestDB] SET ANSI_NULLS OFF +GO +ALTER DATABASE [TestDB] SET ANSI_PADDING OFF +GO +ALTER DATABASE [TestDB] SET ANSI_WARNINGS OFF +GO +ALTER DATABASE [TestDB] SET ARITHABORT OFF +GO +ALTER DATABASE [TestDB] SET AUTO_CLOSE OFF +GO +ALTER DATABASE [TestDB] SET AUTO_SHRINK OFF +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS ON +GO +ALTER DATABASE [TestDB] SET CURSOR_CLOSE_ON_COMMIT OFF +GO +ALTER DATABASE [TestDB] SET CURSOR_DEFAULT GLOBAL +GO +ALTER DATABASE [TestDB] SET CONCAT_NULL_YIELDS_NULL OFF +GO +ALTER DATABASE [TestDB] SET NUMERIC_ROUNDABORT OFF +GO +ALTER DATABASE [TestDB] SET QUOTED_IDENTIFIER OFF +GO +ALTER DATABASE [TestDB] SET RECURSIVE_TRIGGERS OFF +GO +ALTER DATABASE [TestDB] SET ENABLE_BROKER +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF +GO +ALTER DATABASE [TestDB] SET DATE_CORRELATION_OPTIMIZATION OFF +GO +ALTER DATABASE [TestDB] SET TRUSTWORTHY OFF +GO +ALTER DATABASE [TestDB] SET ALLOW_SNAPSHOT_ISOLATION OFF +GO +ALTER DATABASE [TestDB] SET PARAMETERIZATION SIMPLE +GO +ALTER DATABASE [TestDB] SET READ_COMMITTED_SNAPSHOT OFF +GO +ALTER DATABASE [TestDB] SET HONOR_BROKER_PRIORITY OFF +GO +ALTER DATABASE [TestDB] SET RECOVERY FULL +GO +ALTER DATABASE [TestDB] SET MULTI_USER +GO +ALTER DATABASE [TestDB] SET PAGE_VERIFY CHECKSUM +GO +ALTER DATABASE [TestDB] SET DB_CHAINING OFF +GO +ALTER DATABASE [TestDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) +GO +ALTER DATABASE [TestDB] SET TARGET_RECOVERY_TIME = 0 SECONDS +GO +ALTER DATABASE [TestDB] SET DELAYED_DURABILITY = DISABLED +GO +EXEC sys.sp_db_vardecimal_storage_format N'TestDB', N'ON' +GO +USE [TestDB] +GO +/****** Object: Table [dbo].[ClassInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[ClassInfo]( + [ClassId] [int] IDENTITY(1,1) NOT NULL, + [ClassName] [nvarchar](20) NOT NULL, +PRIMARY KEY CLUSTERED +( + [ClassId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[CourseInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[CourseInfo]( + [CourseId] [int] IDENTITY(1,1) NOT NULL, + [CourseName] [nvarchar](50) NOT NULL, + [CourseCredit] [int] NULL DEFAULT ((1)), +PRIMARY KEY CLUSTERED +( + [CourseId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[Scores] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[Scores]( + [ScoreId] [int] IDENTITY(1,1) NOT NULL, + [StuId] [int] NULL, + [CourseId] [int] NULL, + [Score] [int] NULL DEFAULT ((0)), +PRIMARY KEY CLUSTERED +( + [ScoreId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[StuInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[StuInfo]( + [StuId] [int] IDENTITY(1,1) NOT NULL, + [ClassId] [int] NULL, + [StuName] [nvarchar](10) NOT NULL, + [StuSex] [nvarchar](1) NULL DEFAULT ('男'), + [StuBrithday] [date] NULL, + [StuPhone] [nvarchar](11) NULL, + [StuProvince] [nvarchar](200) NULL, + [CreateDate] [datetime] NULL DEFAULT (getdate()), + [StuAge] [int] NULL, +PRIMARY KEY CLUSTERED +( + [StuId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] ON + +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (1, N'软件1班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (2, N'软件2班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (3, N'软件3班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (4, N'软件4班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (5, N'软件5班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (6, N'软件6班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (7, N'软件7班') +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] ON + +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (1, N'计算机基础', 3) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (2, N'HTML+CSS网页制作', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (3, N'JAVA编程基础', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (4, N'SQL Server数据库基础', 4) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (5, N'C#面向对象编程', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (6, N'Winform桌面应用程序设计', 5) +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[Scores] ON + +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (1, 1, 1, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (2, 1, 2, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (3, 1, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (4, 1, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (5, 2, 1, 60) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (6, 2, 2, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (7, 2, 3, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (8, 2, 4, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (9, 3, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (10, 3, 2, 45) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (11, 3, 3, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (12, 3, 4, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (13, 4, 1, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (14, 4, 2, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (15, 4, 3, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (16, 4, 4, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (17, 5, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (18, 5, 2, 79) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (19, 5, 3, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (20, 5, 4, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (21, 6, 1, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (22, 6, 2, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (23, 6, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (24, 6, 5, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (25, 7, 1, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (26, 7, 2, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (27, 7, 3, 92) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (28, 7, 5, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (29, 8, 1, 58) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (30, 8, 2, 59) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (31, 8, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (32, 8, 5, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (33, 9, 1, 48) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (34, 9, 2, 67) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (35, 9, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (36, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (37, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (38, 1, 1, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (39, 1, 2, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (40, 1, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (41, 1, 4, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (42, 2, 1, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (43, 2, 2, 82) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (44, 2, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (45, 2, 4, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (46, 3, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (47, 3, 2, 50) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (48, 3, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (49, 3, 4, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (50, 4, 1, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (51, 4, 2, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (52, 4, 3, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (53, 4, 4, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (54, 5, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (55, 5, 2, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (56, 5, 3, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (57, 5, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (58, 6, 1, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (59, 6, 2, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (60, 6, 3, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (61, 6, 5, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (62, 7, 1, 89) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (63, 7, 2, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (64, 7, 3, 97) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (65, 7, 5, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (66, 8, 1, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (67, 8, 2, 64) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (68, 8, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (69, 8, 5, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (70, 9, 1, 53) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (71, 9, 2, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (72, 9, 3, 76) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (73, 9, 5, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (74, 9, 5, 61) +GO +SET IDENTITY_INSERT [dbo].[Scores] OFF +GO +SET IDENTITY_INSERT [dbo].[StuInfo] ON + +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (1, 1, N'刘正', N'男', CAST(N'2002-08-02' AS Date), N'13245678121', N'广西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (2, 1, N'黄贵', N'男', CAST(N'2003-07-02' AS Date), N'13345678121', N'江西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (3, 1, N'陈美', N'女', CAST(N'2002-07-22' AS Date), N'13355678125', N'福建省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (4, 2, N'江文', N'男', CAST(N'2001-07-02' AS Date), N'13347678181', N'湖南省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (5, 2, N'钟琪', N'女', CAST(N'2004-01-13' AS Date), N'13345778129', N'安徽省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (6, 3, N'曾小林', N'男', CAST(N'2005-05-15' AS Date), N'13345378563', N'安徽省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (7, 3, N'欧阳天天', N'女', CAST(N'2000-08-19' AS Date), N'13347878121', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (8, 3, N'李逍遥', N'男', CAST(N'1999-09-02' AS Date), N'13345678557', N'广东省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 22) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (9, 4, N'刘德华', N'男', CAST(N'1995-06-11' AS Date), N'15345679557', N'福建省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 26) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (10, 4, N'刘翔', N'男', CAST(N'1996-07-09' AS Date), N'18346679589', N'江西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (11, 4, N'曾小贤', N'男', CAST(N'2003-07-02' AS Date), N'18348979589', N'湖南省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (12, 5, N'刘德华', N'男', CAST(N'2002-07-02' AS Date), N'18348979509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (13, 5, N'陈天翔', N'男', CAST(N'2003-07-02' AS Date), N'18348079509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (14, 5, N'刘能', N'男', CAST(N'2005-08-02' AS Date), N'13245678122', N'广西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (15, 5, N'钟馗', N'男', CAST(N'2004-08-02' AS Date), N'13245678123', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (16, 5, N'钟吴艳', N'女', CAST(N'2002-08-02' AS Date), N'13245678124', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (17, 5, N'刘欢', N'男', CAST(N'2001-07-02' AS Date), N'13245678125', N'湖南省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (18, 5, N'张庭', N'女', CAST(N'2000-07-02' AS Date), N'13245678126', N'江西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (19, 5, N'曹植', N'男', CAST(N'2000-08-02' AS Date), N'13245678127', N'福建省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (20, 5, N'曹操', N'男', CAST(N'2002-08-02' AS Date), N'13245678128', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (21, 5, N'孙尚香', N'女', CAST(N'2003-08-02' AS Date), N'13245678129', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (22, 3, N'老1', N'女', CAST(N'2002-08-02' AS Date), N'13245678130', N'广东省', CAST(N'2021-03-14 17:02:36.347' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (24, 2, N'老2', N'男', CAST(N'2002-08-03' AS Date), N'13345678945', NULL, CAST(N'2021-03-14 17:03:37.733' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (25, 4, N'老3', N'男', NULL, N'13645987545', N'广东省', CAST(N'2021-03-14 17:03:43.307' AS DateTime), NULL) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (28, 5, N'老4', N'男', CAST(N'2006-03-05' AS Date), N'13456987456', NULL, CAST(N'2021-03-14 17:04:03.957' AS DateTime), 15) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (29, 5, N'老5', N'女', CAST(N'1998-04-12' AS Date), N'15978456123', NULL, CAST(N'2021-03-14 17:04:47.103' AS DateTime), 23) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (30, 4, N'老6', N'男', CAST(N'1996-08-06' AS Date), N'18945674561', NULL, CAST(N'2021-03-14 17:05:04.990' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (31, 3, N'老7', N'女', CAST(N'1997-04-06' AS Date), N'18845678912', NULL, CAST(N'2021-03-14 17:05:20.570' AS DateTime), 24) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (32, 2, N'老10', N'女', CAST(N'1998-08-09' AS Date), N'19945645612', NULL, CAST(N'2021-03-14 17:06:08.107' AS DateTime), 23) +GO +SET IDENTITY_INSERT [dbo].[StuInfo] OFF +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__CourseIn__9526E2773AB7BECE] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[CourseInfo] ADD UNIQUE NONCLUSTERED +( + [CourseName] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__StuInfo__2D85FC63AF6FC6FA] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[StuInfo] ADD UNIQUE NONCLUSTERED +( + [StuPhone] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([CourseId]) +REFERENCES [dbo].[CourseInfo] ([CourseId]) +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([StuId]) +REFERENCES [dbo].[StuInfo] ([StuId]) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD FOREIGN KEY([ClassId]) +REFERENCES [dbo].[ClassInfo] ([ClassId]) +ON DELETE SET NULL +GO +ALTER TABLE [dbo].[CourseInfo] WITH CHECK ADD CHECK (([CourseCredit]>=(1) AND [CourseCredit]<=(5))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK ((len([StuPhone])=(11))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK (([StuSex]='女' OR [StuSex]='男')) +GO +USE [master] +GO +ALTER DATABASE [TestDB] SET READ_WRITE +GO +select*from ClassInfo +select*from CourseInfo +select*from Scores +select*from StuInfo + +select ClassId 班级编号,count(*)男生人数 from StuInfo where StuSex='男' group by ClassId--统计每个班的男生数 +select ClassId 班级编号,StuSex 性别,count(*)性别人数 from StuInfo group by ClassId,StuSex order by ClassId--统计每个班的男、女生数 +select ClassId 班级编号,count(*)福建人数 from StuInfo where StuProvince='福建省'group by ClassId--统计每个班的福建人数 +select ClassId 班级编号,StuProvince 省份,sum(StuId)总人数 from StuInfo group by ClassId,StuProvince order by ClassId--统计每个班的各个省的总人数 +select StuProvince 省份,count(*) 女生人数 from StuInfo where StuSex='女' group by StuProvince--统计每个省的女生数 +select StuProvince 省份,StuSex 性别,count(*) 人数 from StuInfo group by StuProvince,StuSex order by StuProvince--统计每个省的男、女生数 +select StuId 学生编号,sum(Score)考试总分,Avg(Score)平均分数 from Scores group by StuId--统计每个学生的考试总分、平均分 +select StuId 学生编号,sum(Score)考试总分 from Scores group by StuId having sum(Score)>620--统计出考试总分大于620的学生的考试总分 +select CourseId 课程编号,max(Score)最高分,min(Score)最低分 from Scores group by CourseId--统计出每门考试成绩最高分和最低分 +select StuId 学生编号,CourseId 课程编号,Avg(Score)平均成绩 from Scores group by CourseId,StuID--统计出每个学生的各门成绩的平均分 \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery5.sql" "b/20210326\344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4d672e794d252df1b79361f08711ddbfb311bf48 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery5.sql" @@ -0,0 +1,77 @@ +use master +go +if exists(select*from sys.databases where name='TestDB' ) +drop database TestDB +create database TestDB +on +( + name='TestDB', + filename='D:\SQLcunchu\TestDB.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +log on +( name='TestDB_log', + filename='D:\SQLcunchu\TestDB_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +go +use TestDB +go +create table orders +( +orderId int primary key identity(1,1), +orderDate datetime +) +create table orderItem +( +ItemiD int identity(1,1), +orderId int , +itemType nvarchar(10), +itemName nvarchar(20), +theNumber int, +theMoney int +) +insert into orderItem values +(1,'文具','笔',72,2),(1,'文具','尺',10,1), +(1,'体育用品','篮球',1,56),(2,'文具','笔',36,2), +(2,'文具','固体胶',20,3),(2,'日常用品','笔透明胶',2,1), +(2,'体育用品','羽毛球',20,3),(3,'文具','订书机',20,3), +(3,'文具','订书针',10,3),(3,'文具','裁纸刀',5,5), +(4,'文具','笔',20,2),(4,'文具','信纸',50,1), +(4,'日常用品','毛巾',4,5),(4,'日常用品','透明胶',30,1), +(4,'体育用品','羽毛球',20,3) +insert into orders values +('2008-01-12'), +('2008-02-10'), +('2008-02-15'), +('2008-03-10') + +select*from orders +select*from orderItem + +select orders.orderId 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价 from orders inner join orderItem on orders.orderId=orderItem .orderId--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 + +select orders.orderId 订单编号,orderDate 订单日期,itemType 订购类别,itemName 产品名称 from orders inner join orderItem on orders.orderId=orderItem .orderId where theNumber>50--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 + +select orders.orderId 订单编号,orderDate 订单日期,itemType 订购类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价,sum(theMoney*theNumber)订购总价 from orders inner join orderItem on orders.orderId=orderItem .orderId group by orders .orderId,orderDate,itemType,itemName,theNumber,theMoney--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +select orders.orderId 订单编号,orderDate 订单日期,itemType 订购类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价,sum(theMoney)订购总价 from orders inner join orderItem on orders.orderId=orderItem .orderId group by orders .orderId,orderDate,itemType,itemName,theNumber,theMoney having theMoney>=5 and sum(theNumber)>=50--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +select orderId 编号,theNumber 订购产品数 from orderItem--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 + +select orderId 订单编号,itemType 产品类别,theNumber 订购次数,sum(theNumber) 总数量 from orderItem group by orderId,itemType,theNumber--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +--订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery6.sql" "b/20210326\344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery6.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c1ced314d12d36b483bc46d64f08c08e32e164a9 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\203\221\347\276\216\345\251\267/SQLQuery6.sql" @@ -0,0 +1,70 @@ +use master +go +if exists(select*from sys.databases where name='Dwu') +drop database Dwu +create database Dwu +on +( +name='Dwu', +filename='D:\SQLcunchu\Dwu.mdf', +size=5mb, +maxsize=100mb, +filegrowth=10mb +) +log on +( +name='Dwu_log', +filename='D:\SQLcunchu\Dwu_log.ldf', +size=5mb, +maxsize=100mb, +filegrowth=10mb +) +go +use Dwu +go +create table stuinfo +( +stuNo char(5) primary key not null, +stuName nvarchar(20) not null, +stuAge char(3) not null, +stuAddress nvarchar(200), +stuSeat int identity(1,1)not null, +stuSex char(1) check(stuSex in (1,0)) default(1) not null +) + +insert into stuinfo(stuNo,stuName,stuAge,stuAddress,stuSex) values +('s2501','张秋利',20,'美国硅谷',1), +('s2502','李斯文',18,'湖北武汉',0), +('s2503','马文才',22,'湖南长沙',1), +('s2504','欧阳俊雄',21,'湖北武汉',0), +('s2505','梅超风',20,'湖北武汉',1), +('s2506','陈旋风',19,'美国硅谷',1), +('s2507','陈凤',20,'美国硅谷',0) +create table stuexam +( +examNo int primary key identity(1,1), +stuNo char(5) references stuinfo(stuNo) not null, +writtenExam int not null, +labExam int not null +) +insert stuexam(stuNo,writtenExam,labExam)values +('s2501',50,70), +('s2502',60,65), +('s2503',86,85), +('s2504',40,80), +('s2505',70,90), +('s2506',85,90) + +select*from stuexam +select*from stuinfo +select stuName 学生姓名,stuAge 学生年龄,writtenExam 笔试成绩,labExam 机试成绩 from stuinfo inner join stuexam on stuinfo.stuNo = stuexam.stuNo--1.查询学生的姓名,年龄,笔试成绩和机试成绩 + +select stuinfo.stuNo 学生学号,stuName 学生姓名,writtenExam 笔试成绩,labExam 机试成绩 from stuinfo inner join stuexam on stuinfo.stuNo=stuexam.stuNo where writtenExam>=60 and labExam>=60--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 + +select stuinfo.stuNo 学生学号,stuName 学生名字,writtenExam 笔试成绩,labExam 机试成绩 from stuexam left join stuinfo on stuinfo.stuNo=stuexam.stuNo--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 + +select stuName 学生姓名,stuAge 学生性别,writtenExam 笔试成绩,labExam 机试成绩 from stuinfo inner join stuexam on stuexam.stuNo=stuinfo.stuNo where stuAge>=20 order by writtenExam desc --4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列-- + +select stuSex 学生性别,avg(labExam) 平均机试成绩 from stuinfo right join stuExam on stuexam.stuNo=stuinfo.stuNo group by stuSex --5.查询男女生的机试平均分 + +select stuSex 学生性别,sum(writtenExam) 笔试成绩总分 from stuinfo left join stuexam on stuinfo.stuNo=stuexam.stuNo group by stuSex--6.查询男女生的笔试总分 \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/groupby02.sql" "b/20210326\344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/groupby02.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b1c4527befa1866586e3180c05dd196986a9b53b --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/groupby02.sql" @@ -0,0 +1,41 @@ +--统计每个班的男生数 + +select ClassId,COUNT(StuSex) 男生人数 from StuInfo where StuSex='男' group by ClassId + +--统计每个班的男、女生数 + +select ClassId,StuSex,COUNT(StuSex) 人数 from StuInfo group by ClassId,StuSex order by ClassId + +--统计每个班的福建人数 + +select ClassId,StuProvince 省份,COUNT(StuId) 人数 from StuInfo where StuProvince='福建省' group by ClassId,StuProvince + +--统计每个班的各个省的总人数 + +select ClassId,StuProvince 省份,COUNT(StuProvince) 人数 from StuInfo group by ClassId,StuProvince order by ClassId + + +--统计每个省的女生数 + +select StuProvince 省份,COUNT(StuSex) 女生人数 from StuInfo where StuSex='女' group by StuProvince + +--统计每个省的男、女生数 + +select StuProvince 省份,StuSex 性别,COUNT(StuSex) from StuInfo group by StuProvince,StuSex + +--统计每个学生的考试总分、平均分 + +select StuId,SUM(Score) 总分,AVG(Score) 平均分 from Scores group by StuId + +--统计出考试总分大于620的学生的考试总分 + +select StuId,SUM(Score) 总分 from Scores group by StuId having SUM(Score)>620 + +--统计出每门考试成绩最高分和最低分 + +select CourseId,MAX(Score) 最高分,MIN(Score) 最低分 from Scores group by CourseId + + +--统计出每个学生的各门成绩的平均分 + +select StuId,CourseId,AVG(Score) 平均分 from Scores group by StuId,CourseId order by StuId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/\344\275\234\344\270\2321.sql" "b/20210326\344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/\344\275\234\344\270\2321.sql" new file mode 100644 index 0000000000000000000000000000000000000000..527b4cc3abeab5a05209e928003bd49aeeea6da5 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/\344\275\234\344\270\2321.sql" @@ -0,0 +1,141 @@ +use master +go +create database Students +on +( + name='Students', + filename='D:\Stundents.mdf', + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) +log on +( + name='Students_log', + filename='D:\Stundents_log.ldf', + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) + +use Students +go + +create table StuInfo +( + StuNo varchar(10), + StuName nvarchar(20), + StuAge varchar(10), + StuAddress nvarchar(200), + StuSeat char(10), + StuSex char(1) default(1) check(StuSex in(1,0)) +) + + +create table StuExam +( + ExamNo char(10), + StuNo varchar(10), + WittenExam int, + LabExam int +) + +insert into StuInfo(StuNo,StuName,StuAge,StuAddress,StuSeat,StuSex) values('s2501','张秋利',20,'美国硅谷',1,1), +('s2502','李斯文',18,'湖北武汉',2,0), +('s2503','马文才',22,'湖南长沙',3,1), +('s2504','欧阳俊雄',21,'湖北武汉',4,0), +('s2505','梅超风',20,'湖北武汉',5,1), +('s2506','陈旋风',19,'美国硅谷',6,1), +('s2507','陈风',20,'美国硅谷',7,0) + +insert into StuExam(ExamNo,StuNo,WittenExam,LabExam) values(1,'s2501',50,70), +(2,'s2502',60,65), +(3,'s2503',86,85), +(4,'s2504',40,80), +(5,'s2505',70,90), +(6,'s2506',85,90) + +select * from StuInfo +select * from StuExam +select 学号=StuNo,学生姓名=StuName,年龄=StuAge,家庭住址=StuAddress,座号=StuSeat,性别=StuSex from StuInfo + +select StuName,StuAge,StuAddress from StuInfo + +select StuNo,WittenExam,LabExam from StuExam + +select 学号=StuNo,笔试成绩=WittenExam,机试成绩=LabExam from StuExam +select StuNo as 学号, WittenExam as 笔试成绩, LabExam as 机试成绩 from StuExam +select StuNo 学号, WittenExam 笔试成绩, LabExam 机试成绩 from StuExam + +select StuName,StuAge,StuAddress from StuInfo +select StuName+'@'+StuAddress as 邮箱 from StuInfo + +select StuNo,WittenExam,LabExam,WittenExam+LabExam as 总分 from StuExam + +select StuAddress from StuInfo + +select StuAge 所有年龄 from StuInfo + +select * from StuInfo where StuSeat in (1,2,3) + +select top 4 StuSeat,StuName from StuInfo + +select top 50 percent * from StuInfo + +select * from StuInfo where StuAddress in ('湖北武汉') +select * from StuInfo where StuAge in (20) + +select * from StuExam where LabExam>=60 and LabExam<=80 order by LabExam DESC + +select * from StuInfo where StuAddress =('湖北武汉') or StuAddress =('湖南长沙') + +select * from StuExam where WittenExam<70 or WittenExam>90 order by WittenExam DESC + +select * from StuInfo where StuAge is null + +select * from StuInfo where StuAge is not null + +select * from StuInfo where StuName like '张%' + +select * from StuInfo where StuAddress like '湖%' + +select * from StuInfo where StuName like '张_' + +select * from StuInfo where StuName like '__俊%' + +select * from StuInfo order by StuAge DESC + +select * from StuInfo order by StuAge DESC , StuSeat ASC + +select top 1 * from StuExam order by WittenExam DESC + +select top 1 * from StuExam order by LabExam ASC + + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 + +select StuName 姓名,StuAge 年龄,WittenExam 笔试成绩,LabExam 机试成绩 + from StuInfo SI inner join StuExam SE on SI.StuNo=SE.StuNo + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 + +select StuName 姓名,SI.StuNo 学号,WittenExam 笔试成绩,LabExam 机试成绩 + from StuInfo SI inner join StuExam SE on SI.StuNo=SE.StuNo where WittenExam>60 and LabExam>60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 + +select StuName 姓名,SI.StuNo 学号,WittenExam 笔试成绩,LabExam 机试成绩 + from StuInfo SI inner join StuExam SE on SI.StuNo=SE.StuNo + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 + +select StuName 姓名,StuAge 年龄,WittenExam 笔试成绩,LabExam 机试成绩 + from StuInfo SI inner join StuExam SE on SI.StuNo=SE.StuNo where StuAge>=20 order by WittenExam DESC + +--5.查询男女生的机试平均分 + +select StuSex 性别,AVG(LabExam) 平均成绩 from StuInfo SI inner join StuExam SE on SI.StuNo=SE.StuNo group by StuSex + +--6.查询男女生的笔试总分 + +select StuSex 性别,SUM(WittenExam) 笔试总分 from StuInfo SI inner join StuExam SE on SI.StuNo=SE.StuNo group by StuSex diff --git "a/20210326\344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/\344\275\234\344\270\2322.sql" "b/20210326\344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/\344\275\234\344\270\2322.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b67cf414d344b0efb588d5c5f3de3592c6ee462a --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/\344\275\234\344\270\2322.sql" @@ -0,0 +1,111 @@ +use master +go +create database OrderInfo +on +( + name='OrderInfo', + filename='D:\OrderInfo.mdf', + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) +log on +( + name='OrderInfo_log', + filename='D:\OrderInfo_log.ldf', + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) + +use OrderInfo +go + +create table orders +( + orderId int primary key identity(1,1), + orderDate datetime +) + +create table orderItem +( + ItemiD int primary key identity(1,1), + orderId int references orders(orderId), + itemType nvarchar(20), + itemName nvarchar(20), + theNumber int, + theMoney Money +) + +select * from orderItem + + +insert into orders values('2008-01-12'),('2008-02-10'),('2008-02-15'),('2008-03-10') + +insert into orderItem values(1,'文具','笔',72,2), +(1,'文具','尺',10,1), +(1,'体育用品','篮球',1,56), +(2,'文具','笔',36,2), +(2,'文具','固体胶',20,3), +(2,'日常用品','透明胶',2,1), +(2,'体育用品','羽毛球',20,3), +(3,'文具','订书机',20,3), +(3,'文具','订书针',10,3), +(3,'文具','裁纸刀',5,5), +(4,'文具','笔',20,2), +(4,'文具','信纸',50,1), +(4,'日常用品','毛巾',4,5), +(4,'日常用品','透明胶',30,1), +(4,'体育用品','羽毛球',20,3) + +select SUM(theNumber) from dbo.orderItem + +select orderId,SUM(theNumber) 总数量,AVG(theMoney) 平均单价 from dbo.orderItem where orderId<3 group by orderId HAVING AVG(theMoney)<10 + +select orderId,SUM(theNumber) 总数量,AVG(theMoney) 平均单价 from dbo.orderItem group by orderId HAVING AVG(theMoney)<10 and SUM(theNumber)>50 + +select itemType,COUNT(itemType) 订购次数 from dbo.orderItem group by itemType + +select itemType,SUM(theNumber) 总数量,AVG(theMoney) 平均单价 from dbo.orderItem group by itemType HAVING SUM(theNumber)>100 + +select itemName,COUNT(itemName)订购次数, SUM(theNumber) 总数量, AVG(theMoney) 平均单价 from dbo.orderItem group by itemName + +--使用上次作业的订单数据库,完成下列题目: + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 + +select OS.orderId 订单的编号,orderDate 订单日期,itemType 产品的类别,itemName 订购的产品名称 from orders OS inner join orderItem OM on OS.orderId=OM.orderId + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 + +select OS.orderId 订单的编号,orderDate 订单日期,itemType 产品的类别,itemName 订购的产品名称 from orders OS inner join orderItem OM on OS.orderId=OM.orderId where theNumber>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +select OS.orderId 订单的编号,orderDate 订单日期,itemType 产品的类别,itemName 订购的产品名称,theNumber 订购数量,theMoney 订购单价,SUM(theNumber) 订购总价 +from orders OS inner join orderItem OM on OS.orderId=OM.orderId group by OS.orderId ,orderDate ,itemType ,itemName ,theNumber ,theMoney + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +select OS.orderId 订单的编号,orderDate 订单日期,itemType 产品的类别,itemName 订购的产品名称,theNumber 订购数量,theMoney 订购单价,SUM(theNumber) 订购总价 +from orders OS inner join orderItem OM on OS.orderId=OM.orderId where theMoney>=5 and theNumber>=50 group by OS.orderId ,orderDate ,itemType ,itemName ,theNumber ,theMoney + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 + +select OS.orderId 订单的编号,theNumber 订购数量 from orders OS inner join orderItem OM on OS.orderId=OM.orderId group by OS.orderId ,theNumber + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + +select OS.orderId 订单的编号,itemType 产品的类别,COUNT(*) 订购次数,SUM(theNumber) 总数量 from orders OS inner join orderItem OM on OS.orderId=OM.orderId +group by OS.orderId,itemType order by OS.orderId diff --git "a/20210326\344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/\344\275\234\344\270\2323.sql" "b/20210326\344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/\344\275\234\344\270\2323.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8a40212b64f6adcc4269310b164e957e05f1dae0 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\203\255\347\220\252\346\236\253/\344\275\234\344\270\2323.sql" @@ -0,0 +1,161 @@ +use master +go +create database BBS +on +( + name='BBS', + filename='D:\BBS.mdf', + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) +log on +( + name='BBS_log', + filename='D:\BBS_log.ldf', + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) +go +use BBS +go + +create table bbsUsers +( + bbsUID int identity(1,1), + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) + +create table bbsSection +( + sID int identity(1,1), + sName varchar(10) not null, + sUid int +) + +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int references bbsUsers(bbsUID), + tSID int references bbsSection(sID), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) + +create table bbsReply +( + rID int primary key identity(1,1), + rUID int references bbsUsers(bbsUID), + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +alter table bbsUsers add constraint PK_bbsUsers_bbsUID primary key(bbsUID) +alter table bbsUsers add constraint UQ_bbsUsers_uName unique(uName) +alter table bbsUsers add constraint CK_bbsUsers_uSex check(uSex in('男','女')) +alter table bbsUsers add constraint CK_bbsUsers_uAge check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint CK_bbsUsers_uPoint check(uPoint>= 0) +alter table bbsSection add constraint PK_bbsSection_sID primary key(sID) +alter table bbsSection add constraint FK_bbsUsers_bbsUID foreign key(sUid) references bbsUsers(bbsUID) + +select * from bbsReply + +insert into bbsUsers(uName,uSex,uAge,uPoint) values('小雨点','女',20,0), +('逍遥','男',18,4), +('七年级生','男',19,2) + +select * from bbsUsers + +select uName,uPoint into bbsPoint from bbsUsers + +select * from bbsPoint + +insert into bbsSection(sName,sUid) values('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) + +select * from bbsSection + +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values(2,4,'范跑跑','谁是范跑跑','2008-07-08',1), +(3,1,'.NET','与JAVA的区别是什么呀?','2008-09-01',2), +(1,3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?','2008-09-10',0) + +insert into bbsReply(rUID,rTID,rMsg,rTime) values(1,1,'不知道','2008-07-09'), +(1,2,'不知道','2008-09-09'), +(2,2,'不知道','2008-10-09') + +select * from bbsReply +select * from bbsTopic +select * from bbsSection + +-- 5.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户,请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) + +delete from bbsReply where rUID=2 +delete from bbsReply where rTID=1 +delete from bbsTopic where tUID=2 +delete from bbsUsers where bbsUID=2 + +update bbsUsers set uPoint=10 where bbsUID=1 + +select uPoint from bbsUsers +-- 7.因为板块“生活百科”灌水的人太少,现决定取消该板块,即删除(注意主外键) +delete from bbsTopic where tSID=3 +delete from bbsSection where sID=3 + +truncate table bbsReply + + +select * from bbsReply +alter table bbsTopic drop column tCount + +alter table bbsUsers add telephone char(10) + +alter table bbsReply alter column rMsg varchar(200) + +alter table bbsUsers drop constraint CK_bbsUsers_uPoint + +update bbsUsers set uName='小雪' where bbsUID=1 + +update bbsUsers set uPoint=uPoint+100 + + + +select * from bbsUsers + +select * into bbsUsers2 from bbsUsers + +drop table bbsUsers2 +truncate table bbsUsers2 + +--在论坛数据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 + +select bbsUID 版主编号,uName 版主姓名,sName 版块名称 from bbsUsers BU inner join bbsSection BS on bbsUID=sUid + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 + +select bbsUID 发帖人编号,uName 发帖人姓名,tTitle 帖子的标题,tMsg 帖子的内容,tTime 发帖时间 from bbsUsers BU inner join bbsTopic BT on bbsUID=tUID + + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 + +select sID 版主的编号,uName 版主的名称,sName 版块的名称 from bbsUsers BU inner join bbsSection BS on bbsUID=sUid where uAge<20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 + +select bbsUID 主贴的发帖人编号,uName 发帖人姓名,tTitle 主贴标题,tMsg 主贴内容,tCount 回复数量 from bbsUsers BU inner join bbsTopic BT on bbsUID=tUID +where tCount=(select MAX(tCount) from bbsTopic) + + +--5.在主贴表中查询每个版块中每个用户的发帖总数 + +select tID,tUID,COUNT(*) 发帖总数 from bbsTopic group by tID,tUID + + + + \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\237\246\345\244\251\347\277\224/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\351\237\246\345\244\251\347\277\224/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4f532255a9352233fc43b03d130f1a5229db98af --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\237\246\345\244\251\347\277\224/SQLQuery2.sql" @@ -0,0 +1,29 @@ +use TestDB +go + +select * from ClassInfo +select * from CourseInfo +select * from Scores +select * from StuInfo + +--统计 每个班 男生数 +select count(*)班级男生数 from StuInfo where(StuSex='男') group by ClassId +--统计 每个班 的 男、女生数 +select classid,StuSex,count(*) from stuinfo group by classid,stusex order by ClassId +--统计每个班的福建人数 +select count(*)班级男生数 from StuInfo where(StuProvince='福建省') group by ClassId +--统计每个班的各个省的总人数 +select StuProvince,ClassId,count(*)人数 from StuInfo group by StuProvince,ClassId +--统计每个省的女生数 +select StuProvince,StuSex,count(*)女生数 from StuInfo where(StuSex='女')group by StuProvince,Stusex +--统计每个省的男、女生数 +select StuProvince,StuSex,count(*)人数 from StuInfo group by StuProvince,Stusex +--统计每个学生的考试总分、平均分 +select Stuid,avg(Score)平均分,sum(Score)总分 from Scores group by StuId +--统计出考试总分大于620 的 学生 的 考试总分 + select Stuid,sum(Score) from Scores group by Stuid having (sum(Score)>620) +--统计出每门考试成绩最高分和最低分 +select courseID,max(Score)最高分,min(Score)最低分 from Scores group by courseID +--统计出 每个学生 各门成绩 平均分 +select CourseId,Stuid,avg(score)平均分 from Scores group by CourseId,Stuid +select * from Scores \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b4d1d0278aa143109d280336b27a74b43c3c34f0 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery1.sql" @@ -0,0 +1,88 @@ +create database bbs +on +( +name = 'bbs', +size = 5, +maxsize = 500, +filename = 'D:\新建文件夹2\bbs.mdf', +filegrowth = 10% +) +log on +( +name = 'bbs_log', +size = 5, +maxsize = 500, +filename = 'D:\新建文件夹2\bbs_log.ldf', +filegrowth = 10% +) +use bbs +go +create table bbsUsers --用户信息表 +( +UID int identity (1,1), --用户编号 +uName varchar(10) not null , --用户名 +uSex varchar(2) not null, --性别 +uAge int not null, --年龄 +uPoint int not null, --积分 +) +go +create table bbsSection --板块表 +( + sID int identity, --板块编号 + sName varchar(10) not null,--板块名称 + sUid int, --版主编号 +) +go +alter table bbsUsers add constraint PK_UID primary key(UID); +alter table bbsUsers add constraint UQ_uName unique(uName); +alter table bbsUsers add constraint CK_uSex check(uSex = '男' or uSex = '女'); +alter table bbsUsers add constraint CK_uAge check(uAge>=15 and uAge<=60); +alter table bbsUsers add constraint CK_uPoint check(uPoint>=0); +alter table bbsSection add constraint CK_sID primary key (sID); +alter table bbsSection add constraint FK_sUid foreign key(sUid) references bbsUsers(UID) +go +create table bbsTopic --主贴表 +( +tID int primary key identity, --主贴编号 +tUID int references bbsUsers(UID),--发帖人编号 +tSID int references bbsSection(sID),--板块编号 +tTitle varchar(100) not null,--贴纸标题 +tMsg text not null,--帖子内容 +tTime datetime ,--发帖时间 +tCount int--回复数量 +) +alter table bbsTopic alter column tMsg varchar(60) +go +create table bbsReply --回贴表 +( +rID int primary key identity,--回帖编号 + rUID int references bbsUsers(UID),--回帖人编号 + rTID int references bbsTopic(tID),--对应主贴编号 + rMsg text not null,--回帖内容 + rTime datetime --回帖时间 +) +go +insert into bbsUsers(uName,uSex,uAge,uPoint) values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) +insert into bbsSection (sName,sUid) values('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values(2 ,4,'范跑跑','谁是范跑跑 ','2008-7-8',1) +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values(3 ,1,'.NET','与JAVA的区别是什么呀? ','2008-9-1',2) +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values(1 ,3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀 ','2008-9-10',2) +insert into bbsReply(rTID,rMsg,rTime) values(1,'不认识',2008-7-11) +insert into bbsReply(rTID,rMsg,rTime) values(2,'没有区别',2008-9-11) +insert into bbsReply(rTID,rMsg,rTime) values(2,'请百度',2008-9-12) + +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select sUid'版主编号',uName'版主姓名',sName'板块名称' from bbsSection inner join bbsUsers on bbsSection.sUid=bbsUsers.UID +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID,uName,tTitle,tMsg,tTime from bbsUsers left join bbsTopic on bbsUsers.UID=bbsTopic.tID where tTime>'2008-9-8' +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sUid'版主编号',uName'版主姓名',sName'板块名称',uAge 年龄 from bbsSection inner join bbsUsers on bbsSection.sUid=bbsUsers.UID where uAge<20 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select top 1 tUID,uName,tTitle,tMsg,tCount from bbsUsers left join bbsTopic on bbsUsers.UID=bbsTopic.tID order by tCount desc +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select uName 用户姓名,sName 板块名,tSID 板块编号,count(tuid)该用户发帖总数 from bbsTopic left join bbsUsers on bbsUsers.UID=bbsTopic.tID +left join bbsSection on bbsTopic.tSID=bbsSection.sID group by tSID,uName,sName +select * from bbsSection +select * from bbsUsers +select * from bbsReply +select * from bbsTopic \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..53b021d53b7fc7336abe5471ecee3d731c47a46d --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery2.sql" @@ -0,0 +1,461 @@ +USE [master] +GO +/****** Object: Database [TestDB] Script Date: 2021/3/15 16:11:24 ******/ +CREATE DATABASE [TestDB] + CONTAINMENT = NONE + ON PRIMARY +( NAME = N'TestDB', FILENAME = N'D:\TestDB.mdf' , SIZE = 4288KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) + LOG ON +( NAME = N'TestDB_log', FILENAME = N'D:\TestDB_log.ldf' , SIZE = 1072KB , MAXSIZE = 2048GB , FILEGROWTH = 10%) +GO +ALTER DATABASE [TestDB] SET COMPATIBILITY_LEVEL = 120 +GO +IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled')) +begin +EXEC [TestDB].[dbo].[sp_fulltext_database] @action = 'enable' +end +GO +ALTER DATABASE [TestDB] SET ANSI_NULL_DEFAULT OFF +GO +ALTER DATABASE [TestDB] SET ANSI_NULLS OFF +GO +ALTER DATABASE [TestDB] SET ANSI_PADDING OFF +GO +ALTER DATABASE [TestDB] SET ANSI_WARNINGS OFF +GO +ALTER DATABASE [TestDB] SET ARITHABORT OFF +GO +ALTER DATABASE [TestDB] SET AUTO_CLOSE OFF +GO +ALTER DATABASE [TestDB] SET AUTO_SHRINK OFF +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS ON +GO +ALTER DATABASE [TestDB] SET CURSOR_CLOSE_ON_COMMIT OFF +GO +ALTER DATABASE [TestDB] SET CURSOR_DEFAULT GLOBAL +GO +ALTER DATABASE [TestDB] SET CONCAT_NULL_YIELDS_NULL OFF +GO +ALTER DATABASE [TestDB] SET NUMERIC_ROUNDABORT OFF +GO +ALTER DATABASE [TestDB] SET QUOTED_IDENTIFIER OFF +GO +ALTER DATABASE [TestDB] SET RECURSIVE_TRIGGERS OFF +GO +ALTER DATABASE [TestDB] SET ENABLE_BROKER +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF +GO +ALTER DATABASE [TestDB] SET DATE_CORRELATION_OPTIMIZATION OFF +GO +ALTER DATABASE [TestDB] SET TRUSTWORTHY OFF +GO +ALTER DATABASE [TestDB] SET ALLOW_SNAPSHOT_ISOLATION OFF +GO +ALTER DATABASE [TestDB] SET PARAMETERIZATION SIMPLE +GO +ALTER DATABASE [TestDB] SET READ_COMMITTED_SNAPSHOT OFF +GO +ALTER DATABASE [TestDB] SET HONOR_BROKER_PRIORITY OFF +GO +ALTER DATABASE [TestDB] SET RECOVERY FULL +GO +ALTER DATABASE [TestDB] SET MULTI_USER +GO +ALTER DATABASE [TestDB] SET PAGE_VERIFY CHECKSUM +GO +ALTER DATABASE [TestDB] SET DB_CHAINING OFF +GO +ALTER DATABASE [TestDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) +GO +ALTER DATABASE [TestDB] SET TARGET_RECOVERY_TIME = 0 SECONDS +GO +ALTER DATABASE [TestDB] SET DELAYED_DURABILITY = DISABLED +GO +EXEC sys.sp_db_vardecimal_storage_format N'TestDB', N'ON' +GO +USE [TestDB] +GO +/****** Object: Table [dbo].[ClassInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[ClassInfo]( + [ClassId] [int] IDENTITY(1,1) NOT NULL, + [ClassName] [nvarchar](20) NOT NULL, +PRIMARY KEY CLUSTERED +( + [ClassId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[CourseInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[CourseInfo]( + [CourseId] [int] IDENTITY(1,1) NOT NULL, + [CourseName] [nvarchar](50) NOT NULL, + [CourseCredit] [int] NULL DEFAULT ((1)), +PRIMARY KEY CLUSTERED +( + [CourseId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[Scores] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[Scores]( + [ScoreId] [int] IDENTITY(1,1) NOT NULL, + [StuId] [int] NULL, + [CourseId] [int] NULL, + [Score] [int] NULL DEFAULT ((0)), +PRIMARY KEY CLUSTERED +( + [ScoreId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[StuInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[StuInfo]( + [StuId] [int] IDENTITY(1,1) NOT NULL, + [ClassId] [int] NULL, + [StuName] [nvarchar](10) NOT NULL, + [StuSex] [nvarchar](1) NULL DEFAULT ('男'), + [StuBrithday] [date] NULL, + [StuPhone] [nvarchar](11) NULL, + [StuProvince] [nvarchar](200) NULL, + [CreateDate] [datetime] NULL DEFAULT (getdate()), + [StuAge] [int] NULL, +PRIMARY KEY CLUSTERED +( + [StuId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] ON + +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (1, N'软件1班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (2, N'软件2班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (3, N'软件3班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (4, N'软件4班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (5, N'软件5班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (6, N'软件6班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (7, N'软件7班') +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] ON + +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (1, N'计算机基础', 3) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (2, N'HTML+CSS网页制作', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (3, N'JAVA编程基础', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (4, N'SQL Server数据库基础', 4) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (5, N'C#面向对象编程', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (6, N'Winform桌面应用程序设计', 5) +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[Scores] ON + +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (1, 1, 1, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (2, 1, 2, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (3, 1, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (4, 1, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (5, 2, 1, 60) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (6, 2, 2, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (7, 2, 3, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (8, 2, 4, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (9, 3, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (10, 3, 2, 45) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (11, 3, 3, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (12, 3, 4, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (13, 4, 1, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (14, 4, 2, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (15, 4, 3, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (16, 4, 4, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (17, 5, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (18, 5, 2, 79) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (19, 5, 3, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (20, 5, 4, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (21, 6, 1, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (22, 6, 2, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (23, 6, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (24, 6, 5, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (25, 7, 1, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (26, 7, 2, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (27, 7, 3, 92) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (28, 7, 5, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (29, 8, 1, 58) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (30, 8, 2, 59) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (31, 8, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (32, 8, 5, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (33, 9, 1, 48) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (34, 9, 2, 67) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (35, 9, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (36, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (37, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (38, 1, 1, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (39, 1, 2, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (40, 1, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (41, 1, 4, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (42, 2, 1, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (43, 2, 2, 82) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (44, 2, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (45, 2, 4, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (46, 3, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (47, 3, 2, 50) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (48, 3, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (49, 3, 4, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (50, 4, 1, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (51, 4, 2, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (52, 4, 3, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (53, 4, 4, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (54, 5, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (55, 5, 2, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (56, 5, 3, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (57, 5, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (58, 6, 1, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (59, 6, 2, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (60, 6, 3, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (61, 6, 5, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (62, 7, 1, 89) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (63, 7, 2, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (64, 7, 3, 97) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (65, 7, 5, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (66, 8, 1, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (67, 8, 2, 64) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (68, 8, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (69, 8, 5, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (70, 9, 1, 53) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (71, 9, 2, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (72, 9, 3, 76) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (73, 9, 5, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (74, 9, 5, 61) +GO +SET IDENTITY_INSERT [dbo].[Scores] OFF +GO +SET IDENTITY_INSERT [dbo].[StuInfo] ON + +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (1, 1, N'刘正', N'男', CAST(N'2002-08-02' AS Date), N'13245678121', N'广西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (2, 1, N'黄贵', N'男', CAST(N'2003-07-02' AS Date), N'13345678121', N'江西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (3, 1, N'陈美', N'女', CAST(N'2002-07-22' AS Date), N'13355678125', N'福建省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (4, 2, N'江文', N'男', CAST(N'2001-07-02' AS Date), N'13347678181', N'湖南省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (5, 2, N'钟琪', N'女', CAST(N'2004-01-13' AS Date), N'13345778129', N'安徽省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (6, 3, N'曾小林', N'男', CAST(N'2005-05-15' AS Date), N'13345378563', N'安徽省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (7, 3, N'欧阳天天', N'女', CAST(N'2000-08-19' AS Date), N'13347878121', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (8, 3, N'李逍遥', N'男', CAST(N'1999-09-02' AS Date), N'13345678557', N'广东省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 22) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (9, 4, N'刘德华', N'男', CAST(N'1995-06-11' AS Date), N'15345679557', N'福建省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 26) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (10, 4, N'刘翔', N'男', CAST(N'1996-07-09' AS Date), N'18346679589', N'江西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (11, 4, N'曾小贤', N'男', CAST(N'2003-07-02' AS Date), N'18348979589', N'湖南省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (12, 5, N'刘德华', N'男', CAST(N'2002-07-02' AS Date), N'18348979509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (13, 5, N'陈天翔', N'男', CAST(N'2003-07-02' AS Date), N'18348079509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (14, 5, N'刘能', N'男', CAST(N'2005-08-02' AS Date), N'13245678122', N'广西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (15, 5, N'钟馗', N'男', CAST(N'2004-08-02' AS Date), N'13245678123', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (16, 5, N'钟吴艳', N'女', CAST(N'2002-08-02' AS Date), N'13245678124', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (17, 5, N'刘欢', N'男', CAST(N'2001-07-02' AS Date), N'13245678125', N'湖南省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (18, 5, N'张庭', N'女', CAST(N'2000-07-02' AS Date), N'13245678126', N'江西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (19, 5, N'曹植', N'男', CAST(N'2000-08-02' AS Date), N'13245678127', N'福建省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (20, 5, N'曹操', N'男', CAST(N'2002-08-02' AS Date), N'13245678128', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (21, 5, N'孙尚香', N'女', CAST(N'2003-08-02' AS Date), N'13245678129', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (22, 3, N'老1', N'女', CAST(N'2002-08-02' AS Date), N'13245678130', N'广东省', CAST(N'2021-03-14 17:02:36.347' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (24, 2, N'老2', N'男', CAST(N'2002-08-03' AS Date), N'13345678945', NULL, CAST(N'2021-03-14 17:03:37.733' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (25, 4, N'老3', N'男', NULL, N'13645987545', N'广东省', CAST(N'2021-03-14 17:03:43.307' AS DateTime), NULL) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (28, 5, N'老4', N'男', CAST(N'2006-03-05' AS Date), N'13456987456', NULL, CAST(N'2021-03-14 17:04:03.957' AS DateTime), 15) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (29, 5, N'老5', N'女', CAST(N'1998-04-12' AS Date), N'15978456123', NULL, CAST(N'2021-03-14 17:04:47.103' AS DateTime), 23) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (30, 4, N'老6', N'男', CAST(N'1996-08-06' AS Date), N'18945674561', NULL, CAST(N'2021-03-14 17:05:04.990' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (31, 3, N'老7', N'女', CAST(N'1997-04-06' AS Date), N'18845678912', NULL, CAST(N'2021-03-14 17:05:20.570' AS DateTime), 24) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (32, 2, N'老10', N'女', CAST(N'1998-08-09' AS Date), N'19945645612', NULL, CAST(N'2021-03-14 17:06:08.107' AS DateTime), 23) +GO +SET IDENTITY_INSERT [dbo].[StuInfo] OFF +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__CourseIn__9526E2773AB7BECE] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[CourseInfo] ADD UNIQUE NONCLUSTERED +( + [CourseName] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__StuInfo__2D85FC63AF6FC6FA] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[StuInfo] ADD UNIQUE NONCLUSTERED +( + [StuPhone] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([CourseId]) +REFERENCES [dbo].[CourseInfo] ([CourseId]) +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([StuId]) +REFERENCES [dbo].[StuInfo] ([StuId]) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD FOREIGN KEY([ClassId]) +REFERENCES [dbo].[ClassInfo] ([ClassId]) +ON DELETE SET NULL +GO +ALTER TABLE [dbo].[CourseInfo] WITH CHECK ADD CHECK (([CourseCredit]>=(1) AND [CourseCredit]<=(5))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK ((len([StuPhone])=(11))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK (([StuSex]='女' OR [StuSex]='男')) +GO +USE [master] +GO +ALTER DATABASE [TestDB] SET READ_WRITE +GO +select * from stuinfo +select * from scores +--1.统计每个班的男生数 +select classid 班级,count(classid) 男生总数 from stuinfo where stusex='男' group by classid +--2.统计每个班的男、女生数 +select classid 班级,count(classid) 该班级性别人数,stusex 性别 from stuinfo group by classid,stusex order by classid +--3.统计每个班的福建人数 +select classid 班级,count(classid)人数 from stuinfo where stuprovince='福建省' group by classid +--4.统计每个班的各个省的总人数 +select classid,count(classid)总人数,stuprovince from stuinfo group by classid,stuprovince order by classid +--5.统计每个省的女生数 +select classid,stuprovince from stuinfo where stusex='女' group by classid,stuprovince order by classid +--6.统计每个省的男、女生数 +select stuprovince,count(stusex) 人数 from stuinfo group by stuprovince +--7.统计每个学生的考试总分、平均分 +select stuid,sum(score)总分,avg(score)平均分 from scores group by stuid +--8.统计出考试总分大于620的学生的考试总分 +select stuid,sum(score)总分 from scores group by stuid having sum(score)>620 +--9.统计出每门考试成绩最高分和最低分 +select courseid,max(score)最高分,min(score)最低分 from scores group by courseid +--10.统计出每个学生的各门成绩的平均分 +select stuid,Courseid ,avg(score) 平均分 from scores group by stuid,Courseid order by stuid \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d348d994b4bfa62ded1fc1977348bb9e8c35d5ab --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery3.sql" @@ -0,0 +1,75 @@ +create database orders +on +( + name = 'order', + size = 5, + maxsize = 500, + filename = 'D:\新建文件夹3\order.mdf', + filegrowth = 10% +) +log on +( + name = 'order_LOG', + size = 5, + maxsize = 500, + filename = 'D:\新建文件夹3\order.ldf', + filegrowth = 10% +) +use orders +go +create table orders --订单表 +( + orderId int primary key, --订单编号 + orderDate smalldatetime --订购日期 +) +go +create table orderItem --订购项目表 +( + itemName nvarchar(10) not null, --产品名称 + theNumber int not null, --订购数量 + theMoney money not null, --订购单价 + itemType nvarchar(4) not null,--产品类别 + orderId int references orders(orderId) not null, --订单编号 + ItemiD int identity(1,1) primary key, --项目编号 +) +insert into orders(orderId,orderDate) values(1,'2008-01-12'),(2,'2008-02-10'),(3,'2008-02-15'),(4,'2008-03-10') +insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) +select 1,'文具','笔',72,2 union +select 1,'文具','尺',10,1 union +select 1, '体育用品','篮球',1,56 union +select 2,'文具','笔',36,2 union +select 2,'文具','固体胶',20,3 union +select 2,'日常用品','透明胶',2,1 union +select 2,'体育用品','羽毛球',20,3 union +select 3,'文具','订书机',20,3 union +select 3,'文具','订书机',10,3 union +select 3,'文具','裁缝刀',5,5 union +select 4,'文具','笔',20,2 union +select 4,'文具','信纸',50,1 union +select 4,'日常用品','毛巾',4,5 union +select 4,'日常用品','透明胶',30,1 union +select 4,'体育用品','羽毛球',20,3 +select * from orders +select * from orderItem +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select orderItem.orderId,orderDate,itemType,itemName,theNumber,theMoney from orders left join orderItem on orders.orderId=orderItem.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orderItem.orderId,orderDate,itemType,itemName,theNumber from orders left join orderItem on orders.orderId=orderItem.orderId where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderItem.orderId 订单的编号,orderDate 订单日期,itemType 订购产品的类别,theNumber 订购数量,theMoney 订购单价,theNumber*theMoney 订购总价 from orderItem left join orders on orders.orderId=orderItem.orderId +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderItem.orderId 订单编号, orderDate 订单日期,itemType 订购产品的类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价,theNumber*theMoney 订购总价 from orderItem left join orders on orders.orderId=orderItem.orderId where theMoney>5 and theNumber >=50 +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderId 编号,count(itemType) 订购产品数 from orderItem group by orderId +select orders.orderId 编号,count(itemType) 订购产品数 from orderItem left join orders on orderItem.orderId=orders.orderId group by orders.orderId +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: +-- 订单编号 产品类别 订购次数 总数量 +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 +select Distinct orderId 编号,itemType 产品类型,count(theNumber)订购产品数,sum(theNumber)总数量 from orderItem group by orderId ,itemType order by orderId asc \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery4.sql" "b/20210326\344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0c5c8cbb3a4470cae32fb42af015d55a49a8c151 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\237\246\346\230\214\345\235\244/SQLQuery4.sql" @@ -0,0 +1,60 @@ +create database Student +on +( + name='Stuinfo', + filename='D:\新建文件夹.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% + +) +log on +( + name='Stuinfo_log', + filename='D:\新建文件夹.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +use Student +go + +create table Stuinfo +( + stuNO varchar(5) unique not null, --学生学号 + stuName nvarchar(20), --学生姓名 + stuAge int, --学生年龄 + stuAddress nvarchar(20), --学生住址 + stuSeat int identity(1,1) unique not null, --学生座位号 + stuSex nchar(1) check(stuSex='男' or stuSex='女') --学生性别 +) +go + +create table Scoreinfo +( + examNO int primary key identity(1,1), --考试座位号 + stuNO varchar(5) references Stuinfo(stuNO), --学生学号 + writtenExan int, --笔试成绩 + labExam int, --机试成绩 + ) + +go +insert into Stuinfo values ('s2501','张秋利','20','美国硅谷','女'),('s2502','李斯文',18,'湖北武汉','男'),('s2503','马文才',22,'湖南长沙','女'), +('s2504','欧阳俊雄',21,'湖北武汉','男'),('s2505','梅超风',20,'湖北武汉','女'),('s2506','陈旋风',19,'美国硅谷','女'),('s2507','陈风',20,'美国硅谷','男') + +insert into Scoreinfo values('s2501',50,70),('s2502',60,65),('s2503',86,85),('s2504',40,80),('s2505',70,90),('s2506',85,90) +select * from Stuinfo +select * from Scoreinfo +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName,stuAge,writtenExan,labExam from Stuinfo inner join Scoreinfo on Stuinfo.stuNO=Scoreinfo.stuNO +select stuName,stuAge,writtenExan,labExam from Stuinfo left join Scoreinfo on Stuinfo.stuNO=Scoreinfo.stuNO +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select Stuinfo.stuNO,stuName,writtenExan,labExam from Stuinfo left join Scoreinfo on Stuinfo.stuNO=Scoreinfo.stuNO where writtenExan>60 and labExam>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select Stuinfo.stuNO,stuName,writtenExan,labExam from Stuinfo left join Scoreinfo on Stuinfo.stuNO=Scoreinfo.stuNO +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName,stuAge,writtenExan,labExam from Stuinfo right join Scoreinfo on Stuinfo.stuNO=Scoreinfo.stuNO where stuAge>=20 order by stuAge desc +--5.查询男女生的机试平均分 +select stuSex 性别, avg(labExam)机试平均分 from Stuinfo left join Scoreinfo on Stuinfo.stuNO=Scoreinfo.stuNO group by stuSex +--6.查询男女生的笔试总分 +select stuSex 性别, sum(labExam)机试平均分 from Stuinfo left join Scoreinfo on Stuinfo.stuNO=Scoreinfo.stuNO group by stuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\253\230\345\245\225/Student.sql" "b/20210326\344\275\234\344\270\232/\351\253\230\345\245\225/Student.sql" new file mode 100644 index 0000000000000000000000000000000000000000..762adf50535070e4e4991e505f7aa2429190a3b7 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\253\230\345\245\225/Student.sql" @@ -0,0 +1,47 @@ + +--统计每个班的男生数 +select '男生的数量',count(StuSex) from StuInfo group by StuSex having StuSex='男' + + +--统计每个班的男、女生数 + +select ClassId,'男生的数量',count(StuSex) from StuInfo where StuSex='男' group by ClassId +union +select ClassId,'女生的数量',count(StuSex) from StuInfo where StuSex='女' group by ClassId + + + + --统计每个班的男、女生数 + +select ClassId,Stusex,count(StuSex) from StuInfo group by ClassId,StuSex + + + --统计每个班的福建人数 +select ClassId,count(StuProvince)福建省的人数 from StuInfo group by ClassId,StuProvince having StuProvince='福建省' + + +--统计每个班的各个省的总人数 +select ClassId,StuProvince,count(StuProvince) from StuInfo group by ClassId,StuProvince order by ClassId + + +--统计每个省的女生数 +select StuProvince,count(StuSex)女生人数 from StuInfo where StuSex='女' group by StuProvince + + +--统计每个省的男、女生数 +select StuProvince,StuSex,count(StuSex)人数 from StuInfo group by StuProvince,StuSex + + +--统计每个学生的考试总分、平均分 +select StuId 学号,sum(Score)总分,avg(Score)平均分 from Scores group by StuId + +--统计出考试总分大于620的学生的考试总分 +select StuId 学号,sum(Score)总分 from Scores group by StuId having sum(Score)>620 + + +--统计出每门考试成绩最高分和最低分 +select CourseId,max(Score)最高分,min(Score)最低分 from Scores group by CourseId + + +--统计出每个学生的各门成绩的平均分 +select StuId,CourseId,avg(score) from Scores group by StuId,CourseId order by StuId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\253\230\345\245\225/bbs.sql" "b/20210326\344\275\234\344\270\232/\351\253\230\345\245\225/bbs.sql" new file mode 100644 index 0000000000000000000000000000000000000000..21ffe32b5c87a68a65a82b50a9d85a9f76f4b1cf --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\253\230\345\245\225/bbs.sql" @@ -0,0 +1,123 @@ +use master +create database bbs +on +( + name='bbs', + filename='D:\Demo\bbs.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='bbs_log', + filename='D:\Demo\bbs_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go +use bbs +create table bbsUser +( + uID int identity(1,1) not null, + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) + +create table bbsSection +( + sID int identity(1,1) not null , + sName varchar(10) not null, + sUid int + +) +--添加约束 +-- alter table 表名 add constraint 约束名 约束类型 +alter table bbsUser add constraint PK_bbsUser_uID primary key(uID) +alter table bbsUser add constraint UK_bbsUser_uName unique(uName) +alter table bbsUser add constraint CK_bbsUser_uSex check(uSex='男' or uSex='女') +alter table bbsUser add constraint CK_bbsUser_uAge check(uAge>=15 and uAge<=60) +alter table bbsUser add constraint CK_bbsUser_uPoint check(uPoint>=0) + +alter table bbsSection add constraint PK_bbsSection_sID primary key(sID) +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUser(uID) + + +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int references bbsUser(uID), + tSID int references bbsSection(sID), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) +create table bbsReply +( + rID int identity(1,1) primary key, + rUID int references bbsUser(uID), + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +insert into bbsUser (uName,uSex,uAge,uPoint) values ('小雨点','女','20','0') +,('逍遥','男','18','4'),('七年级生','男','19','2') + +select uName,uPoint into bbsPoint from bbsUser + +insert into bbsSection (sName,sUid) values ('技术交流',1), +('读书世界',3),('生活百科',1),('八卦区',3) + +insert into bbsTopic (tUID,tSID,tTitle,tMsg,tTime,tCount) values ('2','4','范跑跑','谁是范跑跑','2008-7-8','1'), +('3','1','.NET','谁是范跑跑','2008-9-1','2'),('1','3','.今年夏天最流行什么呀?','有谁知道今年夏天最流行','2008-9-10','0') + +insert into bbsReply (rUID,rMsg,rTime) values ('1','范跑跑是赵家俊小名','2021-1-1'), +('2','名字不一样','2021-1-2'),('3','EMO','2021-1-3') + + +select * from bbsUser +select * from bbsSection +select * from bbsTopic +select * from bbsReply + + +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select sUid,uName,sName from bbsUser inner join bbsSection on bbsUser.uID=bbsSection.sUid + + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 + +select uID,uName,tTitle,tMsg,tTime from bbsTopic inner join bbsUser on bbsTopic.tSID=bbsUser.uID where tTime>'2008-9-1' + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sID,uName,sName,uAge from bbsUser inner join bbsSection on bbsUser.uID=bbsSection.sUid where uAge<20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select max(tCount) from bbsTopic +select uID,uName,tTitle,tMsg,tCount from bbsTopic inner join bbsUser on bbsTopic.tSID=bbsUser.uID where tCount= (select max(tCount) from bbsTopic) + +--5.在主贴表中查询每个版块中每个用户的发帖总数 + +select tsid,count(tSID)from bbsTopic group by tUID,tSID + + + + + + + + + + + + + + + + + diff --git "a/20210326\344\275\234\344\270\232/\351\253\230\345\245\225/shopping.sql" "b/20210326\344\275\234\344\270\232/\351\253\230\345\245\225/shopping.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e29a48552eefb3638ba52e988884c5df6f2ad86c --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\253\230\345\245\225/shopping.sql" @@ -0,0 +1,101 @@ +use master +go + +create database shopping +on +( + name='shopping', + filename='D:\Demo\shopping.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='shopping_log', + filename='D:\Demo\shopping_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use shopping +go + +create table orders +( + orderId int primary key identity(1,1) , + orderDate datetime default(getdate()) +) + +create table orderItem +( + ItemiD int primary key identity(1,1), + orderId int references orders(orderId) not null, + itemType nvarchar(4) check(itemType='文具' or itemType='日常用品' or itemType='体育用品' ) not null, + itemName nvarchar(4) not null, + theNumber int not null, + theMoney money not null +) + +insert into orders values (default),(default),(default),(default) + + +insert into orderItem (orderId,itemType,itemName,theNumber,theMoney) values (1 ,'文具',' 笔' ,72, 2) +,(1,'文具','尺',10,1) +,(1,'体育用品','篮球',1,56) +,(2,'文具','笔',36,2) +,(2,'文具','固体胶',20,3) +,(2,'日常用品','透明胶',2,1) +,(2,'体育用品','羽毛球',20,3) +,(3,'文具','订书机',20,3) +,(3,'文具','订书针',10,3) +,(3,'文具','裁纸刀',5,5) +,(4,'文具','笔',20,2) +,(4,'文具','信纸',50,1) +,(4,'日常用品','毛巾',4,5) +,(4,'日常用品','透明胶',30,1) +,(4,'体育用品','羽毛球',20,3) + + + +select * from orderItem +select * from orders + + + +--使用上次作业的订单数据库,完成下列题目: + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select orders.orderId,orderDate,itemType,itemName,theNumber,theMoney from orders inner join orderItem on orders.orderId=orderItem.orderId + + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orders.orderId,orderDate,itemType,itemName from orders inner join orderItem on orders.orderId=orderItem.orderId where theNumber>=50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderId,orderDate,itemType,itemName,theNumber,theMoney,sum(theMoney*theNumber)订购总价 from orders inner join orderItem on orders.orderId=orderItem.orderId group by theMoney,orders.orderId,orderDate,itemType,itemName,theNumber,theMoney + + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderId,orderDate,itemType,itemName,theNumber,theMoney,sum(theMoney*theNumber)订购总价 from orders inner join orderItem on orders.orderId=orderItem.orderId group by theMoney,orders.orderId,orderDate,itemType,itemName,theNumber,theMoney having theMoney>4 and theNumber>=3 + + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orders.orderId,theNumber from orderItem inner join orders on orderItem.orderId=orders.orderId + +--6.查询 每个订单里的 每个类别的产品 分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + +select orderId,itemType,count(theNumber) from orderItem group by orderId,itemType order by orderId diff --git "a/20210326\344\275\234\344\270\232/\351\253\230\345\245\225/stuInfo.sql" "b/20210326\344\275\234\344\270\232/\351\253\230\345\245\225/stuInfo.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9bc9386cfbe69b61faf374cb72107f0cae3d435a --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\253\230\345\245\225/stuInfo.sql" @@ -0,0 +1,99 @@ +create database Student +on +( + name='Stuinfo', + filename='D:\Demo\Stuinfo.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% + +) +log on +( + name='Stuinfo_log', + filename='D:\Demo\Stuinfo_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% + +) +go +use Student + +create table Stuinfo +( + stuNO varchar(5) unique not null, + stuName nvarchar(20), + stuAge int, + stuAddress nvarchar(20), + stuSeat int identity(1,1) unique not null, + stuSex nchar(1) check(stuSex='男' or stuSex='女') +) +go + +create table Scoreinfo +( + examNO int primary key identity(1,1), + stuNO varchar(5) references Stuinfo(stuNO), + writtenExan int, + labExam int, +) + +insert into Stuinfo values ('s2501','张秋利','20','美国硅谷','女'),('s2502','李斯文',18,'湖北武汉','男'),('s2503','马文才',22,'湖南长沙','女'), +('s2504','欧阳俊雄',21,'湖北武汉','男'),('s2505','梅超风',20,'湖北武汉','女'),('s2506','陈旋风',19,'美国硅谷','女'),('s2507','陈风',20,'美国硅谷','男') + + +insert into Scoreinfo values('s2501',50,70),('s2502',60,65),('s2503',86,85),('s2504',40,80),('s2505',70,90),('s2506',85,90) + + + +select * from Scoreinfo +select * from Stuinfo + + + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName,stuAge,writtenExan,labExam from Stuinfo inner join Scoreinfo on Scoreinfo.stuNO =Stuinfo.stuNO + + + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select Scoreinfo.stuNO,stuName,writtenExan,labExam from Stuinfo inner join Scoreinfo on Scoreinfo.stuNO =Stuinfo.stuNO where writtenExan>60 and labExam>60 + + + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select Scoreinfo.stuNO,stuName,writtenExan,labExam from Stuinfo left join Scoreinfo on Scoreinfo.stuNO =Stuinfo.stuNO + + + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName,stuAge,writtenExan,labExam from Stuinfo inner join Scoreinfo on Scoreinfo.stuNO =Stuinfo.stuNO where stuAge>20 order by writtenExan desc + + +--5.查询男女生的机试平均分 +select stuSex,avg(labExam)机试平均分 from Stuinfo inner join Scoreinfo on Scoreinfo.stuNO =Stuinfo.stuNO group by stuSex + + +--6.查询男女生的笔试总分 + +select stuSex,sum(writtenExan)笔试平均分 from Stuinfo inner join Scoreinfo on Scoreinfo.stuNO =Stuinfo.stuNO group by stuSex + + + + + + + + + + + + + + + + + + + diff --git "a/20210326\344\275\234\344\270\232/\351\253\230\345\245\225/\345\224\220\345\207\241\350\276\211/SQLserver06.sql" "b/20210326\344\275\234\344\270\232/\351\253\230\345\245\225/\345\224\220\345\207\241\350\276\211/SQLserver06.sql" new file mode 100644 index 0000000000000000000000000000000000000000..110af985d3e0ab0286a358276058576a7b67aeaa --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\253\230\345\245\225/\345\224\220\345\207\241\350\276\211/SQLserver06.sql" @@ -0,0 +1,126 @@ +use master +go + +create database ABC +on +( + name='ABC', + filename='D:\ABC.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='ABC_log', + filename='D:\ABC_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +go +use ABC +go + +create table orders +( + orderId int primary key identity , + orderDate datetime +) +go + +create table orderItem +( + ItemiD int primary key identity , + orderId int references orders(orderId), + itemType varchar(10), + itemName varchar(10), + theNumber int , + theMoney int +) +go + +insert orders values +('2008-01-12'),('2008-02-10'), +('2008-02-15'),('2008-03-10') +go + +insert orderItem values +('1','鏂囧叿','绗',72,2), +('1','鏂囧叿','灏',10,1), +('1','浣撹偛鐢ㄥ搧','绡悆',1,56), +('2','鏂囧叿','绗',36,2), +('2','鏂囧叿','鍥轰綋鑳',20,3), +('2','鏃ュ父鐢ㄥ搧','閫忔槑鑳',2,1), +('2','浣撹偛鐢ㄥ搧','缇芥瘺鐞',20,3), +('3','鏂囧叿','璁功鏈',20,3), +('3','鏂囧叿','璁功閽',10,3), +('3','鏂囧叿','瑁佺焊鍒',5,5), +('4','鏂囧叿','绗',20,2), +('4','鏂囧叿','淇$焊',50,1), +('4','鏃ュ父鐢ㄥ搧','姣涘肪',4,5), +('4','鏃ュ父鐢ㄥ搧','閫忔槑鑳',30,1), +('4','浣撹偛鐢ㄥ搧','缇芥瘺鐞',20,3) +go + +select SUM(theNumber)as 鎬绘暟 from dbo.orderItem +go + +select SUM(theNumber) as 鏁伴噺 ,AVG(theMoney) as 骞冲潎鍗曚环 from dbo.orderItem where orderId<3 group by orderId having AVG(theMoney)<10 +go + +select SUM(theNumber) as 鏁伴噺,AVG(theMoney) as 骞冲潎鍗曚环 from dbo.orderItem where theNumber<50 group by orderId having AVG(theMoney)<10 +go + +select '鏂囧叿锛' as 绫诲瀷,COUNT(itemType) 鏁伴噺 from orderItem where itemType='鏂囧叿' union +select '浣撹偛鐢ㄥ搧锛' as 绫诲瀷,COUNT(itemType) 鏁伴噺 from orderItem where itemType='浣撹偛鐢ㄥ搧' union +select '鏃ュ父鐢ㄥ搧锛' as 绫诲瀷, COUNT(itemType) 鏁伴噺 from orderItem where itemType='鏃ュ父鐢ㄥ搧' +go + +select itemType,SUM(theNumber) as 鏁伴噺,AVG(theMoney) as 骞冲潎鍗曚环 from dbo.orderItem where theNumber>100 group by itemType +go + +select itemName as 浜у搧鍚嶇О,COUNT(itemName) as 璁㈣喘娆℃暟,SUM(theNumber) as 鎬绘暟閲,AVG(theMoney) as 骞冲潎鍗曚环 from dbo.orderItem group by itemName +go +--浣跨敤涓婃浣滀笟鐨勮鍗曟暟鎹簱锛屽畬鎴愪笅鍒楅鐩細 + +--1.鏌ヨ鎵鏈夌殑璁㈠崟鐨勮鍗曠殑缂栧彿锛岃鍗曟棩鏈燂紝璁㈣喘浜у搧鐨勭被鍒拰璁㈣喘鐨勪骇鍝佸悕绉帮紝璁㈣喘鏁伴噺鍜岃璐崟浠 +select * from orders +select * from orderItem +SELECT ItemiD,orderDate,itemType,itemName,theNumber,theMoney from orderItem +inner join orders on orderItem.orderId=orders.orderId +--2.鏌ヨ璁㈣喘鏁伴噺澶т簬50鐨勮鍗曠殑缂栧彿锛岃鍗曟棩鏈燂紝璁㈣喘浜у搧鐨勭被鍒拰璁㈣喘鐨勪骇鍝佸悕绉 +SELECT ItemiD,orderDate,itemType,itemName,theNumber,theMoney from orderItem +inner join orders on orderItem.orderId=orders.orderId where theNumber>50 +--3.鏌ヨ鎵鏈夌殑璁㈠崟鐨勮鍗曠殑缂栧彿锛岃鍗曟棩鏈燂紝璁㈣喘浜у搧鐨勭被鍒拰璁㈣喘鐨勪骇鍝佸悕绉帮紝璁㈣喘鏁伴噺鍜岃璐崟浠蜂互鍙婅璐讳环 +SELECT ItemiD,orderDate,itemType,itemName,theNumber,theMoney, theNumber*theMoney 鎬讳环 from orderItem +inner join orders on orderItem.orderId=orders.orderId + +--4.鏌ヨ鍗曚环澶т簬绛変簬5骞朵笖鏁伴噺澶т簬绛変簬50鐨勮鍗曠殑璁㈠崟鐨勭紪鍙凤紝璁㈠崟鏃ユ湡锛岃璐骇鍝佺殑绫诲埆鍜岃璐殑浜у搧鍚嶇О锛岃璐暟閲忓拰璁㈣喘鍗曚环浠ュ強璁㈣喘鎬讳环 +SELECT ItemiD,orderDate,itemType,itemName,theNumber,theMoney from orderItem +inner join orders on orderItem.orderId=orders.orderId where theNumber>50 and theMoney>1 +--5.鏌ヨ姣忎釜璁㈠崟鍒嗗埆璁㈣喘浜嗗嚑涓骇鍝侊紝渚嬪锛 +-- 缂栧彿 璁㈣喘浜у搧鏁 +-- 1 3 +-- 2 4 +select ItemiD,COUNT(orderId) from orderItem group by I + +select orders.orderId as 缂栧彿,count(*) as 璁㈣喘浜у搧鏁 from orderItem +inner join orders on orderItem.orderId = orders.orderId +group by orders.orderId +--6.鏌ヨ姣忎釜璁㈠崟閲岀殑姣忎釜绫诲埆鐨勪骇鍝佸垎鍒璐簡鍑犳鍜屾绘暟閲忥紝渚嬪锛 + +-- 璁㈠崟缂栧彿 浜у搧绫诲埆 璁㈣喘娆℃暟 鎬绘暟閲 + +-- 1 鏂囧叿 2 82 +-- 1 浣撹偛鐢ㄥ搧 1 1 +-- 2 鏂囧叿 2 56 +-- 2 浣撹偛鐢ㄥ搧 1 2 +-- 2 鏃ュ父鐢ㄥ搧 1 20 + +select * from orderItem +inner join orders on orderItem.orderId=orders.orderId +select orders.orderId as 璁㈠崟缂栧彿,itemType as 浜у搧绫诲埆,count(*)as 璁㈣喘娆℃暟,sum(theNumber) as 鎬绘暟閲 from orderItem +inner join orders on orderItem.orderId = orders.orderId +group by orders.orderId,itemType +order by orders.orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\253\230\345\245\225/\345\224\220\345\207\241\350\276\211/SQLserver07.sql" "b/20210326\344\275\234\344\270\232/\351\253\230\345\245\225/\345\224\220\345\207\241\350\276\211/SQLserver07.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4ccb951494e08c237f13fa4067176784bcc44a54 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\253\230\345\245\225/\345\224\220\345\207\241\350\276\211/SQLserver07.sql" @@ -0,0 +1,93 @@ +use master +go + +create database Dwu +on +( +name='Dwu', +filename='DtestDwu.mdf', +size=5mb, +maxsize=100mb, +filegrowth=10mb +) +log on +( +name='Dwu_log', +filename='DtestDwu_log.ldf', +size=5mb, +maxsize=100mb, +filegrowth=10mb +) + +go +use Dwu +go + +create table stuinfo +( +stuNo char(5) primary key not null, +stuName nvarchar(20) not null, +stuAge char(3) not null, +stuAddress nvarchar(200), +stuSeat int identity(1,1)not null, +stuSex char(1) check(stuSex in (1,0)) default(1) not null +) +go + +insert into stuinfo(stuNo,stuName,stuAge,stuAddress,stuSex) values +('s2501','寮犵鍒',20,'缇庡浗纭呰胺',1), +('s2502','鏉庢柉鏂',18,'婀栧寳姝︽眽',0), +('s2503','椹枃鎵',22,'婀栧崡闀挎矙',1), +('s2504','娆ч槼淇婇泟',21,'婀栧寳姝︽眽',0), +('s2505','姊呰秴椋',20,'婀栧寳姝︽眽',1), +('s2506','闄堟棆椋',19,'缇庡浗纭呰胺',1), +('s2507','闄堝嚖',20,'缇庡浗纭呰胺',0) +go + +create table stuexam +( +examNo int primary key identity(1,1), +stuNo char(5) references stuinfo(stuNo) not null, +writtenExam int not null, +labExam int not null +) +go + +insert stuexam(stuNo,writtenExam,labExam)values +('s2501',50,70), +('s2502',60,65), +('s2503',86,85), +('s2504',40,80), +('s2505',70,90), +('s2506',85,90) +go + +select stuName,stuAge,writtenExam,labExam from stuexam +inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +go + + +select stuName,stuAge,writtenExam,labExam from stuexam +inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +where writtenExam 60 and labExam 60 +go + +select stuName,stuAge,writtenExam,labExam from stuinfo +left join stuexam on stuexam.stuNo = stuinfo.stuNo +go + +select stuName,stuAge,writtenExam,labExam from stuexam +inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +where stuAge = 20 +order by writtenExam desc +go + +select stuSex,avg(labExam) from stuexam +inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +group by stuSex +go + +select stuSex,sum(writtenExam) from stuexam +inner join stuinfo on stuexam.stuNo = stuinfo.stuNo +group by stuSex +go \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\253\230\345\245\225/\345\224\220\345\207\241\350\276\211/SQLserver08.sql" "b/20210326\344\275\234\344\270\232/\351\253\230\345\245\225/\345\224\220\345\207\241\350\276\211/SQLserver08.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6f7522cbd5d165c6fa745190d5dcd29a4253227d --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\253\230\345\245\225/\345\224\220\345\207\241\350\276\211/SQLserver08.sql" @@ -0,0 +1,122 @@ +use master +go +create database BBS +on +(name='BBS', + filename='D:\BSS.mdf', + size=20, + maxsize=300, + filegrowth=50 +) +log on +(name='BBS_log', + filename='D:\BSS_log.ldf', + size=20, + maxsize=300, + filegrowth=50 +) +go +use BBS +create table bbsUsers +( +uIDD int primary key identity(1,1), +uName varchar(10) not null unique , + uSex varchar(2) not null check(uSex='鐢' or uSex='濂'), + uAge int not null check(uAge>15 and uAge<60), + uPoint int not null check(uPoint>=0) +) + +create table bbsTopic( + tID int primary key identity(1,1), + tUID int references bbsUsers(uIDD), + tSID int references bbsSection(sIDD), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime , + tCount int +) +create table bbsReply( +rID int primary key identity(1,1), + rUID int references bbsUsers(uIDD ), + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime + +) +create table bbsSection( +sIDD int primary key identity(1,1), +sName varchar(10) not null , + sUid int references bbsUsers(uIDD) +) +--1.鐜板湪鏈3涓細鍛樻敞鍐屾垚鍔燂紝璇风敤涓娆℃彃鍏ュ琛屾暟鎹殑鏂规硶鍚慴bsUsers琛ㄧ鎻掑叆3琛岃褰曪紝璁板綍鍊煎涓嬶細 +-- 灏忛洦鐐 濂 20 0 +-- 閫嶉仴 鐢 18 4 +-- 涓冨勾绾х敓 鐢 19 2 +insert into bbsUsers(uName,uSex,uAge,uPoint) +select'灏忛洦鐐','濂','20','0'union +select'閫嶉仴','鐢','18','4'union +select'涓冨勾绾х敓','鐢','19','2' +go + + --2.灏哹bsUsers琛ㄤ腑鐨勭敤鎴峰悕鍜岀Н鍒嗕袱鍒楀浠藉埌鏂拌〃bbsPoint琛ㄤ腑锛屾彁绀烘煡璇㈤儴鍒嗗垪:select 鍒楀悕1锛屽垪鍚2 from 琛ㄥ悕 + select uName,uPoint into bbsPoint from bbsUsers + go + --3.缁欒鍧涘紑璁4涓澘鍧 + -- 鍚嶇О 鐗堜富鍚 + -- 鎶鏈氦娴 灏忛洦鐐 + -- 璇讳功涓栫晫 涓冨勾绾х敓 + -- 鐢熸椿鐧剧 灏忛洦鐐 + -- 鍏崷鍖 涓冨勾绾х敓 + insert into bbsSection(sName,sUid) + select'鎶鏈氦娴',3 union + select'璇讳功涓栫晫',1 union + select'鐢熸椿鐧剧',3 union + select'鍏崷鍖',1 + go + truncate table bbsSection + --4.鍚戜富璐村拰鍥炲笘琛ㄤ腑娣诲姞鍑犳潯璁板綍 + insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) +select '2','1','鑼冭窇璺','璋佹槸鑼冭窇璺','2008-7-8','1'union +select '1','3','.NET','涓嶫ava鐨勫尯鍒槸浠涔堝憖锛','2008-9-1','2'union +select '3','4','浠婂勾澶忓ぉ鏈娴佽浠涔堝憖锛','鏈夎皝鐭ラ亾浠婂勾澶忓ぉ鏈娴佽浠涔堝憖锛','2008-9-10','0' + + +-- 鍥炲笘锛 +-- 鍒嗗埆缁欎笂闈笁涓富璐存坊鍔犲搴旂殑鍥炲笘锛屽洖甯栫殑鍐呭锛屾椂闂达紝鍥炲笘浜鸿嚜瀹 + +insert into bbsReply(rTID,rMsg,rTime,rUId) +select'1','涓嶇煡閬','2008-9-5','2'union +select'2','涓嶈璇','2008-9-10','1'union +select'3','涓嶇煡閬','2008-10-1','3' + + +-- 6.鍥犱负灏忛洦鐐瑰彂甯栬緝澶氾紝灏嗗叾绉垎澧炲姞10鍒 + + +update bbsUsers set uPoint=(uPoint + 10) where uName='灏忛洦鐐' + +--鍦ㄨ鍧涙暟鎹簱涓畬鎴愪互涓嬮鐩 +--1.鏌ヨ鍑烘瘡涓増鍧楃殑鐗堜富缂栧彿锛岀増涓诲鍚嶅拰鐗堝潡鍚嶇О + select uIDD ,uName,sName from bbsSection + inner join bbsUsers on bbsSection.sUid=bbsUsers.uIDD + + select * from bbsReply + select * from bbsSection + select * from bbsTopic + select * from bbsUsers + +--2.鏌ヨ鍑轰富璐寸殑鍙戝笘鏃堕棿鍦2008-9-15浠ュ悗鐨勪富璐寸殑鍙戝笘浜虹紪鍙凤紝鍙戝笘浜哄鍚嶏紝甯栧瓙鐨勬爣棰橈紝甯栧瓙鐨勫唴瀹瑰拰鍙戝笘鏃堕棿 + select tID,uName,tTitle,tMsg,tTime from bbsTopic + inner join bbsUsers on bbsTopic.tID =bbsUsers.uIDD where bbsTopic.tTime>'2008-9-01' +--3.鏌ヨ鍑哄勾榫勫湪20浠ヤ笅鐨勭増涓荤殑缂栧彿锛岀増涓荤殑鍚嶇О鍜岀増鍧楃殑鍚嶇О + select sIDD,uName,sName from bbsUsers + inner join bbsSection on bbsUsers.uIDD=bbsSection.sIDD where bbsUsers.uAge<20 +--4.鏌ヨ鍑哄洖澶嶆暟閲忔渶澶氱殑涓昏创鐨勫彂甯栦汉缂栧彿锛屽彂甯栦汉濮撳悕锛屼富璐存爣棰橈紝涓昏创鍐呭鍜屽洖澶嶆暟閲 + select top 1 tID,uName,tTitle,tMsg,tCount from bbsTopic + inner join bbsUsers on bbsTopic.tUID=bbsUsers.uIDD + order by tCount desc +--5.鍦ㄤ富璐磋〃涓煡璇㈡瘡涓増鍧椾腑姣忎釜鐢ㄦ埛鐨勫彂甯栨绘暟 +select sName,uName,COUNT(tID) from bbsTopic +inner join bbsUsers on bbsTopic.tUID=bbsUsers.uIDD +inner join bbsSection on bbsSection.sIDD=bbsTopic.tSID +group by tSID, uIDD diff --git "a/20210326\344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7e369bb2d6fa91cbfa2e76928edab98dd1f57c9b --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery1.sql" @@ -0,0 +1,88 @@ +use master +go +--作业1 +create database Studen +on +( + name=Studen, + filename='D:\Studen.mdf', + size=10MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name=Studen_log, + filename='D:\Studen_log.ldf', + size=10MB, + maxsize=50MB, + filegrowth=10% +) +go + +use Studen +go + +create table Stuinfo +( + stuNO varchar(5) unique not null, + stuName nvarchar(20), + stuAge int, + stuAddress nvarchar(20), + stuSeat int identity(1,1) unique not null, + stuSex nchar(1) check(stuSex='男' or stuSex='女') +) +go +insert into Stuinfo values ('s2501','张秋利',20,'美国硅谷','女'),('s2502','李斯文',18,'湖北武汉','男'),('s2503','马文才',22,'湖南长沙','女'), +('s2504','欧阳俊雄',21,'湖北武汉','男'),('s2505','梅超风',20,'湖北武汉','女'),('s2506','陈旋风',19,'美国硅谷','女'),('s2507','陈风',20,'美国硅谷','男') + +select 学号=stuNO,stuName as 姓名,stuAge as 年龄,stuAddress as 地址,stuSeat as 座位号,stuSex as 性别 from Stuinfo +select stuName,stuAge,stuAddress from Stuinfo +select stuNO,邮箱=stuName+'@'+stuAddress from Stuinfo +select stuAddress from Stuinfo +select distinct 所有年龄=stuAge from Stuinfo +select * from Stuinfo where stuSeat<=3 +select stuName,stuSeat from Stuinfo where stuSeat<=4 +select top 50 percent * from Stuinfo +select * from Stuinfo where stuAddress='湖北武汉' and stuAge=20 +select * from Stuinfo where stuAddress='湖北武汉' or stuAddress='湖南长沙' +select * from Stuinfo where stuAddress in('湖北武汉' , '湖南长沙') +select * from Stuinfo where stuAge is null +select * from Stuinfo where stuAge is not null +select * from Stuinfo where stuName like '张%' +select * from Stuinfo where stuAddress like '湖%' +select * from Stuinfo where stuName like '张_' +select * from Stuinfo where stuName like '__俊_' +select * from Stuinfo order by stuAge desc +select * from Stuinfo order by stuAge desc,stuSeat asc + + + +create table Scoreinfo +( + examNO int primary key identity(1,1), + stuNO varchar(5) references Stuinfo(stuNO), + writtenExan int, + labExam int, +) +insert into Scoreinfo values('s2501',50,70),('s2502',60,65),('s2503',86,85),('s2504',40,80),('s2505',70,90),('s2506',85,90) + +--数据如图片1,使用上次作业的数据 + +select * from Stuinfo +select * from Scoreinfo + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName,stuAge,writtenExan,labExam from Stuinfo s inner join Scoreinfo sc on s.stuNO=sc.stuNO + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select stuName,stuAge,writtenExan,labExam from Stuinfo s inner join Scoreinfo sc on s.stuNO=sc.stuNO +where writtenExan >60 and labExam >60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select Stuinfo.stuNO,stuName,writtenExan,labExam from Stuinfo inner join Scoreinfo on Stuinfo.stuNO=Scoreinfo.stuNO +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName,stuAge,writtenExan,labExam from Stuinfo s inner join Scoreinfo sc on s.stuNO=sc.stuNO where stuAge>=20 order by writtenExan desc +--5.查询男女生的机试平均分 +select stuSex,avg (labExam)平均分 from Stuinfo inner join Scoreinfo ON Stuinfo.stuNO=Scoreinfo.stuNO GROUP BY stuSex +--6.查询男女生的笔试总分 +select stuSex,sum(writtenExan)总分 from Stuinfo inner join Scoreinfo ON Stuinfo.stuNO=Scoreinfo.stuNO GROUP BY stuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1fbc8b15579677074047e42cff2569766dad376e --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery2.sql" @@ -0,0 +1,97 @@ +use master +go +--作业三 +create database bbs +on +( + name='bbs', + filename='D:\bbs.mdf', + size=10, + maxsize=100, + filegrowth=10% +) +log on +( + name='bbs_log', + filename='D:\bbs.ldf', + size=5, + maxsize=15, + filegrowth=10% +) + +go +use bbs +create table bbsUsers +( + bbbsUID int identity(1,1), + uName varchar(10) not null, + uSex varchar(2) not null , + uAge int not null , + uPoint int not null +) +go +alter table bbsUsers add constraint PK_bbbsUID primary key (bbbsUID) +alter table bbsUsers add constraint UK_uName unique(uName) +alter table bbsUsers add constraint CK_uSex check(uSex in('男','女')) +alter table bbsUsers add constraint CK_uAge check(uAge>14 and uAge<61 ) +alter table bbsUsers add constraint CK_uPoint check(len(uPoint)>=0) +go +create table bbsSection +( + bbssID int identity(1,1), + sName varchar(10) not null, + sUid int +) + +go +alter table bbsSection add constraint PK_bbssID primary key (bbssID) +alter table bbsSection add constraint FK_sUid foreign key (sUid) references bbsUsers(bbbsUID) +go +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int foreign key references bbsUsers(bbbsUID), + tSID int foreign key references bbsSection(bbssID), + tTitle varchar(100) not null, + tMsg text, + tTime datetime default(getdate()), + tCount int +) + +create table bbsReply +( + rID int primary key identity(1,1), + rUID int foreign key references bbsUsers(bbbsUID), + rTID int foreign key references bbsTopic(tID), + rMsg text NOT NULL, + rTime datetime default(getdate()) +) + +go +insert into bbsUsers(uName,uSex,uAge,uPoint) values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) +select uName,uPoint into bbsPoint from bbsUsers +insert into bbsSection (sName,sUid) values('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) + +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values(2 ,4,'范跑跑','谁是范跑跑 ','2008-7-8',1) +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values(3 ,1,'.NET','与JAVA的区别是什么呀? ','2008-9-1',2) +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values(1 ,3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀 ','2008-9-10',0) + +insert into bbsReply(rUID,rTID,rMsg,rTime) values(1 ,1,'不认识','2008-7-11') +insert into bbsReply(rUID,rTID,rMsg,rTime) values(1 ,2,'没有区别','2008-9-11') +insert into bbsReply(rUID,rTID,rMsg,rTime) values(2 ,2,'请百度','2008-9-12') +--在论坛数据库中完成以下题目 +select * from bbsUsers +select * from bbsSection +select * from bbsTopic +select * from bbsReply +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select uName,sUid,sName from bbsSection inner join bbsUsers on bbsSection.bbssID=bbsUsers.bbbsUID +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select * from bbsTopic where tTime<2008-9-15 +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select bbssID,uName,sName from bbsUsers inner join bbsSection on bbsUsers.bbbsUID=bbsSection.bbssID where uAge<20 group by bbssID,uName,sName +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select top 1 * from bbsTopic inner join bbsSection on bbsTopic.tUID=bbsSection.bbssID order by tCount desc +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select tSID 版块,uName 用户,tCount 发帖总数 from bbsTopic inner join bbsUsers on bbsTopic.tUID=bbsUsers.bbbsUID group by uName,tSID,tCount + diff --git "a/20210326\344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2cd09dad0c1ff6dfddb80e9bdb2a281d9b318003 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery3.sql" @@ -0,0 +1,82 @@ +use master +go + +create database AAA +on +( + name=AAA, + filename='D:\AAA.mdf', + size=10MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name=Studen_log, + filename='D:\AAA_log.ldf', + size=10MB, + maxsize=50MB, + filegrowth=10% +) +go + +use AAA +go +--订单表(orders)列为:订单编号(orderId 主键) 订购日期(orderDate) + +--订购项目表(orderItem),列为: +--项目编号(ItemiD)订单编号(orderId)产品类别(itemType) +--产品名称(itemName) 订购数量(theNumber) 订购单价(theMoney) +create table orders +( +orderId INT primary key identity(1,1), + +orderDate datetime , + +) +create table orderItem +( +ItemiD INT primary key identity(1,1), +orderId int not null, +itemType varchar(10) not null, +itemName varchar(10)not null, +theNumber int , +theMoney money not null +) +insert into orders values('2008-01-11'),('2008-02-11'),('2008-03-11'),('2008-04-11') +select * from orderItem +insert into orderItem values (1,'文具','笔',72,2),(2,'文具','尺',10,1),(1,'体育用品','篮球',1,56),(2,'文具','笔',36,2), +(2,'文具','固体胶',20,3),(2,'日常用品','透明胶',2,1),(2,'体育用品','羽毛球',20,3),(3,'文具','订书机',20,3),(3,'文具','订书针',10,3), +(3,'文具','裁纸刀',5,5),(4,'文具','笔',20,2),(4,'文具','信纸',50,1),(4,'日常用品','毛巾',4,5),(4,'日常用品','透明胶',30,1),(4,'体育用品','羽毛球',20,3) +--使用上次作业的订单数据库,完成下列题目: +select * from orders +select * from orderItem +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select * from orders inner join orderItem on orders.orderId=orderItem.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select * from orders inner join orderItem on orders.orderId=orderItem.orderId where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderId,orderDate,itemType,itemName,theNumber,theNumber*sum(theMoney)订购总价 from orders + inner join orderItem on orders.orderId=orderItem.orderId group by orders.orderId,orderDate,itemType,itemName,theNumber,theMoney + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +select orders.orderId,orderDate,itemType,itemName,theNumber,theNumber*sum(theMoney)订购总价 from orders + inner join orderItem on orders.orderId=orderItem.orderId group by orders.orderId,orderDate,itemType,itemName,theNumber,theMoney having theNumber>50 and theMoney>5 +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 + select orders.orderId 编号,count(*)订购产品数 from orders inner join orderItem on orders.orderId=orderItem.orderId where orders.orderId=orderItem.orderId group by orders.orderId + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + + select orders.orderId 编号,itemType 产品类别,count(*)订购产品数,sum(theNumber)总数量 from orders inner join orderItem on orders.orderId=orderItem.orderId where orders.orderId=orderItem.orderId group by orders.orderId,itemType,theNumber \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery4.sql" "b/20210326\344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..09ce15ca7f0717ce5a8fdad1883965547c2ba84e --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\273\204\345\277\240\347\243\212/SQLQuery4.sql" @@ -0,0 +1,461 @@ +use master +GO +/****** Object: Database [TestDB] Script Date: 2021/3/15 16:11:24 ******/ +CREATE DATABASE [TestDB] + CONTAINMENT = NONE + ON PRIMARY +( NAME = N'TestDB', FILENAME = N'D:\TestDB.mdf' , SIZE = 4288KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) + LOG ON +( NAME = N'TestDB_log', FILENAME = N'D:\TestDB_log.ldf' , SIZE = 1072KB , MAXSIZE = 2048GB , FILEGROWTH = 10%) +GO +ALTER DATABASE [TestDB] SET COMPATIBILITY_LEVEL = 120 +GO +IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled')) +begin +EXEC [TestDB].[dbo].[sp_fulltext_database] @action = 'enable' +end +GO +ALTER DATABASE [TestDB] SET ANSI_NULL_DEFAULT OFF +GO +ALTER DATABASE [TestDB] SET ANSI_NULLS OFF +GO +ALTER DATABASE [TestDB] SET ANSI_PADDING OFF +GO +ALTER DATABASE [TestDB] SET ANSI_WARNINGS OFF +GO +ALTER DATABASE [TestDB] SET ARITHABORT OFF +GO +ALTER DATABASE [TestDB] SET AUTO_CLOSE OFF +GO +ALTER DATABASE [TestDB] SET AUTO_SHRINK OFF +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS ON +GO +ALTER DATABASE [TestDB] SET CURSOR_CLOSE_ON_COMMIT OFF +GO +ALTER DATABASE [TestDB] SET CURSOR_DEFAULT GLOBAL +GO +ALTER DATABASE [TestDB] SET CONCAT_NULL_YIELDS_NULL OFF +GO +ALTER DATABASE [TestDB] SET NUMERIC_ROUNDABORT OFF +GO +ALTER DATABASE [TestDB] SET QUOTED_IDENTIFIER OFF +GO +ALTER DATABASE [TestDB] SET RECURSIVE_TRIGGERS OFF +GO +ALTER DATABASE [TestDB] SET ENABLE_BROKER +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF +GO +ALTER DATABASE [TestDB] SET DATE_CORRELATION_OPTIMIZATION OFF +GO +ALTER DATABASE [TestDB] SET TRUSTWORTHY OFF +GO +ALTER DATABASE [TestDB] SET ALLOW_SNAPSHOT_ISOLATION OFF +GO +ALTER DATABASE [TestDB] SET PARAMETERIZATION SIMPLE +GO +ALTER DATABASE [TestDB] SET READ_COMMITTED_SNAPSHOT OFF +GO +ALTER DATABASE [TestDB] SET HONOR_BROKER_PRIORITY OFF +GO +ALTER DATABASE [TestDB] SET RECOVERY FULL +GO +ALTER DATABASE [TestDB] SET MULTI_USER +GO +ALTER DATABASE [TestDB] SET PAGE_VERIFY CHECKSUM +GO +ALTER DATABASE [TestDB] SET DB_CHAINING OFF +GO +ALTER DATABASE [TestDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) +GO +ALTER DATABASE [TestDB] SET TARGET_RECOVERY_TIME = 0 SECONDS +GO +ALTER DATABASE [TestDB] SET DELAYED_DURABILITY = DISABLED +GO +EXEC sys.sp_db_vardecimal_storage_format N'TestDB', N'ON' +GO +USE [TestDB] +GO +/****** Object: Table [dbo].[ClassInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[ClassInfo]( + [ClassId] [int] IDENTITY(1,1) NOT NULL, + [ClassName] [nvarchar](20) NOT NULL, +PRIMARY KEY CLUSTERED +( + [ClassId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[CourseInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[CourseInfo]( + [CourseId] [int] IDENTITY(1,1) NOT NULL, + [CourseName] [nvarchar](50) NOT NULL, + [CourseCredit] [int] NULL DEFAULT ((1)), +PRIMARY KEY CLUSTERED +( + [CourseId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[Scores] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[Scores]( + [ScoreId] [int] IDENTITY(1,1) NOT NULL, + [StuId] [int] NULL, + [CourseId] [int] NULL, + [Score] [int] NULL DEFAULT ((0)), +PRIMARY KEY CLUSTERED +( + [ScoreId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +/****** Object: Table [dbo].[StuInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[StuInfo]( + [StuId] [int] IDENTITY(1,1) NOT NULL, + [ClassId] [int] NULL, + [StuName] [nvarchar](10) NOT NULL, + [StuSex] [nvarchar](1) NULL DEFAULT ('男'), + [StuBrithday] [date] NULL, + [StuPhone] [nvarchar](11) NULL, + [StuProvince] [nvarchar](200) NULL, + [CreateDate] [datetime] NULL DEFAULT (getdate()), + [StuAge] [int] NULL, +PRIMARY KEY CLUSTERED +( + [StuId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] ON + +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (1, N'软件1班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (2, N'软件2班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (3, N'软件3班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (4, N'软件4班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (5, N'软件5班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (6, N'软件6班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (7, N'软件7班') +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] ON + +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (1, N'计算机基础', 3) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (2, N'HTML+CSS网页制作', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (3, N'JAVA编程基础', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (4, N'SQL Server数据库基础', 4) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (5, N'C#面向对象编程', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (6, N'Winform桌面应用程序设计', 5) +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[Scores] ON + +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (1, 1, 1, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (2, 1, 2, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (3, 1, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (4, 1, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (5, 2, 1, 60) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (6, 2, 2, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (7, 2, 3, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (8, 2, 4, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (9, 3, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (10, 3, 2, 45) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (11, 3, 3, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (12, 3, 4, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (13, 4, 1, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (14, 4, 2, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (15, 4, 3, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (16, 4, 4, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (17, 5, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (18, 5, 2, 79) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (19, 5, 3, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (20, 5, 4, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (21, 6, 1, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (22, 6, 2, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (23, 6, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (24, 6, 5, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (25, 7, 1, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (26, 7, 2, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (27, 7, 3, 92) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (28, 7, 5, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (29, 8, 1, 58) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (30, 8, 2, 59) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (31, 8, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (32, 8, 5, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (33, 9, 1, 48) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (34, 9, 2, 67) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (35, 9, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (36, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (37, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (38, 1, 1, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (39, 1, 2, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (40, 1, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (41, 1, 4, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (42, 2, 1, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (43, 2, 2, 82) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (44, 2, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (45, 2, 4, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (46, 3, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (47, 3, 2, 50) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (48, 3, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (49, 3, 4, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (50, 4, 1, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (51, 4, 2, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (52, 4, 3, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (53, 4, 4, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (54, 5, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (55, 5, 2, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (56, 5, 3, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (57, 5, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (58, 6, 1, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (59, 6, 2, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (60, 6, 3, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (61, 6, 5, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (62, 7, 1, 89) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (63, 7, 2, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (64, 7, 3, 97) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (65, 7, 5, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (66, 8, 1, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (67, 8, 2, 64) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (68, 8, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (69, 8, 5, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (70, 9, 1, 53) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (71, 9, 2, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (72, 9, 3, 76) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (73, 9, 5, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (74, 9, 5, 61) +GO +SET IDENTITY_INSERT [dbo].[Scores] OFF +GO +SET IDENTITY_INSERT [dbo].[StuInfo] ON + +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (1, 1, N'刘正', N'男', CAST(N'2002-08-02' AS Date), N'13245678121', N'广西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (2, 1, N'黄贵', N'男', CAST(N'2003-07-02' AS Date), N'13345678121', N'江西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (3, 1, N'陈美', N'女', CAST(N'2002-07-22' AS Date), N'13355678125', N'福建省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (4, 2, N'江文', N'男', CAST(N'2001-07-02' AS Date), N'13347678181', N'湖南省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (5, 2, N'钟琪', N'女', CAST(N'2004-01-13' AS Date), N'13345778129', N'安徽省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (6, 3, N'曾小林', N'男', CAST(N'2005-05-15' AS Date), N'13345378563', N'安徽省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (7, 3, N'欧阳天天', N'女', CAST(N'2000-08-19' AS Date), N'13347878121', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (8, 3, N'李逍遥', N'男', CAST(N'1999-09-02' AS Date), N'13345678557', N'广东省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 22) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (9, 4, N'刘德华', N'男', CAST(N'1995-06-11' AS Date), N'15345679557', N'福建省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 26) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (10, 4, N'刘翔', N'男', CAST(N'1996-07-09' AS Date), N'18346679589', N'江西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (11, 4, N'曾小贤', N'男', CAST(N'2003-07-02' AS Date), N'18348979589', N'湖南省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (12, 5, N'刘德华', N'男', CAST(N'2002-07-02' AS Date), N'18348979509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (13, 5, N'陈天翔', N'男', CAST(N'2003-07-02' AS Date), N'18348079509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (14, 5, N'刘能', N'男', CAST(N'2005-08-02' AS Date), N'13245678122', N'广西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (15, 5, N'钟馗', N'男', CAST(N'2004-08-02' AS Date), N'13245678123', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (16, 5, N'钟吴艳', N'女', CAST(N'2002-08-02' AS Date), N'13245678124', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (17, 5, N'刘欢', N'男', CAST(N'2001-07-02' AS Date), N'13245678125', N'湖南省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (18, 5, N'张庭', N'女', CAST(N'2000-07-02' AS Date), N'13245678126', N'江西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (19, 5, N'曹植', N'男', CAST(N'2000-08-02' AS Date), N'13245678127', N'福建省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (20, 5, N'曹操', N'男', CAST(N'2002-08-02' AS Date), N'13245678128', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (21, 5, N'孙尚香', N'女', CAST(N'2003-08-02' AS Date), N'13245678129', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (22, 3, N'老1', N'女', CAST(N'2002-08-02' AS Date), N'13245678130', N'广东省', CAST(N'2021-03-14 17:02:36.347' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (24, 2, N'老2', N'男', CAST(N'2002-08-03' AS Date), N'13345678945', NULL, CAST(N'2021-03-14 17:03:37.733' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (25, 4, N'老3', N'男', NULL, N'13645987545', N'广东省', CAST(N'2021-03-14 17:03:43.307' AS DateTime), NULL) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (28, 5, N'老4', N'男', CAST(N'2006-03-05' AS Date), N'13456987456', NULL, CAST(N'2021-03-14 17:04:03.957' AS DateTime), 15) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (29, 5, N'老5', N'女', CAST(N'1998-04-12' AS Date), N'15978456123', NULL, CAST(N'2021-03-14 17:04:47.103' AS DateTime), 23) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (30, 4, N'老6', N'男', CAST(N'1996-08-06' AS Date), N'18945674561', NULL, CAST(N'2021-03-14 17:05:04.990' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (31, 3, N'老7', N'女', CAST(N'1997-04-06' AS Date), N'18845678912', NULL, CAST(N'2021-03-14 17:05:20.570' AS DateTime), 24) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (32, 2, N'老10', N'女', CAST(N'1998-08-09' AS Date), N'19945645612', NULL, CAST(N'2021-03-14 17:06:08.107' AS DateTime), 23) +GO +SET IDENTITY_INSERT [dbo].[StuInfo] OFF +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__CourseIn__9526E2773AB7BECE] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[CourseInfo] ADD UNIQUE NONCLUSTERED +( + [CourseName] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__StuInfo__2D85FC63AF6FC6FA] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[StuInfo] ADD UNIQUE NONCLUSTERED +( + [StuPhone] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([CourseId]) +REFERENCES [dbo].[CourseInfo] ([CourseId]) +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([StuId]) +REFERENCES [dbo].[StuInfo] ([StuId]) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD FOREIGN KEY([ClassId]) +REFERENCES [dbo].[ClassInfo] ([ClassId]) +ON DELETE SET NULL +GO +ALTER TABLE [dbo].[CourseInfo] WITH CHECK ADD CHECK (([CourseCredit]>=(1) AND [CourseCredit]<=(5))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK ((len([StuPhone])=(11))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK (([StuSex]='女' OR [StuSex]='男')) +GO +USE [master] +GO +ALTER DATABASE [TestDB] SET READ_WRITE +GO +select * from stuinfo +select * from scores +--1.统计每个班的男生数 +select classid 班级,count(classid) 男生总数 from stuinfo where stusex='男' group by classid +--2.统计每个班的男、女生数 +select classid 班级,count(classid) 该班级性别人数,stusex 性别 from stuinfo group by classid,stusex order by classid +--3.统计每个班的福建人数 +select classid 班级,count(classid)人数 from stuinfo where stuprovince='福建省' group by classid +--4.统计每个班的各个省的总人数 +select classid,count(classid)总人数,stuprovince from stuinfo group by classid,stuprovince order by classid +--5.统计每个省的女生数 +select classid,stuprovince from stuinfo where stusex='女' group by classid,stuprovince order by classid +--6.统计每个省的男、女生数 +select stuprovince,count(stusex) 人数 from stuinfo group by stuprovince +--7.统计每个学生的考试总分、平均分 +select stuid,sum(score)总分,avg(score)平均分 from scores group by stuid +--8.统计出考试总分大于620的学生的考试总分 +select stuid,sum(score)总分 from scores group by stuid having sum(score)>620 +--9.统计出每门考试成绩最高分和最低分 +select courseid,max(score)最高分,min(score)最低分 from scores group by courseid +--10.统计出每个学生的各门成绩的平均分 +select stuid,Courseid ,avg(score) 平均分 from scores group by stuid,Courseid order by stuid \ No newline at end of file diff --git "a/NBA\344\275\234\344\270\232/.keep" "b/NBA\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/NBA\344\275\234\344\270\232/\345\217\266\347\234\237/NBA.sql" "b/NBA\344\275\234\344\270\232/\345\217\266\347\234\237/NBA.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8c3e62d6dd28482a2d7b60ea960cc108100906d6 --- /dev/null +++ "b/NBA\344\275\234\344\270\232/\345\217\266\347\234\237/NBA.sql" @@ -0,0 +1,114 @@ +use master +go + +create database NBADate +go + +use NBA +go + +create table Team +( + TeamId int primary key identity not null , + reamName varchar(20) not null , + victory int not null , + coach varchar(20) not null +) +go + +create table UsersInfo +( + UsersId int primary key identity not null, + UsersName varchar(20) not null , + UsersSex varchar(2) , + jingli int not null , + Team varchar(20) +) +go + +create table UsersData +( + UsersId int references UsersInfo(UsersId) not null, + TeamId int references Team(TeamId) not null , + chuchang int not null , + mingzhong varchar(10) not null , + trisection varchar(10) not null , + faqiu varchar(10) not null , + lanban float not null , + zhugong float not null , + defeng float not null , + qiangduan float not null , + gaimao float not null +) +go + +create table Game +( + GameId int primary key identity not null , + TeamId1 int references Team(TeamId) not null , + defen1 int not null, + TeamId2 int references Team(TeamId) not null , + defen2 int not null, + date date not null +) +go + +create table champion +( + TeamId int references Team(TeamId) not null , + Mvp nvarchar(20) not null, + duoguan int not null, + victory int not null , + failing int not null +) +go + +insert into UsersInfo + select '勒布朗-詹姆斯','男','17','湖人' union + select '贾里德-杜德利 ','男','13','湖人' union + select '扬尼斯-阿德托昆博 ','男','7','雄鹿' union + select '布鲁克-洛佩兹','男','12','雄鹿' union + select '斯蒂芬-库里 ','男','11','勇士' union + select '克雷-汤普森','男','8','勇士' union + select '凯文-杜兰特','男','12','篮网' union + select '詹姆斯-哈登 ','男','11','篮网' union + select '科怀-莱昂纳德 ','男','9','快船' union + select '拉简-朗多 ','男','14','快船' +go + +insert into Team + select '湖人','32','弗兰克-沃格尔' union + select '雄鹿','32','迈克-布登霍尔泽' union + select '勇士','24','史蒂夫-科尔' union + select '篮网','36','史蒂夫-纳什' union + select '快船','34','泰伦-卢' +go + +insert into Game + select '4','138','5','99','2020-12-26 03:30:00' union + select '1','87','2','81','2020-12-12 11:00:00' union + select '1','131','2','106','2020-12-14 09:30:00' union + select '3','125','5','99','2020-12-23 08:00:00' union + select '1','109','2','116','2020-12-23 11:00:00' +go + +insert into UsersData + select '1','4','32','53.0','44.3','87.3','7.6','5.2','28.3','0.6','1.0' union + select '2','1','23','54.0','48.2','86.3','7.3','5.1','28.6','0.4','1.1' union + select '3','3','20','53.0','44.3','87.3','7.3','5.3','28.4','0.7','1.4' union + select '4','2','36','51.0','44.6','86.2','8.1','5.5','28.4','0.8','1.0' union + select '5','5','40','54.0','46.2','76.5','7.4','6.5','28.4','0.6','1.3' union + select '6','2','41','54.5','44.2','84.6','8.1','5.6','28.6','0.7','1.5' union + select '7','1','41','51.3','36.8','70.3','7.9','7.9','25.4','1','0.6' union + select '8','5','43','48.1','40.8','92.5','5.5','6','29.7','1.3','0.1' union + select '9','4','45','56.5','30.2','68.7','11.4','6.2','28.8','1.1','1.3' union + select '10','3','42','46.3','35.8','87.0','8','10.9','25.2','1.2','0.7' +go + +insert into champion + select '1','沙奎尔·奥尼尔','2000','4','2' union + select '1','沙奎尔·奥尼尔','2001','4','1' union + select '1','沙奎尔·奥尼尔','2002','4','0' union + select '1','科比-布莱恩特','2008','4','1' union + select '1','科比-布莱恩特','2009','4','3' +go diff --git "a/NBA\344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/NBA.sql" "b/NBA\344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/NBA.sql" new file mode 100644 index 0000000000000000000000000000000000000000..af1c26606b002f6a23fd5661a8c6bdef4063f789 --- /dev/null +++ "b/NBA\344\275\234\344\270\232/\346\234\261\345\245\207\344\274\237/NBA.sql" @@ -0,0 +1,128 @@ +create database NBADB +on +( + name='NBADB', + filename='D:\NBADB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='NBADB_log', + filename='D:\NBADB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use NBADB +go + +create table clubs +( + cid int primary key, + cname varchar(50), + ctiy varchar(50) +) +go + +create table players +( + pid int primary key, + pname varchar(50) not null, + birthday date, + height int, + weight int, + position varchar(10), + cid int, + foreign key(cid) references clubs(cid) +) +go + + +create table abilities( + pid int, + body numeric(4,1), + shoot numeric(4,1), + control numeric(4,1), + foreign key(pid) references players(pid) +) +go + +create table game +( + gameid int primary key identity(1,1), + gclubs nvarchar(20) not null, + gametime datetime, + winner varchar(20) not null +) +go + +create table winner +( + winerid int, + winner varchar(20) not null +) +go +select * from players +select*from winner +select * from game +select*from clubs +select * from abilities + +insert into game(gclubs,gametime,winner)values +('篮网','2013-5-18 16:00','德隆'),('凯尔特人','2017-3-26 19:03','KG'), +('老鹰','2019-8-27 18:47','鹰王'),('黄蜂','2015-6-7 19:39','韦斯特'), +('火箭','2015-5-6 19:00','姚明'),('湖人','2015-8-23 14:32','科比'), +('老鹰','2018-3-14 18:40','梅斯'),('篮网','2017-5-16 19:54','杜兰特'), +('76人','2015-8-17 17:08','布兰德'),('雄鹿','2018-5-27 20:06','詹宁斯'), +('公牛','2017-6-20 19:48','布泽尔'),('热火','2018-5-21 17:20','韦德'), +('勇士','2019-5-12 20:18','库里'),('湖人','2016-7-16 19:36','詹姆斯') + +insert into winner (winner)values +('德隆'),('鹰王'),('KG'),('韦斯特'), +('姚明'),('科比'),('梅斯'),('杜兰特'),('布兰德'),('詹宁斯'),('布泽尔'), +('韦德'),('库里'),('詹姆斯') + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git "a/NBA\344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/NBA.sql" "b/NBA\344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/NBA.sql" new file mode 100644 index 0000000000000000000000000000000000000000..89d6d291b32d40b4571eb47f677a5523c0c169e4 --- /dev/null +++ "b/NBA\344\275\234\344\270\232/\346\235\216\345\255\220\346\272\220/NBA.sql" @@ -0,0 +1,83 @@ +use master +go +create database BB +on +( + name='BB', + filename='D:\BB.mdf', + size=10, + maxsize=200, + filegrowth=10% +) +log on +( + name='BB_log', + filename='D:\BB_log.ldf', + size=10, + maxsize=200, + filegrowth=10% +) +go +use BB +create table Team +( + Id int primary key identity, + TeamName varchar(20) not null, + victory int not null , + chief varchar(20) not null +) +create table AA +( + Id int primary key identity, + name varchar(20) not null, + Sex varchar(2) not null, + location varchar(20) not null, + height varchar(10) not null, + weight varchar(20) not null , + number varchar(20) not null, + TeamId int not null references Team(Id) +) + +create table Game +( + GameID int primary key identity not null , + home int not null references Team(Id) , + visiting int references Team(Id) not null , + date date not null, + site varchar(50) not null + +) +create table UserDate +( + UserName int references AA(Id), + TeamId int references Team(Id), + appearances int not null, + MVPTime int , + hit varchar(10), + faqiu varchar(10) not null , + lanban float not null , + zhugong float not null , + defeng float not null , + qiangduan float not null , + gaimao float not null , +) +create table champion +( + Id int primary key , + TeamID int references Team(Id) , + data datetime not null , + +) +insert into AA values ('LeBron James','男','前锋','203CM','113KG','23',3),('Michael Jordan ','男','后卫','198CM','88KG','23',4),('Kobe Bean Bryant','男','后卫','198CM','96KG','24',2), +('Yao Ming ','男','中锋','229CM','141KG','11',1) +insert into Team values ('Houston Rockets',50,'Mike D Antoni'),('Los Angeles Lakers',33,'Frank Vogel '),('Cleveland Cavaliers',50,'John Beilein'), +('Chicago Bulls',50,'Jim Boylen') +insert into Game values (1,2,getdate(),'休斯顿'),(2,3,'1999-12-12','洛杉矶'),(3,1,'1999-12-25','克里夫兰'),(4,3,'1999-12-28','芝加哥') +insert into UserDate values (1,3,6,8,'1000','120',50,20,10,5,10),(3,2,8,12,'11000','100',50,30,50,8,13),(2,4,10,15,'500','220',40,66,20,8,16), +(4,1,10,8,'500','100',30,66,11,5,12) +insert into champion values (1,3,getdate()),(2,2,'2016-10-5'),(3,4,'2017-8-12'),(4,1,'2018-10-12') +select * from Team +select * from AA +select * from Game +select * from UserDate +select * from champion diff --git "a/NBA\344\275\234\344\270\232/\346\235\250\345\270\206/NBA.sql" "b/NBA\344\275\234\344\270\232/\346\235\250\345\270\206/NBA.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6b7edf9fe12a5aad01dc9ce4c7d9af103bd0fa0d --- /dev/null +++ "b/NBA\344\275\234\344\270\232/\346\235\250\345\270\206/NBA.sql" @@ -0,0 +1,198 @@ +use master +go + +create database NBA +on +( + name='NBA', + filename='D:\NBA.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='NBA_log', + filename='D:\NBA_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +go + +use NBA +go + +create table UsersInfo +( + UsersId int primary key identity not null, --编号 + UsersName varchar(20) not null , --名字 + UsersSex varchar(2) , --性别 + jingli int not null , --经历 + Team varchar(20) not null , --队伍 +) +go + +create table Team +( + TeamId int primary key identity not null , --编号 + TeamName varchar(20) not null , --名字 + Competition varchar(20) check(Competition in('东部','西部'))not null ,--赛区 + victory int not null , --胜场 + coach varchar(20) not null , --教练 +) +go + +create table Game +( + GameId int primary key identity not null , --编号 + TeamId1 int references Team(TeamId) not null , --主场 + defen1 int not null, --得分 + TeamId2 int references Team(TeamId) not null , --客场 + defen2 int not null, --得分 + date date not null --日期 +) +go + +create table UsersData +( + UsersId int references UsersInfo(UsersId) not null,--球员编号 + TeamId int references Team(TeamId) not null , --队伍编号 + chuchang int not null , --出场次数 + mingzhong varchar(10) not null , --命中率 + trisection varchar(10) not null , --三分命中率 + faqiu varchar(10) not null , --罚球率 + lanban float not null , --篮板 + zhugong float not null , --助攻 + defen float not null , --得分 + qiangduan float not null , --抢断 + gaimao float not null , --盖帽 +) +go + +create table champion +( + TeamId int references Team(TeamId) not null , --队伍编号 + Mvp nvarchar(20) not null, --球员编号 + duoguan int not null, --哪一年夺冠 + victory int not null , --胜场 + failing int not null , --负场 +) +go + + + +insert UsersInfo values + ('埃里克-戈登','男',12,'火箭'), + ('姚明','男',8,'火箭'), + ('凯文·加内特','男',20,'凯尔特人'), + ('雷·阿伦','男',18,'凯尔特人'), + ('甘尼斯·阿德托昆博','男',8,'雄鹿'), + ('艾森·伊利亚索瓦','男',14,'雄鹿'), + ('卢卡·东契奇','男',3,'独行侠'), + ('小蒂姆·哈达威','男',7,'独行侠'), + ('约翰-沃尔','男',12,'奇才'), + ('德怀特·霍华德','男',16,'奇才'), + ('达米恩·利拉德','男',10,'开拓者'), + ('C.J.麦科勒姆','男',9,'开拓者'), + ('韦恩-埃灵顿','男',11,'活塞'), + ('科里-约瑟夫','男',9,'活塞') +go + +insert Team values + ('雄鹿','东部',32,'迈克-布登霍尔泽'), + ('独行侠','西部',28,'里克-卡莱尔'), + ('奇才','东部',17,'斯科特-布鲁克斯'), + ('开拓者','西部',30,'里克-卡莱尔'), + ('凯尔特人','东部',25,'布拉德-史蒂文斯'), + ('火箭','西部',13,'斯蒂芬-塞拉斯'), + ('活塞','东部',15,'德维恩-凯西') +go + +insert Game values + (2,112,1,102,'2020-12-13'), + (3,96,7,99,'2020-12-20'), + (1,121,5,122,'2020-12-24'), + (6,126,4,128,'2020-12-27') +go + +insert UsersData values + (1,6,692,'42.5%','36.8%','66.7%',2.5,2.8,17.8,0.9,0.3), + (2,6,486,'52.4%','20.0%','52.7%',9.2,1.6,19.0,0.4,1.9), + (3,5,1462,'0.5%','0.3%','66.7%',10.0,3.7,16.6,1.3,1.4), + (4,5,1300,'45.2%','40.0%','95.2%',4.1,3.4,18.9,1.1,0.2), + (5,1,522,'52.5%','28.5%','72.4%',8.9,4.3,20.0,1.2,1.3), + (6,1,801,'44.4%','36.6%','77.6%',5.7,1.1,10.3,0.7,0.4), + (7,2,126,'44.3%','32.2%','73.3%',8.5,7.1,24.4,1.1,0.3), + (8,2,867,'43.1%','35.5%','78.2%',3.3,8.2,17.7,1.6,0.1), + (9,3,573,'43.3%','32.4%','78.1%',4.3,9.2,19.0,1.7,0.7), + (10,3,1106,'58.6%','13.2%','56.5%',12.3,1.4,16.8,0.9,1.9), + (11,4,607,'43.6%','37.1%','88.9%',4.2,6.5,24.0,1.0,0.3), + (12,4,506,'18.7%','45.4%','84.4%',3.3,3.2,18.7,0.9,0.4), + (13,7,681,'40.8%','37.8%','84.6%',2.2,1.1,8.0,0.5,0.1), + (14,7,592,'44.1%','33.1%','76.6%',2.6,2.9,6.9,0.8,0.2) +go +insert champion values + ('5','保罗·皮尔斯','2008','4','2'), + ('7','昌西·比卢普斯','2004','4','1'), + ('6','哈基姆·奥拉朱旺','1994','4','3'), + ('1','贾巴尔','1971','4','0') +go + + + +select * from UsersInfo +select * from Team +select * from Game +select * from UsersData +select * from champion + +select + TeamId as 队伍编号 , + TeamName as 队伍名称 , + Competition as 队伍区部 , + victory as 胜场 , + coach as 教练 +from Team + +select + UsersId as 球员编号 , + UsersName as 球员名称 , + UsersSex as 性别 , + jingli as 经历 , + Team as 队伍 +from UsersInfo + +select + GameId as 比赛编号 , + TeamId1 as 主场 , + defen1 as 得分 , + TeamId2 as 客场 , + defen2 as 得分 , + date as 时间 +from Game + +select + UsersName as 球员名称 , + TeamName as 队伍名称 , + chuchang as 出场次数 , + mingzhong as 命中率 , + trisection as 三分命中率 , + faqiu as 罚球率 , + lanban as 篮板 , + zhugong as 助攻 , + defen as 得分 , + qiangduan as 抢断 , + gaimao as 盖帽 +from UsersData A +inner join UsersInfo B on A.UsersId = B.UsersId +inner join Team C on A.TeamId = C.TeamId + +select + duoguan as 赛季, + TeamName, + Mvp, + A.victory as 胜场, + failing as 负场 +from champion A +inner join Team C on A.TeamId = C.TeamId \ No newline at end of file diff --git "a/NBA\344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/SQLQuery2.sql" "b/NBA\344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0881fd40e552ce75e95a37470b08e7abc365df01 --- /dev/null +++ "b/NBA\344\275\234\344\270\232/\346\242\201\345\220\257\351\221\253/SQLQuery2.sql" @@ -0,0 +1,152 @@ + +create database NBA +go +use NBA +go +create table Team +( + TID int primary key identity, + --队名 + Tname varchar(50), + --分区 + region varchar(50), + --创建年份 + CreateTime varchar(20), + --口号 + intro text, + +) +go + +insert into Team values +('亚特兰大 老鹰','东部联盟东南赛区参赛球队','1946','不尝试,就永远不会成功'), +('夏洛特 黄蜂','东部联盟东南赛区参赛球队','2004','从历史的心中出发,迈向更加悸动的未来'), +('洛杉矶湖人队','美国加利福尼亚州洛杉矶','1947','1、2、3、Manba!曼巴精神永存') +go + +create table Player + ( + PID int primary key identity, + --球队 + TID int references Team(TID) not null, + --名字 + PName varchar(20) not null, + --性别 + PSex varchar(2) default('男') check(PSex in('男','女')) not null, + --位置 + PPosition varchar(20), + --身高 + Phigh varchar(20), + --体重 + Pweight varchar(20), + --生日 + Pbirthday date, + --参赛年数 + Pexperience int not null, + --家乡 + Pnationality varchar(20) not null + ) + + insert into Player values (1,'马克加·索尔','男','中锋','2.11m','115.7公斤','1985-01-29 ',12,'西班牙'), + (1,'勒布朗·詹姆斯','男','前锋','2.06m','113.4公斤','1984-12-30',17,'美国'), + (1,'亚历克斯·卡鲁索','男','后卫','1.93m','84.4公斤',' 1994-02-28', 3,'美国') + + + insert into Player values (2,'拉梅洛·鲍尔','男','后卫','1.98m','81.6公斤','2001-08-22',0,'美国'), + (2,'俾斯麦·比永博','男','中锋','2.03m','115.7公斤','1992-08-28',9,'刚果民主共和国'), + (2,'迈尔斯·布里奇斯','男','前锋','1.98m','102.1公斤','1998-03-21',2,'美国') + + insert into Player values (3,'博格丹·博格达诺维奇','男','后卫','1.98m','99.8公斤','1992-08-18',3,'塞尔维亚'), + (3,'克林特·卡佩拉','男','中锋','2.08m','108.9公斤','1994-05-18',6 ,'瑞士'), + (3,'约翰·科林斯','男','前锋-中锋','2.06m','106.6公斤 ','1997-09-23',3,'美国') + +create table ContestInfo +( + TeamId int foreign key references Team(TID), + --本赛季场次 + GamePlayed int not null, + --进攻篮板 + Attack int not null, + --防守篮板 + Defend int not null, + --得分 + Score int not null, + --失误 + Fault int not null, + --犯规 + Foul int not null +) + +insert into ContestInfo(TeamId,GamePlayed,Attack,Defend,Score,Fault,Foul) +values +(1,51,10.8,34.7,113.4,13.7,19.9), +(2,19,10.5,33.6,110.7,15.4,18.1), +(3,51,9.7,35.5,110.0,15.6,19.0) + +create table Gameinformation +( +PID int primary key references Player(PID),--对应球员信息表的ID +GameOpponent varchar(20) not null,--比赛对手球队 +Score int not null,--该球员比赛得分 +BackBoard int not null,--篮板次数 +Assist int not null,--助攻次数 +Steal int not null,--抢断次数 +Block int not null,--盖帽次数 +Hit int not null,--命中次数 +Sell int not null,--出手次数 +Offensive int null,--进攻次数 +Guard int not null,--防守次数 +Fault int not null,--失误次数 +Foul int not null,--犯规次数 +) +insert into Gameinformation values +(1,'猛龙',13,9,5,0,4,6,9,3,6,5,3), +(2,'老鹰',10,1,4,0,0,3,6,0,1,1,0), +(3,'猛龙',13,5,4,2,0,4,7,0,5,4,3), +(4,'快船',13,5,2,3,0,4,12,0,5,1,4), +(5,'凯尔特人',6,3,2,0,2,2,2,1,2,0,3), +(6,'凯尔特人',10,5,2,0,0,3,9,0,5,4,1), +(7,'灰熊',24,6,3,2,0,9,20,3,3,2,3), +(8,'鹈鹕',12,12,1,0,3,5,7,5,7,1,3), +(9,'太阳',9,4,3,0,0,4,6,0,4,0,1) + + +create table NBAOne +( + --夺冠年份 + year datetime unique , + TID int foreign key references Team(TID) +) +insert into NBAOne values('1958',1), + + ('1978',2) , + + ('1949',3), + + ('1950',3), + + ('1952',3), + + ('1953',3), + + ('1954',3), + + ('1972',3), + + ('1980',3), + + ('1982',3), + + ('1985',3), + + ('1987',3), + + ('1988',3), + + ('2000',3), + + ('2001',3), + + ('2002',3), + + ('2009',3) diff --git "a/NBA\344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery1.sql" "b/NBA\344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..09e057fe0c1136e8905eaf8580e0443de5bb18ce --- /dev/null +++ "b/NBA\344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery1.sql" @@ -0,0 +1,63 @@ +create database nba +on +( + name='nba.mdf', + filename='D:\nba.mdf', + maxsize =50, + size=20, + filegrowth=10% +) + log on + ( + name='nba_log.ldf', + filename='D:\nba_log.ldf', + size=20, + maxsize=50, + filegrowth=10% + + ) + create table teamtable( + teamID int primary key identity(1,1), + teamName nvarchar(20) unique not null, + teamcreationtime datetime default(getdate()) not null, + teambriefintroduction ntext + ) + + +create table playertable( +playerID int identity(1,1) primary key , +playName nvarchar(20) not null, +playerSex nvarchar(1) check(playerSex='男' or playerSex='女'), +playerStature varchar(3) not null, +playerWeight varchar(3) not null, +team int references teamtable(teamID), +nationality nvarchar(10) not null, +location nvarchar(5) check(location='得分后卫' or location='控球后卫' or location='小前锋' or location='大前锋' or location='中锋' ), +sservice nvarchar(2) check(sservice='服役' or sservice='现役') +) +create table playerdatainfo( +PDID int not null primary key identity(1,1), +PDNAMEid int not null references playertable(playerID), +PDSHOAT int not null, +PDST int not null, +PDFOUL int not null, +PDTHREE int not null, +PDTWO int not null, +PDdunk int not null + + +) +create table champion +( +championID int primary key identity(1,1), +championTime datetime not null, +championteam int references teamtable(teamID), + +) +create table gametable( +gameD int identity(1,1) primary key, +teamID int references teamtable(teamID), +gametime datetime not null, +gamesite nvarchar(20) not null, +score varchar(10) not null +) diff --git "a/NBA\344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/NBA.sql" "b/NBA\344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/NBA.sql" new file mode 100644 index 0000000000000000000000000000000000000000..90ffb232b62d70b4cdec113a828c458386f0f7bd --- /dev/null +++ "b/NBA\344\275\234\344\270\232/\350\224\241\344\270\234\347\224\237/NBA.sql" @@ -0,0 +1,210 @@ +use master +go + +create database NBA +on +( + name='NBA', + filename='D:\NBA.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='NBA_log', + filename='D:\NBA_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +go + +use NBA +go + +create table UsersInfo +( + UsersId int primary key identity not null, --编号 + UsersName varchar(20) not null , --名字 + UsersSex varchar(2) , --性别 + jingli int not null , --经历 + Team varchar(20) not null , --队伍 +) +go + +create table Team +( + TeamId int primary key identity not null , --编号 + TeamName varchar(20) not null , --名字 + Competition varchar(20) check(Competition in('东部','西部'))not null ,--赛区 + victory int not null , --胜场 + coach varchar(20) not null , --教练 +) +go + +create table Game +( + GameId int primary key identity not null , --编号 + TeamId1 int references Team(TeamId) not null , --主场 + defen1 int not null, --得分 + TeamId2 int references Team(TeamId) not null , --客场 + defen2 int not null, --得分 + date date not null --日期 +) +go + +create table UsersData +( + UsersId int references UsersInfo(UsersId) not null,--球员编号 + TeamId int references Team(TeamId) not null , --队伍编号 + chuchang int not null , --出场次数 + mingzhong varchar(10) not null , --命中率 + trisection varchar(10) not null , --三分命中率 + faqiu varchar(10) not null , --罚球率 + lanban float not null , --篮板 + zhugong float not null , --助攻 + defen float not null , --得分 + qiangduan float not null , --抢断 + gaimao float not null , --盖帽 +) +go + +create table champion +( + TeamId int references Team(TeamId) not null , --队伍编号 + Mvp nvarchar(20) not null, --球员编号 + duoguan int not null, --那一年夺冠 + victory int not null , --胜场 + failing int not null , --负场 +) +go + + + +insert UsersInfo values +('埃里克-戈登','男',12,'火箭'), +('姚明','男',8,'火箭'), +('凯文·加内特','男',20,'凯尔特人'), +('雷·阿伦','男',18,'凯尔特人'), +('迈克尔·乔丹','男',15,'公牛'), +('斯科蒂·皮蓬','男',20,'公牛'), +('科比·布莱恩特','男',20,'湖人'), +('沙奎尔·奥尼尔','男',18,'湖人'), +('约翰-沃尔','男',12,'奇才'), +('德怀特·霍华德','男',16,'奇才'), +('达米恩·利拉德','男',10,'开拓者'), +('C.J.麦科勒姆','男',9,'开拓者'), +('韦恩-埃灵顿','男',11,'活塞'), +('科里-约瑟夫','男',9,'活塞') +go + +insert Team values +('雄鹿','东部',32,'迈克-布登霍尔泽'), +('马刺','西部',28,'波波维奇'), +('公牛','东部',70,'菲尔·杰克逊'), +('湖人','西部',62,'菲尔·杰克逊'), +('凯尔特人','东部',40,'布拉德-史蒂文斯'), +('火箭','西部',40,'斯蒂芬-塞拉斯'), +('活塞','东部',55,'德维恩-凯西'), +('奇才','东部',20,'斯科特-布鲁克斯'), +('开拓者','西部',30,'特里-斯托茨') +go + +insert Game values +(2,112,1,102,'2020-12-13'), +(1,121,5,122,'2020-12-24'), +(3,115,4,117,'2021-01-09'), +(4,121,5,122,'2021-01-31'), +(6,125,3,120,'2021-01-19') +go + +insert UsersData values +(1,6,692,'42.5%','36.8%','66.7%',2.5,2.8,17.8,4.9,5.3), +(2,6,486,'52.4%','20.0%','52.7%',9.2,1.6,19.3,2.4,1.9), +(3,5,1462,'0.5%','0.3%','66.7%',10.0,3.7,16.6,1.3,1.4), +(4,5,1300,'45.2%','40.0%','95.2%',4.1,3.4,18.9,1.1,3.2), +(5,1,522,'52.5%','28.5%','72.4%',8.9,4.3,20.0,1.2,1.3), +(6,1,801,'44.4%','36.6%','77.6%',5.7,1.1,10.3,2.7,4.4), +(7,2,126,'44.3%','32.2%','73.3%',8.5,7.1,24.4,1.1,0.3), +(8,2,867,'43.1%','35.5%','78.2%',3.3,8.2,17.7,1.6,0.1), +(9,3,1750,'43.3%','32.4%','92.1%',4.3,9.2,19.3,1.7,2), +(10,3,1348,'58.6%','13.2%','85.5%',12.3,1.4,16.8,2,1.9), +(11,4,1562,'43.6%','37.1%','88.9%',4.2,6.5,24.4,5.3,7.3), +(12,4,1105,'18.7%','45.4%','84.4%',3.3,3.2,18.7,2.9,8.4), +(13,7,681,'40.8%','37.8%','84.6%',2.2,1.1,8.3,2.5,3.1), +(14,7,592,'44.1%','33.1%','76.6%',2.6,2.9,6.9,1.8,2.2) +go +insert champion values + +('3','迈克尔·乔丹','1991','4','1'), +('3','迈克尔·乔丹','1992','4','2'), +('3','迈克尔·乔丹','1993','4','2'), +('6','哈基姆·奥拉朱旺','1994','4','3'), +('6','哈基姆·奥拉朱旺','1995','4','0'), +('3','迈克尔·乔丹','1996','4','2'), +('3','迈克尔·乔丹','1997','4','2'), +('3','迈克尔·乔丹','1998','4','2'), +('2','蒂姆·邓肯','1999','4','1'), +('4','沙奎尔·奥尼尔','2000','4','2'), +('4','沙奎尔·奥尼尔','2001','4','1'), +('4','沙奎尔·奥尼尔','2002','4','0'), +('2','蒂姆·邓肯','2003','4','2'), +('7','昌西·比卢普斯','2004','4','1'), +('2','蒂姆·邓肯','2005','4','3'), +('5','保罗·皮尔斯','2008','4','2') + + +go + + + +select * from UsersInfo +select * from Team +select * from Game +select * from UsersData +select * from champion + +select +TeamId as 队伍编号 , +TeamName as 队伍名称 , +Competition as 队伍区部 , +victory as 胜场 , +coach as 教练 +from Team + +select +UsersId as 球员编号 , +UsersName as 球员名称 , +UsersSex as 性别 , +jingli as 经历 , +Team as 队伍 +from UsersInfo + +select +GameId as 比赛编号 , +TeamId1 as 主场 , +defen1 as 得分 , +TeamId2 as 客场 , +defen2 as 得分 , +date as 时间 +from Game + +select +UsersName as 球员名称 , +TeamName as 队伍名称 , +chuchang as 出场次数 , +mingzhong as 命中率 , +trisection as 三分命中率 , +faqiu as 罚球率 , +lanban as 篮板 , +zhugong as 助攻 , +defen as 得分 , +qiangduan as 抢断 , +gaimao as 盖帽 +from UsersData A +inner join UsersInfo B on A.UsersId = B.UsersId +inner join Team C on A.TeamId = C.TeamId + +select duoguan as 赛季,TeamName,Mvp,A.victory as 胜场,failing as 负场 from champion A +inner join Team C on A.TeamId = C.TeamId \ No newline at end of file diff --git "a/NBA\344\275\234\344\270\232/\351\237\246\345\244\251\347\277\224/NBA.sql" "b/NBA\344\275\234\344\270\232/\351\237\246\345\244\251\347\277\224/NBA.sql" new file mode 100644 index 0000000000000000000000000000000000000000..13811b30829776c1f77e8fbadfa6719b8ae2bee9 --- /dev/null +++ "b/NBA\344\275\234\344\270\232/\351\237\246\345\244\251\347\277\224/NBA.sql" @@ -0,0 +1,85 @@ +create table qiuyuan --球员信息表 +( +userid int identity(1,1) primary key not null, --id +username nvarchar(10) not null, --名字 +userage int not null, --年龄 +brithday date not null,--生日 +stature float ,--身高 +weight float,--体重 +nationality nvarchar(10) not null,--国籍 +teamid int not null,--战队id +nbaage int not null --nba球龄 +) + +create table zhandui--战队表 +( +teamid int identity(1,1) primary key not null,--球队id +teamname nvarchar(10) not null,--球队名字 +teamcoach nvarchar(10),--球队教练 +location nvarchar(10),--所在地 +area nvarchar(10)--所属赛区 +) + +alter table qiuyuan add constraint FK_qiuyuan_teamid foreign key(teamid) references zhandui(teamid) + +insert into zhandui (teamname,teamcoach,location,area) values +('华盛顿奇才','斯科特·布鲁克斯','华盛顿特区华盛顿','东部联盟-东南区赛区'), +('夏洛特黄蜂','詹姆斯·博雷戈','北卡罗来纳州夏洛特','东部联盟-东南区赛区'), +('亚特兰大老鹰','劳埃德·皮尔斯','乔治亚州亚特兰大','东部联盟-东南区赛区'), +('迈阿密热火','埃里克·斯波尔斯特拉','佛罗里达州迈阿密','东部联盟-东南区赛区'), +('奥兰多魔术','史蒂夫·克利福德','佛罗里达州奥兰多','东部联盟-东南区赛区') + +insert into qiuyuan (username,userage,brithday,stature,weight,nationality,teamid,nbaage) values +('钱德勒-哈奇森','25','1996-04-26','1.99','96','美国 ','1','2'), +('科迪-泽勒','29','1992-10-05','2.11','108.9','美国','2','7'), +('约翰-科林斯','24','1997-09-23','2.06','106.6','美国','3','3'), +('德维恩-戴德蒙','32','1989-08-12','2.13','111.1','美国','4','7'), +('奥托-波特','28','993-06-03','2.03','89.8','美国','5','7') + +create table saicheng--赛程表 +( +scid int primary key not null,--比赛编号 +riqi date not null,--日期 +zc nvarchar not null ,--主场 +kc nvarchar not null ,--客场 +zcdf int not null,--主场得分 +kcdf int not null,--客场得分 +vit nvarchar not null--胜利队伍 +) +insert into saicheng (scid,riqi,zc,kc,zcdf,kcdf,vit) values +('202104131700','2021-04-13','奇才','爵士','125','121','奇才'), +('202104121800','2021-04-12','黄蜂','老鹰','101','105','老鹰'), +('202104101800','2021-04-10','老鹰','公牛','120','108','老鹰'), +('202104092000','2021-04-09','热火','湖人','110','104','热火'), +('202104132100','2021-04-13','魔术','马刺','97','120','马刺') + +create table vt--赛季冠军表 +( +sj nvarchar not null, --赛季 +guanjun nvarchar not null--冠军 +) +insert into vt(sj,guanjun) values +('2015赛季','软件1班'), +('2016赛季','软件2班'), +('2017赛季','软件3班'), +('2018赛季','软件4班'), +('2019赛季','软件5班'), +('2010赛季','软件1班') + + +create table sj--球员数据表 +( +userid int primary key not null,--球员id +saichang nvarchar not null,--赛季 +changci int not null,--上场次数 +shijian float not null,--上场时间 +defen float not null,--总得分 +) +alter table sj add constraint FK_sj_userid foreign key(userid) references qiuyuan(userid) + +insert into sj(userid,saichang,changci,shijian,defen) values +('1','2021','6','18.7','5.8'), +('2','2021','33','709','304'), +('3','2021','47','1422','855'), +('4','2021','0','0','0'), +('5','2021','3','66','24') \ No newline at end of file diff --git a/README.en.md b/README.en.md index bc7554fc376201b138c5bfa819bdd95b70f96b0f..a433dc3b6d3f8df1ce43f1bdc32e4da8c7aa7ce2 100644 --- a/README.en.md +++ b/README.en.md @@ -1,36 +1,36 @@ -# SQLserver2 - -#### Description -{**When you're done, you can delete the content in this README and update the file with details for others getting started with your repository**} - -#### 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/) +# SQLserver2 + +#### Description +{**When you're done, you can delete the content in this README and update the file with details for others getting started with your repository**} + +#### 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 index 2703662e0ea056cbe6fa6f9495b5844c29cefe0f..098b57a9f4ee8dba8930b451e861c5ad3e734686 100644 --- a/README.md +++ b/README.md @@ -1,39 +1,39 @@ -# SQLserver2 - -#### 浠嬬粛 -{**浠ヤ笅鏄 Gitee 骞冲彴璇存槑锛屾偍鍙互鏇挎崲姝ょ畝浠** -Gitee 鏄 OSCHINA 鎺ㄥ嚭鐨勫熀浜 Git 鐨勪唬鐮佹墭绠″钩鍙帮紙鍚屾椂鏀寔 SVN锛夈備笓涓哄紑鍙戣呮彁渚涚ǔ瀹氥侀珮鏁堛佸畨鍏ㄧ殑浜戠杞欢寮鍙戝崗浣滃钩鍙 -鏃犺鏄釜浜恒佸洟闃熴佹垨鏄紒涓氾紝閮借兘澶熺敤 Gitee 瀹炵幇浠g爜鎵樼銆侀」鐩鐞嗐佸崗浣滃紑鍙戙備紒涓氶」鐩鐪 [https://gitee.com/enterprises](https://gitee.com/enterprises)} - -#### 杞欢鏋舵瀯 -杞欢鏋舵瀯璇存槑 - - -#### 瀹夎鏁欑▼ - -1. xxxx -2. xxxx -3. xxxx - -#### 浣跨敤璇存槑 - -1. xxxx -2. xxxx -3. xxxx - -#### 鍙備笌璐$尞 - -1. Fork 鏈粨搴 -2. 鏂板缓 Feat_xxx 鍒嗘敮 -3. 鎻愪氦浠g爜 -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 灏侀潰浜虹墿鏄竴妗g敤鏉ュ睍绀 Gitee 浼氬憳椋庨噰鐨勬爮鐩 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) +# SQLserver2 + +#### 浠嬬粛 +{**浠ヤ笅鏄 Gitee 骞冲彴璇存槑锛屾偍鍙互鏇挎崲姝ょ畝浠** +Gitee 鏄 OSCHINA 鎺ㄥ嚭鐨勫熀浜 Git 鐨勪唬鐮佹墭绠″钩鍙帮紙鍚屾椂鏀寔 SVN锛夈備笓涓哄紑鍙戣呮彁渚涚ǔ瀹氥侀珮鏁堛佸畨鍏ㄧ殑浜戠杞欢寮鍙戝崗浣滃钩鍙 +鏃犺鏄釜浜恒佸洟闃熴佹垨鏄紒涓氾紝閮借兘澶熺敤 Gitee 瀹炵幇浠g爜鎵樼銆侀」鐩鐞嗐佸崗浣滃紑鍙戙備紒涓氶」鐩鐪 [https://gitee.com/enterprises](https://gitee.com/enterprises)} + +#### 杞欢鏋舵瀯 +杞欢鏋舵瀯璇存槑 + + +#### 瀹夎鏁欑▼ + +1. xxxx +2. xxxx +3. xxxx + +#### 浣跨敤璇存槑 + +1. xxxx +2. xxxx +3. xxxx + +#### 鍙備笌璐$尞 + +1. Fork 鏈粨搴 +2. 鏂板缓 Feat_xxx 鍒嗘敮 +3. 鎻愪氦浠g爜 +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 灏侀潰浜虹墿鏄竴妗g敤鏉ュ睍绀 Gitee 浼氬憳椋庨噰鐨勬爮鐩 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)