diff --git "a/27\345\217\262\347\247\200\347\272\242/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/.png" "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/.png" new file mode 100644 index 0000000000000000000000000000000000000000..af4d71e24164e7650c8d6627e59c9aee419e536c Binary files /dev/null and "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/.png" differ diff --git "a/27\345\217\262\347\247\200\347\272\242/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/SQLQuery7.sql" "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/SQLQuery7.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8aa2c0fc84f5d08cc4a8afd380b57d0f457f90b5 --- /dev/null +++ "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/SQLQuery7.sql" @@ -0,0 +1,115 @@ +create database student +go +use student +go +create table Class( + ClassId int primary key identity,--班级编号 + ClassName nvarchar(20) --班级名称 +); +go +create table student( + studentId int primary key identity, --学生编号 + studentName nvarchar(50), --学生姓名 + studentSex tinyint not null, --学生性别 + studentBirth date, --出生日期 + studentAddress nvarchar(255) not null,--地址 + classId int --班级编号 +); +insert into Class(ClassName) +values ('软件一班'),('软件二班'),('计算机应用技术班') + +insert into student(studentName, studentSex, studentBirth, studentAddress, classId) +values ('刘正',1,'2000-01-01','广西省桂林市七星区空明西路10号鸾东小区',1), +('黄贵',1,'2001-03-20','江西省南昌市青山湖区艾溪湖南路南150米广阳小区',1), +('陈美',2,'2000-07-08','福建省龙岩市新罗区曹溪街道万达小区',1); +select *from student +select * from Class +insert into student(studentName, studentSex, studentBirth, studentAddress, classId) +values ('江文',1,'2000-08-10','安徽省合肥市庐阳区四里河路与潜山路交汇处万科城市之光',2), +( '钟琪',2,'2001-03-21','湖南省长沙市雨花区红花坡社区',2); + +insert into student(studentName, studentSex, studentBirth, studentAddress, classId) +values ('曾小林',1,'1999-12-10','安徽省合肥市庐阳区四里河路与潜山路交汇处万科城市之光',3), +('欧阳天天',2,'2000-04-05','湖北省武汉市洪山区友谊大道与二环线交汇处融侨悦府',3), +('徐长卿',1,'2001-01-30','江苏省苏州市苏州工业园区独墅湖社区',3), +('李逍遥',1,'1999-11-11','广东省广州市白云区金沙洲岛御洲三街恒大绿洲',3); + +insert into student(studentName, studentSex, studentBirth, studentAddress) +values ('东方不败',3,'1999-12-11' ,'河北省平定州西北四十余里的猩猩滩'), +('令狐冲',1 ,'2000-08-11', '陕西省渭南市华阴市玉泉路南段'); +select * from student +create table course ( +courseid int primary key identity , +coursename nvarchar(50) not null, +coutsecredit tinyint not null default 0 +); +create table classcourse( +classcourseid int primary key identity, +classid int references class(classid), +courseid int references course(courseid) +); +insert into course( coursename, coutsecredit) +values ('数据库高级应用',3), + ('javascript编程基础',3), + ('web前端程序设计基础',4), + ('动态网页设计.net基础',6) +select * from Class +select * from classcourse +select *from course +select * from student +insert into classcourse(classid, courseid) +values (1,1),(1,2),(1,3),(1,4), + (2,1),(2,2),(2,3),(2,4), + (3,1),(3,2),(3,3),(3,4) + +create table score ( +scoreid int primary key identity , +studentid int not null references student(studentid), +courseid int references course(courseid), +score int not null +); + +insert into score( studentid, courseid, score) +values (1,1,80),(1,2,78),(1,3,65),(1,4,90), +(2,1,60),(2,2,77),(2,3,68),(2,4,88), +(3,1,88),(3,2,45),(3,3,66),(3,4,75), +(4,1,56),(4,2,80),(4,3,75),(4,4,66), +(5,1,88),(5,2,79),(5,3,72),(5,4,85), +(6,1,68),(6,2,88),(6,3,73),(6,4,63), +(7,1,84),(7,2,90),(7,3,92),(7,4,78), +(8,1,58),(8,2,59),(8,3,65),(8,4,75), +(9,1,48),(9,2,67),(9,3,56),(9,4,48) +select * from course +select * from score +select * from Class +select * from student +select * from course +select * from classcourse +select * from score +select studentname as 姓名,地址=studentAddress from student +select top 20percent * from student +select distinct classid from student +select studentid as 学生编号 from score +select * from course order by coutsecredit desc +select * from score order by score asc +select * from student order by studentBirth desc +select *from student where classid=1 +select *from student where studentsex=2 +select * from student where studentBirth>'2000-01-01' and studentBirth<'2001-01-01'; +select * from student where studentName like '%欧阳%' +select * from student where studentAddress like '%桂林市%' +select * from Student where StudentName like '李__'; +select * from student where classId =1 +select * from classcourse where classid=1 +select sum(score) from score where studentid=1 +select * from score where courseid=1 +select sum(score)as 总分 ,count(0) as 学生总分 from score where courseid=1 +select avg(score) from Score where CourseId = 1; +select * from score; +select StudentId, sum(Score) as TotalScore from Score group by StudentId; +select * from score; +select StudentId, avg(Score) as 平均分 from Score group by StudentId; +select StudentId, avg(Score) as 平均分 from Score group by StudentId order by 平均分 desc; +select StudentId, avg(Score) as 平均分 from Score +group by StudentId +having avg(score)>80; \ No newline at end of file diff --git "a/27\345\217\262\347\247\200\347\272\242/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/2023-02-22_195620.png" "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/2023-02-22_195620.png" new file mode 100644 index 0000000000000000000000000000000000000000..38d982182657ed7f91aee49d644005e9d6aa42f3 Binary files /dev/null and "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/2023-02-22_195620.png" differ diff --git "a/27\345\217\262\347\247\200\347\272\242/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/SQLQuery2.sql" "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..050848c338eee1b2f5adc5b0fd6e85927edf6270 --- /dev/null +++ "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/SQLQuery2.sql" @@ -0,0 +1,33 @@ +create database zuoye1; +use zuoye1; +create table class ( +classid int primary key identity(1,1)not null,--班级编号 +classname nvarchar(50) not null,--班级名称 +); + +create table student( +studentid int primary key identity not null,--班级编号 +studentname nvarchar (50)not null, --学生姓名 +studentsex tinyint default 3 not null,--学生性别 +studentbirth date,--学生生日 +studentaddress nvarchar(255) not null,--学生地址 +classid int not null--所属班级id +); +create table course( +courseld int primary key identity(1,1) not null,--课程编号 +coursename nvarchar(50) not null,--课程名称 +coursecredit tinyint default 0 not null,--课程学分 +); + +create table classcourse( +classcourseid int primary key identity not null,--自增编号 +classid int not null,--班级编号 +courseid int not null,--课程编号 +); + +create table score( +scoreid int primary key identity not null,--自增编号 +studentid int not null,--学生编号 +courseid int not null,--课程编号 +score int not null,--分数 +); diff --git "a/27\345\217\262\347\247\200\347\272\242/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/SQLQuery.sql" "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/SQLQuery.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8d32da3996861041d93ed6c3d8115ca3767e69a5 --- /dev/null +++ "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/SQLQuery.sql" @@ -0,0 +1,125 @@ +create database Student; +go + +use Student; + +--学生信息表 +create table StuInFo( + stuNo nvarchar(25) primary key, + stuName nvarchar(15) not null, + stuAge tinyint not null, + stuAddress nvarchar(30) not null, + stuSeat int not null, + stuSex tinyint default 0 not null +); + +--插入学生信息 +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); + +--学生成绩表 +create table stuexam( + examNo int primary key identity, + stuNo nvarchar(25) references StuInFo(stuNo), + writtenExam int not null, + labExam int not null +) + +--插入学生成绩 +insert into stuexam(stuNo,writtenExam,labExam) + values('s2501',50,70), + ('s2502',60,65), + ('s2503',86,85), + ('s2504',40,80), + ('s2505',70,90), + ('s2506',85,90); + +--查询学生信息表(stuinfo)中所有列信息,给每列取上中文名称 +select stuName as 学生姓名,stuAge as 年龄,stuAddress as 住址,stuSeat as 机试,stuSex as 性别 from StuInFo; + +--查询学生信息表(stuinfo)中的姓名,年龄和地址三列的信息 +select stuName,stuAge,stuAddress from StuInFo; + +--查询学生分数表(stuexam)中的学号,笔试和机试三列的信息,并为这三列取中文名字 +select 学号=stuNo,writtenExam as 笔试,labExam 机试 from stuexam; + +--查询学生信息表(stuInfo)中的学号,姓名,地址,以及将:姓名+@+地址 组成新列 “邮箱”--查询学生信息表(stuInfo)中的学号,姓名,地址,以及将:姓名+@+地址 组成新列 “邮箱” +select 学号=stuNo,stuName as 学生姓名,stuAddress as 住址,stuName + '@' + stuAddress as 邮箱 from StuInFo; + +--查询学生分数表(stuexam)中的学生的学号,笔试,机试以及总分这四列的信息 +select stuNo,writtenExam,labExam,writtenExam+labExam as 总分 from stuexam; + +--查询学生信息表(stuInfo)中学生来自哪几个地方 +select distinct stuAddress from StuInFo + +--查询学生信息表(stuInfo)中学生有哪几种年龄,并为该列取对应的中文列名 +select distinct stuAge as 年龄 from StuInFo + +--查询学生信息表(stuInfo)中前3行记录 +select top 3 * from StuInFo + +--查询学生信息表(stuInfo)中前4个学生的姓名和座位号 +select top 4 stuName,stuSeat from StuInFo + +--查询学生信息表(stuInfo)中一半学生的信息 +select top 50 percent * from StuInFo + +--将地址是湖北武汉,年龄是20的学生的所有信息查询出来 +select * from StuInFo where stuAddress='湖北武汉' and stuAge=20 + +--将机试成绩在60-80之间的信息查询出来,并按照机试成绩降序排列(用两种方法实现) +select * from stuexam where labExam>=60 and labExam<=80 order by labExam desc +select * from stuexam where labExam between 60 and 80 order by labExam desc + +--查询来自湖北武汉或者湖南长沙的学生的所有信息(用两种方法实现) +select * from StuInFo where stuAddress='湖北武汉' or stuAddress='湖南长沙' +select * from StuInFo where stuAddress like '%湖%' --模糊查询 + +--查询出笔试成绩不在70-90之间的信息,并按照笔试成绩升序排列(用两种方法实现) +select * from stuexam where writtenExam not between 70 and 90 order by writtenExam asc +select * from stuexam where writtenExam<70 or writtenExam>90 order by writtenExam asc + +--查询年龄没有写的学生所有信息 +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 writtenExam desc + +--显示机试倒数第一名的学生的考试号,学号,笔试成绩和机试成绩 +select top 1 * from stuexam order by labExam asc + +--查询每个地方的学生的平均年龄 +select stuAddress,avg(stuAge) as 平均年龄 from StuInFo group by stuAddress + +--查询男女生的分别的年龄总和 +select stuSex,sum(stuAge) 年龄总和 from StuInfo group by StuSex + +--查询每个地方的男女生的平均年龄和年龄的总和 +select stuSex,avg(stuAge) 平均年龄,sum(stuAge) as 年龄总和 from StuInfo group by StuSex \ No newline at end of file diff --git "a/27\345\217\262\347\247\200\347\272\242/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.docx" "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.docx" new file mode 100644 index 0000000000000000000000000000000000000000..ac308564dac8d0471fac9f33008171f2beb4ec0d Binary files /dev/null and "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.docx" differ diff --git "a/27\345\217\262\347\247\200\347\272\242/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/SQLQuery9.sql" "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/SQLQuery9.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0c62a0020195546a4f4fba7e9da84a3a340d61b2 --- /dev/null +++ "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/SQLQuery9.sql" @@ -0,0 +1,180 @@ +----80_练习题 + +create database sxh; +use sxh; +create table Class( + ClassId int not null identity(1,1), + ClassName nvarchar(50) not null +); +go + +-- 创建学生表,存储学生信息,其中字段保护:学生id、姓名、性别、生日、家庭住址,所属班级id +create table Student ( + StudentId int not null identity(1, 1), + StudentName nvarchar(50), + StudentSex tinyint not null, + StudentBirth date, + StudentAddress nvarchar(255) not null, + Classid int +); +go + +-- 创建课程表,存储课程信息,其中字段包含:课程id、课程名称、课程学分 +create table Course( + CourseId int identity(1,1), + CourseName nvarchar(50), + CourseCredit int +); +go + +-- 创建班级课程表,存储班级课程信息,其中字段包含:自增id、班级id、课程id +create table ClassCourse( + ClassCourseId int identity(1,1), + ClassId int, + CourseId int +); +go + +-- 创建分数表,存储学生每个课程分数信息,其中字段包含:分数id、学生id、课程id、分数 +create table Score( + ScoreId int identity(1,1), + StudentId int, + CourseId int, + Score int +); +go +insert into class(classname) +values('软件一班'),('软件二班'),('计算机应用技术班') + + +insert into student(StudentName, StudentSex, StudentBirth, StudentAddress, Classid) +values ('刘正',1,'2000-01-01','广西省桂林市七星区空明西路10号鸾东小区',1), + ('黄贵',1,'2001-03-20','江西省南昌市青山湖区艾溪湖南路南150米广阳小区',1), + ('陈美',2,'2000-07-08','福建省龙岩市新罗区曹溪街道万达小区',1) + + + + +insert into student(studentname,studentsex,studentbirth,studentaddress,classid) +values('江文',1,'2000-08-10','安徽省合肥市庐阳区四里河路与潜山路交汇处万科城市之光',2), + ('钟琪',2,'2001-03-21','湖南省长沙市雨花区红花坡社区',2) + +insert into student(studentname,studentsex,studentbirth,studentaddress,classid) +values('曾小林',1,'1999-12-10','安徽省合肥市庐阳区四里河路与潜山路交汇处万科城市之光',3), + ('欧阳天天',2,'2000-04-05','湖北省武汉市洪山区友谊大道与二环线交汇处融侨悦府',3), + ('徐长卿',1,'2001-01-30','江苏省苏州市苏州工业园区独墅湖社区',3), + ('李逍遥',1,'1999-11-11','广东省广州市白云区金沙洲岛御洲三街恒大绿洲',3) + +insert into student(studentname,studentsex,studentbirth,studentaddress) +values('东方不败',3,'1999-12-11','河北省平定州西北四十余里的猩猩滩'), + ('令狐冲',1,' 2000-08-11',' 陕西省渭南市华阴市玉泉路南段') +insert into course(coursename,coursecredit) +values('数据库高级应用',3), + ('javascript编程基础',3), + ('web前端程序设计基础',4), + ('动态网页设计.net基础',6) + +insert into ClassCourse(classid,courseid)values(1,1),(1,2),(1,3),(1,4); +insert into classcourse(classid,courseid)values(2,1),(2,2),(2,3),(2,4); +insert into ClassCourse(classid,courseid)values(3,1),(3,2),(3,3),(3,4); +delete from ClassCourse where(ClassCourseId=1 or ClassCourseId=2 or ClassCourseId=3 or ClassCourseId=4) +--- 刘正、数据库高级应用、80 +--- 刘正、javascript编程基础、78 +--- 刘正、web前端程序设计基础、65 +--- 刘正、动态网页设计.net基础、90 +insert into score(studentid,courseid,Score)values(1,1,80); +insert into score(studentid,courseid,Score)values(1,2,78); +insert into score(studentid,courseid,Score)values(1,3,65); +insert into score(studentid,courseid,Score)values(1,4,90); +--- 黄贵、数据库高级应用、60 +--- 黄贵、javascript编程基础、77 +--- 黄贵、web前端程序设计基础、68 +--- 黄贵、动态网页设计.net基础、88 +--- 陈美、数据库高级应用、88 +--- 陈美、javascript编程基础、45 +--- 陈美、web前端程序设计基础、66 +--- 陈美、动态网页设计.net基础、75 + +insert into score(studentid ,CourseId,score)values(2,1,60); +insert into score(studentid ,CourseId,score)values(2,2,77); +insert into score(studentid ,CourseId,score)values(2,3,68); +insert into score(studentid ,CourseId,score)values(2,4,88); + +insert into score(studentid ,CourseId,score)values(3,1,88); +insert into score(studentid ,CourseId,score)values(3,2,45); +insert into score(studentid ,CourseId,score)values(3,3,66); +insert into score(studentid ,CourseId,score)values(3,4,75); + +--- 江文、数据库高级应用、56 +--- 江文、javascript编程基础、80 +--- 江文、web前端程序设计基础、75 +--- 江文、动态网页设计.net基础、66 +--- 钟琪、数据库高级应用、88 +--- 钟琪、javascript编程基础、79 +--- 钟琪、web前端程序设计基础、72 +--- 钟琪、动态网页设计.net基础、85 + +insert into score(studentid ,CourseId,score)values(4,1,56); +insert into score(studentid ,CourseId,score)values(4,2,80); +insert into score(studentid ,CourseId,score)values(4,3,75); +insert into score(studentid ,CourseId,score)values(4,4,66); +insert into score(studentid ,CourseId,score)values(5,1,88); +insert into score(studentid ,CourseId,score)values(5,2,79); +insert into score(studentid ,CourseId,score)values(5,3,72); +insert into score(studentid ,CourseId,score)values(5,4,85); +--- 曾小林、数据库高级应用、68 +--- 曾小林、javascript编程基础、88 +--- 曾小林、web前端程序设计基础、73 +--- 曾小林、动态网页设计php基础、63 +--- 欧阳天天、数据库高级应用、84 +--- 欧阳天天、javascript编程基础、90 +--- 欧阳天天、web前端程序设计基础、92 +--- 欧阳天天、动态网页设计php基础、78 + +insert into score(studentid ,CourseId,score)values(6,1,68); +insert into score(studentid ,CourseId,score)values(6,2,88); +insert into score(studentid ,CourseId,score)values(6,3,73); +insert into score(studentid ,CourseId,score)values(6,4,63); +insert into score(studentid ,CourseId,score)values(7,1,84); +insert into score(studentid ,CourseId,score)values(7,2,90); +insert into score(studentid ,CourseId,score)values(7,3,92); +insert into score(studentid ,CourseId,score)values(7,4,78); + +--- 徐长卿、数据库高级应用、58 +--- 徐长卿、javascript编程基础、59 +--- 徐长卿、web前端程序设计基础、65 +--- 徐长卿、动态网页设计php基础、75 +--- 李逍遥、数据库高级应用、48 +--- 李逍遥、javascript编程基础、67 +--- 李逍遥、web前端程序设计基础、71 +--- 李逍遥、动态网页设计.net基础、56 +--- 李逍遥、数据库高级应用、48 + +insert into score(studentid ,CourseId,score)values(8,1,58); +insert into score(studentid ,CourseId,score)values(8,2,59); +insert into score(studentid ,CourseId,score)values(8,3,65); +insert into score(studentid ,CourseId,score)values(8,4,75); +insert into score(studentid ,CourseId,score)values(9,1,48); +insert into score(studentid ,CourseId,score)values(9,2,67); +insert into score(studentid ,CourseId,score)values(9,3,71); +insert into score(studentid ,CourseId,score)values(9,4,56); +insert into score(studentid ,CourseId,score)values(9,1,48); + +delete from score where (scoreid=37); +update student set studentbirth='2000-04-06' where (studentid=7); +update score set score=61 where(scoreid=30); + + + + + + + + + +select * from class; +select * from student; +select * from course; +select * from ClassCourse; +select * from score; + diff --git "a/27\345\217\262\347\247\200\347\272\242/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232.sql" "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8d32da3996861041d93ed6c3d8115ca3767e69a5 --- /dev/null +++ "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232.sql" @@ -0,0 +1,125 @@ +create database Student; +go + +use Student; + +--学生信息表 +create table StuInFo( + stuNo nvarchar(25) primary key, + stuName nvarchar(15) not null, + stuAge tinyint not null, + stuAddress nvarchar(30) not null, + stuSeat int not null, + stuSex tinyint default 0 not null +); + +--插入学生信息 +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); + +--学生成绩表 +create table stuexam( + examNo int primary key identity, + stuNo nvarchar(25) references StuInFo(stuNo), + writtenExam int not null, + labExam int not null +) + +--插入学生成绩 +insert into stuexam(stuNo,writtenExam,labExam) + values('s2501',50,70), + ('s2502',60,65), + ('s2503',86,85), + ('s2504',40,80), + ('s2505',70,90), + ('s2506',85,90); + +--查询学生信息表(stuinfo)中所有列信息,给每列取上中文名称 +select stuName as 学生姓名,stuAge as 年龄,stuAddress as 住址,stuSeat as 机试,stuSex as 性别 from StuInFo; + +--查询学生信息表(stuinfo)中的姓名,年龄和地址三列的信息 +select stuName,stuAge,stuAddress from StuInFo; + +--查询学生分数表(stuexam)中的学号,笔试和机试三列的信息,并为这三列取中文名字 +select 学号=stuNo,writtenExam as 笔试,labExam 机试 from stuexam; + +--查询学生信息表(stuInfo)中的学号,姓名,地址,以及将:姓名+@+地址 组成新列 “邮箱”--查询学生信息表(stuInfo)中的学号,姓名,地址,以及将:姓名+@+地址 组成新列 “邮箱” +select 学号=stuNo,stuName as 学生姓名,stuAddress as 住址,stuName + '@' + stuAddress as 邮箱 from StuInFo; + +--查询学生分数表(stuexam)中的学生的学号,笔试,机试以及总分这四列的信息 +select stuNo,writtenExam,labExam,writtenExam+labExam as 总分 from stuexam; + +--查询学生信息表(stuInfo)中学生来自哪几个地方 +select distinct stuAddress from StuInFo + +--查询学生信息表(stuInfo)中学生有哪几种年龄,并为该列取对应的中文列名 +select distinct stuAge as 年龄 from StuInFo + +--查询学生信息表(stuInfo)中前3行记录 +select top 3 * from StuInFo + +--查询学生信息表(stuInfo)中前4个学生的姓名和座位号 +select top 4 stuName,stuSeat from StuInFo + +--查询学生信息表(stuInfo)中一半学生的信息 +select top 50 percent * from StuInFo + +--将地址是湖北武汉,年龄是20的学生的所有信息查询出来 +select * from StuInFo where stuAddress='湖北武汉' and stuAge=20 + +--将机试成绩在60-80之间的信息查询出来,并按照机试成绩降序排列(用两种方法实现) +select * from stuexam where labExam>=60 and labExam<=80 order by labExam desc +select * from stuexam where labExam between 60 and 80 order by labExam desc + +--查询来自湖北武汉或者湖南长沙的学生的所有信息(用两种方法实现) +select * from StuInFo where stuAddress='湖北武汉' or stuAddress='湖南长沙' +select * from StuInFo where stuAddress like '%湖%' --模糊查询 + +--查询出笔试成绩不在70-90之间的信息,并按照笔试成绩升序排列(用两种方法实现) +select * from stuexam where writtenExam not between 70 and 90 order by writtenExam asc +select * from stuexam where writtenExam<70 or writtenExam>90 order by writtenExam asc + +--查询年龄没有写的学生所有信息 +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 writtenExam desc + +--显示机试倒数第一名的学生的考试号,学号,笔试成绩和机试成绩 +select top 1 * from stuexam order by labExam asc + +--查询每个地方的学生的平均年龄 +select stuAddress,avg(stuAge) as 平均年龄 from StuInFo group by stuAddress + +--查询男女生的分别的年龄总和 +select stuSex,sum(stuAge) 年龄总和 from StuInfo group by StuSex + +--查询每个地方的男女生的平均年龄和年龄的总和 +select stuSex,avg(stuAge) 平均年龄,sum(stuAge) as 年龄总和 from StuInfo group by StuSex \ No newline at end of file diff --git "a/27\345\217\262\347\247\200\347\272\242/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/SQLQuery6.sql" "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/SQLQuery6.sql" new file mode 100644 index 0000000000000000000000000000000000000000..423eb9df0674ae80f39fd52ca84193fa746dfa15 --- /dev/null +++ "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/SQLQuery6.sql" @@ -0,0 +1,112 @@ +create database student8 +go +use student8 +go +create table Class( + ClassId int primary key identity,--班级编号 + ClassName nvarchar(20) --班级名称 +); +go +create table student( + studentId int primary key identity, --学生编号 + studentName nvarchar(50), --学生姓名 + studentSex tinyint not null, --学生性别 + studentBirth date, --出生日期 + studentAddress nvarchar(255) not null,--地址 + classId int --班级编号 +); +insert into Class(ClassName) +values ('软件一班'),('软件二班'),('计算机应用技术班') + +insert into student(studentName, studentSex, studentBirth, studentAddress, classId) +values ('刘正',1,'2000-01-01','广西省桂林市七星区空明西路10号鸾东小区',1), +('黄贵',1,'2001-03-20','江西省南昌市青山湖区艾溪湖南路南150米广阳小区',1), +('陈美',2,'2000-07-08','福建省龙岩市新罗区曹溪街道万达小区',1); +select *from student +select * from Class +insert into student(studentName, studentSex, studentBirth, studentAddress, classId) +values ('江文',1,'2000-08-10','安徽省合肥市庐阳区四里河路与潜山路交汇处万科城市之光',2), +( '钟琪',2,'2001-03-21','湖南省长沙市雨花区红花坡社区',2); + +insert into student(studentName, studentSex, studentBirth, studentAddress, classId) +values ('曾小林',1,'1999-12-10','安徽省合肥市庐阳区四里河路与潜山路交汇处万科城市之光',3), +('欧阳天天',2,'2000-04-05','湖北省武汉市洪山区友谊大道与二环线交汇处融侨悦府',3), +('徐长卿',1,'2001-01-30','江苏省苏州市苏州工业园区独墅湖社区',3), +('李逍遥',1,'1999-11-11','广东省广州市白云区金沙洲岛御洲三街恒大绿洲',3); + +insert into student(studentName, studentSex, studentBirth, studentAddress) +values ('东方不败',3,'1999-12-11' ,'河北省平定州西北四十余里的猩猩滩'), +('令狐冲',1 ,'2000-08-11', '陕西省渭南市华阴市玉泉路南段'); +select * from student +create table course ( +courseid int primary key identity , +coursename nvarchar(50) not null, +coutsecredit tinyint not null default 0 +); +create table classcourse( +classcourseid int primary key identity, +classid int references class(classid), +courseid int references course(courseid) +); +insert into course( coursename, coutsecredit) +values ('数据库高级应用',3), + ('javascript编程基础',3), + ('web前端程序设计基础',4), + ('动态网页设计.net基础',6) +select * from Class +select * from classcourse +select *from course +select * from student +insert into classcourse(classid, courseid) +values (1,1),(1,2),(1,3),(1,4), + (2,1),(2,2),(2,3),(2,4), + (3,1),(3,2),(3,3),(3,4) + +create table score ( +scoreid int primary key identity , +studentid int not null references student(studentid), +courseid int references course(courseid), +score int not null +); + +insert into score( studentid, courseid, score) +values (1,1,80),(1,2,78),(1,3,65),(1,4,90), +(2,1,60),(2,2,77),(2,3,68),(2,4,88), +(3,1,88),(3,2,45),(3,3,66),(3,4,75), +(4,1,56),(4,2,80),(4,3,75),(4,4,66), +(5,1,88),(5,2,79),(5,3,72),(5,4,85), +(6,1,68),(6,2,88),(6,3,73),(6,4,63), +(7,1,84),(7,2,90),(7,3,92),(7,4,78), +(8,1,58),(8,2,59),(8,3,65),(8,4,75), +(9,1,48),(9,2,67),(9,3,56),(9,4,48) +select * from course +select * from score +select * from Class +select * from student +select * from course +select * from classcourse +select * from score +select studentname as 姓名,地址=studentAddress from student +select top 20percent * from student +select distinct classid from student +select studentid as 学生编号 from score +select * from course order by coutsecredit desc +select * from score order by score asc +select * from student order by studentBirth desc +select *from student where classid=1 +select *from student where studentsex=2 +select * from student where studentBirth>'2000-01-01' and studentBirth<'2001-01-01'; +select * from student where studentName like '%欧阳%' +select * from student where studentAddress like '%桂林市%' +select * from Student where StudentName like '李__'; +select * from student where classId =1 +select * from classcourse where classid=1 +select sum(score) from score where studentid=1 +select * from score where courseid=1 +select sum(score)as 总分 ,count(0) as 学生总分 from score where courseid=1 +select avg(score) from Score where CourseId = 1; +select * from score; +select StudentId, sum(Score) as TotalScore from Score group by StudentId; +select * from score; +select StudentId, avg(Score) as TotalScore from Score group by StudentId; + diff --git "a/27\345\217\262\347\247\200\347\272\242/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\234\237\346\234\253\347\255\224\351\242\230\344\275\234\344\270\232.sql" "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\234\237\346\234\253\347\255\224\351\242\230\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..450265913aee3bc5701c0ff7ce1fa12768e6f620 --- /dev/null +++ "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\234\237\346\234\253\347\255\224\351\242\230\344\275\234\344\270\232.sql" @@ -0,0 +1,58 @@ +create database StarManager +go +use StarManager +go + +-- 2. 正确建立数据表结构并添加约束 +create table StarType( + STNO int primary key identity, + SType nvarchar(20) +) +go +create table StarInfo( + SNO int primary key identity, + SName nvarchar(20) not null, + SAge int not null, + SHobby nvarchar(20), + SNative nvarchar(20) default '中国大陆', + STNO int references StarType(STNO) +) +go + +-- 3. 正确为数据表添加数据 +insert into StarType(SType) + values('体育明星'), + ('IT明星'), + ('相声演员'), + ('影视明星'), + ('歌手') +go +insert into StarInfo(SName,SAge,SHobby,SNative,STNO) + values('梅西',36,'足球','阿根廷',1), + ('科比',35,'篮球','美国',1), + ('蔡景现',40,'敲代码','中国',2), + ('马斯克',36,'造火箭','美国',2), + ('郭德纲',50,'相声','中国',3), + ('黄铮',41,'拼多多','中国',2), + ('邓紫棋',33,'音乐','中国',5), + ('周杰伦',39,'创作','中国',5), + ('詹姆斯',34,'篮球','美国',1) +go + +-- 4. 查询年龄最大的3个明星的姓名、特技、籍贯信息和明星类型,要求使用别名显示列名(7分) +select top 3 SName 姓名,SHobby 特技,SNative 籍贯,STNO 明星类型 from StarInfo order by SAge desc + +-- 5. 按明星类型编号分类查询明星人数、明星平均年龄,显示明星人数小于2的分组信息,要求使用别名显示列名(10分) +select count(*) 明星人数,avg(SAge) 明星平均年龄 from StarInfo group by STNO having count(*)<2 + +-- 6. 查询明星类型为‘体育明星’中年纪最大的姓名,特技,籍贯信息,要求显示列别名(8分) +select top 1 SName 姓名,SHobby 特技,SNative 籍贯 from StarInfo where STNO=1 order by SAge desc + +-- 7. 查询名字是三个字,且最后一个字是“斯”字的明星。(8分) +select * from StarInfo where SName like '%斯' + +-- 8. 查询出年龄在25到40之间(包含25和40)的明星,按年龄逆序排序,要展示的字段:姓名、年龄、籍贯、明星类型。(10分) +select SName,SAge,SNative,STNO from StarInfo where SAge between 25 and 40 + +-- 9. 查询出籍贯是中国的明星的人数(包含中国大陆、中国香港、中国台湾)。(7分) +select * from StarInfo where SNative like '%中国%' diff --git "a/27\345\217\262\347\247\200\347\272\242/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\244\215\344\271\240\344\270\200.sql" "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\244\215\344\271\240\344\270\200.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b2ee61f12e9d73e7865f7ba9fa8b259dc8cebe3b --- /dev/null +++ "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\244\215\344\271\240\344\270\200.sql" @@ -0,0 +1,39 @@ +create database GoodsDB +go +use GoodsDB + +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 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(TypeName) + values('服装内衣'), + ('鞋包服饰'), + ('手机数码') + +insert 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 top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo order by GoodsMoney desc +--按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select 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/27\345\217\262\347\247\200\347\272\242/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\244\215\344\271\240\351\242\230\344\270\211.sql" "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\244\215\344\271\240\351\242\230\344\270\211.sql" new file mode 100644 index 0000000000000000000000000000000000000000..01f0b216853132333a010ba6a0b83ac4ae20363f --- /dev/null +++ "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\244\215\344\271\240\351\242\230\344\270\211.sql" @@ -0,0 +1,38 @@ +create database StarManagerDB +go +use StarManagerDB + +create table StarType( + T_NO int primary key identity not null, + T_NAME nvarchar(20) +) + +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) +) + +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) + +--查询数据 +--查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名 +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo order by S_AGE desc +--按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名 +select count(*) 明星人数,avg(S_AGE) 平均年龄 from StarInfo group by S_T_NO having count(*)>2 +--查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名 +select top 1 S_NAME 姓名,S_AGE 年龄,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo where S_T_NO=1 order by S_AGE desc \ No newline at end of file diff --git "a/27\345\217\262\347\247\200\347\272\242/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\244\215\344\271\240\351\242\230\344\272\214.sql" "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\244\215\344\271\240\351\242\230\344\272\214.sql" new file mode 100644 index 0000000000000000000000000000000000000000..23a857fe5d65f24f08e007a984356408b42301b0 --- /dev/null +++ "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\244\215\344\271\240\351\242\230\344\272\214.sql" @@ -0,0 +1,36 @@ +create database HOUSE_DB +go +use HOUSE_DB + +create table HOUSE_TYPE( + [type_id] int primary key identity not null, + [type_name] varchar(50) 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]) not null +) + +insert into HOUSE_TYPE(type_name) + values('小户型'), + ('经济型'), + ('别墅') + +insert into HOUSE(house_name,house_price,type_id) + values('房屋1',2000,1), + ('房屋2',3500,2), + ('房屋3',5500,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 house_name 房屋名称,[type_name] 房屋类型 from HOUSE + 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 order by max(house_price) desc \ No newline at end of file diff --git "a/27\345\217\262\347\247\200\347\272\242/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232.sql" "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fd49fe43b15144617fcbb7d9a374e799097eb593 --- /dev/null +++ "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232.sql" @@ -0,0 +1,127 @@ +create database bbs3 +go +use bbs3 + +--用户信息表(bbsUsers) +create table bbsUsers( + [UID] int primary key identity, + uName varchar(10) unique not null, + uSex varchar(2) not null check(uSex='男' or uSex='女'), + uAge int not null check(uAge between 15 and 60), + uPoint int not null check(uPoint>=0) +) + +--版块表(bbsSection) +create table bbsSection( + [sID] int primary key identity, + sName varchar(10) not null, + sUid int references bbsUsers(UID) +) + +--主贴表(bbsTopic) +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 +) + +--回帖表(bbsReply) +create table bbsReply( + rID int primary key identity, + rUID int references bbsUsers([UID]), + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +--插入数据 +insert into bbsUsers(uName,uSex,uAge,uPoint) + values('小雨点','女',20,0), + ('逍遥','男',18,4), + ('七年级生','男',19,2) + +select * from bbsUsers + +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-7-8',1), + (3,1,'.NET','与JAVA的区别是什么呀?','2008-9-1',2), + (1,3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?','2008-9-10',0) + +select * from bbsTopic + +--自定义回帖 +insert into bbsReply(rUID,rTID,rMsg,rTime) + values(1,1,'不知道','2008-7-9'), + (3,1,'不清楚','2008-9-2'), + (3,2,'不了解','2008-9-11') + +select * from bbsReply + +--删除“逍遥” 先删除引用主键的外键数据行 +delete from bbsReply where rTID=1 +delete from bbsTopic where tUID=2 +delete from bbsUsers where [UID]=2 +select * from bbsUsers + +--因为小雨点发帖较多,将其积分增加10分 +update bbsUsers set uPoint=uPoint+10 where [UID]=1 +select * from bbsUsers + +--删除板块“生活百科” +delete bbsTopic where tSID=3 +delete bbsSection where sID=3 +select * from bbsSection + +--将所有的回帖删除 +Delete from bbsReply where 1=1 + + +--查询练习 +--在主贴表中统计每个版块的发帖总数 +select tSID as 板块编号,count(*) as 发帖总数 from bbsTopic group by tSID + +--在回帖表中统计每个主贴的回帖总数量 +select rTID as 主贴编号,count(*) as 回帖数量 from bbsReply group by rTID + +--在主贴表中统计每个用户的发的主帖的总数 +select tUID as 发帖人编号,count(tID) as 发帖总数 from bbsTopic group by tUID + +--在主贴表中统计每个用户发的主贴的回复数量总和 +select tUID as 发帖人编号,sum(tCount) as 回复数量 from bbsTopic group by tUID + +--在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +select tsid as 板块编号,avg(tCount) as 平均回复数量 from bbsTopic + group by tSID + having avg(tCount)>3 + +--在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select top 1 uName,uSex,uAge,uPoint from bbsUsers + order by uPoint desc + +--在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select * from bbsTopic where tTitle like '%快乐%' or tMsg like '%快乐%' + +--在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +select * from bbsUsers where uAge between 15 and 20 and uPoint>10 + +--在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select * from bbsUsers where uName like '小%' or uName like '__大%' + +--在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 +select tTitle as 帖子标题, tMsg as 帖子内容 from bbsTopic where tTime>'2008-9-10 12:00:00 ' and tCount>10 + +--在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select * from bbsTopic where tTitle like '%!' \ No newline at end of file diff --git "a/27\345\217\262\347\247\200\347\272\242/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/SQLQuery4.sql" "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a59df8da8e67691f4f7ef351ac6f8e99861bab5c --- /dev/null +++ "b/27\345\217\262\347\247\200\347\272\242/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/SQLQuery4.sql" @@ -0,0 +1,120 @@ +create database czy; +go +use czy; +go +create table class1( +classid int identity not null primary key,--班级编号 +classname nvarchar(50) not null ,--班级名称 +); + +create table student1( +studentid int identity not null primary key,--学生编号 +studentname nvarchar(50) not null,--学生姓名 +studentsex tinyint not null default 3,--学生性别 +studentbirth date,--学生生日 +studentaddress nvarchar(255) not null,--学生地址 +classid int not null --所属班级id +); + + +create table course1( +courseid int identity not null primary key,--课程编导 +coursename nvarchar(50) not null,--课程名称 +coursecredit tinyint not null default 0--课程学分 +); +alter table course1 add classid nvarchar; +alter table course1 drop column classid; + +create table classcourse1( +classcourseid int identity not null primary key ,--自增编号 +classid int not null,--班级编号 +courseid int not null --课程编号 +); + + +create table score1( +scoreid int identity not null primary key ,--自增编号 +studentid int not null,--学生编号 +courseid int not null,--课程编号 +score int not null --分数 +); + +insert into class1(classname) + values ('软件技术一班'),('软件技术二班'),('计算机应用技术班'); + + select * from class1; + + insert into student1(studentname,studentsex,studentbirth,studentaddress,classid) + values ('刘正',1,'2000-01-01','广西省桂林市七星区空明西路10号鸾东小区',1), + ('黄贵',1,'2001-03-20','江西省南昌市青山湖区艾溪湖南路南150米广阳小区',1), + ('陈美',2,'2000-07-08','福建省龙岩市新罗区曹溪街道万达小区',1); + insert into student1(studentname,studentsex,studentbirth,studentaddress,classid) + values ('江文',1,'2000-08-10','安徽省合肥市庐阳区四里河路与潜山路交汇处万科城市之光',2), + ('钟琪',2,'2001-03-21','湖南省长沙市雨花区红花坡社区',2); +insert into student1 (studentname,studentsex,studentbirth,studentaddress,classid) +values ('曾小林',1,'1999-12-10','安徽省合肥市庐阳区四里河路与潜山路交汇处万科城市之光',3), + ('欧阳天天',2,'2000-04-05','湖北省武汉市洪山区友谊大道与二环线交汇处融侨悦府',3), + ('徐长卿',1,'2001-01-30','江苏省苏州市苏州工业园区独墅湖社区',3), + ('李逍遥',1,'1999-11-11','广东省广州市白云区金沙洲岛御洲三街恒大绿洲',3); +insert into student1 (studentname,studentsex,studentbirth,studentaddress,classid) +values ('东方不败',3,'1999-12-11','河北省平定州西北四十余里的猩猩滩',0), + ('令狐冲',1,'2000-08-11','陕西省渭南市华阴市玉泉路南段',0); + +select * from student1; + +insert into course1(coursename,coursecredit) +values ('数据库应用高级','3'), + (' javascript编程基础',3), + (' web前端程序设计基础',4), + ('动态网页设计.net基础',6); + +select * from course1; + +insert into classcourse1(classid, courseid) +values (1,1),(1,2),(1,3),(1,4), + (2,1),(2,2),(2,3),(2,4), + (3,1),(3,2),(3,3),(3,4); + +select * from classcourse1; + +insert into score1(studentid, courseid ,score ) +values ('1','1', 80), + ('1','2', 78), + ('1','3', 65), + ('1',' 4', 90), + ('2','1', 60), + ('2',' 2', 77), + ('2',' 3', 68), + ('2',' 4',88), + ('3',' 1', 88), + ('3',' 2', 45), + ('3',' 3', 66), + ('3',' 4',75), + ('4',' 1', 56), + ('4',' 2', 80), + ('4','3', 75), + ('4',' 4',66), + ('5','1', 88), + ('5',' 2', 79), + ('5','3', 72), + ('5',' 4',85), + ('6',' 1',68), + ('6','2',88), + ('6','3',73), + ('6','4',63), + ('7',' 1',84), + ('7','2',90), + ('7','3',92), + ('7','4',78), + ('8',' 1',58), + ('8',' 2',59), + ('8',' 3',65), + ('8',' 4',75), + ('8',' 1',48), + ('9',' 2',67), + ('9',' 3',71), + ('9',' 4',56), + ('9',' 1',48); + + +select * from score1; \ No newline at end of file diff --git "a/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/SQLQuery1.sql" "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..80467118ff0f88e8b909f4208cef40eef42423d7 --- /dev/null +++ "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/SQLQuery1.sql" @@ -0,0 +1,53 @@ + +use sxh; +create table stuinfo( +studentid varchar(10) primary key, +studentname nvarchar(10) not null, +studentage tinyint , +studentaddress nvarchar(30), +studentseat int identity, +studentsex int +); + +create table eaxm( +examid int primary key identity, +studentid varchar(10), +writtenexam tinyint, +labexam int +); +exec sp_rename 'eaxm','exam'; + +select * from stuinfo; +insert into stuinfo(studentid,studentname,studentage,studentaddress,studentsex) +values ('s2501','张秋利',20,'美国硅谷',1) +insert into stuinfo(studentid,studentname,studentage,studentaddress,studentsex) +values ('s2502','李斯文',18,'湖北武汉',0) +insert into stuinfo(studentid,studentname,studentage,studentaddress,studentsex) +values ('s2503','马文才',22,'湖南长沙',1) +insert into stuinfo(studentid,studentname,studentage,studentaddress,studentsex) +values ('s2504','欧阳俊雄',21,'湖北武汉',0) +insert into stuinfo(studentid,studentname,studentage,studentaddress,studentsex) +values ('s2505','梅超风',20,'湖北武汉',1) +insert into stuinfo(studentid,studentname,studentage,studentaddress,studentsex) +values ('s2506','陈旋风',19,'美国硅谷',1) +insert into stuinfo(studentid,studentname,studentage,studentaddress,studentsex) +values ('s2507','陈风',20,'美国硅谷',0) + +select * from exam; +insert into exam(studentid,writtenexam,labexam) +values('s2501',50,70); +insert into exam(studentid,writtenexam,labexam) +values('s2502',60,65); +insert into exam(studentid,writtenexam,labexam) +values('s2503',86,85); +insert into exam(studentid,writtenexam,labexam) +values('s2504',40,80); +insert into exam(studentid,writtenexam,labexam) +values('s2505',70,90); +insert into exam(studentid,writtenexam,labexam) +values('s2506',85,90); + +select * from stuinfo; +select studentid 学生学号,studentname 学生姓名,studentage 学生年龄,studentaddress 学生地址,studentseat 学生座位号,studentsex 学生性别 from stuinfo; +select studentid,studentage,studentaddress from stuinfo; +select studentid,written diff --git "a/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/where\345\255\220\345\217\245.sql" "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/where\345\255\220\345\217\245.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a19dd11d546a68f5dabc22e7fc6c4d720a3c81bc --- /dev/null +++ "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/where\345\255\220\345\217\245.sql" @@ -0,0 +1,78 @@ +--查询 +-- select [top n percent] | [distinct] 列名,列名,....... +-- from 表名,..... +-- where 条件表达式 + + + +--eg:查询所有的学生信息 +select * from StuInfo; + +--eg:查询前10条的学生数据 +select top 10 * from StuInfo; + + +--eg:查询出哪些班级有学生信息 +select distinct classid from StuInfo; + + + +--列名之间可以进行运算 +select *,StuName+'@'+StuAddress as 邮箱 from StuInfo; + + + + +--where 条件子句:对查询结果进行条件筛选,只有记录符合where条件表达式才返回到结果集中 +-- 条件表达式:结果必须是布尔型,字段名和运算符 +-- 运算符: +-- 1)比较运算符:> >= < <= <> != + +--eg:查询考试成绩不及格的成绩信息 +select * from Scores where score<60; + + +-- 2)逻辑运算符:and(与) 、 or(或者) 、not(非) +--eg:查询课程编号为1并且成绩不及格的成绩信息 +select * from Scores where score<60 and CourseId=1; + +--eg:查询班级编号为1和2的学生信息 +select * from StuInfo where ClassId=1 or classId=2; + +--eg:查询班级编号不为1和2的学生信息 +select * from StuInfo where not(ClassId=1 or classId=2); + + +-- 3)范围运算符:between A and B :值在[A,B]范围内;not between A and B :值不在[A,B]范围内 + +--eg:查询出生年份在2001的学生信息,2001-01-01 2001-12-31 +select * from StuInfo where StuBrithday>= '2001-01-01' and StuBrithday<='2001-12-31'; + +select * from StuInfo where StuBrithday between '2001-01-01' and '2001-12-31'; + +select * from StuInfo where year(StuBrithday) =2001 + +--eg:查询出生年份不在2001的学生信息 +select * from StuInfo where StuBrithday not between '2001-01-01' and '2001-12-31' + +-- 4)列表运算符:in(值1,值2,....) not in(值1,值2,....) +-- eg:查询班级编号为1,2,3的学生信息 +select * from StuInfo where classid=1 or ClassId=2 or ClassId=3 + +select * from StuInfo where ClassId in(1,2,3); + +-- eg:查询班级编号不为1,2,3的学生信息 + +select * from StuInfo where ClassId not in(1,2,3); + +-- 5)空值运算符:is null ,not is null +-- NULL代表空值,代表该字段在插入数据时未赋值,并未分配内存地址空间 +-- ''代表空字符串,有存储空间,但是没有字符 +--eg:查询地址为NULL的学生信息 +select * from StuInfo where StuAddress is null; + +--eg:查询地址信息为空(没有数据)的学生信息 +select * from StuInfo where StuAddress is null or StuAddress=''; + + + diff --git "a/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/\346\237\245\350\257\242\346\274\224\347\244\272\346\225\260\346\215\256\345\272\223.sql" "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/\346\237\245\350\257\242\346\274\224\347\244\272\346\225\260\346\215\256\345\272\223.sql" new file mode 100644 index 0000000000000000000000000000000000000000..da8f22ce4cdc9da9f28fe5642d719e161d4150f5 --- /dev/null +++ "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/\346\237\245\350\257\242\346\274\224\347\244\272\346\225\260\346\215\256\345\272\223.sql" @@ -0,0 +1,194 @@ +--使用master数据库 +use master +go + +--如果存在TestDB数据库删除数据库 +if exists(select * from sys.databases where name='TestDB') + drop database TestDB; +go + +--创建数据库 +create database TestDB +go +--go批处理标志,分割sql文件;等待前面语句执行完毕再执行后面的sql语句 + +--使用TestDB数据库 +use TestDB +go + + +----------------- +--建表部分 +----------------- + +--创建班级表 +create table ClassInfo +( + ClassId int identity(1,1) primary key, --主键,班级编号,标识列 + ClassName nvarchar(20) not null --班级名称,非空 +) +go + +--创建学生信息表 +create table StuInfo +( + StuId int identity(1,1) primary key, --主键,学号,标识列 + ClassId int references ClassInfo(ClassId) on delete set null, --所属班级编号,外键关联班级表的班级编号 + StuName nvarchar(10) not null, --姓名,非空 + StuSex nvarchar(1) default('男') check(StuSex in('男','女')), --性别 + StuBrithday date, --出生日期 + StuPhone nvarchar(11) check(len(StuPhone)=11) unique,--手机号,限制11位,唯一不重复 + StuAddress nvarchar(200),--地址 + CreateDate datetime default(getdate()) --创建时间,默认为系统时间 +) +go + +--创建课程信息表 +create table CourseInfo +( + CourseId int identity(1,1) primary key, --课程编号,主键,标识列 + CourseName nvarchar(50) unique not null ,--课程名称,非空,唯一不重复 + CourseCredit int default(1) check(CourseCredit between 1 and 5) --学分,默认值为1,取值范围1-5 +) +go + +--创建成绩表 +create table Scores +( + ScoreId int identity(1,1) primary key,--成绩编号,主键,标识列 + StuId int references StuInfo(StuId),--学号,外键关联学生信息表的学号 + CourseId int references CourseInfo(CourseId),--课程编号,外键关联课程信息表的课程编号 + Score int default(0) --成绩,默认为0 +) +go + + + +---------------- +--插入数据部分 +---------------- +--插入班级信息表 +insert into ClassInfo(ClassName) +values('软件1班'),('软件2班'),('软件3班'),('软件4班'),('软件5班'),('软件6班'),('软件7班') +go + + +--插入学生信息 +insert into StuInfo(ClassId,StuName,StuSex,StuBrithday,StuPhone,StuAddress) +values(1,'刘正','男','2002-08-02','13245678121','广西省桂林市七星区空明西路10号鸾东小区') +insert into StuInfo(ClassId,StuName,StuSex,StuBrithday,StuPhone,StuAddress) +values(1,'黄贵','男','2003-07-02','13345678121','江西省南昌市青山湖区艾溪湖南路南150米广阳小区') +insert into StuInfo(ClassId,StuName,StuSex,StuBrithday,StuPhone,StuAddress) +values(1,'陈美','女','2002-07-22','13355678125','福建省龙岩市新罗区曹溪街道万达小区') + +insert into StuInfo(ClassId,StuName,StuSex,StuBrithday,StuPhone,StuAddress) +values(2,'江文','男','2001-07-02','13347678181','湖南省长沙市雨花区红花坡社区') +insert into StuInfo(ClassId,StuName,StuSex,StuBrithday,StuPhone,StuAddress) +values(2,'钟琪','女','2003-01-13','13345778129','安徽省合肥市庐阳区四里河路与潜山路交汇处万科城市之光') + + +insert into StuInfo(ClassId,StuName,StuSex,StuBrithday,StuPhone,StuAddress) +values(3,'曾小林','男','2003-05-15','13345378563','安徽省合肥市庐阳区四里河路与潜山路交汇处万科城市之光') +insert into StuInfo(ClassId,StuName,StuSex,StuBrithday,StuPhone,StuAddress) +values(3,'欧阳天天','女','2002-08-19','13347878121','湖北省武汉市洪山区友谊大道与二环线交汇处融侨悦府') +insert into StuInfo(ClassId,StuName,StuSex,StuBrithday,StuPhone,StuAddress) +values(3,'李逍遥','男','2003-09-02','13345678557','广东省广州市白云区金沙洲岛御洲三街恒大绿洲') + + +insert into StuInfo(ClassId,StuName,StuSex,StuBrithday,StuPhone,StuAddress) +values(4,'刘德华','男','2003-06-11','15345679557',null) +insert into StuInfo(ClassId,StuName,StuSex,StuBrithday,StuPhone,StuAddress) +values(4,'刘翔','男','2003-07-09','18346679589',null) +insert into StuInfo(ClassId,StuName,StuSex,StuBrithday,StuPhone,StuAddress) +values(4,'曾小贤','男','2003-07-02','18348979589',null) + + +insert into StuInfo(ClassId,StuName,StuSex,StuBrithday,StuPhone,StuAddress) +values(5,'刘','男','2002-07-02','18348979509',null) +insert into StuInfo(ClassId,StuName,StuSex,StuBrithday,StuPhone,StuAddress) +values(5,'陈天翔','男','2002-07-02','18348079509',null) +insert into StuInfo(ClassId,StuName,StuSex,StuBrithday,StuPhone,StuAddress) +values(5,'刘能','男','2002-08-02','13245678122','广西省桂林市七星区空明西路10号鸾东小区') +insert into StuInfo(ClassId,StuName,StuSex,StuBrithday,StuPhone,StuAddress) +values(5,'钟馗','男','2002-08-02','13245678123','广西省桂林市七星区空明西路10号鸾东小区') +insert into StuInfo(ClassId,StuName,StuSex,StuBrithday,StuPhone,StuAddress) +values(5,'钟吴艳','女','2002-08-02','13245678124','广西省桂林市七星区空明西路10号鸾东小区') + +insert into StuInfo(ClassId,StuName,StuSex,StuBrithday,StuPhone,StuAddress) +values(5,'刘欢','男','2002-07-02','13245678125',null) +insert into StuInfo(ClassId,StuName,StuSex,StuBrithday,StuPhone,StuAddress) +values(5,'张庭','女','2002-07-02','13245678126',null) +insert into StuInfo(ClassId,StuName,StuSex,StuBrithday,StuPhone,StuAddress) +values(5,'曹植','男','2002-08-02','13245678127','') +insert into StuInfo(ClassId,StuName,StuSex,StuBrithday,StuPhone,StuAddress) +values(5,'曹操','男','2002-08-02','13245678128','') +insert into StuInfo(ClassId,StuName,StuSex,StuBrithday,StuPhone,StuAddress) +values(5,'孙尚香','女','2002-08-02','13245678129','') +go + + + +--插入课程信息 +insert into CourseInfo(CourseName,CourseCredit) values('计算机基础',3) +insert into CourseInfo(CourseName,CourseCredit) values('HTML+CSS网页制作',5) +insert into CourseInfo(CourseName,CourseCredit) values('JAVA编程基础',5) +insert into CourseInfo(CourseName,CourseCredit) values('SQL Server数据库基础',4) +insert into CourseInfo(CourseName,CourseCredit) values('C#面向对象编程',5) +insert into CourseInfo(CourseName,CourseCredit) values('Winform桌面应用程序设计',5) +go + +--插入成绩信息 +insert into Scores (StuId, CourseId, Score) values (1, 1, 80); +insert into Scores (StuId, CourseId, Score) values (1, 2, 78); +insert into Scores (StuId, CourseId, Score) values (1, 3, 65); +insert into Scores (StuId, CourseId, Score) values (1, 4, 90); + +insert into Scores (StuId, CourseId, Score) values (2, 1, 60); +insert into Scores (StuId, CourseId, Score) values (2, 2, 77); +insert into Scores (StuId, CourseId, Score) values (2, 3, 68); +insert into Scores (StuId, CourseId, Score) values (2, 4, 88); + +insert into Scores (StuId, CourseId, Score) values (3, 1, 88); +insert into Scores (StuId, CourseId, Score) values (3, 2, 45); +insert into Scores (StuId, CourseId, Score) values (3, 3, 66); +insert into Scores (StuId, CourseId, Score) values (3, 4, 75); + +insert into Scores (StuId, CourseId, Score) values (4, 1, 56); +insert into Scores (StuId, CourseId, Score) values (4, 2, 80); +insert into Scores (StuId, CourseId, Score) values (4, 3, 75); +insert into Scores (StuId, CourseId, Score) values (4, 4, 66); + +insert into Scores (StuId, CourseId, Score) values (5, 1, 88); +insert into Scores (StuId, CourseId, Score) values (5, 2, 79); +insert into Scores (StuId, CourseId, Score) values (5, 3, 72); +insert into Scores (StuId, CourseId, Score) values (5, 4, 85); + +insert into Scores (StuId, CourseId, Score) values (6, 1, 68); +insert into Scores (StuId, CourseId, Score) values (6, 2, 88); +insert into Scores (StuId, CourseId, Score) values (6, 3, 73); +insert into Scores (StuId, CourseId, Score) values (6, 5, 63); + +insert into Scores (StuId, CourseId, Score) values (7, 1, 84); +insert into Scores (StuId, CourseId, Score) values (7, 2, 90); +insert into Scores (StuId, CourseId, Score) values (7, 3, 92); +insert into Scores (StuId, CourseId, Score) values (7, 5, 78); + +insert into Scores (StuId, CourseId, Score) values (8, 1, 58); +insert into Scores (StuId, CourseId, Score) values (8, 2, 59); +insert into Scores (StuId, CourseId, Score) values (8, 3, 65); +insert into Scores (StuId, CourseId, Score) values (8, 5, 75); + +insert into Scores (StuId, CourseId, Score) values (9, 1, 48); +insert into Scores (StuId, CourseId, Score) values (9, 2, 67); +insert into Scores (StuId, CourseId, Score) values (9, 3, 71); +insert into Scores (StuId, CourseId, Score) values (9, 5, 56); +insert into Scores (StuId, CourseId, Score) values (9, 5, 56); +go + + + + +select * from ClassInfo; +select * from StuInfo; +select * from Scores; +select * from CourseInfo;