diff --git "a/26\351\231\210\345\256\227\344\271\211/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/Screenshot_20230217_093146.png" "b/26\351\231\210\345\256\227\344\271\211/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/Screenshot_20230217_093146.png" new file mode 100644 index 0000000000000000000000000000000000000000..955ee0473ba704287e74a349b1a6425115287354 Binary files /dev/null and "b/26\351\231\210\345\256\227\344\271\211/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/Screenshot_20230217_093146.png" differ diff --git "a/26\351\231\210\345\256\227\344\271\211/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/SQLQuery7.sql" "b/26\351\231\210\345\256\227\344\271\211/\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/26\351\231\210\345\256\227\344\271\211/\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/26\351\231\210\345\256\227\344\271\211/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/2023-02-20_200728.png" "b/26\351\231\210\345\256\227\344\271\211/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/2023-02-20_200728.png" new file mode 100644 index 0000000000000000000000000000000000000000..b0778435672d931b161f37ee73dcf9c82c4a7f7c Binary files /dev/null and "b/26\351\231\210\345\256\227\344\271\211/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/2023-02-20_200728.png" differ diff --git "a/26\351\231\210\345\256\227\344\271\211/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" "b/26\351\231\210\345\256\227\344\271\211/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..dc3b706ad4992f3dd8736561d0dc146aebeb0936 --- /dev/null +++ "b/26\351\231\210\345\256\227\344\271\211/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,65 @@ +create database student; + +use student; + +create table class( + +Classid int primary key identity,--班级编号 + +ClassName nvarchar(50),--班级名称 + +) + + +create table Student( + +Studentid int primary key identity not null,--班级编号 + +StudentName nvarchar(50) not null,--学生姓名 + +StudentSex tinyint not null,--学生性别 + +StudentBirth date , --学生生日 + +StudentAddress nvarchar(255) not null,--学生地址 + +ClassId int not null ,--所属班级ID + +) + + +create table Course( + +Couresid int primary key identity not null,--课程编号 + +CourseName nvarchar (50),--课程名称 + +CourseCredit tinyint default 0 not null--课程学分 + + ) + + create table ClassCourse ( + + Scoreld int primary key identity(111110,1) not null,--自增编号 + + Studentid int not null,--学生编号 + + Courseid int not null ,--课程编号 + + Score int not null--分数 + + ) + exec sp_rename 'ClassCourse','Score' + + + + create table ClassCourse1( + + ClassCourseid int primary key identity not null,--自增编号 + + Classid int not null,--班级编号 + + Courseld int not null--课程编号 + + ) + diff --git "a/26\351\231\210\345\256\227\344\271\211/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/SQLQuery9.sql" "b/26\351\231\210\345\256\227\344\271\211/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/SQLQuery9.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8d32da3996861041d93ed6c3d8115ca3767e69a5 --- /dev/null +++ "b/26\351\231\210\345\256\227\344\271\211/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/SQLQuery9.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/26\351\231\210\345\256\227\344\271\211/\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/26\351\231\210\345\256\227\344\271\211/\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..87459b2e00e58f58ae97efe0ec7403d14c63f355 Binary files /dev/null and "b/26\351\231\210\345\256\227\344\271\211/\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/26\351\231\210\345\256\227\344\271\211/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/SQLQuery4.sql" "b/26\351\231\210\345\256\227\344\271\211/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a59df8da8e67691f4f7ef351ac6f8e99861bab5c --- /dev/null +++ "b/26\351\231\210\345\256\227\344\271\211/\347\254\254\344\272\224\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/26\351\231\210\345\256\227\344\271\211/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\2328.sql" "b/26\351\231\210\345\256\227\344\271\211/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\2328.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8d32da3996861041d93ed6c3d8115ca3767e69a5 --- /dev/null +++ "b/26\351\231\210\345\256\227\344\271\211/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\2328.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/26\351\231\210\345\256\227\344\271\211/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/SQLQuery6.sql" "b/26\351\231\210\345\256\227\344\271\211/\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/26\351\231\210\345\256\227\344\271\211/\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/26\351\231\210\345\256\227\344\271\211/\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\23211.sql" "b/26\351\231\210\345\256\227\344\271\211/\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\23211.sql" new file mode 100644 index 0000000000000000000000000000000000000000..450265913aee3bc5701c0ff7ce1fa12768e6f620 --- /dev/null +++ "b/26\351\231\210\345\256\227\344\271\211/\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\23211.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/26\351\231\210\345\256\227\344\271\211/\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/26\351\231\210\345\256\227\344\271\211/\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/26\351\231\210\345\256\227\344\271\211/\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/26\351\231\210\345\256\227\344\271\211/\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\2303.sql" "b/26\351\231\210\345\256\227\344\271\211/\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\2303.sql" new file mode 100644 index 0000000000000000000000000000000000000000..01f0b216853132333a010ba6a0b83ac4ae20363f --- /dev/null +++ "b/26\351\231\210\345\256\227\344\271\211/\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\2303.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/26\351\231\210\345\256\227\344\271\211/\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/26\351\231\210\345\256\227\344\271\211/\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/26\351\231\210\345\256\227\344\271\211/\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/26\351\231\210\345\256\227\344\271\211/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\23210.sql" "b/26\351\231\210\345\256\227\344\271\211/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\23210.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fd49fe43b15144617fcbb7d9a374e799097eb593 --- /dev/null +++ "b/26\351\231\210\345\256\227\344\271\211/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\23210.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/26\351\231\210\345\256\227\344\271\211/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/SQLQuery4.sql" "b/26\351\231\210\345\256\227\344\271\211/\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/26\351\231\210\345\256\227\344\271\211/\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/\344\275\234\344\270\232/SQLQuery1.sql" "b/\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..081135e656fbedf0146f4bc64b9b0449045e05a1 --- /dev/null +++ "b/\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,104 @@ +create database stuinfo +go +use stuinfo +go + +create table stuinfo ( + +stuno nvarchar(255) primary key , + +stuname nvarchar(255) not null , + +stuage int , + +stuaddress nvarchar(245) not null , + +stuseat int identity , + +stusex int + +) + +create table stuscore ( + +examno int identity , + +stuno nvarchar(255) primary key , + +writtenexam int not null , + +labexam int not null , + +foreign key(stuno) references stuinfo(stuno), + + ) + +insert stuinfo (stuno,stuname, stuage,stuaddress,stusex) +values ('s2501','张秋利',20,'美国硅谷',1), + ('s2502','李斯文',18,'湖北武汉',0), + ('s2503','马文才',22,'湖南长沙',1), + ('s2504','欧阳俊雄',21,'湖北武汉',0), + ('s2505','梅超风',20,'湖北武汉',1), + ('s2506','陈旋风',19,'美国硅谷',1), + ('s2507','陈风',20,'美国硅谷',0); +insert stuinfo (stuno,stuname, stuage,stuaddress,stusex) +values ('s2508','计入俊据合肥当地',20,'美国硅谷',0); + + +insert stuscore (writtenexam,stuno,labexam) +values (50,'s2501',70),(60,'s2502',65),(86,'s2503',85),(40,'s2504',80),(70,'s2505',90),(85,'s2506',90); + + + +select stuno as 学号 , stuname as 姓名 , stuage as 年龄 , stuaddress as 家庭住址 , stuseat as 编号, stusex as 性别 from stuinfo ; + +select stuname as 姓名 ,stuage as 年龄, stuaddress as 家庭地址 from stuinfo; + +select stuno as 学号, writtenexam as 笔试, labexam as 机试 from stuscore; + +select stuno,stuname, stuaddress,stuname+'@'+stuaddress as 邮箱 from stuinfo; + +select stuno,writtenexam,labexam ,writtenexam+labexam as 总分 from stuscore; + +select distinct stuaddress from stuinfo; + +select distinct stuage as 年龄 from stuinfo ; + +select top 3 * from stuinfo ; + +select top 4 stuname , stuseat from stuinfo; + +select top 50 percent * from stuinfo; + +select * from stuinfo where stuaddress='湖北武汉' and stuage=20; + +select * from stuscore where labexam between 60 and 80 order by labexam desc ; + +select * from stuinfo where stuaddress='湖北武汉' or stuaddress='湖南长沙'; + +select labexam from stuscore where labexam not between 70 and 90 order by labexam asc ; + +select * from stuinfo where stuage is null ; + +select * from stuinfo where stuage is not null ; + +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 stuname desc ; + +select * from stuinfo order by stuno asc ; + +select top 1 writtenexam from stuscore order by writtenexam desc; + +select top 1 * from stuscore order by writtenexam desc; + +select top 1 * from stuscore order by labexam asc; + + + diff --git "a/\344\275\234\344\270\232/where\345\255\220\345\217\245.sql" "b/\344\275\234\344\270\232/where\345\255\220\345\217\245.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a19dd11d546a68f5dabc22e7fc6c4d720a3c81bc --- /dev/null +++ "b/\344\275\234\344\270\232/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/\344\275\234\344\270\232/\346\226\260\345\273\272 Microsoft Visio Drawing.vsdx" "b/\344\275\234\344\270\232/\346\226\260\345\273\272 Microsoft Visio Drawing.vsdx" new file mode 100644 index 0000000000000000000000000000000000000000..e4d269b3793c4153f23d4e6109ec1c9cea3a037f Binary files /dev/null and "b/\344\275\234\344\270\232/\346\226\260\345\273\272 Microsoft Visio Drawing.vsdx" differ diff --git "a/\344\275\234\344\270\232/\346\226\260\345\273\272 Microsoft Word \346\226\207\346\241\243.docx" "b/\344\275\234\344\270\232/\346\226\260\345\273\272 Microsoft Word \346\226\207\346\241\243.docx" new file mode 100644 index 0000000000000000000000000000000000000000..90d0637b53777fefe922da5387ea2f0e669bafbe Binary files /dev/null and "b/\344\275\234\344\270\232/\346\226\260\345\273\272 Microsoft Word \346\226\207\346\241\243.docx" differ diff --git "a/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/01_\345\242\236\345\212\240\346\225\260\346\215\256.md" "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/01_\345\242\236\345\212\240\346\225\260\346\215\256.md" new file mode 100644 index 0000000000000000000000000000000000000000..1c91ae273bd37c4d9e50f2e5476942742f603ebb --- /dev/null +++ "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/01_\345\242\236\345\212\240\346\225\260\346\215\256.md" @@ -0,0 +1,129 @@ +## 鏈妭鐩爣 + +1. 鐔熸倝SQL Server鎻掑叆鏁版嵁 +2. 鐔熸倝SQL Server鎵归噺鎻掑叆鏁版嵁 + +## 鏁版嵁鍑嗗 + +鎻掑叆鏁版嵁鏄彃鍏ュ埌琛ㄤ腑锛岃繖杈瑰厛寤哄ソ瀛︾敓鏁版嵁搴撳拰瀛︾敓淇℃伅琛ㄣ佺彮绾т俊鎭〃锛屼负鍚庨潰婕旂ず渚嬪瓙浣跨敤锛 + +```sql +-------------------- +-- 寤哄簱閮ㄥ垎 +-------------------- +-- 鍒涘缓瀛︾敓绠$悊绯荤粺鏁版嵁搴擄紝鍚嶇О涓篠tudent +use master; +go +-- 妫娴嬪師搴撴槸鍚﹀瓨鍦紝濡傛灉瀛樺湪鍒欏垹闄 +if exists (select * from sys.databases where name = 'Student') + drop database Student; +go +-- 鍒涘缓涓涓簱 +create database Student; +go +-- 浣跨敤杩欎釜搴 +use Student; +go + + +-------------------- +-- 寤鸿〃閮ㄥ垎 +-------------------- +-- 鍒涘缓鐝骇琛紝瀛樺偍鐝骇淇℃伅锛屽叾涓瓧娈靛寘鍚細鐝骇id銆佺彮绾у悕绉 +create table Class( + ClassId int not null identity(1,1), + ClassName nvarchar(50) not null +); +go + +-- 鍒涘缓瀛︾敓琛紝瀛樺偍瀛︾敓淇℃伅锛屽叾涓瓧娈典繚鎶わ細瀛︾敓id銆佸鍚嶃佹у埆銆佺敓鏃ャ佸搴綇鍧锛屾墍灞炵彮绾d +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 +``` + +## 鎻掑叆鏁版嵁 + +鎻掑叆鏁版嵁璇硶锛 + + insert into 琛ㄥ悕 (鍒楀悕1,鍒楀悕2,鍒楀悕3,...) values (鏁版嵁1,鏁版嵁2,鏁版嵁3,...) + +鍏朵腑锛 + +1. insert into 鏄叧閿瘝銆 +2. 琛ㄥ悕 琛ㄧず鎻掑叆鍝釜琛ㄣ +3. 鍒楀悕1,鍒楀悕2,鍒楀悕3,... 琛ㄧず瀛楁鐨勫悕绉帮紝鍚庨潰...琛ㄧず鍙互鏈夊涓殑鎰忔濄 +4. values 鏄叧閿瘝锛岃〃绀鸿鎻掑叆鐨勫笺 +5. 鏁版嵁1,鏁版嵁2,鏁版嵁3,... 琛ㄧず鎻掑叆鏁版嵁鐨勫硷紝瑕佸拰鍓嶉潰鍒楀悕涓涓瀵瑰簲銆 + +**绀轰緥1**锛屽鏍″紑璁句簡3涓彮绾э細杞欢涓鐝佽蒋浠朵簩鐝佽绠楁満搴旂敤鎶鏈彮锛岄渶瑕佹彃鍏3鏉¤褰曪細 + +```sql +insert into Class (ClassName) values ('杞欢涓鐝'); +insert into Class (ClassName) values ('杞欢浜岀彮'); +insert into Class (ClassName) values ('璁$畻鏈哄簲鐢ㄦ妧鏈彮'); +``` + +**绀轰緥2**锛岃蒋浠朵竴鐝湁3涓悓瀛︼紝濮撳悕銆佹у埆銆佺敓鏃ャ佸搴綇鍧 鍒嗗埆鏄細 + + 鍒樻銆佺敺銆2000-01-01銆佸箍瑗跨渷妗傛灄甯備竷鏄熷尯绌烘槑瑗胯矾10鍙烽妇涓滃皬鍖 + 榛勮吹銆佺敺銆2001-03-20銆佹睙瑗跨渷鍗楁槍甯傞潚灞辨箹鍖鸿壘婧箹鍗楄矾鍗150绫冲箍闃冲皬鍖 + 闄堢編銆佸コ銆2000-07-08銆佺寤虹渷榫欏博甯傛柊缃楀尯鏇规邯琛楅亾涓囪揪灏忓尯 + +鎻掑叆杩欎簺鏁版嵁锛 + +```sql +insert into Student (StudentName, StudentSex, StudentBirth, StudentAddress, ClassId) + values ('鍒樻',1,'2000-01-01','骞胯タ鐪佹鏋楀競涓冩槦鍖虹┖鏄庤タ璺10鍙烽妇涓滃皬鍖', 1); +insert into Student (StudentName, StudentSex, StudentBirth, StudentAddress, ClassId) + values ('榛勮吹',1,'2001-03-20','姹熻タ鐪佸崡鏄屽競闈掑北婀栧尯鑹炬邯婀栧崡璺崡150绫冲箍闃冲皬鍖', 1); +insert into Student (StudentName, StudentSex, StudentBirth, StudentAddress, ClassId) + values ('闄堢編',2,'2000-07-08','绂忓缓鐪侀緳宀╁競鏂扮綏鍖烘浌婧閬撲竾杈惧皬鍖', 1); +``` + +## 鎵归噺鎻掑叆鏁版嵁 + +涓婇潰鎻掑叆鏁版嵁锛屽ぇ瀹跺彲浠ョ湅鍒版槸涓鏉℃潯鎻掑叆锛孲QL Server涔熸敮鎸佹壒閲忚繘琛屾彃鍏ワ紝鎵归噺鎻掑叆璇硶锛 + +```sql +insert into 琛ㄥ悕 (鍒楀悕1,鍒楀悕2,鍒楀悕3,...) + values (鏁版嵁1,鏁版嵁2,鏁版嵁3,...) + ,(鏁版嵁1,鏁版嵁2,鏁版嵁3,...) + ,(鏁版嵁1,鏁版嵁2,鏁版嵁3,...) + ,(鏁版嵁1,鏁版嵁2,鏁版嵁3,...) +``` + +鍜屽崟鏉℃彃鍏ユ暟鎹竴鏍凤紝鍙槸鍦╲alues鍚庨潰缃楀垪澶氫釜鏁版嵁椤癸紝鐒跺悗鐢ㄨ嫳鏂囬楀彿 , 鍒嗛殧鍗冲彲銆 + +**绀轰緥1**锛岃蒋浠朵簩鐝湁2涓悓瀛︼紝濮撳悕銆佹у埆銆佺敓鏃ャ佸搴綇鍧 鍒嗗埆鏄細 + + 姹熸枃銆佺敺銆2000-08-10銆佸畨寰界渷鍚堣偉甯傚簮闃冲尯鍥涢噷娌宠矾涓庢綔灞辫矾浜ゆ眹澶勪竾绉戝煄甯備箣鍏 + 閽熺惇銆佸コ銆2001-03-21銆佹箹鍗楃渷闀挎矙甯傞洦鑺卞尯绾㈣姳鍧$ぞ鍖 + +```sql +insert into Student (StudentName, StudentSex, StudentBirth, StudentAddress, ClassId) + values ('姹熸枃',1,'2000-08-10','瀹夊窘鐪佸悎鑲ュ競搴愰槼鍖哄洓閲屾渤璺笌娼滃北璺氦姹囧涓囩鍩庡競涔嬪厜', 2) + , ('閽熺惇',2,'2001-03-21','婀栧崡鐪侀暱娌欏競闆ㄨ姳鍖虹孩鑺卞潯绀惧尯', 2); +``` + +**绀轰緥2**锛岃绠楁満搴旂敤鎶鏈彮鏈4涓悓瀛︼紝濮撳悕銆佹у埆銆佺敓鏃ャ佸搴綇鍧 鍒嗗埆鏄細 + + 鏇惧皬鏋椼佺敺銆1999-12-10銆佸畨寰界渷鍚堣偉甯傚簮闃冲尯鍥涢噷娌宠矾涓庢綔灞辫矾浜ゆ眹澶勪竾绉戝煄甯備箣鍏 + 娆ч槼澶╁ぉ銆佸コ銆2000-04-05銆佹箹鍖楃渷姝︽眽甯傛椽灞卞尯鍙嬭皧澶ч亾涓庝簩鐜嚎浜ゆ眹澶勮瀺渚ㄦ偊搴 + 寰愰暱鍗裤佺敺銆2001-01-30銆佹睙鑻忕渷鑻忓窞甯傝嫃宸炲伐涓氬洯鍖虹嫭澧呮箹绀惧尯 + 鏉庨嶉仴銆佺敺銆1999-11-11銆佸箍涓滅渷骞垮窞甯傜櫧浜戝尯閲戞矙娲插矝寰℃床涓夎鎭掑ぇ缁挎床 + +```sql +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); +``` + diff --git "a/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/02_\346\237\245\350\257\242\346\225\260\346\215\256.md" "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/02_\346\237\245\350\257\242\346\225\260\346\215\256.md" new file mode 100644 index 0000000000000000000000000000000000000000..9d4dc785827ffbad72e501955db61aa5e83942a8 --- /dev/null +++ "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/02_\346\237\245\350\257\242\346\225\260\346\215\256.md" @@ -0,0 +1,419 @@ +## 鏈妭鐩爣 + +1. 鐔熸倝SQL Server鏁版嵁甯歌鏌ヨ鎶宸с + +## 鏁版嵁鍑嗗 + +涓嬮潰婕旂ず涔嬪墠锛屾垜浠渶瑕佹彃鍏ヤ竴浜涙祴璇曡繘琛屾紨绀猴紝璇峰弬鑰冿細 + + 02_鏌ヨ鏁版嵁绀轰緥鏁版嵁.sql + +鏂囦欢锛屽皢鍏跺鍏ュ埌SQL Server鍗冲彲銆 + +## 鏌ヨ鎵鏈夋暟鎹 + +鏁版嵁鎻掑叆鍒拌〃鍚庯紝閭d箞鍦ㄥ簲鐢ㄧ▼搴忎娇鐢ㄧ殑鏃跺欏氨闇瑕佽繘琛屾煡璇簡銆傚湪SQL Server涓煡璇㈢殑璇硶锛 + + select * from 琛ㄥ悕 + +鍏朵腑锛 + +1. select 鍜宖rom 鏄浐瀹氬叧閿瘝锛* 琛ㄧず鏌ヨ鍏ㄩ儴鍒楃殑鎰忔濄 +2. 琛ㄥ悕 琛ㄧず浠庡摢涓〃鏌ヨ + +**绀轰緥1**锛屾煡璇㈡墍鏈夌殑鐝骇 Class 淇℃伅锛 + +```sql +select * from Class; +``` + +**绀轰緥2**锛屾煡璇㈡墍鏈夌殑瀛︾敓 Student 淇℃伅锛 + +```sql +select * from Student; +``` + +**绀轰緥3**锛屾煡璇㈡墍鏈夌殑璇剧▼ Course 淇℃伅锛 + +```sql +select * from Course; +``` + +**绀轰緥4**锛屾煡璇㈡墍鏈夌殑鐝骇璇剧▼ ClassCourse 淇℃伅锛 + +```sql +select * from ClassCourse; +``` + +**绀轰緥5**锛屾煡璇㈡墍鏈夌殑鍒嗘暟 Score 淇℃伅锛 + +```sql +select * from Score; +``` + +## 鏌ヨ鑻ュ共鏉℃暟鎹 + +涓婇潰姣忎釜sql璇彞閮芥煡璇簡鐩稿簲琛ㄧ殑鎵鏈夎褰曪紝璇曟兂濡傛灉涓涓〃鐨勬暟鎹褰曞緢澶氾紝鏈変笂鍗冧竾鏉★紝閭d箞涓娆℃煡璇㈠嚭鏉ュ氨寰堟參浜嗐 + +鍦⊿QL Sql涓厑璁告煡璇㈣嫢骞叉潯璁板綍銆傛煡璇㈣娉曪細 + + select top n [percent] * from 琛ㄥ悕 + +鍏朵腑锛 + + top 鏄叧閿瘝 + n 琛ㄧず闇瑕佹煡璇㈠嚑鏉¤褰曘 + percent 琛ㄧず鐧惧垎姣 + +**绀轰緥1**锛屾煡璇3鏉″鐢 Student 淇℃伅锛 + +```sql +select top 3 * from Student; +``` + +**绀轰緥2**锛屾煡璇3鏉¤绋 Course 淇℃伅锛 + +```sql +select top 3 * from Course; +``` + +## 鏌ヨ鎸囧畾鍒 + +涓婇潰姣忎釜sql璇彞閮芥煡璇簡鐩稿簲琛ㄧ殑鎵鏈夊垪锛岃瘯鎯冲鏋滀竴涓〃鐨勬暟鎹湁寰堝鍒楋紝渚嬪鐢靛瓙鍟嗗姟鐨勫晢鍝佽〃銆佽鍗曡〃銆佽储鍔$郴缁熺浉鍏崇殑琛ㄤ竴鑸兘鏈夊ソ鍑犲崄涓瓧娈点傞偅涔堟瘡娆¢兘鏌ヨ鎵鏈夊垪锛岄偅涔堝氨浼氬緢鎱簡銆 + +鎴栬呮湁鐨勫垪鐨勬暟鎹緢澶э紝姣斿鏂囩珷琛ㄤ腑鐨勬枃绔犲唴瀹瑰瓧娈碉紝涓鑸兘鏈夊ソ鍑犲崈涓瓧锛屽湪鍒楄〃鐨勬椂鍊欓氬父涔熶笉浼氭樉绀恒 + +鍦⊿QL Sql涓厑璁告煡璇㈡寚瀹氱殑鑻ュ共鍒椼傛煡璇㈣娉曪細 + + select 鍒楀悕1,鍒楀悕2,鍒楀悕3,... from 琛ㄥ悕 + +鍏朵腑锛 + + 鍒楀悕1,鍒楀悕2,鍒楀悕3,... 鎸囪鏌ヨ鐨勬寚瀹氬垪锛...琛ㄧず鍙互鏌ヨ澶氫釜銆 + +**绀轰緥1**锛屾煡璇㈠鐢熶俊鎭紝鍙渶瑕佹煡璇㈠嚭瀛︾敓濮撳悕鍜岀敓鏃ュ嵆鍙細锛 + +```sql +select StudentName, StudentBirth from Student; +``` + +**鎸囧畾鍒悕** + +涓婇潰鍒楀悕閮芥槸鑻辨枃锛屼笉澶鏄撶湅锛屽彲浠ョ粰鍒楄捣涓涓埆鍚嶏紝鍒悕璇硶锛 + + select 鍒楀悕1 as 鍒悕1, 鍒楀悕2 as 鍒悕1, 鍒楀悕3 as 鍒悕3, ... from 琛ㄥ悕 + +鏀硅繘涓婁緥锛 + +```sql +select StudentName as 瀛︾敓濮撳悕, StudentBirth as 瀛︾敓鐢熸棩 from Student; +``` + + + +## 鏌ヨ缁撴灉涓嶉噸澶 + +鏌ヨ鎸囧畾鍒楁椂锛屽彲鑳戒細鍑虹幇缁撴灉澶氳閲嶅锛屽彲浠ヤ娇鐢╠istinct鍏抽敭瀛楀幓闄ら噸澶嶉」锛 + +渚嬪锛氭煡璇㈠鐢熻〃涓殑鐝骇缂栧彿淇℃伅 + +```sql +select ClassId from Student +``` + +鏌ヨ缁撴灉鐝骇缂栧彿寰堝閲嶅锛屽彲浠ラ噰鐢╠istinct鍘婚櫎閲嶅 + +```sql +select distinct ClassId from Student +``` + + + + + +## 鏁版嵁鎺掑簭 + +鍦ㄦ棩甯哥敓娲讳腑锛屾垜浠細缁忓父鎸夌収鏌愪釜灞炴ц繘琛屾帓搴忕瓫閫夛紝姣斿锛 + + 鐢靛瓙鍟嗗姟涓寜鐓у晢鍝佷环鏍艰繘琛屾帓搴忥紱 + 鐢靛瓙鍟嗗姟涓寜鐓у晢鍝侀攢閲忚繘琛屾帓搴忥紱 + 瀛︾敓绠$悊绯荤粺涓寜鐓ф垚缁╄繘琛屾帓搴忥紱 + +绛夌瓑銆 + +鍦⊿QL Sql涓厑璁稿鏌ヨ杩涜鎺掑簭锛屾煡璇㈣娉曪細 + + select * from 琛ㄥ悕 order by 瀛楁 [desc|asc] + +鍏朵腑锛 + + order by 鏄叧閿瘝銆 + 瀛楁 琛ㄧず闇瑕佹帓搴忕殑瀛楁銆 + desc琛ㄧず闄嶅簭鎺掑垪锛宎sc琛ㄧず鍗囧簭鎺掑垪銆 + +**绀轰緥1**锛屾煡璇㈡墍鏈夌殑鎴愮哗淇℃伅锛屽苟涓旀寜鐓уぇ鍒板皬杩涜鎺掑垪锛 + +```sql +select * from Score order by Score desc; +``` + +**绀轰緥2**锛屾煡璇㈡墍鏈夌殑瀛︾敓淇℃伅锛屽苟涓旀寜鐓у勾榫勪粠澶у埌灏忔帓鍒楋細 + +```sql +select * from Student order by StudentBirth asc; +``` + +## 鏉′欢绛涢 + +鍋囧鎴戜滑鍙兂鏌ヨ鍑烘煇涓彮绾х殑瀛︾敓锛屽湪SQL Sql涓厑璁稿鏌ヨ杩涜绛涢夛紝鏌ヨ璇硶锛 + + select * from 琛ㄥ悕 where 鏉′欢 + +鍏朵腑锛 + + where 鏄叧閿瘝銆 + 鏉′欢 琛ㄧず绛涢夌殑鏉′欢锛屾瘮濡 ClassId=1锛 + +鍏朵腑鏉′欢锛 + + 澶氫釜鏉′欢閮借婊¤冻鐢 and 鍏抽敭璇嶈繛鎺ャ + 澶氫釜鏉′欢婊¤冻1涓敤 or 鍏抽敭璇嶈繛鎺ャ + +鏉′欢涓彲浠ユ湁濡備笅绗﹀彿锛 + +| 鎿嶄綔绗 | 鎻忚堪 | +| --------------------------------- | ------------ | +| = | 绛変簬 | +| <> | 涓嶇瓑浜 | +| > | 澶т簬 | +| < | 灏忎簬 | +| >= | 澶т簬绛変簬 | +| <= | 灏忎簬绛変簬 | +| between and,not between and | 鍦ㄦ煇涓寖鍥村唴 | +| is null | 涓虹┖ | +| is not null | 涓嶉潪绌 | +| in | 鍖呭惈鍦ㄥ唴 | +| not in | 涓嶅寘鍚湪鍐 | +| like | 鎼滅储鏌愮妯″紡 | + + + +**绀轰緥1**锛屾煡璇㈣蒋浠朵竴鐝墍鏈夌殑瀛︾敓锛 + +```sql +select * from Student where ClassId = 1; +``` + +**绀轰緥2**锛屾煡璇笉鏄蒋浠朵竴鐝墍鏈夌殑瀛︾敓锛 + +```sql +select * from Student where ClassId <> 1; +``` + +**绀轰緥3**锛屾煡璇㈡墍鏈夌殑濂崇敓锛 + +```sql +select * from Student where StudentSex = 2; +``` + +**绀轰緥4**锛屾煡璇2000骞村嚭鐢熺殑鎵鏈夊鐢燂細 + +```sql +select * from Student where StudentBirth >= '2000-01-01' and StudentBirth < '2001-01-01'; +``` + +涔熷彲浠ヤ娇鐢╞etween鍏抽敭璇嶏細 + +```sql +select * from Student where StudentBirth between '2000-01-01' and '2000-12-31'; +``` + +**绀轰緥5**锛屾煡璇㈣蒋浠朵竴鐝拰璁$畻鏈哄簲鐢ㄦ妧鏈彮鎵鏈夌殑瀛︾敓锛 + +```sql +select * from Student where ClassId = 1 or ClassId = 3; +``` + +**绀轰緥6**锛屾煡璇㈢敓鏃ユ病鏈夊綍鍏ョ殑瀛︾敓锛 + +```sql +select * from Student where StudentBirth is null; +``` + +**绀轰緥7**锛屾煡璇㈠搴綇鍧涓嶄负绌虹殑瀛︾敓锛 + +```sql +select * from Student where StudentAddress is not null; +``` + +**绀轰緥8**锛屾煡璇㈣蒋浠1鐝拰2鐝殑瀛︾敓锛 + +```sql +select * from Student where ClassId in ('1', '2'); +``` + +**绀轰緥9**锛屾煡璇㈤挓鐞拰鏇惧皬鏋楃殑鑰冭瘯鎴愮哗锛 + +```sql +select * from Score where StudentId in ('5', '6'); +``` + +**绀轰緥10**锛屾煡璇㈠鍚嶄负 鍒樻銆侀挓鐞 銆佹浘灏忔灄鐨勫鐢熶俊鎭細 + +```sql +select * from Student where StudentName in ('鍒樻', '閽熺惇', '鏇惧皬鏋'); +``` + +**绀轰緥11**锛屾煡璇笉涓鸿蒋浠1鐝拰杞欢2鐝殑瀛︾敓淇℃伅锛 + +```sql +select * from Student where ClassId not in ('1', '2'); +``` + +### 妯$硦鏌ヨ + +鍋囧鎴戜滑鎯虫煡鎵惧嚭妗傛灄甯傜殑鎵鏈夊鐢燂紝灏变笉鑳界敤 = 鍙蜂簡锛屽洜涓哄鐢熷搴綇鍧鏄湴鍧鐨勫瓧绗︿覆锛屽寘鍚簡鐪佷唤銆佸競銆佸尯銆佽閬撶瓑淇℃伅銆 + +鍦⊿QL Sql涓厑璁歌繘琛屾ā绯婃煡璇紝鏌ヨ璇硶锛 + + select * from 琛ㄥ悕 where 瀛楁 like '%鏌ユ壘鐨勮瘝%' + +鍏朵腑锛 + + 1. 瀛楁 琛ㄧず鍦ㄥ摢涓瓧娈甸噷杩涜妯$硦鏌ユ壘銆 + 2. like 鏄叧閿瘝銆 + 3. '%鏌ユ壘鐨勮瘝%' 琛ㄧず鏌ユ壘瀵瑰簲鍒楀唴瀹瑰寘鍚 鏌ユ壘鐨勮瘝 鐨勮褰曘 + +鍏朵腑%鍚箟锛 + + 1. 鏌ユ壘鐨勮瘝 鍓嶉潰娌℃湁%锛岄偅涔堣〃绀鸿繖涓瘝瑕佸湪瀛楃涓茬殑寮濮嬨 + 2. 鏌ユ壘鐨勮瘝 鍚庨潰娌℃湁%锛岄偅涔堣〃绀鸿繖涓瘝瑕佸湪瀛楃涓茬殑缁撳熬銆 + 3. 鏌ユ壘鐨勮瘝 鍓嶅悗閮芥湁%锛岄偅涔堣〃绀鸿繖涓瘝鍦ㄥ瓧绗︿覆鐨勪换鎰忎綅缃兘鍙互銆 + +绀轰緥1锛屾煡璇㈠湴鍧鏄鏋楀競鐨勫鐢燂細 + +```sql +select * from Student where StudentAddress like '%妗傛灄甯%'; +``` + +绀轰緥2锛屾煡璇㈠湴鍧鏄寤虹渷鐨勫鐢燂細 + +```sql +select * from Student where StudentAddress like '%绂忓缓鐪%'; +``` + +绀轰緥3锛屾煡璇㈠鏄闃崇殑瀛︾敓锛 + +```sql +select * from Student where StudentName like '%娆ч槼%'; +``` + +**妯$硦鏌ヨ涓嬪垝绾** + +妯$硦鏌ヨ杩樻湁涓绉嶆柟寮忥紝鐢ㄤ笅鍒掔嚎 _ 鏉ヨ〃绀哄崟鐙殑涓涓瓧銆 + +绀轰緥4锛屾煡璇㈠鏉庣殑瀛︾敓锛屽苟涓旀槸涓変釜瀛楃殑锛 + +```sql +select * from Student where StudentName like '鏉巁_'; +``` + +## 鑱氬悎鍑芥暟鏌ヨ + +鍦╡xcel涓紝鎴戜滑涔熶細缁忓父浣跨敤excel鐨勬眰鎬昏褰曟暟銆佸拰銆佸钩鍧囥佹渶灏忓笺佹渶澶у肩瓑鍑芥暟銆係QL Server涔熸敮鎸佽繖鏍风殑鍑芥暟锛岃繖浜涘彨鑱氬悎鍑芥暟銆 + +鐩稿叧璇硶锛 + + 姹傛昏褰曟暟锛宻elect count(0) from 琛ㄥ悕 + 姹傚拰锛宻elect sum(瀛楁) from 琛ㄥ悕 + 姹傚钩鍧囷紝select avg(瀛楁) from 琛ㄥ悕 + 姹傛渶灏忓硷紝select min(瀛楁) from 琛ㄥ悕 + 姹傛渶澶у硷紝select max(瀛楁) from 琛ㄥ悕 + +绀轰緥1锛屾煡璇 杞欢涓鐝 鏈夊嚑涓鐢燂細 + +```sql +select count(0) from Student where ClassId = 1; +``` + +绀轰緥2锛屾煡璇 杞欢涓鐝 鏈夊嚑闂ㄨ绋嬶細 + +```sql +select count(0) from ClassCourse where ClassId = 1; +``` + +绀轰緥3锛屾煡璇 鍒樻(瀛︾敓缂栧彿涓1) 鐨勬诲垎锛 + +```sql +select sum(score) from Score where StudentId = 1; +``` + +绀轰緥4锛屾煡璇㈡墍鏈夊鐢 鏁版嵁搴撻珮绾у簲鐢 鐨勫钩鍧囧垎锛 + +```sql +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; +``` + +## 鍒嗙粍鏌ヨ + +鍋囧鎴戜滑鐜板湪鏌ヨ姣忎釜瀛︾敓鐨勬诲垎锛屾寜鐓у墠闈㈠杩囩殑鐭ヨ瘑锛屾垜浠彲浠ラ愪釜瀛︾敓杩涜鑱氬悎sum鏌ヨ銆 + +```sql +select sum(Score) as TotalScore from Score where StudentId=1; +select sum(Score) as TotalScore from Score where StudentId=2; +``` + +鍙槸杩欐牱鏈夌偣楹荤儲锛屽湪SQL Server鍙互閫氳繃鍒嗙粍鏌ヨ鐨勫姛鑳借繘琛屽疄鐜般傜浉鍏宠娉曪細 + + select 瀛楁1, 鑱氬悎鍑芥暟(瀛楁2) from 琛ㄥ悕 group by 瀛楁1 + +鍏朵腑 + + 1. 瀛楁1 琛ㄧず闇瑕佸垎缁勭殑瀛楁銆 + 2. 瀛楁2 琛ㄧず鑱氬悎鍑芥暟闇瑕佺殑瀛楁銆 + +绀轰緥1锛屾煡璇 姣忎釜瀛︾敓 鐨勬诲垎锛 + +```sql +select * from Score; +select StudentId, sum(Score) as TotalScore from Score group by StudentId; +``` + +绀轰緥2锛屾煡璇 姣忎釜瀛︾敓 鐨勫钩鍧囧垎锛堣仛鍚堟煡璇 + 鍒嗙粍锛 + +```sql +select * from Score; +select StudentId, avg(Score) as TotalScore from Score group by StudentId; +``` + +绀轰緥3锛屼笂闈㈡煡璇㈠嚭瀛︾敓鐨勫钩鍧囧悗锛岃繕甯屾湜鎸夌収骞冲潎鍒嗕粠楂樺埌浣庢帓鍒楋細 + +```sql +select * from Score; +select StudentId, avg(Score) as AvgScore from Score group by StudentId order by AvgScore desc; +``` + +**鍒嗙粍鍚庣瓫閫** + +涓婇潰鐨勪緥瀛愭煡璇㈠嚭浜嗘瘡涓鐢熺殑骞冲潎娉曪紝濡傛灉闇瑕佺瓫閫夊嚭骞冲潎鍒嗗ぇ浜80鍒嗙殑锛屽彲浠ヤ娇鐢╤aving璇硶锛 + + select 瀛楁1, 鑱氬悎鍑芥暟(瀛楁2) from 琛ㄥ悕 group by 瀛楁1 having 鏉′欢 + +绀轰緥4锛屾煡璇 鎵鏈夊鐢熶腑 骞冲潎鍒 澶т簬80鍒嗙殑瀛︾敓锛 + +```sql +select * from Score; +select StudentId, avg(Score) as AvgScore from Score group by StudentId having avg(Score) > 80 ; +``` + +## 鎬荤粨 + +涓婇潰鍙槸鍗曠嫭閽堝姣忎釜鍦烘櫙杩涜璁茶В锛岃繖浜涘満鏅彲浠ユ贩鍚堣繘琛屾煡璇紝鍦╯ql server 涓煡璇㈢殑鏁翠綋璇硶锛 + +select [top N] [鍒1,鍒2,...|*] from 琛ㄥ悕 + [where 鏉′欢] + [group by 瀛楁] + [order by 瀛楁] + [having 鏉′欢] diff --git "a/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/03_\344\277\256\346\224\271\346\225\260\346\215\256.md" "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/03_\344\277\256\346\224\271\346\225\260\346\215\256.md" new file mode 100644 index 0000000000000000000000000000000000000000..b73630f4064ecdb2ba2c15bfa7ade6691fa288a7 --- /dev/null +++ "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/03_\344\277\256\346\224\271\346\225\260\346\215\256.md" @@ -0,0 +1,58 @@ +## 鏈妭鐩爣 + +1. 鐔熸倝SQL Server淇敼鏁版嵁 + +## 鏁版嵁鍑嗗 + +涓嬮潰婕旂ず涔嬪墠锛屾垜浠渶瑕佹彃鍏ヤ竴浜涙祴璇曡繘琛屾紨绀猴紝璇峰弬鑰冿細 + + 03_淇敼鏁版嵁绀轰緥鏁版嵁.sql + +鏂囦欢锛屽皢鍏跺鍏ュ埌SQL Server鍗冲彲銆 + +## 淇敼鏁版嵁 + +淇敼鏁版嵁璇硶锛 + + update 琛ㄥ悕 set 鍒1='鍊1', 鍒2='鍊',... [where 鏉′欢] + +鍏朵腑锛 + +1. update 鏄叧閿瘝锛岃〃绀轰慨鏀圭殑鎰忔濄 +2. 琛ㄥ悕 琛ㄧず淇敼鍝釜琛ㄣ +3. set 鏄叧閿瘝锛岃〃绀鸿缃殑鎰忔濄 +4. 鍒1='鍊1'锛岃〃绀哄皢 鍒1 鐨勫间慨鏀逛负 鍊1銆 + +**绀轰緥1**锛岃绠楁満搴旂敤鎶鏈彮 鐨 娆ч槼澶╁ぉ 鐢熸棩鍐欓敊浜嗭紝姝g‘鐨勭敓鏃ュ簲璇ユ槸锛2000-04-06锛岃鐢╯ql杩涜淇敼锛 + +```sql +update Student set StudentBirth='2000-04-06' where StudentId = 7; +``` + +**绀轰緥2**锛岃绠楁満搴旂敤鎶鏈彮 鐨 寰愰暱鍗 鐨 javascript缂栫▼鍩虹 鍒嗘暟濉敊锛屾纭殑鍒嗘暟搴旇鏄細61锛岃鐢╯ql杩涜淇敼锛 + +```sql +update Score set Score = 61 where StudentId = 8 and CourseId = 2; +``` + +**绀轰緥3**锛屾睙鏂 鐨 鎬у埆 淇℃伅濉啓閿欒锛屾槸涓哄コ锛岃浆鍏ュ埌杞欢1鐝紝璇风敤sql杩涜淇敼锛 + +```sql +update Student set StudentSex = 2, ClassId = 1 where StudentId = 4; +``` + +**绀轰緥4**锛屽緪闀垮嵖 鍜 鏉庨嶉仴 杞叆鍒颁簡杞欢2鐝紝璇风敤sql杩涜淇敼锛 + +```sql +update Student set ClassId = 2 where StudentId = 8 or StudentId = 9; +``` + +鎬濊冿細濡傛灉娌℃湁鍔爓here鏉′欢浼氬嚭鐜颁粈涔堟儏鍐靛憿锛 + +**绀轰緥5**锛屽鏍″璇剧▼瀛﹀垎杩涜浜嗚皟鏁达紝姣忛棬璇剧▼瀛﹀垎+1锛岃鐢╯ql杩涜淇敼锛 + +```sql +update Course set CourseCredit=CourseCredit+1; +``` + + diff --git "a/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/04_\345\210\240\351\231\244\346\225\260\346\215\256.md" "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/04_\345\210\240\351\231\244\346\225\260\346\215\256.md" new file mode 100644 index 0000000000000000000000000000000000000000..d593dc1ab16fbabc92e54ef8449ffba8f30985a3 --- /dev/null +++ "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/04_\345\210\240\351\231\244\346\225\260\346\215\256.md" @@ -0,0 +1,94 @@ +## 鏈妭鐩爣 + +1. 鐔熸倝SQL Server鍒犻櫎鏁版嵁 + +## 鏁版嵁鍑嗗 + +涓嬮潰婕旂ず涔嬪墠锛屾垜浠渶瑕佹彃鍏ヤ竴浜涙祴璇曡繘琛屾紨绀猴紝璇峰弬鑰冿細 + + 04_鍒犻櫎鏁版嵁绀轰緥鏁版嵁.sql + +鏂囦欢锛屽皢鍏跺鍏ュ埌SQL Server鍗冲彲銆 + +## 鍒犻櫎鏁版嵁 + +鍒犻櫎鏁版嵁璇硶锛 + + delete from 琛ㄥ悕 where 鏉′欢 + +鍏朵腑锛 + +1. delete from 鏄叧閿瘝锛岃〃绀哄垹闄ょ殑鎰忔濄 +2. 琛ㄥ悕 琛ㄧず鍝釜琛ㄥ垹闄ゆ暟鎹 +3. where 鏉′欢 骞堕潪蹇呴』锛屼絾涓鑸兘鏈夈 + +娉ㄦ剰锛 + + 鍦ㄤ笉浣跨敤 where 鏉′欢鐨勬椂鍊欙紝灏嗗垹闄ゆ墍鏈夋暟鎹 + +**绀轰緥1**锛屽垎鏁拌〃杩欒竟 鍒嗘暟琛 鏈鍚庝竴鏉℃彃鍏ラ噸澶嶄簡锛岃甯繖鍒犻櫎杩欐潯鏁版嵁 + +```sql +select * from Score order by ScoreId desc; +delete from Score where ScoreId = 37; +``` + +**绀轰緥2**锛屽悗闈笉鐢ㄥ啀鏁版嵁搴撻珮绾у簲鐢ㄨ绋嬶紝璇峰府蹇欏垹闄よ繖涓绋嬶細 + +```sql +delete from Course where CourseId = 1; +``` + +鎵ц鍒犻櫎鐨勬椂鍊欐垜浠細鍙戠幇锛屼細鎶ヤ竴涓敊璇細 + +![](04_鍒犻櫎鏁版嵁_files/1.jpg) + +瀹為檯涓婅繖涓槸鍥犱负ClassCourse琛ㄦ湁涓涓閿害鏉燂紝鍏宠仈鍒颁簡Course.CourseId瀛楁锛屾墍浠ヤ笉鑳界洿鎺ュ垹闄ゃ + +鍦ㄥ疄闄呰繃绋嬩腑锛岀粡甯镐細閫氳繃閫昏緫鍒犻櫎鐨勬柟寮忓垹闄ゆ暟鎹紝鑰屼笉浼氱湡姝e垹闄ゆ暟鎹 + +**绀轰緥3**锛屽鐢熸垚缁╄〃鐨勬暟鎹渶瑕侀噸鏂板綍鍏ワ紝璇峰府蹇欏垹闄ゅ師鏈夋暟鎹細 + +```sql +delete from Score; +``` + +涓嶅姞鏉′欢鐨勫垹闄わ紝琛ㄧず鍒犻櫎琛ㄦ墍鏈夋暟鎹備竴鑸鎱庨噸浣跨敤銆 + + +## 娓呯┖鏁版嵁 + +鍦⊿QL Server闄や簡鐢╠elete鍒犻櫎鏁版嵁澶栵紝杩樺彲浠ヤ娇鐢╰runcate杩涜銆 + +truncate 鍏抽敭瀛楃敤浜庡畬鍏ㄦ竻绌轰竴涓〃銆傚叾璇硶鏍煎紡濡備笅锛 + + truncate table 琛ㄥ悕 + +鍏朵腑锛 + + table 鍏抽敭瀛楀彲鐪佺暐銆 + +绀轰緥1锛屼娇鐢 truncate 璇彞娓呯┖item琛ㄦ暟鎹細 + +```sql +truncate table Score; +``` + +## truncate 鍜 delete 鐨勫尯鍒 + +浠庨昏緫涓婅锛宼runcate 璇彞涓 delete 璇彞浣滅敤鐩稿悓锛屼絾鏄湪鏌愪簺鎯呭喌涓嬶紝涓よ呭湪浣跨敤涓婃湁鎵鍖哄埆銆 + + 1. delete 鏄 DML 绫诲瀷鐨勮鍙ワ紱truncate 鏄 DDL 绫诲瀷鐨勮鍙ャ傚畠浠兘鐢ㄦ潵娓呯┖琛ㄤ腑鐨勬暟鎹 + 2. delete 鏄愯涓鏉′竴鏉″垹闄よ褰曠殑锛泃runcate 鍒欐槸鐩存帴鍒犻櫎鍘熸潵鐨勮〃锛屽啀閲嶆柊鍒涘缓涓涓竴妯′竴鏍风殑鏂拌〃锛岃屼笉鏄愯鍒犻櫎琛ㄤ腑鐨勬暟鎹紝鎵ц鏁版嵁姣 delete 蹇傚洜姝ら渶瑕佸垹闄よ〃涓叏閮ㄧ殑鏁版嵁琛屾椂锛屽敖閲忎娇鐢 truncate 璇彞锛 鍙互缂╃煭鎵ц鏃堕棿銆 + 3. delete 鍒犻櫎鏁版嵁鍚庯紝閰嶅悎浜嬩欢鍥炴粴鍙互鎵惧洖鏁版嵁锛泃runcate 涓嶆敮鎸佷簨鍔$殑鍥炴粴锛屾暟鎹垹闄ゅ悗鏃犳硶鎵惧洖銆 + 4. delete 鍒犻櫎鏁版嵁鍚庯紝绯荤粺涓嶄細閲嶆柊璁剧疆鑷瀛楁鐨勮鏁板櫒锛泃runcate 娓呯┖琛ㄨ褰曞悗锛岀郴缁熶細閲嶆柊璁剧疆鑷瀛楁鐨勮鏁板櫒銆 + 5. delete 鐨勪娇鐢ㄨ寖鍥存洿骞匡紝鍥犱负瀹冨彲浠ラ氳繃 where 瀛愬彞鎸囧畾鏉′欢鏉ュ垹闄ら儴鍒嗘暟鎹紱鑰 truncate 涓嶆敮鎸 where 瀛愬彞锛屽彧鑳藉垹闄ゆ暣浣撱 + 6. delete 浼氳繑鍥炲垹闄ゆ暟鎹殑琛屾暟锛屼絾鏄 truncate 鍙細杩斿洖 0锛屾病鏈変换浣曟剰涔夈 + +## 鍒犻櫎鏁版嵁鎬荤粨 + + 1. 褰撲笉闇瑕佽琛ㄦ椂锛岀敤 drop锛 + 2. 褰撲粛瑕佷繚鐣欒琛紝浣嗚鍒犻櫎鎵鏈夎褰曟椂锛岀敤 truncate锛 + 3. 褰撹鍒犻櫎閮ㄥ垎璁板綍鏃讹紝鐢 delete銆 + + diff --git "a/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/80_\347\273\203\344\271\240\351\242\230.md" "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/80_\347\273\203\344\271\240\351\242\230.md" new file mode 100644 index 0000000000000000000000000000000000000000..c1708fd66c59aefd20584b908ac8575d97fb48e7 --- /dev/null +++ "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/80_\347\273\203\344\271\240\351\242\230.md" @@ -0,0 +1,127 @@ +## 璇惧爞缁冧範 + +### 澧炲姞鏁版嵁 + +1. 瀛︽牎寮璁句簡3涓彮绾э細杞欢涓鐝佽蒋浠朵簩鐝佽绠楁満搴旂敤鎶鏈彮銆傝鎻掑叆鐝骇琛ㄧ浉鍏虫暟鎹 + +2. 杞欢涓鐝湁3涓悓瀛︼紝濮撳悕銆佹у埆銆佺敓鏃ャ佸搴綇鍧 鍒嗗埆鏄細 + + - 鍒樻銆佺敺锛1锛夈2000-01-01銆佸箍瑗跨渷妗傛灄甯備竷鏄熷尯绌烘槑瑗胯矾10鍙烽妇涓滃皬鍖 + - 榛勮吹銆佺敺銆2001-03-20銆佹睙瑗跨渷鍗楁槍甯傞潚灞辨箹鍖鸿壘婧箹鍗楄矾鍗150绫冲箍闃冲皬鍖 + - 闄堢編銆佸コ锛2锛夈2000-07-08銆佺寤虹渷榫欏博甯傛柊缃楀尯鏇规邯琛楅亾涓囪揪灏忓尯 + +3. 杞欢浜岀彮鏈2涓悓瀛︼紝濮撳悕銆佹у埆銆佺敓鏃ャ佸搴綇鍧 鍒嗗埆鏄細 + + - 姹熸枃銆佺敺銆2000-08-10銆佸畨寰界渷鍚堣偉甯傚簮闃冲尯鍥涢噷娌宠矾涓庢綔灞辫矾浜ゆ眹澶勪竾绉戝煄甯備箣鍏 + - 閽熺惇銆佸コ銆2001-03-21銆佹箹鍗楃渷闀挎矙甯傞洦鑺卞尯绾㈣姳鍧$ぞ鍖 + +4. 璁$畻鏈哄簲鐢ㄦ妧鏈彮鏈4涓悓瀛︼紝濮撳悕銆佹у埆銆佺敓鏃ャ佸搴綇鍧 鍒嗗埆鏄細 + + - 鏇惧皬鏋椼佺敺銆1999-12-10銆佸畨寰界渷鍚堣偉甯傚簮闃冲尯鍥涢噷娌宠矾涓庢綔灞辫矾浜ゆ眹澶勪竾绉戝煄甯備箣鍏 + - 娆ч槼澶╁ぉ銆佸コ銆2000-04-05銆佹箹鍖楃渷姝︽眽甯傛椽灞卞尯鍙嬭皧澶ч亾涓庝簩鐜嚎浜ゆ眹澶勮瀺渚ㄦ偊搴 + - 寰愰暱鍗裤佺敺銆2001-01-30銆佹睙鑻忕渷鑻忓窞甯傝嫃宸炲伐涓氬洯鍖虹嫭澧呮箹绀惧尯 + - 鏉庨嶉仴銆佺敺銆1999-11-11銆佸箍涓滅渷骞垮窞甯傜櫧浜戝尯閲戞矙娲插矝寰℃床涓夎鎭掑ぇ缁挎床 + +5. 鏈2涓鐢熷皻鏈垎閰嶇彮绾э紝濮撳悕銆佹у埆銆佺敓鏃ャ佸搴綇鍧 鍒嗗埆鏄細 + + - 涓滄柟涓嶈触 淇濆瘑 1999-12-11 娌冲寳鐪佸钩瀹氬窞瑗垮寳鍥涘崄浣欓噷鐨勭尒鐚╂哗 + - 浠ょ嫄鍐 鐢 2000-08-11 闄曡タ鐪佹腑鍗楀競鍗庨槾甯傜帀娉夎矾鍗楁 + +6. 杞欢涓鐝紑璁捐绋嬶紝璇剧▼鍚嶇О鍜屽鍒嗗垎鍒负锛 + + - 鏁版嵁搴撻珮绾у簲鐢ㄣ3 + - javascript缂栫▼鍩虹銆3 + - web鍓嶇绋嬪簭璁捐鍩虹銆4 + - 鍔ㄦ佺綉椤佃璁.net鍩虹銆6 + +7. 杞欢浜岀彮寮璁捐绋嬶紝璇剧▼鍚嶇О鍜屽鏃跺垎鍒负锛 + + - 鏁版嵁搴撻珮绾у簲鐢ㄣ3 + - javascript缂栫▼鍩虹銆3 + - web鍓嶇绋嬪簭璁捐鍩虹銆4 + - 鍔ㄦ佺綉椤佃璁.net鍩虹銆6 + +8. 璁$畻鏈哄簲鐢ㄦ妧鏈彮寮璁捐绋嬶紝璇剧▼鍚嶇О鍜屽鏃跺垎鍒负锛 + + - 鏁版嵁搴撻珮绾у簲鐢ㄣ3 + - javascript缂栫▼鍩虹銆3 + - web鍓嶇绋嬪簭璁捐鍩虹銆4 + - 鍔ㄦ佺綉椤佃璁hp鍩虹銆6 + +9. 鑰冭瘯瀹屾垚鍚庯紝鍚勫鐢熷悇璇剧▼寰楀垎锛 + + - 鍒樻銆佹暟鎹簱楂樼骇搴旂敤銆80 + - 鍒樻銆乯avascript缂栫▼鍩虹銆78 + - 鍒樻銆亀eb鍓嶇绋嬪簭璁捐鍩虹銆65 + - 鍒樻銆佸姩鎬佺綉椤佃璁.net鍩虹銆90 + - 榛勮吹銆佹暟鎹簱楂樼骇搴旂敤銆60 + - 榛勮吹銆乯avascript缂栫▼鍩虹銆77 + - 榛勮吹銆亀eb鍓嶇绋嬪簭璁捐鍩虹銆68 + - 榛勮吹銆佸姩鎬佺綉椤佃璁.net鍩虹銆88 + - 闄堢編銆佹暟鎹簱楂樼骇搴旂敤銆88 + - 闄堢編銆乯avascript缂栫▼鍩虹銆45 + - 闄堢編銆亀eb鍓嶇绋嬪簭璁捐鍩虹銆66 + - 闄堢編銆佸姩鎬佺綉椤佃璁.net鍩虹銆75 + - 姹熸枃銆佹暟鎹簱楂樼骇搴旂敤銆56 + - 姹熸枃銆乯avascript缂栫▼鍩虹銆80 + - 姹熸枃銆亀eb鍓嶇绋嬪簭璁捐鍩虹銆75 + - 姹熸枃銆佸姩鎬佺綉椤佃璁.net鍩虹銆66 + - 閽熺惇銆佹暟鎹簱楂樼骇搴旂敤銆88 + - 閽熺惇銆乯avascript缂栫▼鍩虹銆79 + - 閽熺惇銆亀eb鍓嶇绋嬪簭璁捐鍩虹銆72 + - 閽熺惇銆佸姩鎬佺綉椤佃璁.net鍩虹銆85 + - 鏇惧皬鏋椼佹暟鎹簱楂樼骇搴旂敤銆68 + - 鏇惧皬鏋椼乯avascript缂栫▼鍩虹銆88 + - 鏇惧皬鏋椼亀eb鍓嶇绋嬪簭璁捐鍩虹銆73 + - 鏇惧皬鏋椼佸姩鎬佺綉椤佃璁hp鍩虹銆63 + - 娆ч槼澶╁ぉ銆佹暟鎹簱楂樼骇搴旂敤銆84 + - 娆ч槼澶╁ぉ銆乯avascript缂栫▼鍩虹銆90 + - 娆ч槼澶╁ぉ銆亀eb鍓嶇绋嬪簭璁捐鍩虹銆92 + - 娆ч槼澶╁ぉ銆佸姩鎬佺綉椤佃璁hp鍩虹銆78 + - 寰愰暱鍗裤佹暟鎹簱楂樼骇搴旂敤銆58 + - 寰愰暱鍗裤乯avascript缂栫▼鍩虹銆59 + - 寰愰暱鍗裤亀eb鍓嶇绋嬪簭璁捐鍩虹銆65 + - 寰愰暱鍗裤佸姩鎬佺綉椤佃璁hp鍩虹銆75 + - 鏉庨嶉仴銆佹暟鎹簱楂樼骇搴旂敤銆48 + - 鏉庨嶉仴銆乯avascript缂栫▼鍩虹銆67 + - 鏉庨嶉仴銆亀eb鍓嶇绋嬪簭璁捐鍩虹銆71 + - 鏉庨嶉仴銆佸姩鎬佺綉椤佃璁.net鍩虹銆56 + - 鏉庨嶉仴銆佹暟鎹簱楂樼骇搴旂敤銆48 + +### 淇敼鍜屽垹闄ゆ暟鎹 + +1. 璁$畻鏈哄簲鐢ㄦ妧鏈彮 鐨 娆ч槼澶╁ぉ 鐢熸棩鍐欓敊浜嗭紝姝g‘鐨勭敓鏃ュ簲璇ユ槸锛2000-04-06锛岃鐢╯ql杩涜淇敼銆 +2. 璁$畻鏈哄簲鐢ㄦ妧鏈彮 鐨 寰愰暱鍗 鐨 javascript缂栫▼鍩虹 鍒嗘暟濉敊锛屾纭殑鍒嗘暟搴旇鏄細61锛岃鐢╯ql杩涜淇敼銆 +3. 姹熸枃 鐨 鎬у埆 淇℃伅濉啓閿欒锛屾槸涓哄コ锛岃浆鍏ュ埌杞欢1鐝紝璇风敤sql杩涜淇敼 +4. 寰愰暱鍗 鍜 鏉庨嶉仴 杞叆鍒颁簡杞欢2鐝紝璇风敤sql杩涜淇敼 +5. 瀛︽牎瀵硅绋嬪鍒嗚繘琛屼簡璋冩暣锛屾瘡闂ㄨ绋嬪鍒+1锛岃鐢╯ql杩涜淇敼 +6. 鍒嗘暟琛ㄨ繖杈 鏉庨嶉仴 鏁版嵁搴撻珮绾у簲鐢 鎴愮哗 鎻掑叆閲嶅浜嗭紝璇峰府蹇欏垹闄ゅ叾涓竴鏉℃暟鎹; + +### 鏌ヨ鏁版嵁 + +1. 鏌ヨ鎵鏈夌殑鐝骇 Class 淇℃伅銆 +2. 鏌ヨ鎵鏈夌殑瀛︾敓 Student 淇℃伅銆 +3. 鏌ヨ鎵鏈夌殑璇剧▼ Course 淇℃伅銆 +4. 鏌ヨ鎵鏈夌殑鐝骇璇剧▼ ClassCourse 淇℃伅銆 +5. 鏌ヨ鎵鏈夌殑鍒嗘暟 Score 淇℃伅銆 +6. 鍙煡璇㈠鐢熺殑濮撳悕鍜屽湴鍧淇℃伅銆 +7. 鏌ヨ鑾峰彇瀛︾敓淇℃伅琛ㄤ腑20%鐨勫鐢熶俊鎭 +8. 鏌ヨ瀛︾敓淇℃伅琛ㄤ腑鐨勭彮绾х紪鍙锋湁鍝簺锛堝幓闄ら噸澶嶅)銆 +9. 鏌ヨ鍒嗘暟琛ㄤ腑鏈夋垚缁╃殑瀛︾敓缂栧彿锛 +10. 鏌ヨ鎵鏈夌殑璇剧▼ Course 淇℃伅锛屽苟涓旀寜鐓у鍒嗕粠澶у埌灏忔帓鍒椼 +11. 鏌ヨ鎵鏈夌殑鍒嗘暟 Score 淇℃伅锛屽苟涓旀寜鐓у垎鏁颁粠灏忓埌澶ф帓鍒椼 +12. 鏌ヨ鎵鏈夌殑瀛︾敓 Student 淇℃伅锛屽苟涓旀寜鐓у勾榫勪粠灏忓埌澶ф帓鍒椼 +13. 鏌ヨ杞欢涓鐝墍鏈夌殑瀛︾敓淇℃伅锛屾樉绀哄鐢焛d锛屽鍚嶃佹у埆銆佺敓鏃ャ佷綇鍧銆 +14. 鏌ヨ鎵鏈夌殑濂崇敓锛屾樉绀哄鐢焛d锛屽鍚嶃佹у埆銆佺敓鏃ャ佷綇鍧銆 +15. 鏌ヨ2000骞村嚭鐢熺殑鎵鏈夊鐢燂紝鏄剧ず瀛︾敓id锛屽鍚嶃佹у埆銆佺敓鏃ャ佷綇鍧銆 +16. 鏌ヨ濮撴槸娆ч槼鐨勫鐢熴 +17. 鏌ヨ鍦板潃鏄鏋楀競鐨勫鐢熴 +18. 鏌ヨ濮撴潕鐨勫鐢燂紝骞朵笖鏄笁涓瓧鐨勩 +19. 鏌ヨ 杞欢涓鐝 鏈夊嚑涓鐢熴 +20. 鏌ヨ 杞欢涓鐝 鏈夊嚑闂ㄨ绋嬨 +21. 鏌ヨ 鍒樻(瀛︾敓缂栧彿涓1) 鐨勬诲垎銆 +22. 鏌ヨ鎵鏈夊鐢 鏁版嵁搴撻珮绾у簲鐢 鐨勫钩鍧囧垎銆 +23. 鏌ヨ 鎵鏈夊鐢 鐨勬诲垎銆 +24. 鏌ヨ 鎵鏈夊鐢 鐨勫钩鍧囧垎銆 +25. 鏌ヨ 鎵鏈夊鐢 鐨勫钩鍧囧垎锛屽苟涓旀寜鐓у钩鍧囧垎浠庨珮鍒颁綆鎺掑垪銆 +22. 鏌ヨ 鎵鏈夊鐢熶腑 骞冲潎鍒 澶т簬80鍒嗙殑瀛︾敓銆傦紙鑱氬悎鏌ヨ + 鍒嗙粍 + having锛 diff --git "a/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/\344\275\234\344\270\232\344\272\214.txt" "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/\344\275\234\344\270\232\344\272\214.txt" new file mode 100644 index 0000000000000000000000000000000000000000..51ab06c492a0e2c4191eeee160a38456e7b1de42 --- /dev/null +++ "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/\344\275\234\344\270\232\344\272\214.txt" @@ -0,0 +1,28 @@ +先创建如图所示表 + +订单表(orders)列为:订单编号(orderId 主键) 订购日期(orderDate) + +订购项目表(orderItem),列为: +项目编号(ItemiD)订单编号(orderId)产品类别(itemType) +产品名称(itemName) 订购数量(theNumber) 订购单价(theMoney) + +1.查询所有订单订购的所有物品数量总和 + +2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 + + +3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 + +4.查询每种类别的产品分别订购了几次,例如: + 文具 9 + 体育用品 3 + 日常用品 3 + +5.查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 + +6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: + + 产品名称 订购次数 总数量 平均单价 + 笔 3 120 2 + + diff --git "a/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/\344\275\234\344\270\232\344\272\214\346\225\260\346\215\256\345\233\276\347\211\207.bmp" "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/\344\275\234\344\270\232\344\272\214\346\225\260\346\215\256\345\233\276\347\211\207.bmp" new file mode 100644 index 0000000000000000000000000000000000000000..dd34781ae345e0b7c5baa83472ee848095bbcf55 Binary files /dev/null and "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/\344\275\234\344\270\232\344\272\214\346\225\260\346\215\256\345\233\276\347\211\207.bmp" differ diff --git "a/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/\346\250\241\347\263\212\346\237\245\350\257\242\345\222\214\345\210\206\347\273\204\350\201\232\345\220\210\346\237\245\350\257\242.sql" "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/\346\250\241\347\263\212\346\237\245\350\257\242\345\222\214\345\210\206\347\273\204\350\201\232\345\220\210\346\237\245\350\257\242.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6fd6455b694bb1eb7d65ab93e0a7d5fbe62a7c91 --- /dev/null +++ "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/\346\250\241\347\263\212\346\237\245\350\257\242\345\222\214\345\210\206\347\273\204\350\201\232\345\220\210\346\237\245\350\257\242.sql" @@ -0,0 +1,138 @@ +--查询 +--select distinct|top n 列名,...... +--from 表名,...... +--where 条件表达式 ,对数据表的数据进行条件筛选 +--group by 分组字段,...... +--having 条件筛选,对查询统计的结果进行条件筛选 +--order by 排序字段 [asc|desc],排序字段 [asc|desc],...... + + +--1.where 条件表达式, +--运算符: +-- 1)比较运算符: = <> != < > <= >= +-- 2)逻辑运算符:and or not +-- 3)范围运算符:between A and B ,not between A and B +-- 4)列表运算符:in(a,b,c,.....) , not in(a,b,c,.....) +-- 5)空值运算符:is null ,is not null +select * from StuInfo where StuAddress is not null + +-- 6)模式匹配运算符:like '待匹配的模式字符串' +-- 通配符:%,_,[],[^] +-- %:匹配0~n个任意字符 +-- _:匹配1个任意字符 +-- [a,b,c...]:匹配指定集合中的1个字符, +-- [^a,b,c...]:不匹配指定集合中的所有字符 + +--eg:查询桂林市的学生信息 +select * from StuInfo where StuAddress like '%桂林市%' + +--eg:查询姓刘的学生信息,StuName第一字必须是刘 +select * from StuInfo where StuName like '刘%' + +--eg:查询姓刘的学生信息,名字三个字的,StuName第一字必须是刘 +select * from StuInfo where StuName like '刘__' + + + +--eg:查询姓刘的学生信息,名字两个字的,StuName第一字必须是刘 +select * from StuInfo where StuName like '刘_' + +--eg:查询名字以天结尾的学生信息 +select * from StuInfo where StuName like '%天' + +--eg:查询名字包含天的学生信息 +select * from StuInfo where StuName like '%天%' + + +--eg:查询手机尾数为7,8,9的学生信息 +select * from StuInfo where StuPhone like '%[7,8,9]' + + +--eg:查询手机尾数不为7,8,9的学生信息 +select * from StuInfo where StuPhone like '%[^7,8,9]' + + + + + +--聚合函数:对记录的某个字段进行统计计算;select 子句中如果包含聚合函数,那么非group by 的字段不能出现 +--count(字段):对指定字段统计有数据的字段个数,NULL值不列入计数 +--sum(字段):对指定字段进行求和运算 +--avg(字段):对指定字段的值进行求平均值的运算 +--max(字段) +--min(字段) + + +--eg:查询所有学生的成绩信息 +select * from scores; + +--sum(字段):对字段进行求和运算 +--eg:查询所有学生的总成绩信息 +select sum(Score) 总分 from Scores; + + +--查询学生的总人数; +select count(*) 总人数 from StuInfo; + + +--查询地址非空的学生人数 +select count(StuAddress) from StuInfo; +select * from StuInfo + +--查询软件1班的学生人数 +select count(StuId) 一班的人数 from StuInfo where ClassId=1; + +--查询学号为1的学生总成绩、平均成绩的信息 +select sum(score) 总分,avg(score) 平均分 from Scores + + +--查询最高分和最低分 +select * from scores order by Score +select max(Score) 最大值,min(Score) 最小值 from Scores + + + +--分组查询 +--group by 子句进行分组,having 对查询结果进行数据筛选 +--select 分组字段,聚合函数(字段) from 表名 +--group by 分组字段 +--having 包含聚合函数筛选条件 + +--eg:查询学生信息表中男生和女生的人数 +select * from StuInfo + +select count(*) 人数 from StuInfo where StuSex='男' + +select count(*) 人数 from StuInfo where StuSex='女' + + +select count(*) 人数 from StuInfo group by StuSex + + + +select* from StuInfo where StuSex='男' + +select * from StuInfo where StuSex='女' + +--eg:查询每个学生的总成绩信息 +select StuId,sum(score) 总分 from Scores group by StuId + +--eg:查询每门课程的平均分,总分,最高分,最低分,参加考试人数 +select CourseId,avg(score) 平均分,sum(score),max(score),min(score),count(score) from Scores group by CourseId + +--eg:查询每门课程的平均分,且平均分大于等于70的数据 +select CourseId,avg(score) 平均分 from Scores +group by CourseId +having avg(score)>=70 + + +--eg:查询1-3课程的平均分,且平均分大于等于70的数据 +select CourseId,avg(score) 平均分 from Scores +group by CourseId +having avg(score)>=70 and CourseId in(1,2,3) + + +select CourseId,avg(score) 平均分 from Scores +where CourseId in(1,2,3) +group by CourseId +having avg(score)>=70 diff --git "a/\347\214\234\346\213\263\346\270\270\346\210\217/Program.cs" "b/\347\214\234\346\213\263\346\270\270\346\210\217/Program.cs" new file mode 100644 index 0000000000000000000000000000000000000000..53e84306799c04fbde150bb27c3df6ee82be044e --- /dev/null +++ "b/\347\214\234\346\213\263\346\270\270\346\210\217/Program.cs" @@ -0,0 +1,80 @@ +锘縰sing System.Diagnostics; +using System.Reflection.Metadata; + +namespace aaaaaaa +{ + internal class Program + { + + static void Main(string[] args) + { + int user = 0; + int score = 0; + + while (true) + { + Console.WriteLine("鐚滄嫵娓告垙锛"); + Console.WriteLine("1銆佸壀鍒"); + Console.WriteLine("2銆佺煶澶"); + Console.WriteLine("3銆佸竷"); + Console.WriteLine("4銆佺粨鏉"); + try + { + user=Convert.ToInt32((Console.ReadLine())); + } + catch + { + Console.WriteLine("璇疯緭鍏1~3鐨勬暟"); + } + Random random= new Random(); + int pc=random.Next(1,4); + if (user >= 1 && user <= 3) + { + switch (pc) + { + case 1: + Console.WriteLine("璁$畻鏈哄嚭鍓垁"); + break; + case 2: + Console.WriteLine("璁$畻鏈哄嚭鐭冲ご"); + break; + case 3: + Console.WriteLine("璁$畻鏈哄嚭甯"); + break; + } + if (user - pc == -1 || user-pc==2) + { + score--; + Console.WriteLine("璁$畻鏈鸿耽浜"); + Console.WriteLine("寰楀垎涓猴細" + score); + } + else if(user-pc==1 || user-pc==-2) + { + score++; + Console.WriteLine("鎭枩浣犺耽浜"); + Console.WriteLine("寰楀垎涓猴細" + score); + } + else + { + Console.WriteLine("鎵撳钩浜"); + Console.WriteLine("寰楀垎涓猴細" + score); + + } + Console.WriteLine("璇锋寜鍥炶溅閿户缁"); + Console.ReadLine(); + } + else + { + if (user==4) + { + { + break; + } + } + Console.WriteLine("杈撳叆鏈夎锛岃閲嶆柊杈撳叆"); + } + } + + } + } +} \ No newline at end of file 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