diff --git "a/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/lenovo20230217214915.png" "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/lenovo20230217214915.png" new file mode 100644 index 0000000000000000000000000000000000000000..2a1af1b26fadda23a2bb3ef3da8453f12934f988 Binary files /dev/null and "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/lenovo20230217214915.png" differ diff --git "a/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/\347\273\203\344\271\240.sql" "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/\347\273\203\344\271\240.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7f037db669965bd3556dc54badf1032c14e72760 --- /dev/null +++ "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/\347\273\203\344\271\240.sql" @@ -0,0 +1,172 @@ +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),(9,1,48) +select * from score + +delete from score where scoreid=37 + +update student set studentBirth='2000-04-06' +where studentId=7 + +update score set score=61 +where scoreid=30 + +update course set coutsecredit=coutsecredit+1 + +--查询所有的班级 Class 信息。 +select * from Class + +-- 查询所有的学生 Student 信息。 +select * from student + +-- 查询所有的课程 Course 信息。 +select * from course + +-- 查询所有的班级课程 ClassCourse 信息。 +select * from ClassCourse + +-- 查询所有的分数 Score 信息。 +select * from score + +-- 只查询学生的姓名和地址信息。 +select studentName,studentAddress from student + +-- 查询获取学生信息表中20%的学生信息。 +select top 20 percent * from student + +-- 查询学生信息表中的班级编号有哪些(去除重复值)。 +select distinct classid from student + +-- 查询分数表中有成绩的学生编号; +select studentid from score + +-- 查询所有的课程 Course 信息,并且按照学分从大到小排列。 +select * from course order by coutsecredit + +-- 查询所有的分数 Score 信息,并且按照分数从小到大排列。 +select * from score order by score + +-- 查询所有的学生 Student 信息,并且按照年龄从小到大排列。 +select * from student order by studentBirth desc + +-- 查询软件一班所有的学生信息,显示学生id,姓名、性别、生日、住址。 +select * from student where classid= + +-- 查询所有的女生,显示学生id,姓名、性别、生日、住址。 +select * from student where studentSex=2 + +-- 查询2000年出生的所有学生,显示学生id,姓名、性别、生日、住址。 +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 count(0) from Student where ClassId = 1 + +-- 查询 软件一班 有几门课程。 +select count(0) from classcourse where classid=1 + +-- 查询 刘正(学生编号为1) 的总分。 +select sum(score) from score where studentid=1 + +-- 查询所有学生 数据库高级应用 的平均分。 +select avg(score) 平均分 from score where courseid=1 + +-- 查询 所有学生 的总分。 +select studentid, sum(score) 总分 from score group by studentid + +-- 查询 所有学生 的平均分。 +select studentid,avg(score) 平均分 from score group by studentid + +-- 查询 所有学生 的平均分,并且按照平均分从高到低排列。 +select studentid,avg(score) 平均分 from score group by studentid order by 平均分 desc + +-- 查询 所有学生中 平均分 大于80分的学生。(聚合查询 + 分组 + having) +select StudentId, avg(Score) as 平均分 from Score +group by StudentId +having avg(score)>80 \ No newline at end of file diff --git "a/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/2023-02-20_194527.png" "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/2023-02-20_194527.png" new file mode 100644 index 0000000000000000000000000000000000000000..88410b0941df19d4685db4b59c048466a0af84b0 Binary files /dev/null and "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/2023-02-20_194527.png" differ diff --git "a/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..51287b316cca47691949e06d6ae1e8f5028cd2b6 --- /dev/null +++ "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,36 @@ +create database bb +use bb + + +create table Class( +Classld int, --班级编号 +ClassName nvarchar(50)--班级名称 +) + +create table Student( +Studentld int primary key identity(1,1),--班级编号 +StudentName nvarchar(50) not null,--学生姓名 +StudentSex tinyint,--学生性别 +StudentBirth date,--学生生日 +StudentAddress nvarchar(255) not null,--学生地址 +Classld int not null --所属班级ID +) + +create table Course( +Courseld int identity(1,1) primary key,--课程编号 +CourseName nvarchar(50) not null,--课程名称 +CourseCredit tinyint default 0 not null --课程学分 +) + +create table ClassCourse( +ClassCourseld int identity primary key,--自增编号 +Classld int not null,--班级编号 +Courseld int not null--课程编号 +) + +create table Score( +Scoreld int primary key identity(1,1),--自增编号 +Studentld int not null,--学生编号 +Courseld int not null,--课程编号 +Score int not null--分数 +) \ No newline at end of file diff --git "a/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\2322.sql" "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\2322.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9dbb01777f45adf2c95edc723100bb44df041c7a --- /dev/null +++ "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\2322.sql" @@ -0,0 +1,62 @@ +create database orderbb +use orderbb +create table orders( +orderLd int primary key identity, +orderDate nvarchar(255) +) + +create table orderltem( +ltemid int identity, +orderid int, +itemType nvarchar(255) not null, +itemName nvarchar(255) not null, +theNumber int not null, +theMoney int not null +) + +insert into orders(orderDate) +values('2008-01-12 00:00:00.000'), +('2008-02-10 00:00:00.000'), +('2008-02-15 00:00:00.000'), +('2008-03-10 00:00:00.000') + +insert into orderltem(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 sum(theNumber) 物品数量总和 from orderltem + +select orderid,sum(thenumber),avg(themoney) from orderltem +where orderid<3 +group by orderid +having avg(themoney)<10 + +select orderid,sum(thenumber),avg(themoney) from orderltem +group by orderid +having avg(themoney)<10 and sum(theNumber)>50 + +select itemtype,count(*) 订购次数 from orderltem +group by itemtype +order by count(*) desc + +select itemtype,sum(thenumber) 订购数量,avg(themoney) 平均单价 from orderltem +group by itemtype +having sum(thenumber)>100 + +select itemname,count(*) 订购次数,sum(thenumber) 总数量,avg(themoney) 平均单价 from orderltem group by itemName + +select * from orders +select * from orderltem \ No newline at end of file diff --git "a/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/21\350\242\201\350\250\200\350\250\200.docx" "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/21\350\242\201\350\250\200\350\250\200.docx" new file mode 100644 index 0000000000000000000000000000000000000000..3a15f1b906280e9bdabb1653d1e9bf46df27781a Binary files /dev/null and "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/21\350\242\201\350\250\200\350\250\200.docx" differ diff --git "a/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/SQLQuery3.sql" "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..645eaafd7b62b736dbf5ff1f7377cd51164c17b3 --- /dev/null +++ "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/SQLQuery3.sql" @@ -0,0 +1,82 @@ +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),(9,1,48) +select * from score \ No newline at end of file diff --git "a/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\346\210\252\345\233\276_20230226191726.png" "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\346\210\252\345\233\276_20230226191726.png" new file mode 100644 index 0000000000000000000000000000000000000000..a7c421b85fea709183c85f7b8ad1c4b3941c9403 Binary files /dev/null and "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\346\210\252\345\233\276_20230226191726.png" differ diff --git "a/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\346\210\252\345\233\276_20230226191822.png" "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\346\210\252\345\233\276_20230226191822.png" new file mode 100644 index 0000000000000000000000000000000000000000..419abac5a281b0ded21cf6f01c5b981ff40d4ccf Binary files /dev/null and "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\346\210\252\345\233\276_20230226191822.png" differ diff --git "a/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\346\210\252\345\233\276_20230226191858.png" "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\346\210\252\345\233\276_20230226191858.png" new file mode 100644 index 0000000000000000000000000000000000000000..ad5b9a9b06d562b22d3c125c12b0713a4300f23f Binary files /dev/null and "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\346\210\252\345\233\276_20230226191858.png" differ diff --git "a/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\2321(1).sql" "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\2321(1).sql" new file mode 100644 index 0000000000000000000000000000000000000000..2a1e5a3b5580d509b1f998fdb76a9e56a0b4c18d --- /dev/null +++ "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\2321(1).sql" @@ -0,0 +1,125 @@ +create database student6 +go +use student6 + +create table student ( +stuno nvarchar(10) primary key, +stuname nvarchar(20) not null, +stuage int not null, +stuaddress nvarchar(20) not null, +stuseat int identity, +stusex nvarchar(1) not null +); + +insert into student(stuno, stuname, stuage, stuaddress, stusex) +values('s2501','张秋利',20,'美国硅谷',1) +insert into student(stuno, stuname, stuage, stuaddress, stusex) +values('s2502','李斯文',18,'湖北武汉',0), +('s2503','马文才',22,'湖南长沙',1), +('s2504','欧阳俊雄',21,'湖北武汉',0), +('s2505','梅超风',20,'湖北武汉',1), +('s2506','陈旋风',19,'美国硅谷',1), +('s2507','陈风',20,'美国硅谷',0) + +create table exam( +examno int primary key identity, +stuno nvarchar(10) references student (stuno), +writtenexam int not null, +labexam int not null +); + +insert into exam(stuno,writtenexam,labexam) +values('s2501',50,70), +('s2502',60,65), +('s2503',86,85), +('s2504',40,80), +('s2505',70,90), +('s2506',85,90) +--1.查询学生信息表(stuinfo)中所有列信息,给每列取上中文名称 +select stuno as 学生编号, stuname as 学生名, stuage as 学生年龄, stuaddress as 学生地址, stuseat as 学生座位, stusex as 性别 from student + +--2.查询学生信息表(stuinfo)中的姓名,年龄和地址三列的信息 +select stuname,stuage,stuaddress from student + +--3.查询学生分数表(stuexam)中的学号,笔试和机试三列的信息,并为这三列取中文名字 +--注意:要用三种方法 +select stuno 学号, writtenexam 笔试, labexam 机试 from exam +select stuno as 学号, writtenexam as 笔试, labexam as 机试 from exam +select 学号= stuno , 笔试= writtenexam,机试= labexam from exam + +--4.查询学生信息表(stuInfo)中的学号,姓名,地址,以及将:姓名+@+地址 组成新列 “邮箱” +select * ,stuname+'@'+stuaddress as 邮箱 from student + +--5.查询学生分数表(stuexam)中的学生的学号,笔试,机试以及总分这四列的信息 +select stuno, writtenexam, labexam ,writtenexam+labexam as 总分 from exam + +--6.查询学生信息表(stuInfo)中学生来自哪几个地方 +select distinct stuaddress from student + +--7.查询学生信息表(stuInfo)中学生有哪几种年龄,并为该列取对应的中文列名 +select distinct stuage as 年龄 from student + +--8.查询学生信息表(stuInfo)中前3行记录 +select top 3 * from student + +--9.查询学生信息表(stuInfo)中前4个学生的姓名和座位号 +select top 4 stuname,stuseat from student + +--10.查询学生信息表(stuInfo)中一半学生的信息 +select top 50 percent * from student + +--11.将地址是湖北武汉,年龄是20的学生的所有信息查询出来 +select * from student where stuaddress='湖北武汉'and stuage=20 + +--12.将机试成绩在60-80之间的信息查询出来,并按照机试成绩降序排列(用两种方法实现) +select * from exam where labexam>60 and labexam<80 order by labexam desc + +--13.查询来自湖北武汉或者湖南长沙的学生的所有信息(用两种方法实现) +select * from student where stuaddress='湖北武汉'or stuaddress='湖南长沙' +select * from student where not(stuaddress='美国硅谷') + +--14.查询出笔试成绩不在70-90之间的信息,并按照笔试成绩升序排列(用两种方法实现) +select * from exam where writtenexam<70 or labexam>90 order by writtenexam asc +select * from exam where writtenexam not between 70 and 90 order by writtenexam asc + +--15.查询年龄没有写的学生所有信息 +select * from student where stuage=' ' + +--16.查询年龄写了的学生所有信息 +select * from student where not(stuage=' ') + +--17.查询姓张的学生信息 +select * from student where stuname like '张%' + +--18.查询学生地址中有‘湖’字的信息 +select * from student where stuaddress like '%湖%' + +--19.查询姓张但名为一个字的学生信息 +select * from student where stuname like '张_' + +--20.查询姓名中第三个字为‘俊’的学生的信息,‘俊’后面有多少个字不限制 +select * from student where stuname like '__俊%' + +--21.按学生的年龄降序显示所有学生信息 +select * from student order by stuage desc + +--22.按学生的年龄降序和座位号升序来显示所有学生的信息 +select * from student order by stuage desc,stuseat asc + +--23.显示笔试第一名的学生的考试号,学号,笔试成绩和机试成绩 +select top 1 * from exam order by writtenexam + +--24.显示机试倒数第一名的学生的考试号,学号,笔试成绩和机试成绩 +select top 1 * from exam order by labexam asc + +--25.查询每个地方的学生的平均年龄 +select stuaddress,avg(stuage) 平均年龄 from student group by stuaddress + +--26.查询男女生的分别的年龄总和 +select stusex,sum(stuage) 年龄总和 from student group by stusex + +--27.查询每个地方的男女生的平均年龄和年龄的总和 +select stuaddress,stusex ,avg(stuage) 平均年龄,sum(stuage) 年龄总和 from student group by stuaddress,stusex + +select * from exam +select * from student \ No newline at end of file diff --git "a/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/\347\273\203\344\271\240.sql" "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/\347\273\203\344\271\240.sql" new file mode 100644 index 0000000000000000000000000000000000000000..bd1286f7685e066ca04b4c9050df2e163ce06038 --- /dev/null +++ "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/\347\273\203\344\271\240.sql" @@ -0,0 +1,162 @@ +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),(9,1,48) +select * from score + +delete from score where scoreid=37 + +update student set studentBirth='2000-04-06' +where studentId=7 + +update score set score=61 +where scoreid=30 + +update course set coutsecredit=coutsecredit+1 + +--查询所有的班级 Class 信息。 +select * from Class + +-- 查询所有的学生 Student 信息。 +select * from student + +-- 查询所有的课程 Course 信息。 +select * from course + +-- 查询所有的班级课程 ClassCourse 信息。 +select * from ClassCourse + +-- 查询所有的分数 Score 信息。 +select * from score + +-- 只查询学生的姓名和地址信息。 +select studentName,studentAddress from student + +-- 查询获取学生信息表中20%的学生信息。 +select top 20 percent * from student + +-- 查询学生信息表中的班级编号有哪些(去除重复值)。 +select distinct classid from student + +-- 查询分数表中有成绩的学生编号; +select studentid from score + +-- 查询所有的课程 Course 信息,并且按照学分从大到小排列。 +select * from course order by coutsecredit + +-- 查询所有的分数 Score 信息,并且按照分数从小到大排列。 +select * from score order by score + +-- 查询所有的学生 Student 信息,并且按照年龄从小到大排列。 +select * from student order by studentBirth desc + +-- 查询软件一班所有的学生信息,显示学生id,姓名、性别、生日、住址。 +select * from student where classid= + +-- 查询所有的女生,显示学生id,姓名、性别、生日、住址。 +select * from student where studentSex=2 + +-- 查询2000年出生的所有学生,显示学生id,姓名、性别、生日、住址。 +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 count(0) from Student where ClassId = 1 + +-- 查询 软件一班 有几门课程。 +select count(0) from classcourse where classid=1 + +-- 查询 刘正(学生编号为1) 的总分。 +select sum(score) from score where studentid=1 + +-- 查询所有学生 数据库高级应用 的平均分。 + +-- 查询 所有学生 的总分。 +-- 查询 所有学生 的平均分。 +-- 查询 所有学生 的平均分,并且按照平均分从高到低排列。 +-- 查询 所有学生中 平均分 大于80分的学生。(聚合查询 + 分组 + having) diff --git "a/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\2322.sql" "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\2322.sql" new file mode 100644 index 0000000000000000000000000000000000000000..996a0a47bbd5712e78bda2781e51e5272b1cbf56 --- /dev/null +++ "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\2322.sql" @@ -0,0 +1,72 @@ +create database dbtest1 +go +use dbtest1 + +create table orders( +orderid int primary key identity, +orderdate datetime not null +); + +insert into orders(orderdate) +values('2008-01-12') + +select * from orders + +insert into orders(orderdate) +values ('2008-02-10'),('2008-02-15'),('2008-03-10') + +create table orderltem( +itemid int primary key identity, +orderid int references orders(orderid) not null, +itentype nvarchar(20) not null, +itemname nvarchar(20) not null, +thenumber int not null, +themoney money +); + +insert into orderltem(orderid, itentype, itemname, thenumber, themoney) +values(1,'文具','笔',72,2) +select * from orderltem + +insert into orderltem(orderid, itentype, itemname, thenumber, themoney) +values(1,'文具','尺',10,1), +(1,'体育文具','篮球',10,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 orderltem + +select orderid ,sum(thenumber),avg(themoney) from orderltem group by orderid having orderid<3 and avg(themoney)<10 + + +select orderid, sum(thenumber),avg(themoney) from orderltem group by orderid having avg(themoney)<10 and sum(thenumber)>50 +select itentype,count(*) from orderltem group by itentype + +select sum(thenumber),avg(themoney) from orderltem group by itentype having sum(thenumber)>100 + +select itemname 产品名称,count(*) 订购次数,sum(thenumber)总数量,avg(themoney)平均单价 from orderltem group by itemname + +select orderltem.orderid , orderdate,itentype,itemname,thenumber,themoney from orderltem +join orders on orderltem.orderid=orders.orderid + +select orderltem.orderid , orderdate,itentype,itemname from orderltem +join orders on orderltem.orderid=orders.orderid +where thenumber>50 + + +select sum(themoney) from orderltem group by itentype + +select orderid,count(itentype) from orderltem group by orderid + + +select orderid,itentype,count(itentype),sum(thenumber) from orderltem group by itentype,orderid \ No newline at end of file diff --git "a/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\230\266\346\256\2652\344\270\223\344\270\2321.sql" "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\230\266\346\256\2652\344\270\223\344\270\2321.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3ee36773798bc03a42b8effe0d2d769e426d7a95 --- /dev/null +++ "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\230\266\346\256\2652\344\270\223\344\270\2321.sql" @@ -0,0 +1,76 @@ +create database dbtest2 +go +use dbtest2 +--学生信息表(stuInfo) +create table stuinfo( +stuid int primary key identity, +stuname nvarchar(20), +stuage int, +stusex int, +[time] smalldatetime +); + +--课程信息表(courseInfo) +create table courseinfo( +courseid int primary key identity, +coursename nvarchar(20), +coursemarks int +); + +--分数信息表(scoreInfo) + 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) + values ('top',19,1) + + select * from stuinfo + + insert into stuinfo (stuname,stuage,stusex) + values ('jack',20,0),('rose',21,1),('lulu',19,1),('lili',21,0) + + insert into stuinfo (stuname,stuage,stusex,[time]) + values ('abc',20,1,getdate()) + + insert into courseinfo(coursename,coursemarks) + values ('javabase',4),('html',2),('javascript',2),('sqlbase',2) + + select * from courseinfo +select * from scoreinfo + 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 + + --1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 + select scoreinfo.stuid, count(*) 课程的数量,avg(score) 平均分 from stuinfo + join scoreinfo on stuinfo.stuid=scoreinfo.stuid + group by scoreinfo.stuid + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseid,count(*),sum(score) from stuinfo join scoreinfo on stuinfo.stuid=scoreinfo.stuid group by courseid + +--3.查询出性别一样并且年龄一样的学生的信息 +select * from stuinfo s1 join stuinfo s2 on s1.stusex=s2.stusex +where s1.stuid != s2.stuid and s1.stuage=s2.stuage and s1.stusex=s2.stusex + +--4.查询出学分一样的课程信息 +select * from courseinfo c1 join courseinfo c2 on c1.coursemarks=c2.coursemarks where c1.coursemarks=c2.coursemarks and c1.courseid != c2.courseid +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuinfo.stuid 学号,stuinfo.stuname 姓名,courseinfo.coursename 课程号,scoreinfo.score 分数 from stuinfo join scoreinfo on stuinfo.stuid=scoreinfo.stuid join courseinfo on courseinfo.courseid=scoreinfo.courseid +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuinfo.stuid 学号,stuinfo.stuname 姓名,courseinfo.coursename 课程号,courseinfo.coursemarks 课程分数, scoreinfo.score 分数 from stuinfo join scoreinfo on stuinfo.stuid=scoreinfo.stuid join courseinfo on courseinfo.courseid=scoreinfo.courseid +--7.查询出没有参加考试的学生的学号和姓名 +select stuinfo.stuid 学号,stuinfo.stuname 姓名 from stuinfo where stuinfo.stuid not in(select stuinfo.stuid from stuinfo join scoreinfo on stuinfo.stuid=scoreinfo.stuid) +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 +select * from stuinfo where stuname like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuinfo.stuid,avg(score),count(*) from stuinfo join scoreinfo on stuinfo.stuid=scoreinfo.stuid group by stuinfo.stuid having count(*)>2 and avg(score)>70 +select * from scoreinfo +select * from stuinfo \ No newline at end of file diff --git "a/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\230\266\346\256\2652\344\275\234\344\270\2322.sql" "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\230\266\346\256\2652\344\275\234\344\270\2322.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a9d43f64fb49b82ba00e27062d3c9d32f0898040 --- /dev/null +++ "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\230\266\346\256\2652\344\275\234\344\270\2322.sql" @@ -0,0 +1,100 @@ +create database dbtest3 +go +use dbtest3 + + +--表一为上网卡信息表(tbl_card) +--id: 卡号,主键 +--passWord:密码 +--balance:卡上的余额 +--userName:该上网卡所属的用户名 +create table tb1_card( +id nvarchar(20) primary key, +[password] int not null, +balance int , +username nvarchar(20) +); +alter table tb1_card alter column [password] nvarchar(20) +--表2为网吧机器说明表(tbl_computer) +--id:机器编号,主键 +--onUse:该机器是否正在使用,0为未使用,1为正在使用 +--note:该机器的说明 +create table tb1_computer( +id nvarchar(10) primary key , +onuse int check(onuse=1 or onuse=0), +note text +); + +--表3为上网记录表(tbl_record): +--id:记录编号,主键 +--cardId:本次上网的卡号,外键 +--ComputerId:本次上网记录所使用的机器号,外键 +--beginTime:本次上网记录的开始时间 +--endTime:本次上网记录的结束时间 +--fee:本次上网的费用 +create table tb1_record( +id int primary key, +cardid nvarchar(20) references tb1_card(id), +computerid nvarchar(10) references tb1_computer(id), +begintime smalldatetime, +endtime smalldatetime, +fee int +); +insert into tb1_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,'张俊') +select * from tb1_card + +insert into tb1_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 tb1_computer +select * from tb1_card +select * from tb1_record +insert into tb1_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) + + +insert into tb1_record(id, cardid, computerid, begintime, endtime, fee) +values( 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 15:55:00',50), +(48,'0023_ABC','03','2006-1-6 15:26:00','2006-1-6 15:55:00',6), +(55,'0023_ABC','03','2006-7-21 15:26:00','2006-7-21 15: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-28 18:00:00','2006-12-28 22:00:00',23) + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select * from tb1_card +join tb1_record on tb1_card.id=tb1_record.cardid +where tb1_card.username='张军' +order by fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select computerid,count(*),sum(fee) from tb1_record group by computerid +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardid,count(*),sum(fee) from tb1_record group by cardid +--4. 查询出从未消费过的上网卡的卡号和用户名 +select tb1_card.id,tb1_card.username from tb1_card + where tb1_card.id not in(select cardid from tb1_card join tb1_record on tb1_card.id=tb1_record.cardid) +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tb1_card t1 join tb1_card t2 on t1.username=t2.username where t1.password=t2.username +--6. 查询出使用次数最多的机器号和使用次数 +select computerid,max(count(*)),count(*) from tb1_record group by computerid +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select cardid,username,computerid,fee from tb1_card join tb1_record on tb1_card.id= tb1_record.cardid where cardid like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 cardid,username,computerid,begintime,endtime,fee from tb1_card join tb1_record on tb1_card.id= tb1_record.cardid order by fee desc \ No newline at end of file diff --git "a/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/80\347\273\203\344\271\240\351\242\230.sql" "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/80\347\273\203\344\271\240\351\242\230.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1cbbaa727d32af2a9310bc49f078df2ef4ab4634 --- /dev/null +++ "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/80\347\273\203\344\271\240\351\242\230.sql" @@ -0,0 +1,233 @@ +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); + +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', '陕西省渭南市华阴市玉泉路南段'); + +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) + +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),(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 + +update course set coutsecredit=coutsecredit+1 + +--查询所有的班级 Class 信息。 +select * from Class + +-- 查询所有的学生 Student 信息。 +select * from student + +-- 查询所有的课程 Course 信息。 +select * from course + +-- 查询所有的班级课程 ClassCourse 信息。 +select * from ClassCourse + +-- 查询所有的分数 Score 信息。 +select * from score + +-- 只查询学生的姓名和地址信息。 +select studentName,studentAddress from student + +-- 查询获取学生信息表中20%的学生信息。 +select top 20 percent * from student + +-- 查询学生信息表中的班级编号有哪些(去除重复值)。 +select distinct classid from student + +-- 查询分数表中有成绩的学生编号; +select studentid from score + +-- 查询所有的课程 Course 信息,并且按照学分从大到小排列。 +select * from course order by coutsecredit + +-- 查询所有的分数 Score 信息,并且按照分数从小到大排列。 +select * from score order by score + +-- 查询所有的学生 Student 信息,并且按照年龄从小到大排列。 +select * from student order by studentBirth desc + +-- 查询软件一班所有的学生信息,显示学生id,姓名、性别、生日、住址。 +select * from student where classid=1 + +-- 查询所有的女生,显示学生id,姓名、性别、生日、住址。 +select * from student where studentSex=2 + +-- 查询2000年出生的所有学生,显示学生id,姓名、性别、生日、住址。 +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 count(0) from Student where ClassId = 1 + +-- 查询 软件一班 有几门课程。 +select count(0) from classcourse where classid=1 + +-- 查询 刘正(学生编号为1) 的总分。 +select sum(score) from score where studentid=1 + +-- 查询所有学生 数据库高级应用 的平均分。 +select avg(score) 平均分 from score where courseid=1 + +-- 查询 所有学生 的总分。 +select studentid, sum(score) 总分 from score group by studentid + +-- 查询 所有学生 的平均分。 +select studentid,avg(score) 平均分 from score group by studentid + +-- 查询 所有学生 的平均分,并且按照平均分从高到低排列。 +select studentid,avg(score) 平均分 from score group by studentid order by 平均分 desc + +-- 查询 所有学生中 平均分 大于80分的学生。(聚合查询 + 分组 + having) +select StudentId, avg(Score) as 平均分 from Score +group by StudentId +having avg(score)>80 + +--查询学生的班级信息:姓名和所属班级名称 +select studentName 学生姓名,class.ClassName 班级名称 from student +join Class on student.classId=Class.ClassId + +--查询学生的成绩信息:学号、姓名和成绩 +select student.studentId 学号,studentName 姓名,score 成绩 from score +join student on score.studentid=student.studentid + + + + + + + + +--在第4章80练习题的基础上完成连接查询: +--1. 查询 李逍遥(编号id为9) 所在的班级名称(连接查询 2表) +select student.studentId 学生编号,studentName,classname 班级名称 from student +join Class on student.classId=class.ClassId +where studentid=9 + +--2. 查询 李逍遥(学生编号id为9) 学习的课程有哪几门,需要姓名、课程名称、课程学分(连接查询) +select student.studentId 学生编号,studentName 学生姓名,coursename 课程名称,coutsecredit 学分 from student +join classcourse on student.classId=classcourse.classid +join course on classcourse.courseid=course.courseid +where studentid=9 + +--3. 查询 李逍遥(学生编号id为9) 学习的课程考试得分,需要姓名、课程名称、课程学分、得分(连接查询) +select * from score +join student on score.studentid=student.studentId +join course on score.courseid=course.courseid +where student.studentId=9 + +--4. 使用子查询查询 软件一班的每个学生的平均分(聚合查询 + 子查询 + 分组) +select rjclass.studentname,avg(score.score) from +(select student.*,Class.ClassName from student join class on student.classid=class.classid where student.classId='1') as rjclass +join score on rjclass.studentId=score.studentid +group by score.studentid,rjclass.studentName +--5. 使用连接查询 软件二班的每个学生的平均分(聚合查询 + 连接查询 + 分组) +select classid, avg(score) 平均分 from score +join student on score.studentid=student.studentId +group by student.studentId,classid +having classId=2 + +--6. 按照班级查询所有课程的平均分,并且按照平均分高低进行排序。(聚合查询 + 连接查询 + 分组) +select course.courseid,student.classId,avg(score) 平均分 from score +join course on score.courseid=course.courseid +join classcourse on course.courseid=classcourse.courseid +join student on student.classId=classcourse.classid +group by course.courseid,course.coursename,student.classId +order by avg(score) desc + + + + + +select * from Class +select * from classcourse +select * from student +select * from course +select * from score +select * from student +select * from classcourse + diff --git "a/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2f32277c92146de60c94d3e5f93c1ea8632e650d --- /dev/null +++ "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,45 @@ +create database shenlong +go +use shenlong +--班级信息表 +create table Class( +ClassId int primary key identity, --班级编号 +ClassName nvarchar(50) not null unique, --班级名称 +); +--学生信息表 +create table Student( +StudentId int primary key identity ,--班级名称 +StudentName nvarchar(50) not null,--学生姓名 +StudentSex tinyint not null default 3 check(StudentSex=1 or StudentSex=2 or StudentSex=3),--学生性别 +StudentBirth date,--学生生日 +StudentAddress nvarchar(255) not null,--学生地址 +ClassId int not null default 0 --所属班级id +); +--课程信息表 +create table Course( +CourseId int primary key identity,--课程编号 +CourseName nvarchar(50)not null unique,--课程名称 +CourseCredit tinyint not null default 0 --课程学分 +); +--班级课程表 +create table ClassCourse( +ClassCourseId int primary key identity,--自增编号 +ClassId int not null,--班级编号 +CourseId int unique ,--课程编号 +foreign key(ClassId) references Class(ClassId), +foreign key(CourseId) references Course(CourseId) +); +--分数信息表 +create table Score( +ScoreId int primary key identity,--自增编号 +StudentId int not null ,--学生编号 +CourseId int not null ,--课程编号 +Score int not null check(Score>0)--分数 +foreign key (StudentId) references Student(StudentId), +foreign key (CourseId) references ClassCourse(CourseId) +); +alter table Student +add StudentIdentityCard varchar(20) not null default''-- 身份证 + +alter table Score +add IsResit tinyint not null default 0--是否是补考:0不是,1是 \ No newline at end of file diff --git "a/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/SQLQuery2.sql" "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..187da9c7153e46a7f3911478a729db83f2a86fe3 --- /dev/null +++ "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/SQLQuery2.sql" @@ -0,0 +1,31 @@ +create database Bank +go +use Bank +create table AccountInfo( +AccountId int primary key identity,--账户编号 +AccountCode varchar(20) not null unique,--身份证号码 +AccountPhone varchar(20) not null,--电话号码 +RealName varchar(20) not null--真实姓名 +); +create table BankCard( +CardNo varchar(30) primary key,--银行卡号 +AccountId int not null,--账户编号 +CardPwd varchar(30) not null,--银行密码 +CardBalance money not null default 0.00,--银行卡余额 +CardState tinyint not null default 1,--银行卡状态:1正常;2挂失;3冻结;4注销;5睡眠; +CardTime varchar(30) not null,--开卡时间 +foreign key(AccountId) references AccountInfo(AccountId) +); +create table CardExchange( +ExchangeId int primary key identity,--交易编号 +CardNo varchar(30) not null,-- 银行卡号 +MoneyInBank money not null check(MoneyInBank>0 and MoneyInBank=0), --存钱金额 +MoneyOutBank money not null check(MoneyOutBank>0 and MoneyOutBank=0), -- 取钱金额 +ExchangeTime smalldatetime not null --交易时间 +foreign key(CardNo) references BankCard(CardNo) +); +alter table AccountInfo +add OpenTime smalldatetime not null default getdate()--开户时间 + +alter table BankCard +alter column CardTime smalldatetime not null -- 开卡时间 diff --git "a/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\346\210\252\345\233\276_20230224222755.png" "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\346\210\252\345\233\276_20230224222755.png" new file mode 100644 index 0000000000000000000000000000000000000000..2328a2b8304a81ad18b42a4a8dc2e4b0ce56f5cc Binary files /dev/null and "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\346\210\252\345\233\276_20230224222755.png" differ diff --git "a/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\346\210\252\345\233\276_20230224222806.png" "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\346\210\252\345\233\276_20230224222806.png" new file mode 100644 index 0000000000000000000000000000000000000000..a860f5a793606c0e7d23764a4871cd6713b6c04f Binary files /dev/null and "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\346\210\252\345\233\276_20230224222806.png" differ diff --git "a/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\346\210\252\345\233\276_20230224232959.png" "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\346\210\252\345\233\276_20230224232959.png" new file mode 100644 index 0000000000000000000000000000000000000000..1142246c0ddad4f687ef27a9723d9fd061c75aed Binary files /dev/null and "b/21\350\242\201\350\250\200\350\250\200\344\275\234\344\270\232/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\346\210\252\345\233\276_20230224232959.png" differ diff --git "a/\351\231\210\346\230\245\345\273\272.txt" "b/\351\231\210\346\230\245\345\273\272.txt" deleted file mode 100644 index 1ee2ee8e40be40785faf0151350d29e41ab5ac68..0000000000000000000000000000000000000000 --- "a/\351\231\210\346\230\245\345\273\272.txt" +++ /dev/null @@ -1 +0,0 @@ -123165465321546565 4wq54fd65wqer6w74r64w6r6wq4r6wqrwq